• 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

how to send time from javascript to esp8266 (websocket)

Q&A Forum › Category: ESP8266 › how to send time from javascript to esp8266 (websocket)
0 Vote Up Vote Down
Eric Roest asked 4 years ago

In the e-book “build web servers” there is a capital called “Websocket web server”.
I can understand that a little but i want to send the time every minute to the esp8266
I think you need a timer like this

setTimeout(showTime, 1000);
showTime()
 
but i don’t have any idea how to use it.

4 Answers
0 Vote Up Vote Down
Steve Mercer answered 4 years ago

I think you need to elaborate on what you are trying to do here. Why would you want to send the time from your browser (Where Javascript runs) to an ESP8266? If the ESP8266 needs the correct time then it would be better if it connected to an NTP server to get it.

0 Vote Up Vote Down
Eric Roest answered 4 years ago

I want to make a thermostat to regulate the heating in my living room. I have made it so far with the slicers and button’s from the guide. I also made a klok on the website. But not a timer. I have made already a thermostat with only esp8266, but when you can control the heating with a website on your phone it’s much nicer, and the NTP server doesn’t change on winter and summertime or is the time which you get from the website the same as from the NTP-server

0 Vote Up Vote Down
Steve Mercer answered 4 years ago

To get Daylight Saving Time you can use a combination of NTP, Time and Timezone libraries. See https://www.mischianti.org/2020/08/08/network-time-protocol-ntp-timezone-and-daylight-saving-time-dst-with-esp8266-esp32-or-arduino/

0 Vote Up Vote Down
Eric Roest answered 4 years ago

Thanks Steve, may be i need that link in the future.
I already found the solution, the function showtime() i found somewere on the internet and with websocket.send() you can send the data to main.ccp ->”void handleWebSocketMessage”

I had to place a loop within the function to send the text every minute. I tried to send it every second, but that didn’t work.

Start your code here

function showTime() {
vardate=newDate();
varh=date.getHours(); // 0 – 23
varm=date.getMinutes(); // 0 – 59
vars=date.getSeconds(); // 0 – 59
varw=date.getDay();
varm_old;

h= (h<10) ?”0″+h:h;
m= (m<10) ?”0″+m:m;
s= (s<10) ?”0″+s:s;

vardaylist= [“Zondag”, “Maandag”, “Dinsdag”, “Woensdag “, “Donderdag”, “Vrijdag”, “Zaterdag”];
vartime=daylist[w] +” “+h+”:”+m+”:”+s;
document.getElementById(“MyClockDisplay”).innerText=time;
document.getElementById(“MyClockDisplay”).textContent=time;
//websocket.send(“blabla”);                          //doesn’t do anything here
if (s==0){
websocket.send(time);  // send time to handleWebSocketMessage

}

setTimeout(showTime, 1000);
}

showTime();

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

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 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.