Having fiddled a bit with Espruino on ESP8266 -see sister conversation Espruino on ESP8266 ESP-09 - 1 powerful cm2 - I gave this setup a shot for to play with. Final solution will most likely more go into LoRA then Wifi connection between sensors and collector / hub station, and the data will not be pulled but pushed by the sensor node. The browser / viewer wil still do a pull, but pull will not be from the (individual) sensor(s) but from the collector / hub station. For testing things though, the intervalled pull in the browser and use of local Wifi network felt just right.
Initially I used the WebServer module... but with the setup as taken from the Espruino site - w/ inline stings and anonymous function, it stopped work after abut 8..12 pulls, and it did not reconnect. I was a bit disappointed... and lacking the energy to work myself through the WebServer module. Since I served pages before using just the http module and RYO request handler function, I did that again, as you see in the code. Additionally, I converted the http response composing to ES6 Template Strings. Then I gave the WebServer module another chance - with the inline stuff 'externalized', and now it works too... at least longer than before.
The implementation uses two (2) http requests:
One for the base page. It is loaded once, frames the data -in an iframe - and allows pausing and resuming the data pull.
One for the data pull. The data pull is just pulling a terse document into the base page as iframe.
Some trick makes the data available to the main page for further processing/rendering/etc. That I have access to the iframe document and data with .innerHTML on the node with id data, you can see in the debug window of the browser screenshot showing parts of Web page and Espruino Web IDE console. I can even JSON parse the string and get a nice object to work with.
The iframe's document (DOM) makes itself accessible to the base / main / control page by setting itself as a iframeDocument property of the top window. Initially I used a direct setting like top.iframeDocument = document; as the script in the iframe document. Now I check for the presence of the top.setIframeDocument() function / method and use that. Using a setter function provides easy hook into the event of new data for what ever (post) processing / rendering by the application. Instead of iframe, ajax / xhr call would do as well... but is way more code with no added benefits....
Using iframe to avoid ajax and make data pull as lightweight as possible was not the only reason: initially I did serve the top, framing page with file://...`` protocol, because I was not sure if I would have enough space in the ESP-09 for all the dhtml code... but it worked itself nicely out... still about half of the variables free... ;-) ...as you can see from stats:
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Having fiddled a bit with Espruino on ESP8266 -see sister conversation Espruino on ESP8266 ESP-09 - 1 powerful cm2 - I gave this setup a shot for to play with. Final solution will most likely more go into LoRA then Wifi connection between sensors and collector / hub station, and the data will not be pulled but pushed by the sensor node. The browser / viewer wil still do a pull, but pull will not be from the (individual) sensor(s) but from the collector / hub station. For testing things though, the intervalled pull in the browser and use of local Wifi network felt just right.
Initially I used the WebServer module... but with the setup as taken from the Espruino site - w/ inline stings and anonymous function, it stopped work after abut 8..12 pulls, and it did not reconnect. I was a bit disappointed... and lacking the energy to work myself through the WebServer module. Since I served pages before using just the http module and RYO request handler function, I did that again, as you see in the code. Additionally, I converted the http response composing to ES6 Template Strings. Then I gave the WebServer module another chance - with the inline stuff 'externalized', and now it works too... at least longer than before.
The implementation uses two (2) http requests:
Some trick makes the data available to the main page for further processing/rendering/etc. That I have access to the iframe document and data with .innerHTML on the node with id data, you can see in the debug window of the browser screenshot showing parts of Web page and Espruino Web IDE console. I can even JSON parse the string and get a nice object to work with.
The iframe's
document
(DOM) makes itself accessible to the base / main / control page by setting itself as aiframeDocument
property of thetop
window. Initially I used a direct setting liketop.iframeDocument = document;
as the script in the iframe document. Now I check for the presence of thetop.setIframeDocument()
function / method and use that. Using a setter function provides easy hook into the event of new data for what ever (post) processing / rendering by the application. Instead of iframe, ajax / xhr call would do as well... but is way more code with no added benefits....Using iframe to avoid ajax and make data pull as lightweight as possible was not the only reason: initially I did serve the top, framing page with
file://...
`` protocol, because I was not sure if I would have enough space in the ESP-09 for all the dhtml code... but it worked itself nicely out... still about half of the variables free... ;-) ...as you can see from stats:The last visual attachment shows a 404 - page not found - http error... just to verify that my 404 in the RYO WebServer does what it's supposed to to.
Here is the complete code - with static and dynamic Web resources at the end:
3 Attachments