Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By hunterwc
#60633 how can i get "char json[]" in arduino ide skech?

i've tried to do it like in StringExample, but i have a lot of errors with libraries.
code:
Code: Select all#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define WiFiSSID "xxx"
#define WiFiPass "xxx"
const unsigned long HTTP_TIMEOUT = 10000;  // max respone time from server
const size_t MAX_CONTENT_SIZE = 512;       // max size of the HTTP response
 
#define host  "xxx.xxx.x.xx"
#define portSerwera        80
 struct UserData {
  char maxtemp[32];
  char mintemp[32];
  char maxhum[32];
  char minhum[32];
  char minfood[32];

};
 
void setup() {
  Serial.begin(115200);
  wifiConnect();
}
 
void loop() {
  StaticJsonBuffer<200> jsonBuffer;
float t,h;
    t=2;
    h=3;
  UserData userData;
    wyslijDane(t,h,&userData);
    delay((uint16_t) 60 * 1000);
}
 
void wifiConnect()
{
    Serial.print("Trwa laczenie z siecia");
    WiFi.begin(WiFiSSID, WiFiPass);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Nawiazano polaczenie WiFi"); 
}
 
 
void wyslijDane(float t,float h,struct UserData* userData)

   WiFiClient client;
   
   while(!client.connect(host, portSerwera)) {
    Serial.println("blad polaczenia");
    wifiConnect();
  }
 

               
  String url = "/food/conec.php?temperatura="+String(t)+"&wilgotnosc="+String(h);


  Serial.print("Wysylam dane na serwer: ");
  Serial.println(url);


  client.print("GET ");
  client.print(url);
  client.println(" HTTP/1.1");
  client.print("Host: ");
  client.println(host);
  client.println("Connection: close");
  client.println();


  char endOfHeaders[] = "\r\n\r\n";

  client.setTimeout(HTTP_TIMEOUT);
  bool ok = client.find(endOfHeaders);

  if (!ok) {
    Serial.println("No response or invalid response!");
  }
  const size_t BUFFER_SIZE =
      JSON_OBJECT_SIZE(8)    // the root object has 8 elements
      + JSON_OBJECT_SIZE(5)  // the "address" object has 5 elements
      + MAX_CONTENT_SIZE;    // additional space for strings

  // Allocate a temporary memory pool
  DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);

  JsonObject& root = jsonBuffer.parseObject(client);

  if (!root.success()) {
    Serial.println("JSON parsing failed!");
   
  }

  // Here were copy the strings we're interested in
   strcpy(userData->maxtemp, root["temp_max"]);
  strcpy(userData->mintemp, root["temp_min"]);
  Serial.print("maxtemp = ");
  Serial.println(userData->maxtemp);
  Serial.print("mintemp = ");
  Serial.println(userData->mintemp);

   
  }
User avatar
By martinayotte
#60647 I think you did over-complexify your code. Also I'm seeing some undefined code not present in WiFiClient class, such as client.setTimeout() and client.find(). Where this code comes from ? Where are you getting the client.response ?