[SOLVED] Arduino freezes after sending a few sets of Analog data

Just getting into the act of sending data to cloud. The online Getting Started topic was a big help. And of course on the Arduino side I am reasonably familiar - so no issues in getting two analog channels into Ubidots. ( Using Uno + Ethernet Shield W5500)

The Arduino side is given below. But for some odd reason the Arduino code freezes after the first few set of data transmission … any specific reason why this is happening ?

#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
#define ID1 "57c91c4d76254267ffd89e45"           // Put here your Ubidots variable ID
#define ID2 "57c91c3f76254267ffd89e16"
#define TOKEN  "Y9EUvXEMou2cjFdi2UqmzfRTq7i6Cx"  // Put here your Ubidots TOKEN

// Enter a MAC address for your controller below.

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

// Set the static IP address to use if the DHCP fails to assign

IPAddress ip(192, 168, 1, 150);

Ubidots client(TOKEN);

void setup()
{
    Serial.begin(9600);
    
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    
    // give the Ethernet shield a second to initialize:
    delay(1000);
}


void loop()
{
    int value1 = analogRead(A0);
    int value2 = analogRead(A1);

    Serial.println( value1 );
    Serial.println( value2 );
    
    client.add(ID1, value1);
    client.add(ID2, value2);
   
    client.sendAll();

}

Hello @MogaRaghu that problem is an infinite loop on the library i will upload a fix as soon as possible.

Thanks @metavix. So kind of you. Will wait for the update.

Till such time a fix arrives, wanted to try out a sample code from the net - not sure which exact URL I got this from. But this seems to be a popular one for sensing Humidity and Temperature…anyway tried to compile but I get a funny error saying stray “/” in code. As such its pretty cryptic and not sure what is the problem… does anyone have a working version of this code or can say where is the stray slash ??

Tried a different flavour of Arduino code and its working fine as i can see Ethernet activity LEDs on shield as well as Serial monitor updates of two variables… Hmm looks like more troubleshooting…

But the Ubidot Dashboard is now acting funny - no data is coming in. And when i check the IDs of two variables they are same … not sure how this happened. See the snap shot below ( I even tried to delete and re-create the two variables. Still after a while they become same) :

And this is my Arduino code :

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

String Ch01VarID = "57cae55a7625427649428230" ;          // Put here your Ubidots variable ID
String Ch02VarID = "57cae8447625420503abc1e0" ;
String Token =  "Y9EUvXEMou2cjFdi2UqmzfRTq7i6Cx" ;        // Put here your Ubidots TOKEN

char server[] = "things.ubidots.com";
EthernetClient client;

// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 150);


//******************************************************
void setup()
{
  Serial.begin(9600);

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
}


void loop()
{
  int value1 = analogRead(A0);
  int value2 = analogRead(A1);

  Serial.println( value1 );
  Serial.println( value2 );

  save_value( value1, value2);

}

//******************************************************
void save_value(int Ch1_Val, int Ch2_Val)
{
  Serial.println("Sending data...");
  // if you get a connection, report back via serial:
  int num = 0;
  delay(2000);

  String varString = "[{\"variable\": \"" + Ch01VarID + "\", \"value\":" + String(Ch1_Val) + "}";
  varString += ",{\"variable\": \"" + Ch02VarID + "\", \"value\":" + String(Ch2_Val) + "}]";
  num = varString.length();

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

  if (client.connect(server, 80)) {
    client.println("POST /api/v1.6/collections/values HTTP/1.1");
    client.println("Content-Type: application/json");
    client.println("Content-Length: " + String(num));
    client.println("X-Auth-Token: " + Token);
    client.println("Host: things.ubidots.comn");
    client.print(varString);
  }
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  boolean sta = client.connected();
  Serial.println("Connection [" + String(sta) + "]");
  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  Serial.println("Reading..");
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  client.flush();
  client.stop();
}

hello @MogaRaghu maybe the problem is in the next lines:

    client.println("POST /api/v1.6/collections/values HTTP/1.1");
    client.println("Content-Type: application/json");
    client.println("Content-Length: " + String(num));
    client.println("X-Auth-Token: " + Token);
    client.println("Host: things.ubidots.comn");
    client.print(varString);

Change it with the next code and try again please:

    client.println("POST /api/v1.6/collections/values HTTP/1.1");
    client.println("Content-Type: application/json");
    client.println("Content-Length: " + String(num));
    client.println("X-Auth-Token: " + Token);
    client.println("Host: things.ubidots.comn");
    client.println("Connection: Close");
    client.println();
    client.println(varString);
    client.println();

Best regards,
Metavix

@Metavix … thanks for the suggestion. But this time I had a different problem. The ubidots dashboard showed my status as Inactive as I had not logged for the last few days as I was travelling. Did not know how to activate it ?

Anyway created a new Token and also used the on site code generator to get the Arduino code and modified it to get it working ( it does not work as generated. The client.save() is an issue ) . I was able to see the values being updated… and now the freezing issue also is not there - did you do something ??

But still there is no consistency in the connectivity - with all conditions same you try to view the dashboard. Whether it work or not is a bit of a suspense. I am not able to see anything wrong at my end.

@MogaRaghu you used the wizard?
Then with my suggestion worked?

Now, about of activity i will ask to the support team to know about that.

Oh I am sorry @Metavix
I actually recompiled the Arduino code using your suggestion, uploaded and tried. Though my Serial port was registering data being sent, the UbiDots dashboard did not register anything and was showing status as Inactive. I did not know how to make it Active.

So then only I tried the Device Wizard and it worked. Yesterday.

And now today I try the same thing it says "connected before 15 hours " and remains Inactive. I am sure there is something I am missing. Only not sure which.

Does the attached screen shot provide any clue to the Dashboard inactivity ??

@MogaRaghu
no, you need to add “?=YOUR_TOKEN” at the end to provide authentication, to see that right.

Hi @Metavix, MogaRaghu’s problems seem to be like mine. Lost connection, never auto reconnect, using Ethernet Shield with Arduino. I’d tried using another Ethernet Shield, also with another Arduino board, but nothing worked. I’m basing on Ubidots to make my project at Univ. Hope to see the working library updated soon.
Thanks!.

@chauhuynh could you please paste here thte response of serial terminal, what is the last word in serial terminal before the lost connection?

Best regards,
Metavix

1 Like

Hi Metavix,

I made a video on my problem, hope this will help you understand my problem now.

In general, the system will stop working after the line: Posting your variables.

Arduino Ethernet Shield Ubidots lost connection

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

Hi @Metavix,

Sorry for the late reply, I’ve been testing your new library for 3 days from your last comment, and happy, It’s working very well at this time.

Thank you so much for your help. I like Ubidots so much. A good platform.

Sincerely,

Chau Huynh

And I think you better should public this actual working library like a master library for Ethernet Shield so that everyone won’t have this problem like me.

Sincerely,

Chau Huynh

@chauhuynh yes you are right, i upload it in a new branch of master library, it will be in test and when the new version pass the test it will merge to the master branch.

Best regards,
Metavix

1 Like

Hi @Metavix,

Thanks for this update in my Arduino works perfectly.
My problem is with the ESP8266 library because I have the same problem. How can I manage it? what did you do to fix the ethernet library?

Thanks.

Hi @lluis,

ESP8266’s firmware was recently updated by the library’s provider and AT command’s baud speed was upgraded and it is not working properly with the Arduino Software Serial Library, because Ubidot’s Library uses virtual ports and at high baud rates the new firmware produces noise and right now the communication cannot be set between the ESP and the Arduino using only a written code in an Arduino Sketch.

Because the third party ESP’s firmware library doesn’t rest on us, it is a bit difficult for us to solve easily the issue, we highly recommend to our users to use an arduino Board with two hardware serial ports and not to virtualizate the ports in order to set properly the baud rate without any kind of noise, you can use an Arduino Duemilanove or Mega for having more hardware serial ports Available.

We will be working for trying to solve the issue, but the solution is not trivial and it can take us some time so we gently recommend you to implement your code with a different board or trying a search for an older ESP library firmware."

All the best,
Kath :slight_smile: