0

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;
}
7
  • you ask how to parse JSON from payload? use the ArduinoJson library Commented May 12, 2022 at 14:08
  • No, I sent and received in correct way, but the output is temperature = { "temp" : [22,23,27,18,19,20,21,25,23,22] } I need a way to reach to these values what is the change I should do on temperature = httpGETRequest(serverNameTemp); Commented May 12, 2022 at 14:11
  • then use strtok and atoi Commented May 12, 2022 at 14:14
  • I used char out = strtok(temperature, ","); but this error, cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)' Commented May 12, 2022 at 15:04
  • and with temperature = httpGETRequest(serverNameTemp); out[temperature.length()+1]; temperature.toCharArray(out, temperature.length()+1); I get on invalid types 'char[unsigned int]' for array subscript :( Commented May 12, 2022 at 15:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.