[SOLVED] Send Temperature & Humidity data (TH02 sensor) from Arduino Linkit One to Ubidots

Dear Ubidots members,

i’m trying to send temperature & humidity data retrieved from the TH02 sensor (http://wiki.seeed.cc/Grove-TemptureAndHumidity_Sensor-High-Accuracy_AndMini-v1.0/) linked to my Arduino Linkit One to my Ubidots account.

Unfortunately i’m not very good at coding.

This is the code i got so far, the initial code for TH02 works fine in the serial monitor, but how do i send this to my ubidots account? I have created the variables; i followed the guide but it works with “INT” and not with “FLOATS”.
Any help would be appreciated here.

#include <TH02_dev.h>  // this is my temperature & humidity sensor
#include "Arduino.h"
#include "Wire.h"
#include <LGPRS.h>      //include the base GPRS library
#include <LGPRSClient.h>  //include the ability to Post and Get information using HTTP
#include <LGPRSUdp.h>
#include <LWiFi.h>
#include <LWiFiClient.h>

#define WEBSITE "things.ubidots.com"
#define URL "/api/v1.6/collections/values/?token=MYTOKEN&force=true"
#define idvariable1 "xxxxxxxxx"
#define idvariable2 "xxxxxxxxx"

#define WIFI_AP "xxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxx!!"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

const int PACKET_SIZE = 3; 
byte packetBuffer[PACKET_SIZE]; 
LGPRSUDP u;

char payload[180];  // Reserve a char to store the Ubidots data. Account for 60 bytes per variable. 
char le[4];
String response;
int temp;

void setup()

 LWiFi.begin();
  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
  Serial.println("Connected to WiFi");
}

// everything below works fine in the serial monitor, but how do i send this to UBIDOTS?

{
    Serial.begin(115200);        // start serial for output

    Serial.println("****TH02_dev demo by seeed studio****\n");
    /* Power up,delay 150ms,until voltage is stable */
    delay(150);
    /* Reset HP20x_dev */
    TH02.begin();
    delay(100);

    /* Determine TH02_dev is available or not */
    Serial.println("TH02_dev is available.\n");
}


void loop()
{
    float temper = TH02.ReadTemperature();
    Serial.println("Temperature: ");
    Serial.print(temper);
    Serial.println("C\r\n");

    float humidity = TH02.ReadHumidity();
    Serial.println("Humidity: ");
    Serial.print(humidity);
    Serial.println("%\r\n");
    delay(1000);
}

Many thanks in advance for any assistance!

Hello @robken77,

Once you are able to read the variables properly you have to build the HTTP request to Ubidots to be able to handle the data with the platform, the vaues supported by the platform are float, integer or string numbers. To know how to build the request you can reference to the the Ubidots REST API Reference. Also, the following guide will serve you as guide to build the request properly.

Let us know if you have any additional question.

All the best,
Maria C.

The issue is resolved.

For anyone interested in the code, using the TH02 sensor in combination with the Arduino Linkit One, hereby the full code:

#include "Arduino.h"
#include "Wire.h"
#include <LBattery.h>
#include <LGPRS.h>        //include the base GPRS library
#include <LGPRSClient.h>  //include the ability to Post and Get information using HTTP
#include <TH02_dev.h>     // temperature & humidity sensor (high accuracy & mini) from seeedstudio

#define URL    "things.ubidots.com"
#define TOKEN "your token from ubidots"  // replace with your Ubidots token generated in your profile tab
#define VARID1 "your temperature api id"    //Temperature variable id           
#define VARID2 "your humidity api id"    //Humidity variable id
#define VARID3 "your battery api id"    //Battery Level variable id 

char buff[256];  //Info about battery level and charging

//Temperature and Humidity sensor readings from TH02
float tempter = TH02.ReadTemperature();
float humidity = TH02.ReadHumidity();

//Initializing gprs
LGPRSClient client;  //GPRS Client

void setup()
{
  Serial.begin(115200);
  Serial.println("****TH02_dev demo by seeed studio****\n");
/* Power up,delay 150ms,until voltage is stable */
    delay(150);
    /* Reset HP20x_dev */
    TH02.begin();
    delay(100);

     /* Determine TH02_dev is available or not */
    Serial.println("TH02_dev is available.\n");
    

  Serial.println("Attach to GPRS network by APN setting");
  //Note add Access point name, username and password according to your sim card manufacturer/service provide
   while (!LGPRS.attachGPRS("your apn from your access provider","your apn userid or leave empty","your apn pwd or leave empty"))
  {
    delay(500);
   }

  LGPRSClient globalclient ;
  client = globalclient;

  delay(1000);
}

void loop()
{
  Serial.println("Temperature & Humidity Sensor");
      float temper = TH02.ReadTemperature();
    Serial.println("Temperature: ");
    Serial.print(temper);
    Serial.println("C\r\n");

    float humidity = TH02.ReadHumidity();
    Serial.println("Humidity: ");
    Serial.print(humidity);
    Serial.println("%\r\n");
    delay(1000);

  sprintf(buff,"Battery level = %d", LBattery.level() );
  Serial.println(buff);
 
 String payload = "[{\"variable\":\"" VARID1 "\",\"value\":"+ String(temper)+ "},{\"variable\":\"" VARID2 "\",\"value\":" + String(humidity) + "},{\"variable\":\"" VARID3 "\",\"value\":" + String(LBattery.level()) + "}]";
 
 String le = String(payload.length());
  
  // if you get a connection, report back via serial:
  Serial.print("Connect to ");
  Serial.println(URL);
  if (client.connect(URL, 80))
  {
    // Build HTTP POST request
    Serial.println("connected");    
    client.print(F("POST /api/v1.6/collections/values/?token="));
    client.print(TOKEN);
    client.println(F(" HTTP/1.1"));
    client.println(F("Content-Type: application/json"));
    client.print(F("Content-Length: "));
    client.println(le);
    client.print(F("Host: "));
    client.println(URL);
    client.println(); 
    client.println(payload);  
  }
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(100);
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  //Delay of about 1 minute or 60 seconds
   for (int minutes = 0; minutes < 7; minutes++) 
   {
     delay(10000);  //10 seconds delay or 10 thousand milli-seconds
   }
   
  
}

Thanks for sharing it with the community! :wink: