Buonasera a tutti,
Premettendo che sono alle prime armi, vorrei sapere come accendere e spegnere un led collegato alla porta GPIO2 di un modulo ESP8266-01, attraverso l'uso di due link sulla pagina web inserita nel codice allegato.
Descrizione del codice:
Il codice seguente è caricato su un modulo ESP8266-01.
Il modulo, ricevendo dati da una scheda Arduino uno, permette di disegnare un grafico su una pagina web con 20 letture di un sensore.
Io vorrei avere la possibilità di accendere o spegnere un led collegato alla porta GPIO2 del modulo, cliccando sul link led_on oppure led_off, in qualsiasi momento.
Fatto nella maniera come sotto allegato, se clicco su uno dei due link vengo reindirizzato in una pagina con errore 404.
I link in questione sono "LED_ON" e "LED_OFF".
Nel momento in cui clicco su uno dei due link, vengo indirizzato in una pagina con filenotfound.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "^.^"; // ssid Wi-Fi
const char* password = "^.^"; // password
ESP8266WebServer server (80);
const int led = 2;
String data;
String dati[]={""};
String readString;
int hum=0;
int letture=0;
void HomePage() {
char temp[600];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf (temp, 600,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>Web Monitor</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>ESP8266 MONITORAGGIO METEO</h1>\
<p>Tempo di aggiornamento: %02d:%02d:%02d</p>\
<p>Hum: %02d</p>\
<img src=\"/grafico.svg\" />\
<hr />\
\
<a href=\"/LED_On\">LED ACCESO</a>\
<a href=\"/LED_Off\">LED SPENTO</a>\
\
</body>\
</html>", hr, min % 60, sec % 60,hum);
}
void FileNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName (i) + ": " + server.arg (i) + "\n";
}
server.send (404, "text/plain", message);
}
void setup (void) {
pinMode (led, OUTPUT);
digitalWrite (led, 0);
Serial.begin (115200);
WiFi.begin (ssid, password);
Serial.println ("");
// attesa per la connessione
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.print (".");
}
Serial.println ("");
Serial.print ("Connessione a: ");
Serial.println (ssid);
Serial.print ("DHCP IP: ");
Serial.println (WiFi.localIP());
if (MDNS.begin ("esp8266")) {
Serial.println ("MDNS responder");
}
server.on ("/", HomePage);
server.on ("/grafico.svg", grafico);
server.on ("/inline", []()
{
server.send (200, "text/plain", "OK");
});
server.onNotFound (FileNotFound);
server.begin();
Serial.println ("HTTP server avviato");
}
void loop (void) {
if (Serial.available())
{
data = Serial.readString();
char charBuf[20];
data.toCharArray(charBuf, 20);
char * pch;
pch = strtok (charBuf,";");
int count = 0;
while (pch != NULL)
{
dati[count]= pch;
pch = strtok (NULL, ";");
hum = dati[0].toInt();
count++;
}
}
server.handleClient();
}
byte riga[20];
void grafico()
{
letture++;
if (letture == 20) letture = 0;
riga[letture]=hum;
String out = "";
char temp[200];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<line x1=\"0\" y1=\"22\" x2=\"400\" y2=\"22\" style=\"stroke:rgb(0,0,0);stroke-width:1\" />";
out += "<line x1=\"0\" y1=\"52\" x2=\"400\" y2=\"52\" style=\"stroke:rgb(0,0,0);stroke-width:1\" />";
out += "<line x1=\"0\" y1=\"82\" x2=\"400\" y2=\"82\" style=\"stroke:rgb(0,0,0);stroke-width:1\" />";
out += "<line x1=\"0\" y1=\"112\" x2=\"400\" y2=\"112\" style=\"stroke:rgb(0,0,0);stroke-width:1\" />";
out += "<line x1=\"0\" y1=\"142\" x2=\"400\" y2=\"142\" style=\"stroke:rgb(0,0,0);stroke-width:1\" />";
out += "<text x=\"0\" y=\"51\" fill=\"blue\">50</text> />";
out += "<text x=\"0\" y=\"81\" fill=\"blue\">30</text> />";
out += "<text x=\"0\" y=\"111\" fill=\"blue\">20</text> />";
out += "<text x=\"0\" y=\"141\" fill=\"blue\">10</text> />";
out += "<g stroke=\"black\">\n";
int y = 20 * 2;
int inc=0;
for (int x = 0; x < (letture*20); x+=20)
{
inc = x/20;
int y2 = riga[inc] * 2;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"red\" stroke-width=\"2\" />\n", x, 140 - y, x + 20, 140 - y2);
out += temp;
sprintf(temp, "<circle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"black\"/>\n", x+20, 140 - y2, 2);
out += temp;
y=y2;
}
out += "</g>\n</svg>\n";
server.send (200, "image/svg+xml", out);
}
La mia domanda è:
- Come devo modificare il codice in modo tale da permettermi di accendere/spegnere il LED cliccando su uno dei due link?
In attesa di una vostra risposta, vi ringrazio tantissimo in anticipo.
Saluti da Davide!