Отправка электронного письма с java при выполнении условия в arduino ide - PullRequest
0 голосов
/ 21 января 2020

Мне нужен проект для школы, и мне нужно отправить электронное письмо, когда датчик температуры на моем Arduino достигнет определенной степени. Так как мне удобнее всего java, я отправил почтовый отправитель на java с Java Mail API. Теперь мне нужно только соединить мою программу в 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");
  }
}

1 Ответ

0 голосов
/ 21 января 2020

У тебя Arduino скетч, работает веб-сервер. Линия Serial.println(Ethernet.localIP()); выводит свой IP на последовательный интерфейс. Ваша программа java может отправлять http-запрос на этот веб-сервер и повторно отправлять вывод по электронной почте. Или вы можете отправлять электронную почту напрямую из arduino через SMTP, возможно, используя какую-то библиотеку, например SMTPClient

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...