-1

At first I want to say that I have zero experience in Javascript/jQuery, and I know that the solution to my Problem might be actually pretty simple.

I wanted to make a WiFi scanner with my ESP8266, that displays the SSIDs in a table on the Page. The code beneath works so far, but the delay is pretty messy. I thougt It must be possible to check in a while loop if the json file is actually filled, and if so, read it and display the ssids.

var foot = '<tr style="height:63px;"><td>Selected WiFi: ... &nbsp;Password:&nbsp;<input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';

var foot_last = '<tr style="height:63px;"><td>Selected WiFi: ... &nbsp;Password:&nbsp;<input id="pw" type="text"><button onClick="connect()" class="btn btn-primary" type="button" style="margin-left:5%;">Connect</button></td></tr>';

//gets called when the scan button is pressed
function scan() {
  //Tells the ESP8266 to write the SSIDs in the json file scan.json (takes a while until its done)
  $.post("/scan");

  //this delay needs to be replaced. The code should only move on if the scan.json is not empty
  delay(2000);

  //Json gets read and contents displayed 
  $.getJSON("/scan.json", function(data) {
    $.each(data, function(index, value) {
      console.log(value);
      var tbl_hoe = '<tr onClick="select()" style="height:63px;"><td>' + value + '</td></tr>';
      foot = tbl_hoe + foot;
      document.getElementById('tab').innerHTML = foot;
    });
  });

  //at this point the json must be emtyed

}

function delay(ms) {
  ms += new Date().getTime();
  while (new Date() < ms) {}
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • 1
    Why write it to a file? Use the ESP as a wee server and just have it return the HTML (or JSON, doesn't matter) directly, and consume that, instead of a file, on your end? Or use a websocket library and get the results as they're discovered. – Dave Newton Jul 08 '18 at 13:01
  • Thank you man. Didnt thought of that solution, its way simpler than my json solution – TheDude Jul 08 '18 at 13:09
  • :thumbsup: Tiny hosts are ESP's thing. – Dave Newton Jul 08 '18 at 13:11

2 Answers2

0

Regarding //this delay needs to be replaced. The code should only move on if the scan.json is not empty
If you take a look at the docs for $.post you will see that it takes 2 arguments. The first is the url, and the second is the callback that is called once the request is done.

function scan() {
  $.post("/scan", function(data) {
    $.getJSON("/scan.json", function(data) {
      $.each(data, function(index, value) {
        console.log(value);
      });
    });
  });
}

Regarding: //at this point the json must be emtyed
You can't change the contents of the json file from the browser unless you make a request to the server. (And the server must have implemented such a feature)

Olian04
  • 6,480
  • 2
  • 27
  • 54
0

Big tanks to Dave Newton. Didnt come to my mind that I could just use the ESP8266 to send the data directly over http without that json inbetween