首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当满足arduino ide中的条件时,使用java发送电子邮件

当满足arduino ide中的条件时,使用java发送电子邮件
EN

Stack Overflow用户
提问于 2020-01-21 18:23:29
回答 1查看 25关注 0票数 0

我需要为学校做一个项目,它要求我在arduino上的温度传感器达到一定程度时发送电子邮件。因为我对java最熟悉,所以我用java mail API在Java上制作了邮件发送器。现在我只需要在Arduino IDE中连接我的程序,在java上连接我的邮件发送器,这样当温度升高时就会发送一封电子邮件。我在Arduino中的代码还包括一个网站,但这与我的问题没有直接关系。以下是我在Arduino中的代码:

代码语言:javascript
复制
#include <dht.h>
#include <SPI.h>
#include <Ethernet.h>
dht DHT;
#define DHT11_PIN 7    // what pin we're connected to

byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x3A, 0xC4 };
EthernetServer server(80);



#define DHTTYPE DHT11   // DHT 11



void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }



  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {



  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
 /* if (isnan(DHT.temperature) || isnan(DHT.humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(DHT.humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(DHT.temperature);
    Serial.println(" *C");
  }*/

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
   client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          //client.println("<script> alert('TEST'); </script>");
          // output the value of the DHT-11
         client.println("<H2>");
            client.print("Humidity: ");
            client.println("</H2>");
            client.println("<p />");
            client.println("<H1>");
            client.print(DHT.humidity);
            client.print(" %\t");
            client.println("</H1>");
            client.println("<p />"); 
            client.println("<H2>");
            client.print("Temperature: ");
            client.println("</H2>");
            client.println("<H1>");
            client.print(DHT.temperature);
            client.println("&#176;");
            client.println("C");
            client.println("</H1>");



          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-21 19:45:42

Arduino sketch运行web-server。Line Serial.println(Ethernet.localIP());将其IP打印到串行接口。您的java程序可以向此web服务器发出http请求,并通过电子邮件重新发送输出。或者你可以通过SMTP直接从arduino发送电子邮件,也许可以使用像SMTPClient这样的库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59838966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档