将DHT22传感器数据上传到Xively Feed

时间:2013-08-22 19:59:27

标签: python c raspberry-pi sensor xively

我正在尝试修改DHT22传感器存在的一些预定义代码。我想修改Adafruit's DHT_Driver,以便它返回一个对应于传感器输出的Temperature值和Humidity值的数组。我想进行此更改,以便我可以在Python片段中使用输出数组。也就是说,我想使用输出数组值将数据上传到Xively Feed。

我正在寻找类似的东西...

#!/usr/bin/env python
import time
import os
import eeml

# Xively variables specific to my account.
API_KEY = 'API Key Here'
FEED = 'FEED # Here'
API_URL = '/v2/feeds/{feednum}.xml' .format(feednum = FEED)

# Continuously read data from the DHT22 sensor and upload
# the results to the Xively feed. 
while True:
    # Read the data from the sensor.
    sensorData = .... // Call on the main method within the DHT_Driver.c file
    temp_C = sensorData[0]
    humidity = sensorData[1]

    if DEBUG:
        print("sensorData:\t", sensorData)
        print("\n")

    if LOGGER:
        # Initialize the users Xively account.
        pac = eeml.Pachube(API_URL, API_KEY)

        # Prepare the data to be uploaded to the Xively
        # account.
        # temp_C & humidity are extracted from their indices
        # above.
        pac.update([eeml.Data(0, temp_C, unit=eeml.Celsius())])
        pac.update([eeml.Data(1, humidity, unit=eeml.%())])

        # Upload the data to the Xively account.
        pac.put()

        # Wait 30 seconds to avoid flooding the Xively feed.
        time.sleep(30)

我需要一些关于从传感器获取温度和湿度值的反馈。它必须使用C,因为Python不够快,无法处理来自传感器的数据。理想情况下,我只能返回一个包含两个值的数组,并访问如下所示的值:

temp_C = sensorData[0]
humidity = sensorData[1]

另外,如果在这个Python片段中,我要调用DHT_Driver.c文件中的main方法,那么这将受到Python解释器的限制(即基于C的程序将以与基于Python的程序类似的性能运行)?

我对Python非常不熟悉,我刚刚开始使用C,所以如果有任何建议或积极的批评,请随时加入。

1 个答案:

答案 0 :(得分:0)

首先,您应该更新代码以使用Xively提供的official Python module

#!/usr/bin/env python
import time
import os
import xively

# Xively variables specific to my account.
API_KEY = ....
FEED_ID = ....


# Continuously read data from the DHT22 sensor and upload
# the results to the Xively feed. 
while True:

    # Initialize Xively library and fetch the feed
    feed = xively.XivelyAPIClient(API_KEY).feeds.get(FEED_ID)

    feed.datastreams = [
        xively.Datastream(id='tempertature', current_value=getTemperature()),
        xively.Datastream(id='humidity', current_value=getHumidity()),
    ]
    # Upload the data into the Xively feed
    feed.update()

    # Wait 30 seconds to avoid flooding the Xively feed.
    time.sleep(30)

关于传感器驱动程序,我会看一下AirPi