1

I am at beginner level and I wanted to implement EEPROM write after receiving data from a websocket. If the websocket doesn't receive any data within 5 seconds then I want to write to EEPROM the value I sent via websocket to turn on and off the led.

Here is my code please if you can help me out to solve this problem. Thank you. I have modified the code for my need, but I need EEPROM for websocket if possible.

#include <ESP8266WiFi.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>

WiFiServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);

const int pin_led = 5;
unsigned int value;
String data;
const char *msg_toggle_led = "toggleLED";
const char *msg_get_led = "getLEDState";
char msg_buf[10];
int led_state = 0;
int counter = 0; // for EEPROM testing

char* ssid = "esp8266websocket_1";
char* password = "123456789";

void setup() {
  // EEPROM.begin(512);
  pinMode(pin_led, OUTPUT);
  digitalWrite(pin_led, LOW);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Serial started at 115200");
  Serial.println();
  // Connect to a WiFi network
  Serial.print(F("Connecting to ")); Serial.println(ssid);
  WiFi.mode(WIFI_AP);
  IPAddress apLocalIp(10, 50, 1, 1);
  IPAddress apSubnetMask(255, 255, 255, 0);
  WiFi.softAPConfig(apLocalIp, apLocalIp, apSubnetMask);
  WiFi.softAP(ssid, password);
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  Serial.println("");
  Serial.println(F("[CONNECTED]")); Serial.print("[IP "); Serial.print(WiFi.localIP());
  Serial.println("]");
  // start a server
  server.begin();
  Serial.println("Server started");
  webSocket.begin();
  webSocket.onEvent(onWebSocketEvent);
  //Serial.print(webSocket.localIP());
}

void loop() {
  webSocket.loop();
}

// Called when receiving any WebSocket message
void onWebSocketEvent(uint8_t client_num, WStype_t type, uint8_t * payload, size_t length) {
  // Figure out the type of WebSocket event
  switch (type) {
    // Client has disconnected
    case WStype_DISCONNECTED:
      Serial.printf("[%u] Disconnected!\n", client_num);
      break;
    // New client has connected
    case WStype_CONNECTED:
      {
        IPAddress ip = webSocket.remoteIP(client_num);
        Serial.printf("[%u] Connection from ", client_num);
        Serial.println(ip.toString());
      }
      break;
    // Echo text message back to client
    case WStype_TEXT:
      Serial.printf("[%u] Text: %s\n", client_num, payload);
      webSocket.sendTXT(client_num, payload);
      // data = webSocket.getData(payload);
      Serial.printf("[%u] Received text: %s\n", client_num, payload);
      // Toggle LED
      if ( strcmp((char *)payload, "toggleLED") == 0 ) {
        counter++;
        led_state = led_state ? 0 : 1;
        //sprintf(msg_buf, "%d", led_state);
        Serial.printf("Toggling LED to %u\n", led_state);
        digitalWrite(pin_led, led_state);
        // webSocket.sendTXT(client_num, msg_buf);
        // Report the state of the LED
      } else if ( strcmp((char *)payload, "getLEDState") == 0 ) {
        sprintf(msg_buf, "%d", led_state);
        Serial.printf("Sending to [%u]: %s\n", client_num, msg_buf);
        webSocket.sendTXT(client_num, msg_buf);
        // Message not recognized
      } else {
        Serial.println("[%u] Message not recognized");
      }
      break;
    // For everything else: do nothing
    case WStype_BIN:
    case WStype_ERROR:
    case WStype_FRAGMENT_TEXT_START:
    case WStype_FRAGMENT_BIN_START:
    case WStype_FRAGMENT:
    case WStype_FRAGMENT_FIN:
    default:
      break;
  }
}

I am not using SPIFFS. I am calling a webpage in HTML which is:

<style>
input[type="text"]{
width: 90%;
height: 3vh;
}

input[type="button"]{
width: 9%;
height: 3.6vh;
}

.rxd{
height: 90vh;
}

textarea {
width: 99%;
height: 100%;
resize: none;
}

</style> <script> var Socket; function start() { Socket = new WebSocket('ws://10.50.1.1:81/'); Socket.onmessage = function(evt) { document.getElementById("rxConsole").value += evt.data; } } function enterpressed() { Socket.send(document.getElementById("txbuff").value); document.getElementById("txbuff").value = ""; } </script>

<textarea id="rxConsole" readonly></textarea>

Please help to implement EEPROM in websocket because if the websocket starts it stays there as long as clients are connected. So I need help to implement websocket for ToggleLED status to save data after 5 seconds if there is no data coming from the html webpage.

7
  • test and learn how to use the EEPROM on esp8266 with a separate test sketch without any other functions
    – Juraj
    Jan 31 '20 at 15:42
  • Hello Juraj,I am at beginner level so i have done work around with EEPROM without any function but here its tricky because i am using TCP IP protocol in ESP-8266 and also websocket so if my device gets disconnected then only my EEPROM will initiate or else help me with interrupt ISR how to implement it.Kindly help thank you! Feb 1 '20 at 4:29
  • what ISR? in the sketch in the question there is no code working with EEPROM
    – Juraj
    Feb 1 '20 at 5:58
  • Hello Juraj, I would like to implement it but i need some help regarding it i know i haven't used in code and that is my question sir. ISR is Interrupt Service Routine. Feb 3 '20 at 4:32
  • if you can use EEPROM in test sketch what is the problem to use it a sketch in question? and how should we know where and how in the sketch do you want to use it? and what has an ISR to do with this? there is no ISR in the sketch in question? this is a Q&A site not a forum for project guidance. arduino.stackexchange.com/tour
    – Juraj
    Feb 3 '20 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.