SHARE
TWEET

EEPROM

a guest May 24th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <EEPROM.h>
  3.  
  4. void writeString(char add,String data);
  5. String read_String(char add);
  6.  
  7.  
  8.  
  9. unsigned long lastSend;
  10.  
  11.  
  12.  
  13. WiFiClient espClient;
  14. PubSubClient client(espClient);
  15.  
  16.  
  17. char buffer[100] = {0};
  18. char message_buff[100];
  19. String msgString = "";
  20.  
  21. void setup() {
  22.  
  23.   Serial.begin(115200);
  24.   EEPROM.begin(512);
  25.  
  26.   client.setServer(mqttServer, mqttPort);
  27.   client.setCallback(callback);
  28.    pinMode(LED_BUILTIN, OUTPUT);
  29.   digitalWrite(LED_BUILTIN, HIGH);
  30.   setup_wifi();
  31.  
  32.  
  33. }
  34.  
  35.  
  36.  
  37. void setup_wifi() {
  38.  
  39.   delay(10);
  40.   Serial.printf("Connecting to %s ", ssid);
  41.   WiFi.begin(ssid, password);
  42. //  WiFi.config(ip, gateway, subnet, dns1, dns2);
  43.  
  44.   while (WiFi.status() != WL_CONNECTED)
  45.   {
  46.     Serial.print(".");
  47.     delay(5000);
  48.   }
  49.  
  50.   if(client.connect("ESP8266Client", mqttUser, mqttPassword )){
  51.     Serial.println("connected");  
  52.     Serial.println(WiFi.localIP());
  53.      randomSeed(micros());
  54.   }else{
  55.      Serial.print("failed with state ");
  56.      Serial.print(client.state());
  57.      delay(2000);
  58.   }
  59.  
  60. }
  61.  
  62.  
  63.  
  64. void loop()
  65. {
  66.  checkTime();
  67. // send_mqtt ();
  68.  
  69.   String recivedData1;
  70.   String recivedData2;
  71.   recivedData1 = read_String(11);
  72.   recivedData2 = read_String(10);
  73.   Serial.println("Read Data1:");
  74.   Serial.println(recivedData1);
  75.   Serial.println("Read data2:");
  76.   Serial.println(recivedData2);
  77.  
  78.  
  79.  
  80.   client.loop();
  81.   if (!client.connected())
  82.   {
  83.      reconnect();
  84.  
  85.   }
  86.  
  87.   if (WiFi.status() != WL_CONNECTED)
  88.   {
  89.     setup_wifi();
  90.   }
  91.   client.subscribe("IOT/HOME/LAMP");
  92. }
  93.  
  94. void writeString(char add,String data)
  95. {
  96.   int _size = data.length();
  97.   int i;
  98.   for(i=0;i<_size;i++)
  99.   {
  100.     EEPROM.write(add+i,data[i]);
  101.   }
  102.   EEPROM.write(add+_size,'\0');   //Add termination null character for String Data
  103.   EEPROM.commit();
  104. }
  105.  
  106. String read_String(char add)
  107. {
  108.   int i;
  109.   char data[100]; //Max 100 Bytes
  110.   int len=0;
  111.   unsigned char k;
  112.   k=EEPROM.read(add);
  113.   while(k != '\0' && len<500)   //Read until null character
  114.   {    
  115.     k=EEPROM.read(add+len);
  116.     data[len]=k;
  117.     len++;
  118.   }
  119.   data[len]='\0';
  120.   return String(data);
  121. }
  122.  
  123.  
  124. void callback(char* topic, byte* payload, unsigned int length) {
  125.  Serial.print("Message arrived [");
  126.  Serial.print(topic);
  127.  Serial.print("] ");
  128.  int i = 0;
  129.  for (int i=0;i<length;i++) {
  130.   char receivedChar = (char)payload[i];
  131.   String msg = toString(payload, length);
  132.   message_buff[i] = payload[i];
  133.   Serial.println("dari msg" + msg);
  134.  
  135.   String subs = msg.substring(0,1);
  136.  
  137.   //Serial.println(subs);
  138.   if(subs == "1"){
  139.     Serial.println("write1");
  140.    writeString(11, msg);
  141.    return;
  142.    }
  143. //   else if(subs == "2"){
  144. //   Serial.println("write2");
  145. //   writeString(10, msg);
  146.    //}
  147.   }
  148.   Serial.println();
  149.   message_buff[i] = '\0';
  150.   msgString = String(message_buff);
  151. //  Serial.println(msgString);
  152. }
  153.  
  154.  String toString(byte* payload, unsigned int length) {
  155.   int i = 0;
  156.   char buff[length + 1];
  157.   for (i = 0; i < length; i++) {
  158.     buff[i] = payload[i];
  159.   }
  160.   buff[i] = '\0';
  161.   String msg = String(buff);
  162.   return msg;
  163. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top