• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

websocket remember state of switch

Q&A Forum › Category: ESP8266 › websocket remember state of switch
0 Vote Up Vote Down
Eric Roest asked 4 years ago

I downloaded the 2.3 Websocket Web Server off the course Build Web Servers and i want to put the button’s in my own program. But in the program i am making the esp8266 is running al the time and the website is on my phone and is not running constantly.
If i press button ON the state changes that’s ok, but if i close the webpage and open it again the state is OFF. How do i tell the program to remember the state of the button?

12 Answers
0 Vote Up Vote Down
Eric Roest answered 4 years ago

I downloaded the 2.3 Websocket Web Server off the course Build Web Servers and i want to put the button’s in my own program. But in the program i am making the esp8266 is running al the time and the website is on my phone and is not running constantly.
If i press button ON the state changes that’s ok, but if i close the webpage and open it again the state is OFF. How do i tell the program to remember the state of the button?

0 Vote Up Vote Down
Steve Mercer answered 4 years ago

You need to figure out a way to save that yourself. For an ESP32 I would use either the Preferences Library or save a text file to SPIFFS. I don’t know much about the ESP8266 but I think it has an EEPROM Library and LittleFS.

0 Vote Up Vote Down
Eric Roest answered 4 years ago

Sorry Steve, i can’t make sense of your answer. I don’t know what a preferences Library is or save text file to SPIFFS?

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Eric.

The Preferences library that Steve mentioned is a library that allows you to save data permanently on the ESP board, even after rebooting.

But I think that what you are looking for is that the web server is able to show the current state of the output. In that particular example, we use the processor to pass the current output value. Check that you have passed the processor parameter when you serve your web page.

request->send(SPIFFS, "/index.html", "text/html",false, processor);

Alternatively, you can also add some JavaScript to your web page that makes a request for the output state whenever the page loads (this means when a tab is opened). Take a look at project 2.2 (see the getStates() JavaScript function and the corresponding handler on the main.cpp file.
I can explain in more detail if you need.

How many outputs are you trying to control?

Regards,
Sara

0 Vote Up Vote Down
Eric Roest answered 4 years ago

I have made a thermostat for the living room. I need one button to change to automatic or hand control two slicers to setup the temperature and the other for the time when it’s on handcontrol. and 3 indicators for roomtemperature, the set temperature and hand/auto indicator. So i have combined 3 of your examples in one project. It’s working now but now the fine tuning.
I can’t make sense of the processor thing, your tutorial says

The following lines handle what happens when you receive a request on the root (/)
URL (ESP IP address).
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest*request) {
request->send(LittleFS, “/index.html”, “text/html”,false, processor);
});

Does that mean that it sends al the data to the webpage? the processor it also weird for me, if you add a println

          String processor(const String& var) {
=====-> Serial.print(“state “);Serial.println(var);
          …..

in the main.cpp then you don’t see anything?

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.

Yes, with that method it sends the whole HTML text to the webpage and replaces the placeholders with whatever you define on the processor () function at the time you access the web page on your browser.

If you add the Serial.print() it should have printed whatever placeholder you have in your HTML file at the time you access or refresh the web page.

This just happens when you access the web server for the first time or when you open a new web browser tab.

In project 2.3, what keeps the state updated all the time is calling the notifyClients() function whenever you receive new data via websocket, but it is the processor() function that is responsible for having the correct state after closing the tab.

I hope this is not confusing.

Regards,
Sara

0 Vote Up Vote Down
Eric Roest answered 4 years ago

Thanks Sara, now i understand it a little better.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
Then, let me know if you were able to make it work.
Regards,
Sara

0 Vote Up Vote Down
Eric Roest answered 4 years ago

I have got it working now. On my project i have now 2 button’s, 2 slicers , and 3 display sensor’s
I get confused because if you compare “2.3 Websocket Web Server”  with  “2.1 Webserver control outputs” and you reload the page for both projects, then at project 2.3 the ledstate changes and at 2.1 the ledstate stays the same.
Is that the intention?

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Eric.
What do you mean? The state that shows on project 2.1 is not the right state? Or it is not updated automatically?
I’m not sure if I understood your question.
In project 2.1, the page is supposed to show the right state even after reloading. It will not update automatically if the state is changed on another page (because it doesn’t use websocket). If there is something different happening, I need to take a look at the project to check that everything is working as expected.
Regards,
Sara

0 Vote Up Vote Down
Eric Roest answered 4 years ago

When the led on de nodemcu is on and you reload the webpage at project 2.3 the led goes off.
With project 2.1 the led stays on when you reload the webpage, i don’t know whether that is the intention?

edit: if you change the rule ledState = 0; => ledState =1; in the processor then the led doesn’t change.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi Eric.
You are right! It isn’t supposed to work that way in project 2.3.
As you stated, you should change this in the processor:

    • ledState = 0; => ledState =1
    • return “OFF”; => return “ON”;

Thanks for having the time to explain it to me.
The code for that project will be updated in a few minutes. So, you can download the “corrected” version.
Thank you.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara
 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.