A simple burglar alarm: save sensor code in the EEPROM

The estimated reading time for this post is 283 seconds

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!

2 thoughts to “A simple burglar alarm: save sensor code in the EEPROM”

Rispondi