-->
Page 1 of 1

Read JS variable value on esp8266?

PostPosted: Sun Nov 07, 2021 1:04 pm
by derek bernsen
Hi all,

I am trying to use esp8266 as a offline web server that has JavaScript inside. My question is how can the esp8266 read the value of a JavaScript variable?

I am trying to make a speedometer that reads the speed from the geolocation api on a phone.

Below is the JS code that would run on the esp8266 web server. I need it to be able to grab the value of variable "speed".


Code: Select allfunction initGeo() {
    navigator.geolocation.watchPosition(
        geosuccess,
        geofailure,
        {
            enableHighAccuracy:true,
            maximumAge:30000,
            timeout:20000
        }
    );

    //moveSpeed(30);
    //moveCompassNeedle(56);
}

var count = 0;
function geosuccess(event) {

    $("#debugoutput").text("geosuccess: " + count++ + " : " + event.coords.heading + ":" + event.coords.speed);

    var heading = event.coords.heading;
    var speed = event.coords.speed;

    if (heading != null && speed !=null && speed > 0) {
       //moveCompassNeedle(heading);
    }

    if (speed != null) {
        // update the speed
        moveSpeed(speed);
    }
}


Re: Read JS variable value on esp8266?

PostPosted: Sun Nov 07, 2021 7:25 pm
by davydnorris
OK so couple of things here:
- the geolocation API only works on secure pages so you will need to use https on your web server
- the API has two forms, and if you want to use precise location the unit has to be connected to the Internet because it triangluates itself using Wifi points. If it's connected to your ESP as an AP then it's not going to be able to get out to use precise mode
- Javascript runs on the web client, and the geolocation API is a client specific API. It doesn't run on the server side at all, even if you ran a Javascript engine on the ESP

So assuming you decide to use low precision mode, then the only way to get the data on your ESP server is to set up a POST call to the server to send that data from the client to the back end, and then set up a function to receive the POST data and process it on the ESP