I’ve been attempting to post multiple values to a single variable with my Raspberry Pi to no avail. I feel that I’m missing something simple here and would appreciate any help in the right direction.
import json
import requests
token = "my-token"
"""
Get values from variable
"""
def post_values(device_label, var_label, items):
base_url = "http://things.ubidots.com/api/v1.6/devices/" + device_label + "/" + var_label + "/values"
try:
r = requests.post(base_url + '?token=' + token, data=items)
return r.json()
except Exception as e:
print(e)
return {'error':'Request failed or timed out'}
"""
Main function
"""
if __name__ == '__main__':
while True:
device_label = "rpi-1"
var_label = "Temperature"
payload = (("value", 1),("value", 2),("value", 3),("value", 4),("value", 5))
values = post_values(device_label, var_label, payload)
print(values)
I have also tried some variations on the payload:
payload = ([{"value", 1},{"value", 2},{"value", 3},{"value", 4},{"value", 5}])
Error: {u’value’: [u’This field is required.’]}
payload = [{"value": 1},{"value": 2},{"value": 3},{"value": 4},{"value": 5}]
Error: need more than 1 value to unpack
{‘error’: ‘Request failed or timed out’}
payload = {"value": 1},{"value": 2},{"value": 3},{"value": 4},{"value": 5}
Error: need more than 1 value to unpack
{‘error’: ‘Request failed or timed out’}