Python Multi variable not updating


#1

I have a program which reads two files and updates the value to the dashboard.

  1. Putting all variables in one payload doesn’t work. The device creates the variables but they never update.
  2. Separating the variables in 2 payloads using 1-6 for 1 payload and 7-13 in second just updates the 1st payload variables and not the second.

Is there a limit on payload data?
Program:

TOKEN = "**********"  # Put your TOKEN here
DEVICE_LABEL = "dynamicenergy2"  # Put your device label here

path1 = "C:/Users/parth/Documents/Dyn Battery.csv"
path2 = "C:/Users/parth/Documents/Dyn Transfer.csv"

df1 = pd.read_csv(path1)
df2 = pd.read_csv(path2)

df1 = df1.dropna()
df2 = df2.dropna()

i = 0

VARIABLE_LABEL_1 = "Battery_Res1"  # Put your first PI node status here
VARIABLE_LABEL_2 = "Battery_Res2"  # Put your second PI node status here
VARIABLE_LABEL_3 = "Battery_Hospital"  # Put your third PI node status here
VARIABLE_LABEL_4 = "Battery_School"  # Put your fourth PI node status here
VARIABLE_LABEL_5 = "Battery_Playground"  # Put your fifth PI node status here
VARIABLE_LABEL_6 = "Speed_Bump"
VARIABLE_LABEL_7 = "Connection_Residence1_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_8 = "Connection_Playground_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_9 = "Connection_SpeedBump_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_10 = "Connection_SpeedBump_School"  # Put your fifth PI node status here
VARIABLE_LABEL_11 = "Connection_Residence2_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_12 = "TIME"
VARIABLE_LABEL_13 = "GPS"

def build_payload(variable_1, variable_2, variable_3, variable_4, variable_5, variable_6, variable_7, variable_8,variable_9,variable_10,variable_11,variable_12,variable_13,variable_14):

    lat = 40.8145036
    lng = -73.959212
    value_time = df1["Time"][variable_14]
    value_1 = df1["RC1 Battery"][variable_14].round(2)
    value_2 = df1["RC2 Battery"][variable_14].round(2)
    value_3 = df1["Hospital Battery"][variable_14].round(2)
    value_4 = df1["School Battery"][variable_14].round(2)
    value_5 = df1["Playground Battery"][variable_14].round(2)
    value_6 = 0

    if(df2["RC1 Battery"][variable_14] == "N"):
        value_7 = 0
    elif(df2["RC1 Battery"][variable_14] == "H"):
        value_7 = 1
    #elif(df2["RC1 Battery"][variable_14] == "S"):
    #    value_7 = 2

    if(df2["RC2 Battery"][variable_14] == "N"):
        value_11 = 0
    elif(df2["RC2 Battery"][variable_14] == "H"):
        value_11 = 1
    #elif(df2["RC2 Battery"][variable_14] == "S"):
    #    value_7 = 2

    if(df2["Playground Battery"][variable_14] == "N"):
        value_8 = 0
    elif(df2["Playground Battery"][variable_14] == "H"):
        value_8 = 1
    #elif(df2["RC1 Battery"][variable_14] == "S"):
    #    value_7 = 2

    if(df2["Speed Bump"][variable_14] == "N"):
        value_9 = 0
        value_10 =0
    elif(df2["Speed Bump"][variable_14] == "H"):
        value_9 = 1
        value_10 = 0
    elif(df2["RC1 Battery"][variable_14] == "S"):
        value_9 = 0
        value_10 = 1

    # Creates two random values for sending data
    payload = { variable_1: value_1,
                variable_2: value_2,
                variable_3: value_3,
                variable_4: value_4,
                variable_5: value_5,
                variable_6: value_6,
                variable_7: value_7,
                variable_8: value_8,
                variable_9: value_9,
                variable_10: value_10,
                variable_11: value_11,
                variable_12: value_time,
                variable_13: {"value": 1, "context": {"lat": lat, "lng": lng}}}

    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"}

    # 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)

    # Processes results
    if status >= 400:
        print("[ERROR] Could not send data after 5 attempts, please check \
            your token credentials and internet connection")
        return False

    print("[INFO] request made properly, your device is updated")
    return True


def main():
    i =0
    while(i<24):
        payload = build_payload(VARIABLE_LABEL_1, VARIABLE_LABEL_2, VARIABLE_LABEL_3,VARIABLE_LABEL_4, VARIABLE_LABEL_5, VARIABLE_LABEL_6, VARIABLE_LABEL_7, VARIABLE_LABEL_8, VARIABLE_LABEL_9, VARIABLE_LABEL_10,VARIABLE_LABEL_11,VARIABLE_LABEL_12,VARIABLE_LABEL_13, i)

        print("[INFO] Attemping to send data")
        print(payload)
        post_request(payload)
        print("[INFO] finished")
        i=i+1
        time.sleep(5)

if __name__ == '__main__':
    while (True):
        main()
        time.sleep(30)

#2

Hello @parthrsingh,

The maximum payload size is 10.000 bytes.

Taking your code as a reference, I build the sample code to test that everything was working properly and I get a successful response from the server. As you can see below the data is being posted in ubidots without issues:

Sample code used:

import requests
import random
import time

TOKEN = "BBFF-xxxxxxxxxxxxxxxxxxxxxxx"  # Put your TOKEN here
DEVICE_LABEL = "dynamicenergy2"  # Put your device label here

VARIABLE_LABEL_1 = "Battery_Res1"  # Put your first PI node status here
VARIABLE_LABEL_2 = "Battery_Res2"  # Put your second PI node status here
VARIABLE_LABEL_3 = "Battery_Hospital"  # Put your third PI node status here
VARIABLE_LABEL_4 = "Battery_School"  # Put your fourth PI node status here
VARIABLE_LABEL_5 = "Battery_Playground"  # Put your fifth PI node status here
VARIABLE_LABEL_6 = "Speed_Bump"
VARIABLE_LABEL_7 = "Connection_Residence1_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_8 = "Connection_Playground_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_9 = "Connection_SpeedBump_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_10 = "Connection_SpeedBump_School"  # Put your fifth PI node status here
VARIABLE_LABEL_11 = "Connection_Residence2_Hospital"  # Put your fifth PI node status here
VARIABLE_LABEL_12 = "TIME"
VARIABLE_LABEL_13 = "GPS"

def build_payload(variable_1, variable_2, variable_3, variable_4, variable_5, variable_6, variable_7, variable_8,variable_9,variable_10,variable_11,variable_12,variable_13,variable_14):

    lat = 40.8145036
    lng = -73.959212

    value_1 = random.randint(100, 1000)
    value_2 = random.randint(100, 1000)
    value_3 = random.randint(100, 1000)
    value_4 = random.randint(100, 1000)
    value_5 = random.randint(100, 1000)
    value_6 = random.randint(100, 1000)
    value_7 = random.randint(100, 1000)
    value_8 = random.randint(100, 1000)
    value_9 = random.randint(100, 1000)
    value_10 = random.randint(100, 1000)
    value_11 = random.randint(100, 1000)
    value_time = 1527368981000 # Timestamp in milliseconds

    # Creates two random values for sending data
    payload = { variable_1: value_1,
                variable_2: value_2,
                variable_3: value_3,
                variable_4: value_4,
                variable_5: value_5,
                variable_6: value_6,
                variable_7: value_7,
                variable_8: value_8,
                variable_9: value_9,
                variable_10: value_10,
                variable_11: value_11,
                variable_12: value_time,
                variable_13: {"value": 1, "context": {"lat": lat, "lng": lng}}}

    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"}

    # 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)

    # Processes results
    if status >= 400:
        print("[ERROR] Could not send data after 5 attempts, please check \
            your token credentials and internet connection")
        return False

    print("[INFO] request made properly, your device is updated")
    return True


def main():
    payload = build_payload(VARIABLE_LABEL_1, VARIABLE_LABEL_2, VARIABLE_LABEL_3,VARIABLE_LABEL_4, VARIABLE_LABEL_5, VARIABLE_LABEL_6, VARIABLE_LABEL_7, VARIABLE_LABEL_8, VARIABLE_LABEL_9, VARIABLE_LABEL_10,VARIABLE_LABEL_11,VARIABLE_LABEL_12,VARIABLE_LABEL_13, 1)
    print("[INFO] Attemping to send data")
    print(payload)
    post_request(payload)
    print("[INFO] finished")
    time.sleep(5)

if __name__ == '__main__':
    while (True):
        main()
        time.sleep(5)

At this point, based on the behavior presented by your side looks like one of the values sent is not allowed. The Ubidots backend rejects all the data which is being sent and it creates the device with the variables empties. To check this, you can force the request in order to post the other variables and check which variable is not being posted; for this, you should add the force parameter which is carefully explained in the Ubidots Rest API

I hope this would helo you!

All the best,
Maria C.


#3

Thanks Maria,I found the issue, the time variable was not in a proper format and which broke the payload sync. I used the guide below to transfer the value as string and display it.
http://help.ubidots.com/user-guides/using-context-to-display-text-in-ubidots-widgets”.

The system should not return that the payload was posted successfully if there is an error, shouldn’t it?


#4

Dear @parthrsingh,

Regrettably, I’m not able to reproduce the issue presented by your side. As you can see below, is working properly for me:

image

Can you give you more details about the issue presented?

Thanks!
Maria C.


#5

Hey @mariahernandez,
I apologize for the confusion.
Using the guide I did end up transferring the data successfully and display it.
timevalue

My point about the error was that because I was sending a data type which was not an integer, the upload should not have been accepted.
Best,
Parth


#6

Don’t worry! We are here to help you! :slight_smile:

All the best,
Maria C.