[SOLVED] Unable to see device updates

Hi,

I followed the code flow to update temperature and humidity data with the following code:

import smbus
import time
import datetime
import requests
from ubidots import ApiClient
import json

# Get I2C bus
bus = smbus.SMBus(2)

TOKEN = ""  # Put your TOKEN here
DEVICE_LABEL = ""  # Put your device label here 
VARIABLE_LABEL_1 = ""  # Put your first variable label here
VARIABLE_LABEL_2 = "relative-humidity"  # Put your second variable label here
VARIABLE_LABEL_3 = "temperature-in-celsius"  # Put your third variable label here


def build_payload(curr_time, relativeHumidity, temperatureCelsius):
    payload = {"datetime": curr_time,
               "relative-humidity": relativeHumidity,
               "temperature-in-celsius": temperatureCelsius}

    return payload


def post_request(payload):
    # Creates the headers for the HTTP requests
    url = "http://things.ubidots.com"
    url = "{}/api/v1.6/devices/{}".format(url, DEVICE_LABEL)
    headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
    
    print url
    
    # Makes the HTTP requests
    status = 400
    attempts = 0
    while status >= 400 and attempts <= 5:
        req = requests.post(url=url, headers=headers, json=payload)
        status = req.status_code
        attempts += 1
        time.sleep(1)
    
    print status
    # Processes results
    if status >= 400:
        print("[ERROR] Could not send data after 5 attempts, please check \
            your token credentials and internet connection")
        return False
    else:
        print("[INFO] request made properly, your device is updated")
        return True
    
    return status

def main():
    

    bus.write_byte_data(0x40, 0x03, 0x11)
    
    time.sleep(0.5)
    
    # TH02 address, 0x40(64)
    # Read data back from 0x00(00), 3 bytes
    # Status register, cTemp MSB, cTemp LSB
    data = bus.read_i2c_block_data(0x40, 0x00, 3)
    
    # Convert the data to 14-bits
    cTemp = ((data[1] * 256 + (data[2] & 0xFC))/ 4.0) / 32.0 - 50.0
    fTemp = cTemp * 1.8 + 32
    
    # TH02 address, 0x40(64)
    # Select configuration register, 0x03(03)
    #		0x01(01)	Normal mode enabled, Relative humidity
    bus.write_byte_data(0x40, 0x03, 0x01)
    
    time.sleep(0.5)
    
    # TH02 address, 0x40(64)
    # Read data back from 0x00(00), 3 bytes
    # Status register, humidity MSB, humidity LSB
    data = bus.read_i2c_block_data(0x40, 0x00, 3)
    
    # Convert the data to 12-bits
    humidity = ((data[1] * 256 + (data[2] & 0xF0)) / 16.0) / 16.0 - 24.0
    humidity = humidity - (((humidity * humidity) * (-0.00393)) + (humidity * 0.4008) - 4.7844)
    humidity = humidity + (cTemp - 30) * (humidity * 0.00237 + 0.1973)
    
    #Current time
    Curr_Time = datetime.datetime.now()
    
    # Output data to screen
    print "Relative Humidity : %.2f %%" %humidity
    print "Temperature in Celsius : %.2f C" %cTemp
    print "Temperature in Fahrenheit : %.2f F" %fTemp
    print "Current Time: " + str(Curr_Time)
    
    
    payload = build_payload(json.dumps(Curr_Time, indent=4, sort_keys=True, default=str),humidity,cTemp)

    print("[INFO] Attemping to send data")
    print(payload)
    post_request(payload)
    print("[INFO] finished")
    
    
if __name__ == '__main__':
    while (True):
        main()
        time.sleep(1)

On execution I get the following message on running the code:

Although it looks like successful updation, the variable are empty.

Relative Humidity : 46.08 %
Temperature in Celsius : 31.28 C
Temperature in Fahrenheit : 88.31 F
Current Time: 2018-05-15 20:35:22.727166
[INFO] Attemping to send data
{'temperature-in-celsius': 31.28125, 'relative-humidity': 46.08213189049126, 'datetime': '"2018-05-15 20:35:22.727166"'}
http://things.ubidots.com/api/v1.6/devices/bbgw_01
200
[INFO] request made properly, your device is updated
[INFO] finished
Relative Humidity : 46.10 %
Temperature in Celsius : 31.34 C
Temperature in Fahrenheit : 88.42 F
Current Time: 2018-05-15 20:35:26.897965
[INFO] Attemping to send data
{'temperature-in-celsius': 31.34375, 'relative-humidity': 46.10123106045425, 'datetime': '"2018-05-15 20:35:26.897965"'}
http://things.ubidots.com/api/v1.6/devices/bbgw_01
200
[INFO] request made properly, your device is updated
[INFO] finished

Dear @rohan.mishra,

As I can noted in your payload, you are sending a value that is not supported for the Ubidots Backend which is datatime variable. If you reference to the Ubidots REST API Reference you will find all the parameters supported:

image

The thing here is that one of the parameters of the payload is not allowed, so the Ubidots backend reject the data which is being sent and it create the device empty. To force this request in order to post the other two variables you should add the force parameter which is carefully explained in the Ubidots Rest API.

I hope this would help you!

All the best,
Maria C.

Great answer. Thanks so much. Can finally see my variables updated.

A follow up question… is timestamp a default variable that is always updated? When i use the temperature and humidity variable for charting, the X axis has automatic time stamps updated.

Yes, the values sent takes the timestamp assigned from the server automatically when they are received.

All the best,
Maria C.