Python:通过树莓派将传感器数据推送到xively时出现TypeError:__init__()错误

2 投票
1 回答
574 浏览
提问于 2025-04-20 20:02

我尝试用树莓派读取我的DS18B20温度传感器的数据,并把这些数据上传到Xively。

在控制台执行了一些前期准备和Python文件:

sudo modprobe w1-gpio && sudo modprobe w1_therm
source .envs/venv/bin/activate
FEED_ID=244127069 API_KEY=Nqeje SENSOR_ID=28-00000539324e python xively_ds18b20.py

之后出现了以下错误:

Traceback (most recent call last):
  File "xively_ds18b20.py", line 59, in <module>
    run()
  File "xively_ds18b20.py", line 42, in run
    feed = api.feeds.get(FEED_ID)
  File "/home/pi/xively_tutorial/.envs/venv/local/lib/python2.7/site-packages/xively/managers.py", line 266, in get
    feed = self._coerce_feed(data)
  File "/home/pi/xively_tutorial/.envs/venv/local/lib/python2.7/site-packages/xively/managers.py", line 289, in _coerce_feed
    feed = Feed(**feed_data)
TypeError: __init__() got an unexpected keyword argument 'email'

我该如何解决这个错误呢?有趣的是,在另一台树莓派上这个是可以工作的……

这是我的代码(xively_ds18b20.py):

#!/usr/bin/env python

import os
import xively
import subprocess
import time
import datetime
import requests


FEED_ID = os.environ["FEED_ID"]
API_KEY = os.environ["API_KEY"]
SENSOR_ID_PRE = os.environ["SENSOR_ID"]
SENSOR_ID = SENSOR_ID_PRE[-7:]

# initialize api client
api = xively.XivelyAPIClient(API_KEY)

# function to read the temperature from ds18b20 temperature sensor on i2c 
def read_temperature():
   tempfile = open("/sys/bus/w1/devices/"+SENSOR_ID_PRE+"/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n")[1].split(" ")[9]
   temperature = float(tempdata[2:])
   temperature = temperature / 1000
   return temperature

# 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("PiTemperature"+SENSOR_ID)
    return datastream
  except:
    datastream = feed.datastreams.create("PiTemperature"+SENSOR_ID, tags="temperature")
    return datastream

# main program entry point - runs continuously updating our datastream with the
# latest temperature reading
def run():
  feed = api.feeds.get(FEED_ID)

  datastream = get_datastream(feed)
  datastream.max_value = None
  datastream.min_value = None

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

    time.sleep(10)

run()

1 个回答

3

根据这个错误报告,你可以通过从数据源的元数据中去掉你的邮箱地址来解决这个问题:

当我通过工作台把我的邮箱地址从数据源的元数据中去掉,然后重新运行api.feeds.get(FEED_ID)时,一切就正常了。

不过,当我查看GitHub上的最新源代码时,我发现email是被支持的:

class Feed(Base):
    """Xively Feed, which can contain a number of Datastreams.
    :param title: A descriptive name for the feed
    :param description: A longer text description of the feed
    :param website: The URL of a website which is relevant to this feed e.g.
    home page
    :param email: A public contact email address for the provider of this feed
    :param tags: Tagged metadata about the environment (characters ' " and
    commas will be stripped out)
    :param location: :class:`.Location` object for this feed
    :param private: Whether the environment is private or not.
    :type private: bool
    Usage::
    >>> import xively
    >>> xively.Feed(title="Xively Office environment")
    <xively.Feed(None)>
    """
    VERSION = "1.0.0"
    # Set id and feed directly as they aren't part of state. By setting them on
    # the class they won't get entered into _data and will be set on the
    # instance itself.
    id = None
    feed = None
    _datastreams_manager = None
    def __init__(self, title, description=None, website=None, email=None,
    tags=None, location=None, private=None, datastreams=None):

看起来这个错误最近才被修复(6月27日)。你能试着从GitHub上下载最新版本的xively-python,然后用这个版本替换掉你现在的版本吗?

撰写回答