【问题标题】:Python: Error while pushing sensor data with raspberry pi to xively TypeError: __init__()Python:使用树莓派将传感器数据推送到 xively 时出错 TypeError:__init__()
【发布时间】:2014-09-10 07:51:00
【问题描述】:

我尝试使用树莓派从我的 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'

我该如何解决这个错误?这很有趣,但在另一个 Raspberry Pi 上它正在工作......

这是我的代码 (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()

【问题讨论】:

  • 能否提供无效的代码?
  • 嗨,代码在 xively_ds18b20.py 文件中,我已在我的问题中发布了该文件。
  • 好吧,您的 Feed 244127069 中似乎有一封电子邮件,但 Feed 对象并不期待...您能检查您的 Feed 并告诉我们其中的内容吗?

标签: python raspberry-pi temperature xively


【解决方案1】:

根据this bug report,您可以通过从提要元数据中删除您的电子邮件地址来解决此问题:

当我从 Feed 元数据中删除我的电子邮件地址时(通过 工作台)并重新运行api.feeds.get(FEED_ID) 它工作得很好。

但是,当我查看latest source code on 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):

It seems this bug was only fixed recently (June 27th)。您可以尝试从 github 下载最新版本的 xively-python,并使用它来代替您拥有的当前版本吗?

【讨论】:

  • 我已经使用pip install xively-python --upgrade 安装了最新版本...但它说:Requirement already up-to-date: xively-python in ./.envs/venv/lib/python2.7/site-packages Requirement already up-to-date: requests&gt;=1.1.0 in ./.envs/venv/lib/python2.7/site-packages (from xively-python) Cleaning up...。之后,我收到与以前相同的错误
  • @mcknight 我认为包含此更改的新版本尚未正式发布。您要么需要手动应用补丁,要么使用 git 下载最新代码并从源代码安装。
  • @mcknight 从 git 安装就像 git clone https://github.com/xively/xively-python.git ; cd xively-python ; sudo python setup.py install 一样简单。不过,您可能应该先卸载您已经拥有的 xively 版本。
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
相关资源
最近更新 更多