1

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

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

#!/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,所以如果有任何建议和或积极的批评,请随时加入。

4

1 回答 1

0

首先,您应该更新代码以使用Xively 提供的官方 Python 模块。

#!/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

于 2013-08-23T10:58:04.943 回答