A simple arduino burglar alarm: save sensor codes via web interface

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 .

Schermata 2014-12-17 alle 22.31.29

Once data is saved , clicking ” View data sensors .”, you can view a summary list of what is saved in the EEPROM.

Schermata 2014-12-17 alle 22.31.01

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!

A simple burglar alarm: save sensor code in the EEPROM

Here I am again , with flue … good excuse for not going to the gym and have fun with Arduino.
As I said in the previous article I’d like to make code improvements : in particular I don’t want that sensor code are written in the code but I want to give to the user the possibility to store sensore value from a web interface.
Pressing a web interface button, the wireless receiver module will listen the sensor code and store somewhere.
The first problem is : where and how to store them ? I thought at EEPROM memory in Arduino.
The values ​​that I want to use are long ( as you can see from the sketch lines 26,27,28,29 the previous article ) so I cannot  just  use a single cell in EEPROM (which can contain only one byte from 0 to 255 ) .
The long data type instead is four times a byte : 4 bytes . To summarize :

boolean – It can assume only two values ​​: true or false .

char – It contains a single character . Arduino register it as a number ( but we see the text ) . When the characters are used to record a number, may contain a value between -128 and 127 .

byte – It can contain a number between 0 and 255. As a character uses only one byte of memory .

int – It contains a number between -32’768 and 32’767 . It’s the variable type most used and uses two bytes of memory . unsigned int – It has the same function as int , it just can not contain negative numbers , but numbers between 0 and 65,535 . long – It’s twice the size of an int and contains the numbers between-2’147’483’648 and 2’147’483’647. unsigned long – Unsigned long version from 0 to 4’294’967’295 . float – It can store numbers with a comma . Occupies 4 bytes of RAM . double – A double-precision floating point with maximum 1’7976931348623157×10 ^ 308 .

So what I want to do is to split my code sensor (long) in 4 bytes and then I want to store each of these four bytes in a cell in the EEPROM . In this article I found this helpful code. I just checked if the code is ok, in my case :


/*
 I write and read a long i the EEPROM
 */
#include <EEPROM.h>;
// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
 delay(5000);
 long address=0;
 long sensore1= 3557625;
 long sensore2= 10521177;
 EEPROMWritelong(address, sensore1);
 Serial.println("writing 1 code");
 address+=4;
 EEPROMWritelong(address, sensore2);
 Serial.println("writing 2 code");
 /*Serial.println(EEPROMReadlong(0));
 Serial.println(EEPROMReadlong(4));*/
}
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);
}
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);
}

line 5: include EEPROM library line 19-22: with the EEPROMWritelong  function I write the first sensor code in the address 0 then the second sensor in the address 4. Rightly 4 bytes after the first byte . At this point I turned off Arduino and then reconnect it. Let’s check if i read the value I stored before!!! I changed the loop library to read EEPROM.

void loop() {
/*delay(5000);
long address=0;
long sensore1= 3557625;
long sensore2= 10521177;
EEPROMWritelong(address, sensore1);
Serial.println("writing 1 code");
address+=4;
EEPROMWritelong(address, sensore2);
Serial.println("writing 2 code");*/

  Serial.println("Reading Sensor 1");
Serial.println(EEPROMReadlong(0));
Serial.println("Reaging Sensor 2");
Serial.println(EEPROMReadlong(4));
delay(10000000);
}

I read this values: Schermata 2014-12-03 alle 22.25.17 It works!! Ok , at this point I know how to store the values ​​in EEPROM , in the next post I will try to build the web interface to storage sensor codes. I hope this post can be helpful! Have a good day!