ARDUINO
Come al solito, lo sketch completo è disponibile nel repository GitHub di questo tutorial.
Lo sketch visualizzerà la seguente pagina html:
Ad un click sull’icona del led, verrà richiamata la pagina con il parametro ?LEDx che indicherà ad Arduino di cambiare lo stato (e il colore della relativa icona) del led.
Vediamo gli elementi principali:
if(strstr((char *)Ethernet::buffer + pos, "GET /led_off.png") != 0) send_png_image(led_off, sizeof(led_off)); else if(strstr((char *)Ethernet::buffer + pos, "GET /led_on.png") != 0) send_png_image(led_on, sizeof(led_on)); |
Lo sketch analizza la richiesta del browser e – se è relativa ad una delle due immagini – chiama il metodo per inviare tale risorsa:
void send_png_image(PGM_P png_image, unsigned int image_size) { BufferFiller bfill = ether.tcpOffset(); bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n" "Content-Type: image/png\r\n\r\n")); bfill.emit_raw_p(png_image, image_size); ether.httpServerReply(bfill.position()); } |
Il metodo prepara la risposta costruendo l’header e utilizza il metodo emit_raw_p() per aggiungere alla risposta i bytes dell’immagine; infine risponde al browser con il metodo httpServerReply.
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED1") != 0) { led1Status = !led1Status; digitalWrite(LED1PIN, led1Status); } |
Se invece la richiesta del browser contiene ?LEDx, viene cambiato lo stato del led; infine viene costruita la pagina html con le icone corrette in base a tale stato:
Thank you for your tutorials very informative.
would it be the case to add more leds by putting more lines on with led2, led3 and so on.
regards
andy
Ciao Luca,
ho caricato lo sketch su Arduino, ma quando dal browser inserisco l’IP per visualizzare la pagina mi dice “impossibile collegarsi a 192.168.1.2”
Sapresti dirmi come posso risolvere il problema???
grazie 🙂
ciao Andrea
la tua rete locale ha indirizzi 192.168.1.x? Il tuo PC che indirizzo IP ha? Altrimenti va adattato lo sketch in modo che Arduino abbia un IP sulla tua rete…
Ciao Luca,
prima di tutto voglio farti i complimenti, in internet si trovano pochissime info (in ialiano) su ENC28J60 e arduino, ho seguito questo tutorial e ha funzionato tutto. Vorrei gestire la mia centralina per caldaie a biomassa (arduino) tramite browser, ma non riesco a capire come. Vorrei gestire tramite un form la temperatura di soglia, l’intervallo di tempo per l’azionamento della cloclea… e visualizzare la temperatura in tempo reale. Mi potresti aiutare indicandomi una guida anche in inglese? Grazie
Ciao Giovanni,
temo che una guida già pronta non ci sia, il mio consiglio é: inizia a progettare la pagina HTML “statica” e poi renderla dinamica lato Arduino… come te la cavi in HTML? Tieni anche conto che Arduino ha parecchie limitazioni, se vuoi qualcosa di evoluto potresti pensare di usare Arduino Yun.
hello sir..
this tutuorial was verry useful but how can we add more then two led’s…i need to add 4 in my project…please help sir…
hello sir…
first of all i would like to thank u for this awesome tutorial..it really helped me a lot…
i want to send temperature readings from my lm35 to the same web page…and also from my pir sensor…so how can i add the code for the same in the above web page??
thanksss..
Hello Luca,
Greetings for the day!
with reference to the above led code, i have integrated the code for lm 35 temperature sensor and pir sensor in the same web page. the problem i am facing is that the page is not refreshed even if reading of temp sensor or pir sensor changes. I included the line “” but due to this the led whose status was changed last keeps on getting continuously on and off after every after 5 seconds.How can i get through with dis problem..??
Looking forward to a positive response
Regards
Hi! please share your code…
Hello,
I’m writing a program to control 3 fan in manual and automatic mode, the problem I have is that sometimes I add one dummy line and the software crashes.
The software is based in your tutorials.
Could you contact me in my personal email, I could send you more details and code.
thank you.
Sempre grazie per diffondere conoscenza …. ottengo questi errori dante la compilazione !!! Mi potete aiutare ?
Complimenti e Saluti Willy
Arduino:1.6.5 (Windows 8.1), Scheda:”Arduino/Genuino Uno”
WebLeds.ino:6: error: variable ‘led_off’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
WebLeds.ino:26: error: variable ‘led_on’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
variable ‘led_off’ must be const in order to be put into read-only section by means of ‘__attribute__((progmem))’
Questo report potrebbe essere più ricco
di informazioni con
“Mostra un output dettagliato durante la compilazione”
abilitato in “File > Impostazioni”
Thanks for sharing your codes. It has helped me a lot. But in this jercicio I get this error:
6: error: ‘prog_char’ does not name a type
PROGMEM prog_char led_off[]= {
^
26: error: ‘prog_char’ does not name a type
PROGMEM prog_char led_on[]= {
^
ino: In function ‘void loop()’:
82: error: ‘led_off’ was not declared in this scope
send_png_image(led_off, sizeof(led_off));
^
84: error: ‘led_on’ was not declared in this scope
send_png_image(led_on, sizeof(led_on));
^
exit status 1
‘prog_char’ does not name a type
..help please
Hi! it seems that “prog_char” has been deprecated… replace it with “const char” and everything should work fine!