So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By derek bernsen
#92784 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);
    }
}

User avatar
By davydnorris
#92787 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