Advertisement
GTP95

Doesn't send email

Jul 8th, 2017
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.54 KB | None | 0 0
  1. /* ESP8266 Moisture Sensor
  2.    This sketch uses an ESP8266 to read the analog signal from a moisture sensor. The data is then displayed
  3.    using the serial console or a web browser. Based on the moisture reading, the ESP8266 will blink a RGB LED
  4.    red, green or blue.
  5.  
  6.    Red = Dry
  7.    Green = In between Wet and Dry
  8.    Blue = Wet
  9.  
  10.      Viewing the data via web browser by going to the ip address. In this sketch the address is
  11.      http://192.168.1.221
  12.  
  13.       The browser data includes a Google Chart to visually illustrate the moisture reading as a guage.
  14.  
  15.    ///////////////////////////////////////
  16.    Arduino IDE Setup
  17.    File:
  18.       Preferences
  19.         Add the following link to the "Additional Boards Manager URLs" field:
  20.         http://arduino.esp8266.com/stable/package_esp8266com_index.json
  21.    Tools:
  22.       board: NodeMCU 1.0 (ESP-12 Module)
  23.       programmer: USBtinyISP
  24.  
  25.      
  26.   ///////////////////////////////
  27. */
  28. #include <ESP8266WiFi.h>
  29. #include "ESPMailer.h"
  30. #include <Time.h>
  31. #include <NTP.h>
  32.  
  33. const char* ssid = "*";
  34. const char* password = "*";
  35.  
  36. bool sendEmail=false, mailSent=false, diagnosticMode=false;
  37.  
  38. int WiFiStrength = 0, numeroErroriInvioEmail=0;
  39.  
  40. WiFiServer server(80);
  41.  
  42. void setup() {
  43.   Serial.begin(115200);
  44.   delay(10);
  45.  
  46.   pinMode(4, INPUT);
  47.   pinMode(16, INPUT);
  48.  
  49.  
  50.  
  51.  
  52.   // Connect to WiFi network
  53.   Serial.println();
  54.   Serial.println();
  55.   Serial.print("Connecting to ");
  56.   Serial.println(ssid);
  57.  
  58.   WiFi.begin(ssid, password);
  59.  
  60.   // Set the ip address of the webserver
  61.   // WiFi.config(WebServerIP, Gatway, Subnet)
  62.   // or comment out the line below and DHCP will be used to obtain an IP address
  63.   // which will be displayed via the serial console
  64.  
  65.   WiFi.config(IPAddress(192, 168, 1, 221), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
  66.  
  67.   // connect to WiFi router
  68.   while (WiFi.status() != WL_CONNECTED) {
  69.     delay(500);
  70.     Serial.print(".");
  71.   }
  72.  
  73.   Serial.println("");
  74.   Serial.println("WiFi connected");
  75.  
  76.   // Start the server
  77.   server.begin();
  78.   Serial.println("Server started");
  79.  
  80.   // Print the IP address
  81.   Serial.print("Use this URL to connect: ");
  82.   Serial.print("http://");
  83.   Serial.print(WiFi.localIP());
  84.   Serial.println("/");
  85.  
  86. }
  87.  
  88. double analogValue = 0.0;
  89. double analogVolts = 0.0;
  90.  
  91.  
  92. void loop() {
  93. yield();
  94.  
  95.   WiFiStrength = WiFi.RSSI(); // get dBm from the ESP8266
  96.   analogValue = analogRead(A0); // read the analog signal
  97.   int  digitalValue=digitalRead(4);  //leggi sensore
  98.  
  99.   if(digitalValue==1) sendEmail=true;
  100.   if(digitalRead(16)) diagnosticMode=true;
  101.     else diagnosticMode=false;
  102.   // convert the analog signal to voltage
  103.   // the ESP2866 A0 reads between 0 and ~3 volts, producing a corresponding value      sbagliato! legge tra 0 e 1V
  104.   // between 0 and 1024. The equation below will convert the value to a voltage value.
  105.  
  106.  
  107.  
  108.   analogVolts = (analogValue * 3.08) / 1024;
  109.  
  110.  
  111.  
  112.  
  113.   // Serial data
  114.   Serial.print("Analog raw: ");
  115.   Serial.println(analogValue);
  116.   Serial.print("Analog V: ");
  117.   Serial.println(analogVolts);
  118.   Serial.print("millis(): ");
  119.   Serial.println(millis());
  120.   Serial.print("WiFi Strength: ");
  121.   Serial.print(WiFiStrength); Serial.println("dBm");
  122.   Serial.print("digitalValue: ");
  123.   Serial.println(digitalValue);
  124.   Serial.print("sendEmail: ");
  125.   Serial.println(sendEmail);
  126.   Serial.print("mailSent: ");
  127.   Serial.println(mailSent);
  128.   Serial.print("numeroErroriInvioEmail: ");
  129.   Serial.println(numeroErroriInvioEmail);
  130.   Serial.print("diagnosticMode: ");
  131.   Serial.println(diagnosticMode);
  132.   Serial.println("");
  133.   delay(1000); // slows amount of data sent via serial
  134.  
  135.   if(diagnosticMode==true){
  136.   // check to for any web server requests. ie - browser requesting a page from the webserver
  137.   WiFiClient client = server.available();
  138.   if (!client) {
  139.     return;
  140.   }
  141.  
  142.   // Wait until the client sends some data
  143.   Serial.println("new client");
  144.  
  145.   // Read the first line of the request
  146.   String request = client.readStringUntil('\r');
  147.   Serial.println(request);
  148.   client.flush();
  149.  
  150.   // Return the response
  151.   client.println("HTTP/1.1 200 OK");
  152.   client.println("Content-Type: text/html");
  153.   client.println(""); //  do not forget this one
  154.   client.println("<!DOCTYPE HTML>");
  155.  
  156.   client.println("<html>");
  157.   client.println(" <head>");
  158.   client.println("<meta http-equiv=\"refresh\" content=\"60\">");
  159.  
  160.  
  161.   client.println("  </head>");
  162.   client.println("  <body>");
  163.  
  164.   client.print("<h1 style=\"size:12px;\">ESP8266 Pagina Diagnostica</h1>");
  165.  
  166.   // show some data on the webpage and the guage
  167.   client.println("<table><tr><td>");
  168.  
  169.   client.print("Potenza segnale WiFi: ");
  170.   client.println(WiFiStrength);
  171.   client.println("dBm<br>");
  172.  
  173.   client.print("Analog Raw: ");
  174.   client.println(analogValue);
  175.   client.print("<br>Analog Volts: ");
  176.  
  177.   client.print(analogVolts);
  178.   client.println("V<br>");
  179.  
  180.   client.print("Uptime: ");
  181.  long int tempo=millis();
  182.  long int secondi=(tempo/1000)%60;
  183.  long int minuti=(tempo/60000)%60;
  184.  long int ore=(tempo/3600000)%24;
  185.  long int giorni=tempo/86400000;
  186.   client.print(giorni);
  187.   client.print("D ");
  188.   client.print(ore);
  189.   client.print("H ");
  190.   client.print(minuti);
  191.   client.print("m ");
  192.   client.print(secondi);
  193.   client.println("s<br>");
  194.  
  195.   client.print("sendEmail: ");
  196.   client.print(sendEmail);
  197.   client.println("<br>");
  198.  
  199.   client.print("Tentativi di invio email falliti: ");
  200.   client.print(numeroErroriInvioEmail);
  201.   client.println("<br>");
  202.  
  203.   client.println("<br><a href=\"/REFRESH\"\"><button>Refresh</button></a>");
  204.  
  205.   client.println("</td><td>");
  206.  
  207.  
  208.   client.println("</body>");
  209.   client.println("</html>");
  210.   delay(1);
  211.   Serial.println("Client disconnected");
  212.   Serial.println("");
  213.  // yield();
  214.   }
  215.  
  216.  
  217.   if(sendEmail==true && mailSent==false){
  218.     yield();
  219.     Serial.println("Tentativo invio email in corso");
  220.   ESPMailer* mail = new ESPMailer();
  221.  
  222.   mail->setDebugLevel(0);
  223.   mail->Host = "mail.vfemail.net";
  224.   mail->Port = 587;
  225.   mail->SMTPAuth = true;
  226.   mail->AuthType = LOGIN;
  227.   mail->Username = "*";
  228.   mail->Password = "*";
  229.   mail->setFrom("*","ESP8266 Example Mailer");
  230.   mail->setTimezone(1); //defaults to UTC
  231.   mail->addAddress("*");
  232. //  mail->addBCC("bcc@example.org");
  233.   mail->Subject = "Test!";
  234.   mail->isHTML(true);
  235.   mail->Body = "<html><body>Hello <strong>ESP</strong> user!</body></html>";
  236.   mail->AltBody = "Hello ESP user!";
  237.   if (mail->send()){
  238.     mailSent=true;
  239.     Serial.println("Mail sent sucessfully!");
  240. }
  241. else {
  242.   numeroErroriInvioEmail++;
  243.   Serial.println("errore nell'invio dell'email");
  244. }
  245.   }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement