In a previous post, I’ve already introduced the UIPEthernet library, that allows to reuse the sketches designed for the official ethernet shield also with the ENC28J60 chip.
Some days ago Giorgio (official site), an Arduino maker from the Province of Turin, kindly sent me one of his sketches which uses that library to publish a webserver which let you change the status of 4 outputs with an AJAX interface:
The idea to use AJAX calls comes from a tutorial found on the StartingElectronics site.
It’s not possible to use both the UIPEthernet and the SD libraries (together they consume too much memory), the HTML page is stored in the program memory (PROGMEM) as a char array using the common sintax for C strings (the new line char has been replaced with ‘\n’ and the quotes have been inserted with the escape char ‘\’).
The printPage(const char *str) function retrieves the page from the program memory and sends it to the client.
To avoid hangs, it’s suggested to increase the request buffer:
#define REQ_BUF_SZ 120 |
and add 10ms delay before sending the file.
To save some RAM memory, you can take advantage of the F() macro that allows to save in the program memory all the static strings.
To prevent the browser from caching the AJAX page (without retrieving it from Arduino every some seconds), Giorgio added to the page URL a random variable, that changes at each request:
All the requests are visible using the serial monitor:
Sketch and documentation
The sketch is published on Github… thanks Giorgio!
You can also read the UIPEthernet library’s official thread on the Arduino forum, good starting point for information and suggestions!
It’s suggested to always use the latest version of the library; moreover – to save some RAM memory – you can disable some protocols (UDP, DHCP) if not used: in the Utility subfolder of the library’s main folder change the uipethernet-conf.h file:
UIP_CONF_UDP = 0 UIP_CLIENT_TIMER = -1 UIP_CONF_MAX_CONNECTIONS = 1
The first line disable the UDP protocol, the second (library version 1.08 or newer) frees some more RAM and the third limits the number of concurrent connections to 1.
Salve, devo dire che i tuoi articoli sono davvero molto interessanti.
Ho provato ad usare la libreria UIPEthernet e sono riuscito a far funzionare gli esempi che hai proposto.
Ho solo un piccolo grande problema: tutti gli esempi che provato, funzionano perfettamente in rete Locale LAN ma se provo ad indirizzare arduino tramite WEB, non riesco ad accedervi.
Mi spiego meglio: connettendomi(dallo smartphone tramite 3g) al mio indirizzo ip pubblico es: “75.10.25.111:4665” non riesco ad accedere al webserver, nonostante abbia abilitato la porta TCP remota 4665 all’indirizzo ip locale di Arduino + ENC28j60.
Ora quello che volevo capire è se per caso la Libreria UIPEthernet non sia in grado di gestire connessioni tcp/ip dal web oppure c’è qualcosa che non va nella mia configurazione.
Grazie
Ciao Davide, Arduino non sa neppure che la connessione non arriva dalla rete locale: il tuo router se configurato correttamente fa NAT quindi Arduino pensa di parlare con il router (che poi gira le connessioni al vero client). Questo per dire che sicuramente il problema sta o nel router o nel fatto che la porta che hai scelto viene filtrata (non tutti i providers consentono connessioni dall’esterno su porte non standard).
ho utilizzato con successo questo progetto per chreare una presa comandata da remoto, ma ho aggiunto un display per visualizzare se e connesso o no alla rete perche ho inserito un client wifi. il tutto funziona ma vorrei fare del codice che faccia un ping , ma ho poche basi per fare questo. mi potresti aiutare? grazie in anticipo
Ciao Domenico, sto preparando un tutorial proprio con questa tematica (PING).
Buongiorno Luca,
ho collegato una scheda ENC28J60 con un ARDUINO NANO EVERY e vorrei eseguire il ping verso un dispositivo collegato alla mia rete locale. Ho provato molte soluzioni che ho trovato, ma non riesco a farle funzionare.
Sarei interessato a leggere il tuo tutorial per la tematica PING. Come posso averne accesso?
grazie
Ciao Luca, complimenti! Punto (e virgola, vista la tematica!).
Ho caricato lo sketch e funziona benissimo in locale ed in remoto.
Vorrei fosse controllato l’accesso ma altri esempi usano la WebServer e non riesco ad implementare.
Inoltre, vorrei essere sicuro di spegnere il controllo. comunque, ad un certo punto. Pensi che col timer interno sia possibile o col timer “cinese” che ho ordinato oppure pesa troppo sulla ram?
Ciao Nino! per l’autenticazione guarda questo mio tutorial. Io utilizzerei un RTC esterno collegato ad arduino, così sei sicuro che se anche Arduino viene riavviato, il timer continua a funzionare e ti da sempre l’ora esatta.
Ciao Luca, grazie per la tempestiva risposta.
Mi accingo a provare e ti faccio sapere.
Avevo dubbi sull’implementazione del codice descritto nel tutorial 19 poiché non c’è scheda SD per memorizzare la pagina web e nel tutorial hai espresso preoccupazioni per la saturazione della RAM.
Ho abilito parti del codice relativi ad alcuni attuatori (mi serve attivare un relais, al massimo due) spero che l’aggiunta non faccia bloccare tutto…
Ciao Luca,
non sono riuscito a fare le prove di “sicurezza”.
Però mi succede un’altra cosa e non so se è normale, vista la mia ignoranza.
Ho modificato lo sketch per utilzzare un solo pulsante per accendere due led. Provando da un client in rete locale, dato il comando di accensione, accesi i led, chiuso il client, e dopo attivato il monitor seriale, si spengono i led. Se non capisco male, l’apertura del monitor “resetta” il client. Che dici?
confermo, ogni volta che apri il serial monitor arduino si resetta
Actually I want to acquire data from 6 to 7 arduinos using enc28j60 ethernet shields. I want the information to be displayed on single web page I shall dedicate single PC to fetch information from all these arduinos. I think I shall need ajax web page which shall fetch required data from each arduinos..Can you guide me how to proceed.?
Amit, the best way to do it in my opinion is to use an MQTT broker and an ajax page that fetches data from it… you can find some tutorials on my website and on the Internet