-->
Page 1 of 3

Save data to eeprom every 10 sec

PostPosted: Sat Dec 12, 2015 5:53 pm
by donnib
Hi,
I have following code. I need to do following :

Read the Temperature and Humidity, Save to EEPROM, Go to deep sleep, Wake after 10 sec. (all of this without Wifi enabled)

When the EEPROM is full i want to turn the Wifi on/connect and send the data from EEPROM. My question is if somebody could help with the EEPROM write/read code that i need ?

Code: Select all// Import required libraries
#include "ESP8266WiFi.h"
#include "DHT.h"
#include <EEPROM.h>

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;

// WiFi parameters
const char* ssid = "xxxx";
const char* password = "xxxx";

// Pin
#define DHTPIN 2

// Use DHT11 sensor
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

// Host
const char* host = "dweet.io";

void setup() {

  EEPROM.begin(512);
 
  // Start Serial
  Serial.begin(115200);
  delay(10);
 
  // Init DHT
  dht.begin();

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
 
  Serial.print("Connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // Reading temperature and humidity
  int h = dht.readHumidity();
  // Read temperature as Celsius
  int t = dht.readTemperature();
 
  // This will send the request to the server
  client.print(String("GET /dweet/for/XXXXXX?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(30);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
 
  // Repeat every 10 seconds
  EEPROM.write(addr, t);
  ESP.deepSleep(10000000, WAKE_RF_DEFAULT);
 
}

Re: Save data to eeprom every 10 sec

PostPosted: Sat Dec 12, 2015 11:20 pm
by Venkatesh
Code: Select all/*
 * EEPROM Write
 *
 * Stores values read from analog input 0 into the EEPROM.
 * These values will stay in the EEPROM when the board is
 * turned off and may be retrieved later by another sketch.
 */

#include <EEPROM.h>

// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;

void setup()
{
  EEPROM.begin(512);
}

void loop()
{
  // need to divide by 4 because analog inputs range from
  // 0 to 1023 and each byte of the EEPROM can only hold a
  // value from 0 to 255.
  int val = analogRead(A0) / 4;

  // write the value to the appropriate byte of the EEPROM.
  // these values will remain there when the board is
  // turned off.
  EEPROM.write(addr, val);

  // advance to the next address.  there are 512 bytes in
  // the EEPROM, so go back to 0 when we hit 512.
  // save all changes to the flash.
  addr = addr + 1;
  if (addr == 512)
  {
    addr = 0;
    EEPROM.commit();
  }

  delay(100);
}

Re: Save data to eeprom every 10 sec

PostPosted: Sun Dec 13, 2015 9:56 am
by gmag11
Hi,

Have a look to FS.H. it allows to use eeprom as a file system. It is less efficient than using raw data but It is easier to use. You could store info as Json if you like.

Regards,
German

Re: Save data to eeprom every 10 sec

PostPosted: Sun Dec 13, 2015 10:31 am
by xtal
EEprom normally has a write life time before getting flaky
@ 10 seconds = 6 writes/minute
if lifetime is 50000 writes @ 6/ minute 50000/6 = 8333 /60 = 138 hours
if my math is correct :mrgreen: