Is there a way to call javascript functions from ESP? I don't want to call ajax requests every few seconds. I want to update my values only when they change.

Can it by done? Thanx.

  • much simpler: you can connect both the browser and the mcu to a real server. the browser connects to it via websocket, and the MCU pushes updates to the server as data becomes available. – dandavis Jan 16 '20 at 18:08
  • Do you want to access the JavaScript from a browser or do you want the esp to call a server which provides JavaScript? – Sim Son Jan 16 '20 at 18:14
  • First thank you for the answers. I have the esp as a server and it runs a website that i am using as GUI for my esp program. Now i use XMLHttpRequest so that the browser gets data every 5 seconds and updates values in the gui. And i want to call the update function in the page whenever the values in the esp change. That's posible with the server-push or server send event? I need to google this first. Thanks for now. – Tomas Jan 17 '20 at 6:50
  • @Tomas Did you see my comment on my answer? It might help you understand – Sim Son Jan 17 '20 at 17:53
  • Thank you all. Yes i read it and it helped. I understand it now. Sorry that i write you only now but this is my first night shift with no real work, that means that i had time to read all of your sugestions. – Tomas Feb 7 '20 at 21:59

If you want server-push, you can use Server Sent Events or a Websocket instead of polling with XMLHttpRequest.

So yes, it can be done, and I have implemented both using a Nano 33 IoT at one end and Javascript in a browser on the other. Some example code is here: https://github.com/ocrdu/arduino-webinterface-sse and here: https://github.com/ocrdu/arduino-webinterface-websocket.

I don't know if there are convenient libraries for the ESPs to do this, though.

  • I am gonna Google it. Thanks – Tomas Jan 17 '20 at 7:01

I assume that you misunderstand how JavaScript works. JS is executed on the client side and therefore the esp can easily serve JS code the same way it would provide any html content. Take a look at the Webserver examples and embed your JavaScript code into the html content that the examples use.

The important thing to note is that the esp does not execute JavaScript, from its perspective the content is just an ordinary string without functionality.

The easiest and fastest way to update values on a web interface is probably using a websocket: the esp is a websocket server (using link2004‘s websocket library) and the JavaScript binds to the esp‘s websocket as a client (this has nothing to do with the esp/arduino/C++, but you’ll find examples out there).

  • I am a beginner in this stuff. I know or think that my esp is a websocket server. But what that means i have no idea. To build the website gui i learned about spiffs, and little bit of html, css and javascript. It is more complex that i firstly hade in mind when i started this project. I try to learn only what i need directly for my needs. I learn mostly from examples and Google is my best friend but all the examples i found used the XMLHttpRequest. Or maybe i asked Google the wrong questions. – Tomas Jan 17 '20 at 6:59
  • Well, I can‘t make you understand that within a second, but an important hint: you still need the Webserver to provide the client with html/JS code. And beside that you also need the esp to be a websocketserver. Once the websocket client (JavaScript, executed by browser, provided by esp-Webserver) has connected to the websocket server, you have a full-duplex connection in which both devices can send at any time. In the JavaScript you have to parse whatever the server sends and update the relevant values. I’ll add an example for a JS websocket client. – Sim Son Jan 17 '20 at 13:46
  • Here, exampleSocket.onmessage() is where all messages sent by the server arrive – Sim Son Jan 17 '20 at 13:47
  • This was a big help. Thanks – Tomas Feb 7 '20 at 22:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.