In a previous post, I’ve already introduced the UIPEthernet library, that allows to reuse the sketchesdesigned 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.
For more details,please refer to original post