Ubidots Community

Esp32 ubidots eeprom

my project requires an external eeprom for recording data. Irrespective of the wifi condition it should store the data . When WIFI is connected it should record the data as well as when there is no wifi. Once wifi connection is back it should upload the the data to the ubidots server. EEPROM used for my project is an I2C device.I am uploading the code below.

#include<WiFi.h>
#include “UbidotsEsp32Mqtt.h”
#include <OneWire.h>
#include <DallasTemperature.h>
#include<Wire.h>

#define eeprom 0x50
#define ONE_WIRE_BUS 13
OneWire oneWire(ONE_WIRE_BUS); //Access 1-wire temperature sensors, memory and other chips.
DallasTemperature sensors(&oneWire);

float Room_Temperature, Coil_Temperature;
uint8_t sensor1[8] = {0x28, 0x20, 0x77, 0x07, 0xD6, 0x01, 0x3C, 0x03};
uint8_t sensor2[8] = {0x28, 0x88, 0x55, 0x79, 0xA2, 0x01, 0x03, 0xB6};
const int maxaddress = 1024;
const char *WIFI_SSID = “”; // Put here your Wi-Fi SSID
const char *WIFI_PASS = “”; // Put here your Wi-Fi password
const char *UBIDOTS_TOKEN = “UBIDOTS TOKEN”;
const char *DEVICE_LABEL = “esp_eeprom_new”;
const char *SUBSCRIBE_VARIABLE_LABEL_1 = “temp_coil”;
const char *SUBSCRIBE_VARIABLE_LABEL_2 = “temp_room”;
unsigned long previousMillis = 0;
unsigned long interval = 30000;
float readval = 0.0;

Ubidots ubidots(UBIDOTS_TOKEN);

void setup()
{
Serial.begin(115200);
Wire.begin();
sensors.begin();
ConnectWiFi();
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
//ubidots.setCallback(callback);
ubidots.setup();
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, SUBSCRIBE_VARIABLE_LABEL_1);
ubidots.subscribeLastValue(DEVICE_LABEL, SUBSCRIBE_VARIABLE_LABEL_2);
}

void loop()
{

unsigned long currentMillis = millis();
if (!ubidots.connected())
{
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, SUBSCRIBE_VARIABLE_LABEL_1); // Insert the device and variable’s Labels, respectively
ubidots.subscribeLastValue(DEVICE_LABEL, SUBSCRIBE_VARIABLE_LABEL_2);
//WiFi.reconnect();
}
ubidots.loop();
ubidots.add(“temp_coil”, Coil_Temperature);
ubidots.publish(DEVICE_LABEL);
ubidots.add(“temp_room”, Room_Temperature);
ubidots.publish(DEVICE_LABEL);
delay(1000);
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval))
{
Serial.println(“Reconnecting to WiFi…”);
WiFi.disconnect();
WiFi.reconnect();
ubidots.add(“temp_coil”, readval);
ubidots.publish(DEVICE_LABEL);
ubidots.add(“temp_room”, Room_Temperature);
ubidots.publish(DEVICE_LABEL);
//eeprom_new();
previousMillis = currentMillis;
}
}

WIFI FUNCTION

void ConnectWiFi()
{
if (WiFi.status() != WL_CONNECTED) {

while ((WiFi.status() != WL_CONNECTED)) {

  WiFi.begin(WIFI_SSID, WIFI_PASS); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
  Serial.print(".");
  delay(5000);
  //eeprom_new();
  
}
if  (WiFi.status() == WL_CONNECTED)
  Serial.println("\n Wifi Connected");
  eeprom_new();
WiFi.mode(WIFI_STA);

}

}

eeprom data display

void eeprom_new()
{
Serial.println(“Displaying Temperature fot T1 & T2 sensor…”);
Serial.print("\t");
Serial.print(“Room Temperature”);
Serial.print("\t");
Serial.println(“Coil Temperature”);
for (int deviceaddress = 1; deviceaddress <= maxaddress; deviceaddress++)
{
sensors.requestTemperatures();

Room_Temperature = sensors.getTempC(sensor1);
Coil_Temperature = sensors.getTempC(sensor2);
writeEEPROM(deviceaddress, Room_Temperature, Coil_Temperature, eeprom);
Serial.print("ADDR= ");
Serial.print(deviceaddress);
Serial.print("\t");
Serial.print(Room_Temperature);
Serial.print("C°");
Serial.print("           \t");;
Serial.print(Coil_Temperature);
Serial.println("C°");

}

Serial.println(“EEPROM Writing Finished:”);
delay(5000);

Serial.println(“EEPROM Retreiving the data:”);

for (int deviceaddress = 1; deviceaddress < maxaddress; deviceaddress++)
{
readval = readEEPROM(deviceaddress, eeprom);
Serial.print(“ADDR= “);
Serial.print(deviceaddress);
Serial.print(”\t”);
//Serial.println(readval);
Serial.print(Room_Temperature);
Serial.print(“C°”);
Serial.print(" \t");;
Serial.print(Coil_Temperature);
Serial.println(“C°”);
}
}

eeprom read and write

void writeEEPROM(int deviceaddress, byte Room_Temperature, byte Coil_Temperature, int eeaddress)
{
Wire.beginTransmission(eeaddress);
Wire.write((int)(deviceaddress >> 8)); //writes the MSB
Wire.write((int)(deviceaddress & 0xFF)); //writes the LSB
Wire.write(Room_Temperature);
Wire.write(Coil_Temperature);
Wire.endTransmission();
delay(5);
}

byte readEEPROM(int deviceaddress, int eeaddress)
{
byte rdata = 0xFF;
Wire.beginTransmission(eeaddress);
Wire.write((int)(deviceaddress >> 8)); //writes the MSB
Wire.write((int)(deviceaddress & 0xFF)); //writes the LSB
Wire.endTransmission();
Wire.requestFrom(eeaddress, 1);
rdata = Wire.read();
return rdata;
}