As mentioned last time I want to make sure that the codes sensors can be stored in the EEPROM , all from a web interface !
Here’s the code that does this thing :
#include <String.h> #include <Time.h> #include <SPI.h> #include <Ethernet.h> #include <EEPROM.h> #include <RCSwitch.h> byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; int SERIAL_BAUD = 9600; EthernetServer server(8081); RCSwitch mySwitch = RCSwitch(); int RECEIVE_PIN = 0; int nSensori = 5; void setup() { Serial.begin(SERIAL_BAUD); setupComm(); mySwitch.enableReceive(RECEIVE_PIN); } void loop() { getClientConnection(); } void getClientConnection(){ EthernetClient client = server.available(); if (client) { String postString =""; Serial.println("nuova richiesta"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if(postString.length()<15){ postString +=c; } if (c == 'n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<h1>Configurazione</h1>"); client.print("<br>"); for (int i=0; i<nSensori; i++) { String linkCompleto = ""; linkCompleto = "<a href="./?save"+ String(i); linkCompleto +="">Save Sensor " + String(i); linkCompleto += "</a><br/>"; client.println(linkCompleto); Serial.println(linkCompleto); //client.println("<a href="./?save1">Salva Sensore 1</a>"); } client.println("<br/>"); client.println("<a href="./?elenco">Sensors list</a>"); client.println("</html>"); break; //} } if (c == 'n') { // you're starting a new line currentLineIsBlank = true; } else if (c != 'r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } //fine client.connected Serial.println("-------------"); Serial.println(postString); Serial.println("-------------"); if(postString.indexOf("?save") > 0){ int indexSave = postString.indexOf("?save"); Serial.println("indexOf"); Serial.println(indexSave); //cerco valore del sensore String sSensore = postString.substring(indexSave+5 ,indexSave+6); Serial.println("Sensore"); Serial.println(sSensore); int iSensore = sSensore.toInt(); long valore = salvaSensore(iSensore); client.println("<br/>"); client.println("<p>Salvataggio sensore effettuato</p>"); client.println("<br/>"); client.println("Codice sensore " + sSensore); client.print(valore); } if(postString.indexOf("?elenco") > 0){ for (int i=0; i<nSensori; i++) { client.println("<p>Sensor" + String(i)+" : </p>"); client.print(String(EEPROMReadlong(i*4))); Serial.println(EEPROMReadlong(i*4)); } } delay(1); // close the connection: client.stop(); Serial.println("client disonnected"); } } long salvaSensore(int iSensore) { int addressTosSave = iSensore*4; long valore = 0; Serial.println("salvaSensore"); if (mySwitch.available()) { Serial.println("mySwitch.available"); valore = mySwitch.getReceivedValue(); Serial.println("valore "); Serial.print(valore); EEPROMWritelong(addressTosSave,valore); } delay(1000); return valore; } void EEPROMWritelong(int address, long value) { //Decomposition from a long to 4 bytes by using bitshift. //One = Most significant -> Four = Least significant byte byte four = (value & 0xFF); byte three = ((value >> 8) & 0xFF); byte two = ((value >> 16) & 0xFF); byte one = ((value >> 24) & 0xFF); //Write the 4 bytes into the eeprom memory. EEPROM.write(address, four); EEPROM.write(address + 1, three); EEPROM.write(address + 2, two); EEPROM.write(address + 3, one); } void setupComm() { Serial.println("Trying to connect"); Serial.print("n"); if (!Ethernet.begin(mac)){ Serial.println("Failed to DHCP"); // no point in carrying on, so do nothing forevermore: while(true); } // print your local IP address: Serial.print("My IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: Serial.print(Ethernet.localIP()[thisByte], DEC); Serial.print("."); } Serial.println("fine"); } long EEPROMReadlong(long address) { //Read the 4 bytes from the eeprom memory. long four = EEPROM.read(address); long three = EEPROM.read(address + 1); long two = EEPROM.read(address + 2); long one = EEPROM.read(address + 3); //Return the recomposed long by using bitshift. return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF); }
The resulting web interface is as follow : (avoid comments 🙂 )
You must write in the browser the IP address that is printed in the Arduino console and also specifying port 8081 .
In my case , for example : 192.168.1.136:8081
There are 5 links that allow you to save the data sensors : pressing the button ” Save sensor 0 ” and pressing the sensor ( opening the door for example ) is stored the data in the EEPROM .
Once data is saved , clicking ” View data sensors .”, you can view a summary list of what is saved in the EEPROM.
Thie evening if I’ll have time I’ll make a video where you can see better storing operations.
The code is relatively simple.
I now try to explain how this links are done and their management.
line 14: Declaring variable nSensori: the number of sensors that I want to manage
line 53-57: Links are created dynamically according to the html syntax.
Links have this format: save0 , SAVE1 , SAVE2 . .. , depending on the link you want to press .
line 62: link to view stored code sensors.
riga 86-99: I save codes sensor
In detail:
line 84: I received “save” in the querystring, I know I need to save a sensor code…which one?
line 90: now i know the sensor code number!
line 94: I call salvaSensore: function: this function use mySwitch.getReceivedValue() function to save code sensor in the EEPROM.
line 95-99: I write in the HTML page the code sensor just saved
line 102-109: I write in HTML page the saved sensor list.it’s a simple for cycle that use EEPROMReadlong to read each sensor code.
At this point , in the next article , we will implement what we saw today in our anti-theft !
So funny!!!!
See you soon!