python

时间:2018-01-17 13:17:31

标签: python json

我收到此错误:'int' object is not iterable以下代码用于将Raspberry Pi中的传感器值发送到Ubidots。我已经阅读了类似的问题和答案,但我无法弄清楚错误在哪里。

import time
import sys
import traceback
from time import sleep
import requests
import Adafruit_ADS1x15

# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')

while True:
   m = adc.read_adc(0, gain=GAIN)
   try:
    r = requests.post('http://things.ubidots.com/api/v1.6/devices/raspberry/?token={A1E-6l4DwwC86SDR6QRTJWXGDwshTGPHFl}', m)
    print('Moisture Level:{0:>6}'.format(m))
    time.sleep(1)
   except KeyboardInterrupt:
    break
   traceback.print_exc()

回溯:

Reading ADS1x15 values, press Ctrl-C to quit...
Traceback (most recent call last):
File "soilhumidity2.py", line 23, in <module>
r = requests.post('http://things.ubidots.com/api/v1.6/devices/raspberry/?token={A1E-6l4DwwC86SDR6QRTJWXGDwshTGPHFl}', m)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 443, in send
for i in request.body:
TypeError: 'int' object is not iterable

我使用此链接作为参考:http://help.ubidots.com/iot-projects-tutorials/diy-raspberry-pi-temperature-system-with-ubidots

3 个答案:

答案 0 :(得分:2)

来自requests.post()的文档:

  

参数:
  url - 新Request对象的URL   数据 - (可选)      要发送的字典(将进行表单编码),字节或类文件对象      在请求的正文中。

看起来read_adc()会返回一个整数,因此您作为data参数传递的内容不是post()所期望的。

我对Ubidots并不熟悉,因此我无法准确地告诉您预期发布数据的格式。您需要找到它并提供{{1} }或dict对象格式化。

编辑:根据您的comment,如果您需要发送JSON数据,请根据需要准备数据结构并发送:

bytes

答案 1 :(得分:1)

request.post中的有效负载必须是字典:

import time
import sys
from time import sleep
import requests
import Adafruit_ADS1x15

# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')

while True:
    m = adc.read_adc(0, gain=GAIN)
    payload={'adc value ': m}
    try:
        r = requests.post('http://things.ubidots.com/api/v1.6/devices/raspberry/?token={A1E-6l4DwwC86SDR6QRTJWXGDwshTGPHFl}', data=payload)
        print('Moisture Level:{0:>6}'.format(m))
        time.sleep(1)
    except KeyboardInterrupt:
        break
    except Exception as e:
        print (e) 

参见http://help.ubidots.com/iot-projects-tutorials/diy-raspberry-pi-temperature-system-with-ubidots

中的payload = {'temp_celsius': temp_c, 'temp_fahrenheit': temp_f}

答案 2 :(得分:0)

这种方法对我有用:

import time
import sys
from time import sleep
import requests
import Adafruit_ADS1x15
from ubidots import ApiClient
import RPi.GPIO as GPIO

# Create an ApiClient object
api = ApiClient("A1E-0d8bec822d9d5ccae0fb08a75dfdb22e17c3")


# Get a Ubidots Variable
variable = api.get_variable("5a5f80eec03f971388983b8e")


# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')

while True:
    ldr = adc.read_adc(1, gain=GAIN)
    try:
       response = variable.save_value({'value': ldr})
       print response
       print "Light Intensity: %d" % ldr
       time.sleep(0.5)
    except KeyboardInterrupt:
       break
    except Exception as e:
       print (e)