gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:29 25 4
gpt4 key购买 nike

我正在尝试修改 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 文件中的主要方法,这是否会受到 Python 解释器的限制(即,基于 C 的程序是否会以与基于 Python 的程序类似的性能运行)?

我对 Python 非常陌生,我刚刚开始学习 C,所以如果有任何建议或正面批评,请随时加入。

最佳答案

首先你应该更新代码以使用official Python module由 Xively 提供。

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

关于python - 将 DHT22 传感器数据上传到 Xively Feed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18389761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com