使用wi_fi发送数据
我正在尝试通过使用WiFi发送数组,并使用此代码: 我在链接中依赖此代码:在这里
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
const char* ssid = "ESP32-Access-Point";
const char* password = "***";
AsyncWebServer server(80);
unsigned char a[10] = {22, 23, 27, 18, 19, 20, 21, 25, 23, 22};
String json;
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
json = "{ \"temp\" : [" + (String)a[0];
for (int i=1; i < sizeof(a); i++) {
json += "," + (String)a[i];
}
json += "] }";
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "application/json",json);
});
bool status;
server.begin();
}
void loop(){}
我如何收到值作为价值,因为我需要处理此收到的数组来进行一些处理,然后再打印出来: 这是接收数组的代码(我仅发布了循环):
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
if(WiFi.status()== WL_CONNECTED ){
temperature = httpGETRequest(serverNameTemp); // Here I
// need to deal with the received values
Serial.println("Temperature: " + temperature );
Serial.println(temperature);
// save the last HTTP GET Request
previousMillis = currentMillis;
}
else {
Serial.println("WiFi Disconnected");
}
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
I'm trying to send array by using wifi and I use this code:
I depend on this code in link: here
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
const char* ssid = "ESP32-Access-Point";
const char* password = "***";
AsyncWebServer server(80);
unsigned char a[10] = {22, 23, 27, 18, 19, 20, 21, 25, 23, 22};
String json;
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
json = "{ \"temp\" : [" + (String)a[0];
for (int i=1; i < sizeof(a); i++) {
json += "," + (String)a[i];
}
json += "] }";
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "application/json",json);
});
bool status;
server.begin();
}
void loop(){}
How can I receive the values as value because I need to deal with this received array to do some processing before print it :
This is the code for receiving array (I posted just the loop):
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
if(WiFi.status()== WL_CONNECTED ){
temperature = httpGETRequest(serverNameTemp); // Here I
// need to deal with the received values
Serial.println("Temperature: " + temperature );
Serial.println(temperature);
// save the last HTTP GET Request
previousMillis = currentMillis;
}
else {
Serial.println("WiFi Disconnected");
}
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论