[SOLVED] Ubidots connection failed / ESP8266 ESP-12

Hi Ubidots

I made a project with some temp-sensors and a DHT with a ESP8266 ESP-12 with the Arduino. The values have to be send to Ubidots.
Wifi-Conntecting is mostly working fine, the values are after that send 1-2 times and after that, the connection to Ubidots is not anymore possible (Error: “Ubidots connection failed…”)

Do you see an obvious error I made in the sending-routine?
Thanks in advance!
Best regards,
Joe


The COM-Output I get:

Connecting to xxx
scan done
......
Wi-Fi connected
{"value":53.00}
{"value":21.44}
{"value":21.50}
{"value":21.44}
{"value":21.12}
{"value":21.44}
{"value":22.75}
_______________________________
Feuchtigkeit:   53.00
         5.5:   21.44
         3.5:   21.50
         3.0:   21.44
         2.5:   21.12
     Heizung:   21.44
      Keller:   22.75
______________________________
{"value":52.33}
{"value":21.44}
{"value":21.50}
{"value":21.44}
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**
_______________________________
Feuchtigkeit:   52.33
         5.5:   21.44
         3.5:   21.50
         3.0:   21.44
         2.5:   21.12
     Heizung:   21.44
      Keller:   22.75
_______________________________
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**
**Ubidots connection failed...**

–> from now on, only Errors, but never a succesfully connection.


Code I use:

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define errorPin 16
#define DHTPIN D1
#define DHTTYPE DHT11

#define led_red D6     // the number of the LED pin
#define led_orange D5  // the number of the LED pin
#define led_green D7   // the number of the LED pin

DHT dht(DHTPIN, DHTTYPE);

//////////////////////////////////////////////////////////////////////
#define ONE_WIRE_BUS      D3            //Pin to which is attached a temperature sensor
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
int numberOfDevices;                        //Number of temperature devices found
// Resolution bei allen DS18B20-Sensoren auf 10 bit gesetzt = 0.25 Grad.

DeviceAddress B5_5_Sensor = {0x28, 0xFF, 0x3f, 0x25, 0x25, 0x17, 0x03, 0x93};      // 28ff3f2525170393
DeviceAddress B3_5_Sensor = {0x28, 0xFF, 0x99, 0x88, 0x24, 0x17, 0x03, 0xa3};      // 28ff9988241703a3
DeviceAddress B3_0_Sensor = {0x28, 0xFF, 0xd4, 0xdf, 0x80, 0x14, 0x02, 0x9b};      // 28ffd4df8014029b
DeviceAddress B2_5_Sensor = {0x28, 0xFF, 0x66, 0xda, 0x80, 0x14, 0x02, 0xd6};      // 28ff66da801402d6
DeviceAddress Heizung_Sensor = {0x28, 0xFF, 0xd2, 0x67, 0x30, 0x17, 0x04, 0xbe};   // 28ffd267301704be
DeviceAddress Keller_Sensor = {0x28, 0x0a, 0x3e, 0x94, 0x05, 0x00, 0x00, 0x2d};    // 280a3e940500002d

float temp55 = 0.0;
float temp35 = 0.0;
float temp30 = 0.0;
float temp25 = 0.0;
float tempHeizung = 0.0;
float tempKeller = 0.0;

int alarm_low_boiler = 30;
int alarm_low_heizung = 20;
int alarm_low_keller = 12;
int alarm_high_keller = 22;

int flag_alarm_boiler = 0;
int flag_alarm_heizung = 0;
int flag_alarm_keller = 0;
//////////////////////////////////////////////////////////////////////

unsigned long sleep_time = 10000;
between posts to Ubidots
WiFiClient client;

String variable_Feuchtigkeit = "xxx";
String variable_2_5 = "xxx";
String variable_3_0 = "xxx";
String variable_3_5 = "xxx";
String variable_5_5 = "xxx";
String variable_Heizung = "xxx";
String variable_Keller = "xxx";

String token = "xxx-xxx";

const char* ssid = "xxx";
const char* password = "xxx";

int wlan_cnt = 0;
// Function Prototypes
void ubiSave_value(String, String);
void(* resetFunc) (void) = 0; //declare reset function @ address 0


void setup()
{
  for (int i=0;i<2; i++)
  {
    digitalWrite(errorPin ,HIGH);
    delay(100);
    digitalWrite(errorPin ,LOW);
    delay(100);
  }

  Serial.begin(115200);

  dht.begin();
  delay(10);

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

  // Use the scanNetworks method inside the Wi-Fi class to scan for any available Wi-Fi networks
  // nearby. If none are found, go to sleep
  int n = WiFi.scanNetworks();

  Serial.println("scan done"); 
  if (n == 0)
  {
    Serial.println("no networks found");
    Serial.println("Going into sleep");

    for (int i=0;i<10; i++)
  {
    digitalWrite(errorPin ,HIGH);
    delay(300);
    digitalWrite(errorPin ,LOW);
    delay(300);
  }
// ESP.deepSleep(sleep_time * 1000000);
  }

  // If networks are found, attempt to connect to our Wi-Fi network
  WiFi.begin(ssid, password);

  // While the connection is not established, IDLE inside this while loop
  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(errorPin ,LOW);
    delay(500);
    digitalWrite(errorPin ,HIGH);
    delay(500);
    Serial.print(".");

    wlan_cnt++;
    if(wlan_cnt > 30)
    {
      Serial.println("No WLAN-Connection established--> Self-Reset now!");
      resetFunc();  //call reset
    }
  }

  // Once the connection to our Wi-Fi netowrk is successful, print some debug messages
  Serial.println("");
  Serial.println("Wi-Fi connected");
  digitalWrite(errorPin ,LOW);

  DS18B20.begin();    
}

///////////////////////////////////////////////////////////////////////
void ReadTemp(void)
{
delay(1000);
DS18B20.requestTemperatures();                    //Initiate the temperature measurement
DS18B20.setWaitForConversion(false);              //No waiting for measurement
delay(2100);
  
 temp55 = DS18B20.getTempC( B5_5_Sensor );     //Measuring temperature in Celsius  
    delay(100);

 temp35 = DS18B20.getTempC( B3_5_Sensor );     //Measuring temperature in Celsius
    delay(100);

 temp30 = DS18B20.getTempC( B3_0_Sensor );     //Measuring temperature in Celsius
    delay(100);

 temp25 = DS18B20.getTempC( B2_5_Sensor );     //Measuring temperature in Celsius 
    delay(100);
}

////////////////////////////////////////////////////////////////////////////////
// Main code
void loop()
{
// Read the current temperature and humidity measured by the sensor
//  float temp = dht.readTemperature(false);
//  float hum = dht.readHumidity();

float hum1 = dht.readHumidity();
delay(2100);
float hum2 = dht.readHumidity();
delay(2100);
float hum3 = dht.readHumidity();

float hum = (hum1 + hum2 + hum3)/3;

 ReadTemp();

  ubiSave_value(String(variable_Feuchtigkeit), String(hum));
  delay(2000);
  ubiSave_value(String(variable_5_5), String(temp55));
  delay(2000);
  ubiSave_value(String(variable_3_5), String(temp35));
  delay(2000);
  ubiSave_value(String(variable_3_0), String(temp30));
  delay(2000);
  ubiSave_value(String(variable_2_5), String(temp25));
  delay(2000);
  ubiSave_value(String(variable_Heizung), String(tempHeizung));
  delay(2000);
  ubiSave_value(String(variable_Keller), String(tempKeller));
  delay(2000);

                                                                // Send some debug messages over USB
  Serial.println("_______________________________" );
  //Serial.println("*** Ubidots-Daten ***");
  Serial.println("Feuchtigkeit:   "+String(hum));
  Serial.println("         5.5:   "+String(temp55));
  Serial.println("         3.5:   "+String(temp35));
  Serial.println("         3.0:   "+String(temp30));
  Serial.println("         2.5:   "+String(temp25));
  Serial.println("     Heizung:   "+String(tempHeizung));
  Serial.println("      Keller:   "+String(tempKeller));
  Serial.println("_______________________________" );
    
  // Wait a few seconds before publishing additional data to avoid saturating the system
  delay(sleep_time);  
}

////////////////////////////////////////////////////////////////////////////////
void ubiSave_value(String variable_id, String value)
{
  // Prepare the value that we're to send to Ubidots and get the length of the entire string
  // that's being sent
  String var = "{\"value\":" + value +"}"; // We'll pass the data in JSON format
  String length = String(var.length());

  // If we get a proper connection to the Ubidots API
  if (client.connect("things.ubidots.com", 80))
  {
    digitalWrite(errorPin ,HIGH);                     // LED leuchtet während erfolgreicher Übertragung
    
//    Serial.println("--->>> Connected to Ubidots...");
//delay(20);    // 50

            // Construct the POST request that we'd like to issue
    client.println("POST /api/v1.6/variables/"+variable_id+"/values HTTP/1.1");
            // delay(10); 
            // We also use the Serial terminal to show how the POST request looks like
            // Serial.println("POST /api/v1.6/variables/"+variable_id+"/values HTTP/1.1");
            // Specify the contect type so it matches the format of the data (JSON)
    client.println("Content-Type: application/json");
            // delay(10); 
            // Serial.println("Content-Type: application/json");
            // Specify the content length
     client.println("Content-Length: "+ length);
             // delay(10); 
             // Serial.println("Content-Length: "+ length);
             // Use our own API token so that we can actually publish the data
     client.println("X-Auth-Token: "+ token);
             // delay(10); 
             // Serial.println("X-Auth-Token: "+ token);
             // Specify the host
      client.println("Host: things.ubidots.com\n");
              // delay(10); 
              // Serial.println("Host: things.ubidots.com\n");

              // Send the actual data
    client.print(var);
              // delay(10); 
    Serial.print(var+"\n");

    digitalWrite(errorPin ,LOW);                     // LED leuchtet während erfolgreicher Übertragung
  }
  else
  {
    // If we can't establish a connection to the server:
    Serial.println("Ubidots connection failed...");
client.stop();
  }
  
  // If our connection to Ubidots is healthy, read the response from Ubidots
  // and print it to our Serial Monitor for debugging!
  while (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  
  // Done with this iteration, close the connection.
  if (client.connected())
  {
//    Serial.println("       Disconnecting from Ubidots <<<--- ");
    client.stop();
  }
}

Dear user,

I recommend you establish a timeout in the ubiSave_value method, in order to giving to the server a certain period of time to establish the connection. To get a better idea you can reference to this section of the code from one of our libraries to learn how to build it!

I hope this would help you.

All the best,
Maria C.