上传DHT22传感器数据至Xively Feed

2024-06-16 14:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试修改^{}传感器的一些预定义代码。我想修改^{},以便它返回与传感器输出的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不够快,无法处理来自传感器的数据。理想情况下,我可以返回一个包含这两个值的数组,然后访问这些值:

^{pr2}$

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

我对Python非常陌生,而且我刚刚开始使用C语言,所以如果有任何建议和/或积极的批评,请随时加入进来。在


Tags: thetofromimportapidatafeedaccount
1条回答
网友
1楼 · 发布于 2024-06-16 14:24:25

首先,您应该更新代码以使用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。在

相关问题 更多 >