【发布时间】:2019-11-03 10:10:45
【问题描述】:
我正在使用 ESP32 Wifi 模块作为主机来托管一个网页,该网页以 OK 或 FAILED 形式显示表格中的多个二进制数据点的值。我需要让此设备从本地 IP 上的另一个 ESP32 客户端检索数据,即192.168.138.50/readVal1,此地址将仅显示平面文本OK 或FAILED。我想取这个值并将其显示在我的主模块生成的表中。我该怎么做呢?
我已尝试在 Arduino 代码中使用 HTTP 获取请求,如下所示。
void runTest6(){
String payload;
HTTPClient http;
http.begin("192.168.137.50/readBatt1");
int httpCode = http.GET();
if(httpCode > 0) {
payload = http.getString();
Serial.println(payload);
}else{
Serial.println("HTTP request error");
}
http.end();
String batt6val = payload;
server.send(200, "text/plane", batt6val);
}
这是我在根目录上处理更新的 Javascript\
function getData(){
try{
console.log("Getting Data...");
for(var i = 1;i<=NUMOFUNITS;i++){
(function (i){
setTimeout(function () {
console.log("(debug msg)in loop #: " + i)
var xhttp = new XMLHttpRequest();
var current = "batt" + i + "val";
var dataRead = "readBatt" + i;
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
console.log("updating innerHTML for data value: " + i);
document.getElementById(current).innerHTML = this.responseText;
}else if(this.readyState == 4 && this.status == 404){
console.log("no battery @ " + i);
document.getElementById(current).innerHTML = "None found";
}
};
xhttp.open("GET", dataRead, true);
xhttp.send();
if(i == 1){
updateTime();
console.log("Updated times.")
}
}, 400*i);
})(i);
};
console.log("Data update complete.");
}
catch(err){
alert(err.name);
throw err;
getData(); //try to get data again
}
finally{
console.log("DONE");
}
}
【问题讨论】:
标签: javascript html http arduino esp32