gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:46:05 29 4
gpt4 key购买 nike

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

#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");
}
}

最佳答案

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

关于java - 当满足arduino ide中的条件时用java发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59838966/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com