使用wi_fi发送数据

发布于 2025-01-28 16:22:40 字数 2171 浏览 4 评论 0原文

我正在尝试通过使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文