I have a program which reads two files and updates the value to the dashboard.
- Putting all variables in one payload doesn’t work. The device creates the variables but they never update.
- 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)