[SOLVED] Stops connecting to Ubidots

Hi, I post temperature data on here every minute and after a couple days, It stops posting and stops trying to connect to ubidots.

In this code, instead of having that for loop that does nothing. How can I make it to where it tries to reconnect to the client?

 if (!client.connected()) {
    Serial.println("NotConnected");
    Serial.println("disconnecting ubidots.");
    client.stop();
    // do nothing forevermore:
    for(;;);
  }

Thank you, Neil

Hi Neil, have your tried erasing the lines

// do nothing forevermore:
for(;;);

?

If this doesn’t work, please post your entire code to be able to suggest something better.

I will try that, this is my code. Thank you for your help.

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <WiFiServer.h>
    #include <WiFiUdp.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    extern "C"{
        #include "user_interface.h"
      }
    
    WiFiClient client;
    char results[4];
    
    String idvariable1 = "56a6cfa77625420da62c1127"; //heater temp
    
    String token = "qTN43QeDnJLpPs2sei1gKUHIM2arAJartjJQvKO9EBucYFuS9DC0adeYNtpC";
    
    
    OneWire oneWire(D1);
    DallasTemperature sensors(&oneWire);
    
    //////////////////////////////////////////////////////////////////////////////////
    
    void setup(){
    
    //  const char* ssid = "Suite B";
    //  const char* password = "99aa88bb77";
    
    const char *ssid    =   "ratfink3-EXT";
    const char *password =  "franklin9447";
    
      // Create an instance of the server
      // specify the port to listen on as an argument
    
      WiFiServer server(80);
      Serial.begin(115200);
      delay(10);
    
      // Connect to WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");
    
      // Start the server
      server.begin();
      Serial.println("WiFi Server started");
      Serial.println(WiFi.localIP());
    
      sensors.requestTemperatures();
      
      float currentTemp1;
    
      currentTemp1 = sensors.getTempCByIndex(0);
      currentTemp1 = (currentTemp1 * 1.8) + 32;
        
    
      
      int value1 = currentTemp1;
      ubiSave_value(String(value1));
      
    }
    
    
      system_deep_sleep_set_option(0);
      system_deep_sleep(1*60*1000000); 
      
    
    
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    
    void loop(){
    
    void ubiSave_value(String value1) {
      // if you get a connection, report back via serial:
      int num1, check;
    
    
      String var1 = "{\"value\": " + String(value1)+"}";
      num1 = var1.length();
    do{
      if (client.connect("things.ubidots.com", 80)) {
        Serial.println("connected to ubidots");
        delay(100);
        client.println("POST /api/v1.6/variables/"+idvariable1+"/values HTTP/1.1");
        client.println("Content-Type: application/json");
        client.println("Content-Length: "+String(num1));
        Serial.println(String(num1));
        client.println("X-Auth-Token: "+token);
        client.println("Host: things.ubidots.com\n");
        client.print(var1);
        Serial.print(var1+"\n");
        check = 1;
      }
      else {
        // if you didn't get a connection to the server:
        Serial.println("ubidots connection failed");
        check = 0;
      }
    }
     while(check == 0);
    
        if (!client.connected()) {
        Serial.println("NotConnected");
        Serial.println("disconnecting ubidots.");
        client.stop();
      }
    
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
      }
    }

Hi npatel023,

I currently have the same problem to you. Everything works fine, but after about 5-30mins, the connection will be lost and never reconnected automatically, I have to reconnect by press reset on board and then the connection will be established again.
Have you got the solution for your problem yet?
Please share me if you got any solutions.
Thanks.

@chauhuynh paste here your code, then i could try to help you.

Best regards,
Metavix

1 Like

Hi @Metavix, Here is my code using Arduino Ethernet Shield(board Arduino Mega 2560 - Ethernet Shield compatibale) with Ubidots Library:

#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
#include <DS18xx.h>   

#define ID  "57de408676254229e66e5dd9"  // Put here your Ubidots variable ID
#define ID1 "57de514f76254201c5e9aa4c"
#define ID2 "57de541e7625420ea0fad351"
#define TOKEN  "oZ6cj8dVfA2ULrevdjkb4pd10bHT3Q"  // Put here your Ubidots TOKEN

#define oneWire_pin 3    

OneWire oneWire(oneWire_pin);
DS18xx sensors(&oneWire);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(10, 10, 10, 177);

Ubidots client(TOKEN);

void setup() 
{  
  Serial.begin(115200);
  sensors.begin();
  if (Ethernet.begin(mac) == 0) 
  {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(3000);
}

void loop() 
{
  sensors.requestTemperatures();
  float ds_value = sensors.getTempCByIndex(0);
  client.add(ID, ds_value);
  client.sendAll();

  int led = client.getValue(ID1);
  int pwm = client.getValue(ID2);
  Serial.println(led);
  Serial.println(pwm);

  //client.freeRam();
}

This is just a normal code, nothing is too complex, but the Ethernet Shield will stop working after a short time connected to Ubidots Cloud(5-30 mins as I mentioned in the last comment).

Also, I had edited, exactly commented two of code lines in UbidotsEthernet.cpp", those are:

  1. Serial.println(reply); in float Ubidots::getValue(char* id) into //Serial.println(reply);
  2. Serial.write(c); in bool Ubidots::sendAll() into //Serial.write(c);

That’s it, only these two lines of code, nothing else.
Attached is my dashboard for this code.

Thanks for your help.

hello @chauhuynh i know what was the problem and i fixed it in this new version of the library download it and add it to the arduino IDE, it will work for you. By the way don’t forget to delete the old version of the library

Best regards,
Metavix

1 Like

Thanks @Metavix,

For those who encountered similar cases, please refer to this post for more details:

Sincerely,

Chau Huynh

thank you @chauhuynh
I will answer similar cases to help all user with the same problem.

Best regards,
Metavix

1 Like

@Metavix I also faced the same problem, but the microcontroller im using is LoPy and the programming language is micropython. i followed this tutorial but i cant send analog data. can you help?