【问题标题】:Raspberry Pi GPIO to Xively python example?Raspberry Pi GPIO 到 Xively python 示例?
【发布时间】:2014-10-14 02:03:26
【问题描述】:

我是一个非程序员,我正在使用运行 python 的 Raspberry Pi 体验 Xively。我已经成功完成了 xively 网站上给出的教程:https://xively.com/dev/tutorials/pi/

我项目的下一步是不断地从其中一个 GPIO 引脚读取读数(无论是高电平还是低电平)进行一些计算,然后每分钟向 Xively 提要提供一个数字。

我的 GPIO 部分在单独的程序中正常工作。但是当我尝试将其合并到我运行的 Xively 教程示例程序中时,我遇到了问题。

我现在想我应该让 GPIO 程序和 Xively 程序同时运行,同时 GPIO 程序将数据写入文件,Xively 程序从该文件读取数据并将数据上传到提要。所以我的问题是:这是否可能并且相当容易执行?

或者,如果有人可以指出一个示例,其中 Xively 教程示例被修改为接受 GPIO 输入,那也会有所帮助。

如果有更好/更简单的方法来完成我所追求的,我愿意接受建议......

【问题讨论】:

    标签: python raspberry-pi gpio xively


    【解决方案1】:

    你需要:

    首先下载并安装 Xively Python 库。 下载并安装 httplib2 模块。

    您可以将“GPIO Sensor”脚本和“Send Data to Xively”脚本结合起来。

    这是一个示例代码(适用于 Raspberry Pi 和 DHT 11):

    #!/usr/bin/python
    
    # Import required Python libraries
    import RPi.GPIO as GPIO
    import time
    import random
    import datetime
    import json
    import sys
    import Adafruit_DHT
    # Custom HTTP Library
    import httplib2
    
    # Use BCM GPIO references
    # instead of physical pin numbers
    #GPIO.setmode(GPIO.BCM)
    
    # Define GPIO to use on Pi
    
    # Set pin as input
    #GPIO.setup(GPIO_PIR,GPIO.IN)
    
    # Define sensor state (1 by default)
    SENSOR_STATE = 1
    
    DHT_TYPE = Adafruit_DHT.DHT11
    DHT_PIN  = 22
    # Define URLs and API key for sending and receiving requests
    sensorURL = ""
    temperatureURL = ""
    humidityURL = ""
    API_KEY = ""
    
    # Retrieve latest sensor state from Xively via GET request
    def retrieve_sensor_state():
      try:
    
        global SENSOR_STATE  
        print "\nRetrieving from Xively"
    
        h = httplib2.Http(disable_ssl_certificate_validation=True)
        (resp_headers, content) = h.request(sensorURL, "GET", headers={'X-ApiKey':API_KEY})
        jsonData = json.loads(content)
        SENSOR_STATE = jsonData["current_value"]
        print "Sensor state: " + SENSOR_STATE
        if (SENSOR_STATE == str(1)):
           upload_to_xively()
           print "continuing!"
        else:
           print "sensor is disabled!"  
    
      except:
        print "Error occurred!"
    
    # Upload sensor data to Xively via PUT request
    def upload_to_xively():
      try:
    
        print "Uploading to Xively"
    
        now = str(datetime.datetime.now())
        humidity, value = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
        if humidity is not None and value is not None:
            print "temperature value :" + str(value) + "    |    " + str(now)
            print "humidity value :" + str(humidity) + "    |    " + str(now)
    
            jsonString = {"id":"temperature","tags":[],"unit":{},"current_value":value}
            publishString = json.dumps(jsonString)
    
            h = httplib2.Http(disable_ssl_certificate_validation=True)
            (resp, content) = h.request(temperatureURL, "PUT", body=publishString, headers={'X-ApiKey':API_KEY})
    
            jsonString1 = {"id":"humidity","tags":[],"unit":{},"current_value":humidity}
            publishString1 = json.dumps(jsonString1)
            g = httplib2.Http(disable_ssl_certificate_validation=True)
            (resp, content) = g.request(humidityURL, "PUT", body=publishString1, headers={'X-    ApiKey':API_KEY})
        else:
        print "failed to read data! Trying again"
    
      except:
        print "Error occurred!"
    
    # Callback function from event handler when motion is detected
    #def motion_callback(GPIO_PIR):
    
    #  global SENSOR_STATE
      # Log the sensor data down only if sensor state = 1 (enabled)
    #  if SENSOR_STATE == str(1):
    #    if GPIO.input(GPIO_PIR):
    #        print "\nMotion Detected! @ " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    #        upload_to_xively()
    #        time.sleep(1)
    #    else:
    #        print "\nBack to Normal"
    
    # Main program
    print "DHT Module Test (Ctrl+C to exit)"
    time.sleep(2)
    print "Ready"
    
    try:
      while True: 
        retrieve_sensor_state()
        time.sleep(10)
        continue
    
    except KeyboardInterrupt:
      print "Quit"
      GPIO.cleanup()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多