[SOLVED] Arduino-Ethernet combination of getting and sending data to ubidots doesn't work

Dear all,

I am trying to create a smart thermostat using arduino and ubidots.
Several sensors are connected to my arduino + ethernet shield: temperature, humidity and light.
I would like to send this sensor data to ubidots. at the same time I would like to control my thermostat from the cloud using an on/off switch and a slider to set the desired temperature.
If the switch is in ON state and the temperature is lower than the desired the arduino will activate an electric heater.

sending the sensor data (light, humidity, temperature) works fine. and also getting the control inputs from ubidots (switch + desired temperature) works fine seperatly. when combining both in my main arduino loop, it doesn’t work anymore.
plaese find my code below:

void getData()
{
// get data from cloud
enabled = client.getValue(ID_enable);
temperature_control = client.getValue(ID_temperature_control);
}

void sendData()
{
// send data to cloud
client.add(ID_temperature, temperature);
client.add(ID_humidity, humidity);
client.add(ID_light, light);
client.sendAll();
}

void logData()
{
Serial.println();
Serial.print("temperature: “);
Serial.print(temperature);
Serial.print(” / humidity: “);
Serial.print(humidity);
Serial.print(” / light: “);
Serial.print(light);
Serial.print(” / enabled: “);
Serial.print(enabled);
Serial.print(” / temperature control: ");
Serial.println(temperature_control);
}

void loop()
{
// update senor values
thermostat.update();
state = thermostat.state;
humidity = thermostat.humidity;
temperature = thermostat.temperature;
light = thermostat.light;

// get cloud data
getData();

// log sensor and cloud data
logData();

// send data to the cloud
sendData();

delay(2000);
}

In my serial window I get the output below.
the variables enabled and temperature control are always 0.00.
They are however correctly send through in the json as can be seen (0.00 and 18.00 respectively).
Can anybody help with this?

Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 17:59:06 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

148
{“count”: 28, “next”: “http://things.ubidots.com/api/v1.6/variables//values?page=2&page_size=1", “previous”: null, “results”: [{“url”: "http://things.ubidots.com/api/v1.6/values/”, “value”: 0.0, “timestamp”: 1483026323§Î
Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 17:59:07 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

149
{“count”: 33, “next”: “http://things.ubidots.com/api/v1.6/variables//values?page=2&page_size=1", “previous”: null, “results”: [{“url”: "http://things.ubidots.com/api/v1.6/values/”, "value": 18.0, “timestamp”: 148302432

temperature: 13.00 / humidity: 48.00 / light: 7.00 / enabled: 0.00 / temperature control: 0.00
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 17:59:09 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

42
[{“status_code”: 201}, {“status_code”: 201}, {“status_code”: 201}]
0

Hello @NicoCloud,

We’ve made some changes in your code. From what we could observe of your code you have assignment problem of your variables.

Here is the code that i used, take a look of it.

#include "UbidotsMicroESP8266.h"
#define TOKEN  "9DriKmYujOJZ2x637376xbdsfsFtaSLvq0"  // Put here your Ubidots TOKEN
#define ID_temperature "58656f1d762542s4504073c08"
#define ID_humidity "58656f2476254244ffd48a627"
#define ID_light "58656f2b76254245sd04073c63"
#define ID_enable "58656f327625424df5055fd52f"
#define ID_temperature_control "5865sdf6f3876254244ff48a6c1"
#define WIFISSID "UBIWIFI"
#define PASSWORD "clave123456789ubi"

int temperature;
int humidity;
int light;
int enabled;
int temperature_control;

Ubidots client(TOKEN);

void setup(){
    Serial.begin(115200);
    delay(10);
    client.wifiConnection(WIFISSID, PASSWORD);
}

int getData(char* ID)
{
// get data from cloud
Serial.println("getting enable value");
int value = client.getValue(ID);
return value;
}

void sendData()
{ 
// send data to cloud
  client.add(ID_temperature, temperature);
  client.add(ID_humidity, humidity);
  client.add(ID_light, light);
  client.sendAll();
}

void logData()
{
  Serial.println("Printing for debug");
  Serial.print("temperature: ");
  Serial.print(temperature);
  Serial.print(" / humidity: ");
  Serial.print(humidity);
  Serial.print(" / light: ");
  Serial.print(light);
  Serial.print(" / enabled: ");
  Serial.print(enabled);
  Serial.print(" / temperature control: ");
  Serial.println(temperature_control);
}

void loop() 
{
// update senor values
  humidity = 50;
  temperature = 25;
  light = 10;

// get cloud data
enabled = getData(ID_enable);
temperature_control = getData(ID_temperature_control);

// log sensor and cloud data
logData();

// send data to the cloud
sendData();

delay(2000);
}

In my serial window I get the output below.

Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 21:28:54 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

147
{"count": 2, "next": "http://things.ubidots.com/api/v1.6/variables/58656f3276254245055fd52f/values?page=2&page_size=1", "previous": null, "results": [{"url": "http://things.ubidots.com/api/v1.6/values/58657ae676254244ff49067d", "value": 0.0, "timestamp": 1483045606242, "context": {}, "created_at": "2016-12-29T21:06:46.242"}]}
0


147
{"count": 2, "next": "http://things.ubidots.com/api/v1.6/variables/58656f3276254245055fd52f/values?page=2&page_size=1", "previous": null, "results": [{"url": "http://things.ubidots.com/api/v1.6/values/58657ae676254244ff49067d", "value": 0.0, "timestamp": 1483045606242, "context": {}, "created_at": "2016-12-29T21:06:46.242"}]}
0


getting enable value
Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 21:29:18 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

148
{"count": 2, "next": "http://things.ubidots.com/api/v1.6/variables/58656f3876254244ff48a6c1/values?page=2&page_size=1", "previous": null, "results": [{"url": "http://things.ubidots.com/api/v1.6/values/58657ae576254245056032c9", "value": 15.0, "timestamp": 1483045605419, "context": {}, "created_at": "2016-12-29T21:06:45.419"}]}
0


148
{"count": 2, "next": "http://things.ubidots.com/api/v1.6/variables/58656f3876254244ff48a6c1/values?page=2&page_size=1", "previous": null, "results": [{"url": "http://things.ubidots.com/api/v1.6/values/58657ae576254245056032c9", "value": 15.0, "timestamp": 1483045605419, "context": {}, "created_at": "2016-12-29T21:06:45.419"}]}
0


Printing for debug
temperature: 25 / humidity: 50 / light: 10 / enabled: 0 / temperature control: 15
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 21:29:52 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

42
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Hi @mariahernandez,

I changed the code according to your specifications (see below) .
I am using however following setup

  • Arduino Uno + Ethernet Shield W5100
  • Arduino IDE 1.8.0
  • Library <UbidotsEthernet.h>

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

int getData(char* ID)
{
int value = client.getValue(ID);
Serial.println("getting value… ");
return value;
}

void sendData()
{
// send data to cloud
client.add(ID_temperature, temperature);
client.add(ID_humidity, humidity);
client.add(ID_light, light);
client.sendAll();
}

void logData()
{
Serial.println();
Serial.print("temperature: “);
Serial.print(temperature);
Serial.print(” / humidity: “);
Serial.print(humidity);
Serial.print(” / light: “);
Serial.print(light);
Serial.print(” / enabled: “);
Serial.print(enabled);
Serial.print(” / temperature control: ");
Serial.println(temperature_control);
}

void loop()
{
// update senor values
state = “OK”;
humidity = 50;
temperature = 25;
light = 10;

// get data from cloud
temperature_control = getData(ID_temperature_control);
enabled = getData(ID_enable);

// log sensor and cloud data
logData();

// send data to the cloud
sendData();

delay(2000);
}

I get the same behaviour.
Sending the data to ubidots is completely OK.
The problem arrizes when I am getting the values from ubidots (see output with getData and sendData)

  • I receive apparently the good results
  • I cannot save these in a variable.

If I comment the sendData method in the mainloop however, I get the expected behaviour (see output with only getData, sendData is commented out in the mainloop)

output with getData and sendData

Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 22:17:27 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

149
{“count”: 35, “next”: “http://things.ubidots.com/api/v1.6/variables/***/values?page=2&page_size=1”, “previous”: null, “results”: [{“url”: “http://things.ubidots.com/api/v1.6/values/***”, “value”: 16.0, “timestamp”: 148304908
getting value…
Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 22:17:28 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

148
{“count”: 29, “next”: “http://things.ubidots.com/api/v1.6/***/values?page=2&page_size=1”, “previous”: null, “results”: [{“url”: “http://things.ubidots.com/api/v1.6/values/***”, “value”: 1.0, “timestamp”: 1483049084s
getting value…

temperature: 25 / humidity: 50 / light: 10 / enabled: 0 / temperature control: 0
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 22:17:30 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

42
[{“status_code”: 201}, {“status_code”: 201}, {“status_code”: 201}]
0

output with only getData, sendData is commented out in the mainloop

Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 22:33:25 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

149
{“count”: 35, “next”: “http://things.ubidots.com/api/v1.6/variables/***/values?page=2&page_size=1”, “previous”: null, “results”: [{“url”: “http://things.ubidots.com/api/v1.6/values/***”, “value”: 16.0, “timestamp”: 148304908
getting value…
Geting your variable
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 29 Dec 2016 22:33:26 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS

148
{“count”: 29, “next”: “http://things.ubidots.com/api/v1.6/variables/***/values?page=2&page_size=1”, “previous”: null, “results”: [{“url”: “http://things.ubidots.com/api/v1.6/values/***”, “value”: 1.0, “timestamp”: 1483049084ñ
getting value…

temperature: 25 / humidity: 50 / light: 10 / enabled: 1 / temperature control: 16

It seems you have a problem with local and global variables, replace your getdata() function for something like this:

int getData(char* ID)
{
Serial.println("getting value… ");
int value = client.getValue(ID);
Serial.println("got value: ");
Serial.print(value);
return value;
}

check if your value is properly obteined by looking at your serial console, if it’s ok, the problem is the way that you assign the value.

Regards

The problem is resolved.
My sensor readings were causing the problem. I reprogrammed them an it is working fine now.
Thanks!