简体   繁体   中英

How can I send a value to a ESP8266 through a webpage using AJAX?

I'm working with a NodeMCU ESP8266 and I want to control my WS2812B with it.

So I made a HTML page with an input range and I want to send the value of the range to my ESP8266 - where the website is hosted- by using AJAX .

I only found tutorials on how to send data from an ESP8266 to a webpage and can't find any tutorials on how to send any info to my ESP8266 from a webpage.

The input looks like this:

<input type=range id="rangeinput">

To send a GET request from your webpage with the value you want to send, you can do something like this (untested, so check; I just typed it in, but you get the idea):

var valueToSend = document.getElementById("rangeinput").value;

var ESP8266URL = ""; // URL of ESP8266 page that handles request goes here

var sendValueRequest = new XMLHttpRequest();
sendValueRequest.open("GET", ESP8266URL + "?value=" + valueToSend, true);
sendValueRequest.onreadystatechange = processReturn;
sendValueRequest.send(null);

function processReturn() {
  if (sendValueRequest.readyState == 4 && sendValueRequest.status == 200) {
    var return = sendValueRequest.responseText;
    // Do something (or nothing) with what the server sent back
  }
}

You will have to handle the GET request on the ESP8266. How to do that depends on how the webserver on your ESP8266 is set up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM