No se pueden publicar datos en formato JSON en ThingSpeak mediante el panel de desarrollo de objetos ESP8266

0

Estoy tratando de almacenar datos en formato json y actualizar de forma masiva mi canal ThingSpeak. El siguiente código es solo para propósitos de prueba para asegurarnos de que el formato JSON funcione correctamente. Sin embargo, después de varias pruebas no puedo cargar los datos en ThingSpeak porque obtengo un código de respuesta de cero. Utilicé el ejemplo dado aquí: enlace

¿Puede alguien ayudarme con lo que está mal y cómo solucionarlo? ¡Gracias!

 #include <DHT.h>  // Including library for dht
 #include <EthernetClient.h> //Uncomment this library to work with ESP8266
 #include <ESP8266WiFi.h>


 char jsonBuffer[500] = "[";
 unsigned long lastUpdateTime = 0; // Track the last update time

 String apiKey = "6OF022OLH2QM50TP";     //  Enter your Write API key from 
                                          ThingSpeak

   const char *ssid =  "Xperia Z_a769";     // replace with your wifi ssid 
                                              and wpa2 key
   const char *pass =  "7c6761b56ed2";
   const char* server = "api.thingspeak.com";

   #define DHTPIN 14          //pin where the dht22 is connected

   //There are different kind of sensors like DHT11,DHT12
   // For this project I am using DHT22 temperature and humidity sensor. 
      //Also known as AM2302

      DHT dht(DHTPIN, DHT22);

      WiFiClient client;

   void connects() 
         {
            Serial.begin(9600);
            delay(10);
           dht.begin();

           Serial.println("Connecting to ");
           Serial.println(ssid);


           WiFi.begin(ssid, pass);

         while (WiFi.status() != WL_CONNECTED) 
            {
              delay(500);
              Serial.print(".");
           }
          Serial.println("");
          Serial.println("WiFi connected");

      }

     void readings() 
        {

        // Reading temperature or humidity takes about 250 milliseconds!!
         float h = dht.readHumidity();
        // Read temperature as Celsius (the default)
         float t = dht.readTemperature();
       // Read temperature as Fahrenheit (isFahrenheit = true)
       float f = dht.readTemperature(true);

            if (isnan(h) || isnan(t) || isnan(f)) 
                {
                    Serial.println("Failed to read from DHT sensor!");
                     return;
                 }


          /*  "[{\"delta_t\":0,\"field1\":-70}, 
                {\"delta_t\":15,\"field1\":-66}]"   */
    // Format the jsonBuffer as noted above
       strcat(jsonBuffer,"{\"delta_t\":4");

     strcat(jsonBuffer,"},]");

    char data[500] = "{\"write_api_key\":\"6L4GICVTAF3GMAMG\",\"updates\":";
    strcat(data,jsonBuffer);
    strcat(data,"}");

    unsigned long deltaT = (millis() - lastUpdateTime)/1000;
    String data_length = String(strlen(data)+1);




   if (client.connect(server, 80)) {

        client.println("POST /channels/522490/bulk_update.json HTTP/1.1"); 
         //Replace YOUR-CHANNEL-ID with your ThingSpeak channel ID
        client.println("Host: api.thingspeak.com");
       client.println("User-Agent: mw.doc.bulk-update (Arduino ESP8266)");
       client.println("Connection: close");
       client.println("Content-Type: application/json");
       client.println("Content-Length: "+data_length);
       client.println();
       client.println(data);


                         Serial.print("Temperature: ");
                         Serial.print(t);
                         Serial.print(" *C ");
                         Serial.print(f);
                         Serial.print(" *F\t");
                         Serial.print("Humidity: ");
                         Serial.print(h);
                         Serial.print("data: ");
                         Serial.print(data);
                         Serial.print("time: ");
                         Serial.print(deltaT);
                         Serial.println(". Send to Thingspeak.");









                    //}
      client.stop();

      Serial.println("Waiting...");

       // thingspeak needs minimum 15 sec delay between updates, i've set it 
          to 30 seconds
      delay(15000);
      delay(250); //Wait to receive the response
                        client.parseFloat();
                        String resp = String(client.parseInt());
                        Serial.println("Response code:"+resp);
        }

  } 
    ////////////////////////


     void setup() {
     Serial.begin(9600);
     Serial.setTimeout(2000);

    // Wait for serial to initialize.
    while(!Serial) { }

   Serial.println("I'm awake.");
   connects();
   readings();



   Serial.println("Going into deep sleep for 20 seconds");
   ESP.deepSleep(5e6,WAKE_RFCAL); // 20e6 is 20 microseconds
 }

  void loop() {

 }
    
pregunta Ahmed

0 respuestas

Lea otras preguntas en las etiquetas