Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ESP8266 Moisture Sensor
- This sketch uses an ESP8266 to read the analog signal from a moisture sensor. The data is then displayed
- using the serial console or a web browser. Based on the moisture reading, the ESP8266 will blink a RGB LED
- red, green or blue.
- Red = Dry
- Green = In between Wet and Dry
- Blue = Wet
- Viewing the data via web browser by going to the ip address. In this sketch the address is
- http://192.168.1.221
- The browser data includes a Google Chart to visually illustrate the moisture reading as a guage.
- ///////////////////////////////////////
- Arduino IDE Setup
- File:
- Preferences
- Add the following link to the "Additional Boards Manager URLs" field:
- http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Tools:
- board: NodeMCU 1.0 (ESP-12 Module)
- programmer: USBtinyISP
- ///////////////////////////////
- */
- #include <ESP8266WiFi.h>
- #include "ESPMailer.h"
- #include <Time.h>
- #include <NTP.h>
- const char* ssid = "*";
- const char* password = "*";
- bool sendEmail=false, mailSent=false, diagnosticMode=false;
- int WiFiStrength = 0, numeroErroriInvioEmail=0;
- WiFiServer server(80);
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(4, INPUT);
- pinMode(16, INPUT);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- // Set the ip address of the webserver
- // WiFi.config(WebServerIP, Gatway, Subnet)
- // or comment out the line below and DHCP will be used to obtain an IP address
- // which will be displayed via the serial console
- WiFi.config(IPAddress(192, 168, 1, 221), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
- // connect to WiFi router
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.print("Use this URL to connect: ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.println("/");
- }
- double analogValue = 0.0;
- double analogVolts = 0.0;
- void loop() {
- yield();
- WiFiStrength = WiFi.RSSI(); // get dBm from the ESP8266
- analogValue = analogRead(A0); // read the analog signal
- int digitalValue=digitalRead(4); //leggi sensore
- if(digitalValue==1) sendEmail=true;
- if(digitalRead(16)) diagnosticMode=true;
- else diagnosticMode=false;
- // convert the analog signal to voltage
- // the ESP2866 A0 reads between 0 and ~3 volts, producing a corresponding value sbagliato! legge tra 0 e 1V
- // between 0 and 1024. The equation below will convert the value to a voltage value.
- analogVolts = (analogValue * 3.08) / 1024;
- // Serial data
- Serial.print("Analog raw: ");
- Serial.println(analogValue);
- Serial.print("Analog V: ");
- Serial.println(analogVolts);
- Serial.print("millis(): ");
- Serial.println(millis());
- Serial.print("WiFi Strength: ");
- Serial.print(WiFiStrength); Serial.println("dBm");
- Serial.print("digitalValue: ");
- Serial.println(digitalValue);
- Serial.print("sendEmail: ");
- Serial.println(sendEmail);
- Serial.print("mailSent: ");
- Serial.println(mailSent);
- Serial.print("numeroErroriInvioEmail: ");
- Serial.println(numeroErroriInvioEmail);
- Serial.print("diagnosticMode: ");
- Serial.println(diagnosticMode);
- Serial.println("");
- delay(1000); // slows amount of data sent via serial
- if(diagnosticMode==true){
- // check to for any web server requests. ie - browser requesting a page from the webserver
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- // Read the first line of the request
- String request = client.readStringUntil('\r');
- Serial.println(request);
- client.flush();
- // Return the response
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); // do not forget this one
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.println(" <head>");
- client.println("<meta http-equiv=\"refresh\" content=\"60\">");
- client.println(" </head>");
- client.println(" <body>");
- client.print("<h1 style=\"size:12px;\">ESP8266 Pagina Diagnostica</h1>");
- // show some data on the webpage and the guage
- client.println("<table><tr><td>");
- client.print("Potenza segnale WiFi: ");
- client.println(WiFiStrength);
- client.println("dBm<br>");
- client.print("Analog Raw: ");
- client.println(analogValue);
- client.print("<br>Analog Volts: ");
- client.print(analogVolts);
- client.println("V<br>");
- client.print("Uptime: ");
- long int tempo=millis();
- long int secondi=(tempo/1000)%60;
- long int minuti=(tempo/60000)%60;
- long int ore=(tempo/3600000)%24;
- long int giorni=tempo/86400000;
- client.print(giorni);
- client.print("D ");
- client.print(ore);
- client.print("H ");
- client.print(minuti);
- client.print("m ");
- client.print(secondi);
- client.println("s<br>");
- client.print("sendEmail: ");
- client.print(sendEmail);
- client.println("<br>");
- client.print("Tentativi di invio email falliti: ");
- client.print(numeroErroriInvioEmail);
- client.println("<br>");
- client.println("<br><a href=\"/REFRESH\"\"><button>Refresh</button></a>");
- client.println("</td><td>");
- client.println("</body>");
- client.println("</html>");
- delay(1);
- Serial.println("Client disconnected");
- Serial.println("");
- // yield();
- }
- if(sendEmail==true && mailSent==false){
- yield();
- Serial.println("Tentativo invio email in corso");
- ESPMailer* mail = new ESPMailer();
- mail->setDebugLevel(0);
- mail->Host = "mail.vfemail.net";
- mail->Port = 587;
- mail->SMTPAuth = true;
- mail->AuthType = LOGIN;
- mail->Username = "*";
- mail->Password = "*";
- mail->setFrom("*","ESP8266 Example Mailer");
- mail->setTimezone(1); //defaults to UTC
- mail->addAddress("*");
- // mail->addBCC("bcc@example.org");
- mail->Subject = "Test!";
- mail->isHTML(true);
- mail->Body = "<html><body>Hello <strong>ESP</strong> user!</body></html>";
- mail->AltBody = "Hello ESP user!";
- if (mail->send()){
- mailSent=true;
- Serial.println("Mail sent sucessfully!");
- }
- else {
- numeroErroriInvioEmail++;
- Serial.println("errore nell'invio dell'email");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement