Issue
Code backup
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
if (!bme.begin(0x76)) {
|
||||
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
// BME280
|
||||
void readTemperature() {
|
||||
Serial.print("Temperature = ");
|
||||
Serial.print(bme.readTemperature());
|
||||
Serial.println(" *C");
|
||||
}
|
||||
|
||||
void readPressure() {
|
||||
Serial.print("Pressure = ");
|
||||
Serial.print(bme.readPressure() / 100.0F);
|
||||
Serial.println(" hPa");
|
||||
}
|
||||
|
||||
void readHumidity() {
|
||||
Serial.print("Humidity = ");
|
||||
Serial.print(bme.readHumidity());
|
||||
Serial.println(" %");;
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
readTemperature();
|
||||
readPressure();
|
||||
readHumidity();
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
//Temperature = 26.10 *C
|
||||
//Pressure = 1013.46 hPa
|
||||
//Humidity = 56.13 %
|
||||
|
||||
//Temperature = -143.84 *C
|
||||
//Pressure = 1144.83 hPa
|
||||
//Humidity = 100.00 %
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
float temperature, humidity, pressure, altitude;
|
||||
|
||||
/*Put your SSID & Password*/
|
||||
const char* ssid = "Melafonino di Claudio"; // Enter SSID here
|
||||
const char* password = "1234567890"; //Enter Password here
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
|
||||
bme.begin(0x76);
|
||||
|
||||
Serial.println("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
//connect to your local wi-fi network
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
//check wi-fi is connected to wi-fi network
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected..!");
|
||||
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
|
||||
|
||||
server.on("/", handle_OnConnect);
|
||||
server.onNotFound(handle_NotFound);
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
|
||||
}
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
}
|
||||
|
||||
void handle_OnConnect() {
|
||||
temperature = bme.readTemperature();
|
||||
humidity = bme.readHumidity();
|
||||
pressure = bme.readPressure() / 100.0F;
|
||||
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
|
||||
server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude));
|
||||
}
|
||||
|
||||
void handle_NotFound(){
|
||||
server.send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
String SendHTML(float temperature,float humidity,float pressure,float altitude){
|
||||
String ptr = "<!DOCTYPE html>";
|
||||
ptr +="<html>";
|
||||
ptr +="<head>";
|
||||
ptr +="<title>ESP32 Weather Station</title>";
|
||||
ptr +="<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
|
||||
ptr +="<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600' rel='stylesheet'>";
|
||||
ptr +="<style>";
|
||||
ptr +="html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #444444;}";
|
||||
ptr +="body{margin: 0px;} ";
|
||||
ptr +="h1 {margin: 50px auto 30px;} ";
|
||||
ptr +=".side-by-side{display: table-cell;vertical-align: middle;position: relative;}";
|
||||
ptr +=".text{font-weight: 600;font-size: 19px;width: 200px;}";
|
||||
ptr +=".reading{font-weight: 300;font-size: 50px;padding-right: 25px;}";
|
||||
ptr +=".temperature .reading{color: #F29C1F;}";
|
||||
ptr +=".humidity .reading{color: #3B97D3;}";
|
||||
ptr +=".pressure .reading{color: #26B99A;}";
|
||||
ptr +=".altitude .reading{color: #955BA5;}";
|
||||
ptr +=".superscript{font-size: 17px;font-weight: 600;position: absolute;top: 10px;}";
|
||||
ptr +=".data{padding: 10px;}";
|
||||
ptr +=".container{display: table;margin: 0 auto;}";
|
||||
ptr +=".icon{width:65px}";
|
||||
ptr +="</style>";
|
||||
ptr +="</head>";
|
||||
ptr +="<body>";
|
||||
ptr +="<h1>ESP32 Weather Station</h1>";
|
||||
ptr +="<h3>www.how2electronics.com</h3>";
|
||||
ptr +="<div class='container'>";
|
||||
ptr +="<div class='data temperature'>";
|
||||
ptr +="<div class='side-by-side icon'>";
|
||||
ptr +="<svg enable-background='new 0 0 19.438 54.003'height=54.003px id=Layer_1 version=1.1 viewBox='0 0 19.438 54.003'width=19.438px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982";
|
||||
ptr +="C1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718";
|
||||
ptr +="c0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833";
|
||||
ptr +="c0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22";
|
||||
ptr +="s2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z'fill=#F29C21 /></g></svg>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='side-by-side text'>Temperature</div>";
|
||||
ptr +="<div class='side-by-side reading'>";
|
||||
ptr +=(int)temperature;
|
||||
ptr +="<span class='superscript'>°C</span></div>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='data humidity'>";
|
||||
ptr +="<div class='side-by-side icon'>";
|
||||
ptr +="<svg enable-background='new 0 0 29.235 40.64'height=40.64px id=Layer_1 version=1.1 viewBox='0 0 29.235 40.64'width=29.235px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><path d='M14.618,0C14.618,0,0,17.95,0,26.022C0,34.096,6.544,40.64,14.618,40.64s14.617-6.544,14.617-14.617";
|
||||
ptr +="C29.235,17.95,14.618,0,14.618,0z M13.667,37.135c-5.604,0-10.162-4.56-10.162-10.162c0-0.787,0.638-1.426,1.426-1.426";
|
||||
ptr +="c0.787,0,1.425,0.639,1.425,1.426c0,4.031,3.28,7.312,7.311,7.312c0.787,0,1.425,0.638,1.425,1.425";
|
||||
ptr +="C15.093,36.497,14.455,37.135,13.667,37.135z'fill=#3C97D3 /></svg>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='side-by-side text'>Humidity</div>";
|
||||
ptr +="<div class='side-by-side reading'>";
|
||||
ptr +=(int)humidity;
|
||||
ptr +="<span class='superscript'>%</span></div>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='data pressure'>";
|
||||
ptr +="<div class='side-by-side icon'>";
|
||||
ptr +="<svg enable-background='new 0 0 40.542 40.541'height=40.541px id=Layer_1 version=1.1 viewBox='0 0 40.542 40.541'width=40.542px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M34.313,20.271c0-0.552,0.447-1,1-1h5.178c-0.236-4.841-2.163-9.228-5.214-12.593l-3.425,3.424";
|
||||
ptr +="c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293c-0.391-0.391-0.391-1.023,0-1.414l3.425-3.424";
|
||||
ptr +="c-3.375-3.059-7.776-4.987-12.634-5.215c0.015,0.067,0.041,0.13,0.041,0.202v4.687c0,0.552-0.447,1-1,1s-1-0.448-1-1V0.25";
|
||||
ptr +="c0-0.071,0.026-0.134,0.041-0.202C14.39,0.279,9.936,2.256,6.544,5.385l3.576,3.577c0.391,0.391,0.391,1.024,0,1.414";
|
||||
ptr +="c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293L5.142,6.812c-2.98,3.348-4.858,7.682-5.092,12.459h4.804";
|
||||
ptr +="c0.552,0,1,0.448,1,1s-0.448,1-1,1H0.05c0.525,10.728,9.362,19.271,20.22,19.271c10.857,0,19.696-8.543,20.22-19.271h-5.178";
|
||||
ptr +="C34.76,21.271,34.313,20.823,34.313,20.271z M23.084,22.037c-0.559,1.561-2.274,2.372-3.833,1.814";
|
||||
ptr +="c-1.561-0.557-2.373-2.272-1.815-3.833c0.372-1.041,1.263-1.737,2.277-1.928L25.2,7.202L22.497,19.05";
|
||||
ptr +="C23.196,19.843,23.464,20.973,23.084,22.037z'fill=#26B999 /></g></svg>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='side-by-side text'>Pressure</div>";
|
||||
ptr +="<div class='side-by-side reading'>";
|
||||
ptr +=(int)pressure;
|
||||
ptr +="<span class='superscript'>hPa</span></div>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='data altitude'>";
|
||||
ptr +="<div class='side-by-side icon'>";
|
||||
ptr +="<svg enable-background='new 0 0 58.422 40.639'height=40.639px id=Layer_1 version=1.1 viewBox='0 0 58.422 40.639'width=58.422px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M58.203,37.754l0.007-0.004L42.09,9.935l-0.001,0.001c-0.356-0.543-0.969-0.902-1.667-0.902";
|
||||
ptr +="c-0.655,0-1.231,0.32-1.595,0.808l-0.011-0.007l-0.039,0.067c-0.021,0.03-0.035,0.063-0.054,0.094L22.78,37.692l0.008,0.004";
|
||||
ptr +="c-0.149,0.28-0.242,0.594-0.242,0.934c0,1.102,0.894,1.995,1.994,1.995v0.015h31.888c1.101,0,1.994-0.893,1.994-1.994";
|
||||
ptr +="C58.422,38.323,58.339,38.024,58.203,37.754z'fill=#955BA5 /><path d='M19.704,38.674l-0.013-0.004l13.544-23.522L25.13,1.156l-0.002,0.001C24.671,0.459,23.885,0,22.985,0";
|
||||
ptr +="c-0.84,0-1.582,0.41-2.051,1.038l-0.016-0.01L20.87,1.114c-0.025,0.039-0.046,0.082-0.068,0.124L0.299,36.851l0.013,0.004";
|
||||
ptr +="C0.117,37.215,0,37.62,0,38.059c0,1.412,1.147,2.565,2.565,2.565v0.015h16.989c-0.091-0.256-0.149-0.526-0.149-0.813";
|
||||
ptr +="C19.405,39.407,19.518,39.019,19.704,38.674z'fill=#955BA5 /></g></svg>";
|
||||
ptr +="</div>";
|
||||
ptr +="<div class='side-by-side text'>Altitude</div>";
|
||||
ptr +="<div class='side-by-side reading'>";
|
||||
ptr +=(int)altitude;
|
||||
ptr +="<span class='superscript'>m</span></div>";
|
||||
ptr +="</div>";
|
||||
ptr +="</div>";
|
||||
ptr +="</body>";
|
||||
ptr +="</html>";
|
||||
return ptr;
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
// Load Wi-Fi library
|
||||
#include <WiFi.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
|
||||
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||
|
||||
Adafruit_BME280 bme; // I2C
|
||||
//Adafruit_BME280 bme(BME_CS); // hardware SPI
|
||||
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
|
||||
|
||||
// Replace with your network credentials
|
||||
const char* ssid = "REPLACE_WITH_YOUR_SSID";
|
||||
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
|
||||
|
||||
// Set web server port number to 80
|
||||
WiFiServer server(80);
|
||||
|
||||
// Variable to store the HTTP request
|
||||
String header;
|
||||
|
||||
// Current time
|
||||
unsigned long currentTime = millis();
|
||||
// Previous time
|
||||
unsigned long previousTime = 0;
|
||||
// Define timeout time in milliseconds (example: 2000ms = 2s)
|
||||
const long timeoutTime = 2000;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
bool status;
|
||||
|
||||
// default settings
|
||||
// (you can also pass in a Wire library object like &Wire2)
|
||||
//status = bme.begin();
|
||||
if (!bme.begin(0x76)) {
|
||||
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
||||
while (1);
|
||||
}
|
||||
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
// Print local IP address and start web server
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected.");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
WiFiClient client = server.available(); // Listen for incoming clients
|
||||
|
||||
if (client) { // If a new client connects,
|
||||
currentTime = millis();
|
||||
previousTime = currentTime;
|
||||
Serial.println("New Client."); // print a message out in the serial port
|
||||
String currentLine = ""; // make a String to hold incoming data from the client
|
||||
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
|
||||
currentTime = millis();
|
||||
if (client.available()) { // if there's bytes to read from the client,
|
||||
char c = client.read(); // read a byte, then
|
||||
Serial.write(c); // print it out the serial monitor
|
||||
header += c;
|
||||
if (c == '\n') { // if the byte is a newline character
|
||||
// if the current line is blank, you got two newline characters in a row.
|
||||
// that's the end of the client HTTP request, so send a response:
|
||||
if (currentLine.length() == 0) {
|
||||
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||
// and a content-type so the client knows what's coming, then a blank line:
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-type:text/html");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
// Display the HTML web page
|
||||
client.println("<!DOCTYPE html><html>");
|
||||
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||
client.println("<link rel=\"icon\" href=\"data:,\">");
|
||||
// CSS to style the table
|
||||
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
|
||||
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
|
||||
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
|
||||
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
|
||||
client.println("tr:hover { background-color: #bcbcbc; }");
|
||||
client.println("td { border: none; padding: 12px; }");
|
||||
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
|
||||
|
||||
// Web Page Heading
|
||||
client.println("</style></head><body><h1>ESP32 with BME280</h1>");
|
||||
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
|
||||
client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
|
||||
client.println(bme.readTemperature());
|
||||
client.println(" *C</span></td></tr>");
|
||||
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
|
||||
client.println(1.8 * bme.readTemperature() + 32);
|
||||
client.println(" *F</span></td></tr>");
|
||||
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
|
||||
client.println(bme.readPressure() / 100.0F);
|
||||
client.println(" hPa</span></td></tr>");
|
||||
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
|
||||
client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
||||
client.println(" m</span></td></tr>");
|
||||
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
|
||||
client.println(bme.readHumidity());
|
||||
client.println(" %</span></td></tr>");
|
||||
client.println("</body></html>");
|
||||
|
||||
// The HTTP response ends with another blank line
|
||||
client.println();
|
||||
// Break out of the while loop
|
||||
break;
|
||||
} else { // if you got a newline, then clear currentLine
|
||||
currentLine = "";
|
||||
}
|
||||
} else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||
currentLine += c; // add it to the end of the currentLine
|
||||
}
|
||||
}
|
||||
}
|
||||
// Clear the header variable
|
||||
header = "";
|
||||
// Close the connection
|
||||
client.stop();
|
||||
Serial.println("Client disconnected.");
|
||||
Serial.println("");
|
||||
}
|
||||
}
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <Update.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
#pragma region BASIC
|
||||
|
||||
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
IPAddress localIp(192, 168, 0, 24); // IP dell' ESP32
|
||||
const char* description = "ESP32 Temperature server"; // Description
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region GLOBAL VARIABLES
|
||||
|
||||
hw_timer_t *wdt_temperature_zone = NULL; // Timer for single solenoid zone
|
||||
int mustReset = 0;
|
||||
|
||||
|
||||
static long wdt_survey_timeout = 600000; // Time in ms to trigger the watchdog (ten minutes) for solenoid zones
|
||||
|
||||
static bool wdt_survey_triggered = false; // Trigger for a single solenoid zone timer
|
||||
|
||||
static const char* ssid = "Melafonino di Claudio";
|
||||
static const char* password = "1234567890";
|
||||
|
||||
static const IPAddress gateway(192, 168, 0, 1); //IP del gateway
|
||||
|
||||
static const IPAddress subnet(255, 255, 255, 0); // Subnet Mask
|
||||
static const IPAddress primaryDNS(192, 168, 0, 1); // DNS
|
||||
|
||||
WebServer server(80);
|
||||
WebServer otaServer(8080);
|
||||
|
||||
String macAddress = "";
|
||||
|
||||
HTTPClient http;
|
||||
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region WIFI
|
||||
|
||||
void handle_WiFi(){
|
||||
|
||||
if ( WiFi.status() != WL_CONNECTED )
|
||||
{
|
||||
WiFi.begin(ssid, password);
|
||||
int WLcount = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && WLcount < 200 )
|
||||
{
|
||||
delay(100);
|
||||
++WLcount;
|
||||
}
|
||||
mustReset++;
|
||||
if(WiFi.status() != WL_CONNECTED && mustReset > 4)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mustReset = 0;
|
||||
server.handleClient();
|
||||
otaServer.handleClient();
|
||||
}
|
||||
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
#pragma region WEB SERVER OTA
|
||||
|
||||
void handleOtaRoot() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/html", "<form name='loginForm'>"
|
||||
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
||||
"<tr>"
|
||||
"<td colspan=2>"
|
||||
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
|
||||
"<br>"
|
||||
"</td>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"</tr>"
|
||||
"<td>Username:</td>"
|
||||
"<td><input type='text' size=25 name='userid'><br></td>"
|
||||
"</tr>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"<tr>"
|
||||
"<td>Password:</td>"
|
||||
"<td><input type='Password' size=25 name='pwd'><br></td>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"</tr>"
|
||||
"<tr>"
|
||||
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
|
||||
"</tr>"
|
||||
"</table>"
|
||||
"</form>"
|
||||
"<script>"
|
||||
"function check(form)"
|
||||
"{"
|
||||
"if(form.userid.value=='Fin3' && form.pwd.value==''passwordDaImpostarePerSicurezza)"
|
||||
"{"
|
||||
"window.open('/serverIndex')"
|
||||
"}"
|
||||
"else"
|
||||
"{"
|
||||
" alert('Error Password or Username')/*displays error message*/"
|
||||
"}"
|
||||
"}"
|
||||
"</script>");
|
||||
}
|
||||
|
||||
void handleOtaServerIndex() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/html", "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
|
||||
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
|
||||
"<input type='file' name='update'>"
|
||||
"<input type='submit' value='Update'>"
|
||||
"</form>"
|
||||
"<div id='prg'>progress: 0%</div>"
|
||||
"<script>"
|
||||
"$('form').submit(function(e){"
|
||||
"e.preventDefault();"
|
||||
"var form = $('#upload_form')[0];"
|
||||
"var data = new FormData(form);"
|
||||
" $.ajax({"
|
||||
"url: '/update',"
|
||||
"type: 'POST',"
|
||||
"data: data,"
|
||||
"contentType: false,"
|
||||
"processData:false,"
|
||||
"xhr: function() {"
|
||||
"var xhr = new window.XMLHttpRequest();"
|
||||
"xhr.upload.addEventListener('progress', function(evt) {"
|
||||
"if (evt.lengthComputable) {"
|
||||
"var per = evt.loaded / evt.total;"
|
||||
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
|
||||
"}"
|
||||
"}, false);"
|
||||
"return xhr;"
|
||||
"},"
|
||||
"success:function(d, s) {"
|
||||
"console.log('success!')"
|
||||
"},"
|
||||
"error: function (a, b, c) {"
|
||||
"}"
|
||||
"});"
|
||||
"});"
|
||||
"</script>");
|
||||
}
|
||||
|
||||
void handleOtaUpdate() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void otaDoUpdate() {
|
||||
HTTPUpload& upload = otaServer.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
/* flashing firmware to ESP*/
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (Update.end(true)) { //true to set the size to the current progress
|
||||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
} else {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
void handle_SurveyLoop(){
|
||||
|
||||
// Handling temperature zone timer trigger
|
||||
if(wdt_survey_triggered == true)
|
||||
{
|
||||
// execute code when timer trigger
|
||||
// put your code here
|
||||
}
|
||||
}
|
||||
|
||||
void handle_GetLights() {
|
||||
/*DynamicJsonDocument doc(400);
|
||||
|
||||
JsonObject light1obj = doc.createNestedObject();
|
||||
light1obj["LightId"] = light1_Id;
|
||||
light1obj["Enabled"] = light1_enabled;
|
||||
|
||||
JsonObject light2obj = doc.createNestedObject();
|
||||
light2obj["LightId"] = light2_Id;
|
||||
light2obj["Enabled"] = light2_enabled;
|
||||
|
||||
JsonObject light3obj = doc.createNestedObject();
|
||||
light3obj["LightId"] = light3_Id;
|
||||
light3obj["Enabled"] = light3_enabled;
|
||||
|
||||
String jsonData = "";
|
||||
serializeJson(doc, jsonData);
|
||||
server.send(200, "text/plain", jsonData);*/
|
||||
server.send(200, "text/plain", "Funziona!!!!");
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
if (!WiFi.config(localIp, gateway, subnet, primaryDNS)) {
|
||||
Serial.println("STA Failed to configure");
|
||||
}
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
Serial.println("");
|
||||
|
||||
wdt_temperature_zone = timerBegin(0, 8000, true); //timer 0, div 80
|
||||
//timerAttachInterrupt(wdt_temperature_zone, &onWdTimerElapsed_Zone, true);
|
||||
timerAlarmWrite(wdt_temperature_zone, wdt_survey_timeout * 10, true);
|
||||
timerAlarmEnable(wdt_temperature_zone);
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("ESP Board MAC Address: ");
|
||||
macAddress = WiFi.macAddress();
|
||||
Serial.println(macAddress);
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
//server.on("/", handleRoot);
|
||||
//server.on("/setTemperature", HTTP_POST, handle_SetTemperature);
|
||||
//server.on("/getTemperature", HTTP_GET, handle_GetTemperature);
|
||||
//server.on("/setLights", HTTP_POST, handle_SetLights);
|
||||
server.on("/getLights", HTTP_GET, handle_GetLights);
|
||||
//server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP 80 server started");
|
||||
|
||||
otaServer.on("/", HTTP_GET, handleOtaRoot);
|
||||
otaServer.on("/serverIndex", HTTP_GET, handleOtaServerIndex);
|
||||
otaServer.on("/update", HTTP_POST, handleOtaUpdate, otaDoUpdate);
|
||||
otaServer.begin();
|
||||
Serial.println("HTTP 8080 server started");
|
||||
Serial.println(localIp);
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
|
||||
handle_WiFi();
|
||||
|
||||
handle_SurveyLoop();
|
||||
|
||||
}
|
||||
|
||||
#pragma region Timer Functions
|
||||
|
||||
void IRAM_ATTR onWdTimerElapsed_Zone() {
|
||||
wdt_survey_triggered = true;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <Update.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BME280.h>
|
||||
|
||||
const char* host = "esp32";
|
||||
const char* ssid = "Melafonino di Claudio";
|
||||
const char* password = "1234567890";
|
||||
|
||||
Adafruit_BME280 bme;
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
/* Style */
|
||||
String style =
|
||||
"<style>#file-input,input{width:100%;height:44px;border-radius:4px;margin:10px auto;font-size:15px}"
|
||||
"input{background:#f1f1f1;border:0;padding:0 15px}body{background:#3498db;font-family:sans-serif;font-size:14px;color:#777}"
|
||||
"#file-input{padding:0;border:1px solid #ddd;line-height:44px;text-align:left;display:block;cursor:pointer}"
|
||||
"#bar,#prgbar{background-color:#f1f1f1;border-radius:10px}#bar{background-color:#3498db;width:0%;height:10px}"
|
||||
"form{background:#fff;max-width:258px;margin:75px auto;padding:30px;border-radius:5px;text-align:center}"
|
||||
".btn{background:#3498db;color:#fff;cursor:pointer}</style>";
|
||||
|
||||
/* Login page */
|
||||
String loginIndex =
|
||||
"<form name=loginForm>"
|
||||
"<h1>ESP32 Login</h1>"
|
||||
"<input name=userid placeholder='User ID'> "
|
||||
"<input name=pwd placeholder=Password type=Password> "
|
||||
"<input type=submit onclick=check(this.form) class=btn value=Login></form>"
|
||||
"<script>"
|
||||
"function check(form) {"
|
||||
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
|
||||
"{window.open('/serverIndex')}"
|
||||
"else"
|
||||
"{alert('Error Password or Username')}"
|
||||
"}"
|
||||
"</script>" + style;
|
||||
|
||||
/* Server Index Page */
|
||||
String serverIndex =
|
||||
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
|
||||
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
|
||||
"<input type='file' name='update' id='file' onchange='sub(this)' style=display:none>"
|
||||
"<label id='file-input' for='file'> Choose file...</label>"
|
||||
"<input type='submit' class=btn value='Update'>"
|
||||
"<br><br>"
|
||||
"<div id='prg'></div>"
|
||||
"<br><div id='prgbar'><div id='bar'></div></div><br></form>"
|
||||
"<script>"
|
||||
"function sub(obj){"
|
||||
"var fileName = obj.value.split('\\\\');"
|
||||
"document.getElementById('file-input').innerHTML = ' '+ fileName[fileName.length-1];"
|
||||
"};"
|
||||
"$('form').submit(function(e){"
|
||||
"e.preventDefault();"
|
||||
"var form = $('#upload_form')[0];"
|
||||
"var data = new FormData(form);"
|
||||
"$.ajax({"
|
||||
"url: '/update',"
|
||||
"type: 'POST',"
|
||||
"data: data,"
|
||||
"contentType: false,"
|
||||
"processData:false,"
|
||||
"xhr: function() {"
|
||||
"var xhr = new window.XMLHttpRequest();"
|
||||
"xhr.upload.addEventListener('progress', function(evt) {"
|
||||
"if (evt.lengthComputable) {"
|
||||
"var per = evt.loaded / evt.total;"
|
||||
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
|
||||
"$('#bar').css('width',Math.round(per*100) + '%');"
|
||||
"}"
|
||||
"}, false);"
|
||||
"return xhr;"
|
||||
"},"
|
||||
"success:function(d, s) {"
|
||||
"console.log('success!') "
|
||||
"},"
|
||||
"error: function (a, b, c) {"
|
||||
"}"
|
||||
"});"
|
||||
"});"
|
||||
"</script>" + style;
|
||||
|
||||
/* setup function */
|
||||
void setup(void) {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Connect to WiFi network
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
/*use mdns for host name resolution*/
|
||||
if (!MDNS.begin(host)) { //http://esp32.local
|
||||
Serial.println("Error setting up MDNS responder!");
|
||||
while (1) {
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("mDNS responder started");
|
||||
/*return index page which is stored in serverIndex */
|
||||
server.on("/", HTTP_GET, []() {
|
||||
server.sendHeader("Connection", "close");
|
||||
server.send(200, "text/html", loginIndex);
|
||||
});
|
||||
|
||||
server.on("/serverIndex", HTTP_GET, []() {
|
||||
server.sendHeader("Connection", "close");
|
||||
server.send(200, "text/html", serverIndex);
|
||||
});
|
||||
|
||||
server.on("/bme", HTTP_GET, []() {
|
||||
server.sendHeader("Connection", "close");
|
||||
server.send(200, "text/plain", "work!!");
|
||||
});
|
||||
|
||||
/*handling uploading firmware file */
|
||||
server.on("/update", HTTP_POST, []() {
|
||||
server.sendHeader("Connection", "close");
|
||||
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
||||
ESP.restart();
|
||||
}, []() {
|
||||
HTTPUpload& upload = server.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
/* flashing firmware to ESP*/
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (Update.end(true)) { //true to set the size to the current progress
|
||||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
} else {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
});
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
server.handleClient();
|
||||
delay(1);
|
||||
}
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <Update.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_BMP280.h>
|
||||
#include <LiquidCrystal_I2C.h>
|
||||
|
||||
#pragma region GLOBAL VARIABLES
|
||||
|
||||
int mustReset = 0;
|
||||
|
||||
static long wdt_survey_timeout = 10000; // Time in s to trigger the watchdog for solenoid zones
|
||||
static bool wdt_survey_triggered = false; // Trigger for a single solenoid zone timer
|
||||
|
||||
const char* description = "ESP32 Temperature server"; // Description
|
||||
|
||||
static const char* ssid = "XXX"; // SSID
|
||||
static const char* password = "XXX"; // Password Wifi
|
||||
|
||||
static const IPAddress gateway(172, 16, 83, 254); // IP del gateway
|
||||
static const IPAddress subnet(255, 255, 255, 0); // Subnet Mask
|
||||
static const IPAddress primaryDNS(172, 16, 94, 95); // DNS
|
||||
|
||||
hw_timer_t *wdt_survey = NULL; // Timer for single solenoid zone
|
||||
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
WebServer server(80);
|
||||
WebServer otaServer(8080);
|
||||
|
||||
IPAddress localIp(172, 16, 83, 12); // IP dell' ESP32
|
||||
String hostname = "XXX"; // Hostname
|
||||
String macAddress = "";
|
||||
|
||||
HTTPClient http;
|
||||
|
||||
LiquidCrystal_I2C lcd(0x27, 16, 2);
|
||||
|
||||
Adafruit_BME280 bme; // BME variable
|
||||
float Temperature;
|
||||
float Humidity;
|
||||
float Pressure;
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region WIFI
|
||||
|
||||
void handle_WiFi(){
|
||||
|
||||
if ( WiFi.status() != WL_CONNECTED )
|
||||
{
|
||||
WiFi.begin(ssid, password);
|
||||
int WLcount = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && WLcount < 200 )
|
||||
{
|
||||
delay(100);
|
||||
++WLcount;
|
||||
}
|
||||
mustReset++;
|
||||
if(WiFi.status() != WL_CONNECTED && mustReset > 4)
|
||||
{
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mustReset = 0;
|
||||
server.handleClient();
|
||||
otaServer.handleClient();
|
||||
}
|
||||
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
#pragma region WEB SERVER OTA
|
||||
|
||||
void handleOtaRoot() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/html", "<form name='loginForm'>"
|
||||
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
||||
"<tr>"
|
||||
"<td colspan=2>"
|
||||
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
|
||||
"<br>"
|
||||
"</td>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"</tr>"
|
||||
"<td>Username:</td>"
|
||||
"<td><input type='text' size=25 name='userid'><br></td>"
|
||||
"</tr>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"<tr>"
|
||||
"<td>Password:</td>"
|
||||
"<td><input type='Password' size=25 name='pwd'><br></td>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
"</tr>"
|
||||
"<tr>"
|
||||
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
|
||||
"</tr>"
|
||||
"</table>"
|
||||
"</form>"
|
||||
"<script>"
|
||||
"function check(form)"
|
||||
"{"
|
||||
"if(form.userid.value=='paladmin' && form.pwd.value==''passwordDaImpostarePerSicurezza)"
|
||||
"{"
|
||||
"window.open('/serverIndex')"
|
||||
"}"
|
||||
"else"
|
||||
"{"
|
||||
" alert('Error Password or Username')/*displays error message*/"
|
||||
"}"
|
||||
"}"
|
||||
"</script>");
|
||||
}
|
||||
|
||||
void handleOtaServerIndex() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/html", "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
|
||||
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
|
||||
"<input type='file' name='update'>"
|
||||
"<input type='submit' value='Update'>"
|
||||
"</form>"
|
||||
"<div id='prg'>progress: 0%</div>"
|
||||
"<script>"
|
||||
"$('form').submit(function(e){"
|
||||
"e.preventDefault();"
|
||||
"var form = $('#upload_form')[0];"
|
||||
"var data = new FormData(form);"
|
||||
" $.ajax({"
|
||||
"url: '/update',"
|
||||
"type: 'POST',"
|
||||
"data: data,"
|
||||
"contentType: false,"
|
||||
"processData:false,"
|
||||
"xhr: function() {"
|
||||
"var xhr = new window.XMLHttpRequest();"
|
||||
"xhr.upload.addEventListener('progress', function(evt) {"
|
||||
"if (evt.lengthComputable) {"
|
||||
"var per = evt.loaded / evt.total;"
|
||||
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
|
||||
"}"
|
||||
"}, false);"
|
||||
"return xhr;"
|
||||
"},"
|
||||
"success:function(d, s) {"
|
||||
"console.log('success!')"
|
||||
"},"
|
||||
"error: function (a, b, c) {"
|
||||
"}"
|
||||
"});"
|
||||
"});"
|
||||
"</script>");
|
||||
}
|
||||
|
||||
void handleOtaUpdate() {
|
||||
otaServer.sendHeader("Connection", "close");
|
||||
otaServer.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void otaDoUpdate() {
|
||||
HTTPUpload& upload = otaServer.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
/* flashing firmware to ESP*/
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (Update.end(true)) { //true to set the size to the current progress
|
||||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
} else {
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma endregion
|
||||
|
||||
void handle_SurveyLoop(){
|
||||
|
||||
// Handling temperature zone timer trigger
|
||||
if(wdt_survey_triggered == true)
|
||||
{
|
||||
portENTER_CRITICAL(&timerMux);
|
||||
wdt_survey_triggered = false;
|
||||
portEXIT_CRITICAL(&timerMux);
|
||||
|
||||
//Serial.println("Zio mestre!"); // EasterEgg
|
||||
|
||||
Temperature = bme.readTemperature();
|
||||
Humidity = bme.readHumidity();
|
||||
Pressure = bme.readPressure();
|
||||
}
|
||||
}
|
||||
|
||||
void handleRoot() {
|
||||
server.send(200, "text/plain", hostname + " - Climavey");
|
||||
}
|
||||
|
||||
void handle_GetValue() {
|
||||
DynamicJsonDocument doc(400);
|
||||
|
||||
JsonObject obj = doc.createNestedObject("Values");
|
||||
obj["Temperature"] = Temperature; // Temperature
|
||||
obj["Humidity"] = Humidity; // Humidity
|
||||
obj["Pressure"] = Pressure; // Pressure
|
||||
obj["macAddress"] = macAddress; // macAddress
|
||||
|
||||
String jsonData = "";
|
||||
serializeJson(doc, jsonData);
|
||||
server.send(200, "text/plain", jsonData);
|
||||
|
||||
//server.send(200, "text/plain", "{ \"Values\": { \"Temperature\": " + Temperature + ", \"Humidity\": " + Humidity + ", \"Pressure\": " + Pressure + " } }" );
|
||||
|
||||
/*server.send(200, "text/plain",
|
||||
String(Temperature) + ";"
|
||||
+ String(Humidity) + ";"
|
||||
+ String(Pressure) + ";"
|
||||
+ macAddress
|
||||
);*/
|
||||
|
||||
}
|
||||
|
||||
void handleNotFound(){
|
||||
server.send(404, "text/plain", "404: Not found");
|
||||
}
|
||||
|
||||
#pragma region Timer Functions
|
||||
|
||||
void IRAM_ATTR onWdTimerElapsed() {
|
||||
portENTER_CRITICAL_ISR(&timerMux);
|
||||
wdt_survey_triggered = true;
|
||||
portEXIT_CRITICAL_ISR(&timerMux);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region Setup
|
||||
|
||||
void bmeSetup() {
|
||||
bool status = bme.begin(0x76);
|
||||
if (!status) {
|
||||
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
||||
}
|
||||
}
|
||||
|
||||
void webServer() {
|
||||
if (!WiFi.config(localIp, gateway, subnet, primaryDNS)) {
|
||||
Serial.println("STA Failed to configure");
|
||||
}
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setHostname(hostname.c_str());
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
Serial.println("");
|
||||
|
||||
wdt_survey = timerBegin(0, 80, true); //timer 0, div 80
|
||||
timerAttachInterrupt(wdt_survey, &onWdTimerElapsed, true);
|
||||
timerAlarmWrite(wdt_survey, wdt_survey_timeout * 1000, true);
|
||||
timerAlarmEnable(wdt_survey);
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("ESP Board MAC Address: ");
|
||||
macAddress = WiFi.macAddress();
|
||||
Serial.println(macAddress);
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
server.on("/", HTTP_GET, handleRoot);
|
||||
server.on("/getValue", HTTP_GET, handle_GetValue);
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP 80 server started");
|
||||
|
||||
otaServer.on("/", HTTP_GET, handleOtaRoot);
|
||||
otaServer.on("/serverIndex", HTTP_GET, handleOtaServerIndex);
|
||||
otaServer.on("/update", HTTP_POST, handleOtaUpdate, otaDoUpdate);
|
||||
otaServer.begin();
|
||||
Serial.println("HTTP 8080 server started");
|
||||
}
|
||||
|
||||
void lcdInitilizer(){
|
||||
lcd.init();
|
||||
lcd.backlight();
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
void setup(void) {
|
||||
Serial.begin(9600);
|
||||
|
||||
bmeSetup();
|
||||
lcdInitilizer();
|
||||
webServer();
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
handle_WiFi();
|
||||
handle_SurveyLoop();
|
||||
|
||||
lcd.setCursor(0, 0);
|
||||
lcd.print("Hello, World!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
// print a msg Hello word
|
||||
|
||||
//#include <iostream>
|
||||
//
|
||||
//int main(){
|
||||
// std::cout << "Hello World!";
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
|
||||
|
||||
for (const string& word : msg)
|
||||
{
|
||||
cout << word << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "- Sum of Two integers -" << endl;
|
||||
|
||||
int fistNumber, secondNumber, sumOfTwoNumbers;
|
||||
|
||||
cout << "Int1: ";
|
||||
cin >> fistNumber;
|
||||
cout << "Int2: ";
|
||||
cin >> secondNumber;
|
||||
|
||||
// sum of two numbers in stored in variable sumOfTwoNumbers
|
||||
sumOfTwoNumbers = fistNumber + secondNumber;
|
||||
|
||||
// Print sum
|
||||
cout << fistNumber << " + " << secondNumber << " = " << sumOfTwoNumbers << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "- Quotient & Remainder -" << endl;
|
||||
|
||||
int divisor, dividend, quotient, remainder;
|
||||
|
||||
cout << "Enter dividend: ";
|
||||
cin >> dividend;
|
||||
|
||||
cout << "Enter divisor: ";
|
||||
cin >> divisor;
|
||||
|
||||
quotient = dividend / divisor;
|
||||
remainder = dividend % divisor;
|
||||
|
||||
cout << "Quotient = " << quotient << endl;
|
||||
cout << "Remainder = " << remainder << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,30 @@
|
||||
// C++ Swap Number
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 5, b = 10, c = 5, d = 10, temp;
|
||||
|
||||
cout << "Swap Numbers (Using Temporary Variable)" << "\nBefore swapping." << endl;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
|
||||
temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
|
||||
cout << "\nAfter swapping." << endl;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
|
||||
cout << "\nSwap Numbers Without Using Temporary Variable" << "\nBefore swapping." << endl;
|
||||
cout << "a = " << c << ", b = " << d << endl;
|
||||
|
||||
c = c + d;
|
||||
d = c - d;
|
||||
c = c - d;
|
||||
|
||||
cout << "\nAfter swapping." << endl;
|
||||
cout << "a = " << c << ", b = " << d << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
char c;
|
||||
cout << "Enter a character: ";
|
||||
cin >> c;
|
||||
cout << "ASCII Value of " << c << " is " << int(c);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
#include <esp_now.h>
|
||||
#include <WiFI.h>
|
||||
#include <SimpleDHT.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <Arduino_SNMP.h>
|
||||
#include <SSD1306.h>
|
||||
#include <Fs.h>
|
||||
#include <SPIFS.h>
|
||||
|
||||
#define DHTPIN 4
|
||||
#define INTERVAL 100
|
||||
#define ESPNOW_CHANNEL 1
|
||||
#define CONFIG_PATH "/conf.bin"
|
||||
#define SSID "SSID"
|
||||
#define PASSWORD "12345678"
|
||||
#define IP "192.168.0.134"
|
||||
|
||||
WiFiUDP udp;
|
||||
SNMPAgent snmp = SNMPAgent("public"); //Inicia o SMMPAgent
|
||||
|
||||
//Referências para o SNMP
|
||||
char* strHumidity;
|
||||
char* strTemperature;
|
||||
|
||||
//Valores caso nada esteja salvo no arquivo de configuração
|
||||
int maxHumidity = 65;
|
||||
int minHumidity = 55;
|
||||
|
||||
//parametros: address,SDA,SCL
|
||||
SSD1306 display(0x3c, 21, 22); //construtor do objeto que controlaremos o display
|
||||
uint8_t slaveMacAddress[] = {0x1A,0xFE,0x34,0xA5,0x90,0x69};
|
||||
// uint8_t slaveMacAddress[] = {0x18,0xFE,0x34,0xA5,0x90,0x69};
|
||||
|
||||
esp_now_peer_info_t slave;
|
||||
|
||||
//Objeto que realiza a leitura da umidade
|
||||
SimpleDHT22 dht;
|
||||
|
||||
//Variável para guardarmos o valor da umidade
|
||||
float humidity = 0;
|
||||
|
||||
//Variável para guardarmos o valor da temperatura
|
||||
float temperature = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
strHumidity = (char*)malloc(6);
|
||||
strTemperature = (char*)malloc(6);
|
||||
memset(strHumidity, 0, 6);
|
||||
memset(strTemperature, 0, 6);
|
||||
|
||||
if(SPIFFS.begin(true))
|
||||
{
|
||||
loadConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
//Se não conseguiu inicializar
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
}
|
||||
|
||||
setupWiFi();
|
||||
setupSNMP();
|
||||
setupDisplay();
|
||||
setupESPNow();
|
||||
setupSlave();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
readSensor();
|
||||
verifySNMP();
|
||||
showOnDisplay();
|
||||
verifyHumidity();
|
||||
delay(INTERVAL);
|
||||
}
|
||||
|
||||
void setupWiFi()
|
||||
{
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(SSID);
|
||||
|
||||
//Configura o IP
|
||||
IPAddress ipAddress;
|
||||
ipAddress.fromString(IP);
|
||||
WiFi.config(ipAddress, WiFi.gatewayIP(), WiFi.subnetMask());
|
||||
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void setupSNMP()
|
||||
{
|
||||
//Inicializa o snmp
|
||||
snmp.setUDP(&udp);
|
||||
snmp.begin();
|
||||
//Adiciona o OID para umidade (apenas leitura)
|
||||
snmp.addStringHandler(".1.3.6.1.4.1.12345.0", &strHumidity, false);
|
||||
//Adiciona o OID para temperatura (apenas leitura)
|
||||
snmp.addStringHandler(".1.3.6.1.4.1.12345.1", &strTemperature, false);
|
||||
//Adiciona o OID para umidade máxima (leitura e escrita)
|
||||
snmp.addIntegerHandler(".1.3.6.1.4.1.12345.2", &maxHumidity, true);
|
||||
//Adiciona o OID para umidade mínima (leitura e escrita)
|
||||
snmp.addIntegerHandler(".1.3.6.1.4.1.12345.3", &minHumidity, true);
|
||||
}
|
||||
|
||||
void setupDisplay(){
|
||||
display.init(); //inicializa o display
|
||||
display.flipScreenVertically();
|
||||
display.setFont(ArialMT_Plain_10); //configura a fonte
|
||||
}
|
||||
|
||||
void setupESPNow() {
|
||||
//Se a inicialização foi bem sucedida
|
||||
if (esp_now_init() == ESP_OK) {
|
||||
Serial.println("ESPNow Init Success");
|
||||
}
|
||||
//Se houve erro na inicialização
|
||||
else {
|
||||
Serial.println("ESPNow Init Failed");
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void verifySNMP()
|
||||
{
|
||||
//Deve ser sempre chamado durante o loop principal
|
||||
snmp.loop();
|
||||
|
||||
//Se aconteceu alteração de um dos valores
|
||||
if(snmp.setOccurred)
|
||||
{
|
||||
//Salva as os valores
|
||||
saveConfig();
|
||||
//Reseta a flag de alteração
|
||||
snmp.resetSetOccurred();
|
||||
}
|
||||
}
|
||||
|
||||
//Verifica se a umidade está fora dos limites e informa ao ESP8266
|
||||
//se o relê deve ficar ligado ou desligado
|
||||
void verifyHumidity(){
|
||||
if(humidity > maxHumidity)
|
||||
{
|
||||
sendRelayStatus(LOW);
|
||||
}
|
||||
else if(humidity < minHumidity)
|
||||
{
|
||||
sendRelayStatus(HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
//Função responsável por realizar a leitura
|
||||
//da umidade e temperatura
|
||||
void readSensor(){
|
||||
float h, t;
|
||||
int status = dht.read2(DHTPIN, &t, &h, NULL);
|
||||
|
||||
if (status == SimpleDHTErrSuccess) {
|
||||
humidity = h;
|
||||
temperature = t;
|
||||
//Transforma os dados em string
|
||||
String strH = String(humidity);
|
||||
strH.toCharArray(strHumidity, strH.length());
|
||||
String strT = String(temperature);
|
||||
strT.toCharArray(strTemperature, strT.length());
|
||||
}
|
||||
}
|
||||
|
||||
//Mostra a umidade no display
|
||||
void showOnDisplay(){
|
||||
//apaga o conteúdo do display
|
||||
display.clear();
|
||||
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
||||
display.setFont(ArialMT_Plain_16);
|
||||
|
||||
display.drawString(0, 0, "Humidity: ");
|
||||
display.drawString(70, 0, String(humidity));
|
||||
display.drawString(0, 30, "Temperat: ");
|
||||
display.drawString(75, 30, String(temperature));
|
||||
display.display(); //mostra o conteúdo na tela
|
||||
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
{
|
||||
Serial.println("saveConfig");
|
||||
//Abre o arquivo para escrita
|
||||
File file = SPIFFS.open(CONFIG_PATH, FILE_WRITE);
|
||||
|
||||
//Se não conseguiu abrir/criar o arquivo
|
||||
if(!file)
|
||||
{
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
file.seek(0);
|
||||
file.write((uint8_t*)&maxHumidity, sizeof(maxHumidity));
|
||||
file.write((uint8_t*)&minHumidity, sizeof(minHumidity));
|
||||
//Fecha o arquivo
|
||||
file.close();
|
||||
}
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
Serial.println("loadConfig");
|
||||
File file = SPIFFS.open(CONFIG_PATH, FILE_READ);
|
||||
|
||||
//Se arquivo não existe
|
||||
if(!file)
|
||||
{
|
||||
//Na primeira vez o arquivo ainda não foi criado
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
file.read((uint8_t*)&maxHumidity, sizeof(maxHumidity));
|
||||
file.read((uint8_t*)&minHumidity, sizeof(minHumidity));
|
||||
//Fecha o arquivo
|
||||
file.close();
|
||||
}
|
||||
Reference in New Issue
Block a user