将Raspberry Pi Datastream发送到xively

时间:2014-03-02 18:05:10

标签: python raspberry-pi xively

我的rpi通过RS232连接到数字秤,MAX3232串口/ ttl模块连接到GPIO。使用此代码成功收集了来自秤的重量数据(数据为20块,权重为11-14)。这将在终端本地打印出来:

import serial
import time

ser = serial.Serial("/dev/ttyAMA0")
read = ser.read(20)

def get_num(read):
    return float(''.join(ele for ele in read if ele.isdigit() or ele == '.'))

while True:
    print(get_num(read))
    time.sleep(2)

这很好但是我的python代码将数据流发送到xively并不是那么好。

def get_num(read):
    return float(''.join(ele for ele in read if ele.isdigit() or ele == '.'))

# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
    try:
        datastream = feed.datastreams.get("(get_num(read))")
        return datastream
    except:
        datastream = feed.datastreams.create("(get_num(read))", tags = "weight")
        return datastream
def run():
    feed = api.feeds.get(FEED_ID)
    datastream = get_datastream(feed)
    datastream.max_value = None
    datastream.min_value = None

    while True:
        weight = get_num(read)
        datastream.current_value = weight
        datastream.at = datetime.datetime.utcnow()
        try:
            datastream.update()
        except requests.HTTPError as e:
            print "HTTPError({0}): {1}".format(e.errno, e.strerror)

        time.sleep(5)

run()

它在IDLE中检出OK - 但是在运行时它会产生如下错误:

Traceback (most recent call last):
File "xivelycodescales.py", line 51, in <module>
run()
File "xivelycodescales.py", line 36, in run
datastream = get_datastream(feed)
File "xivelycodescales.py", line 32, in get_datastream
datastream = feed.datastreams.create("(get_num(read))")
File "/usr/local/lib/python2.7/dist-packages/xively/managers.py", line 416, in create
response.raise_for_status()
File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 773, in     raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 422 Client Error:

我可以看到错误类型是什么 - 并在RPI论坛上得到了很好的帮助。但无法弄清楚我哪里出错了

非常感谢

0 个答案:

没有答案