zoukankan      html  css  js  c++  java
  • 利用樹霉派採集溫濕度上傳到OneNET(非完整,僅參考)

    看圖:

    Python代碼:

      1 #env /usr/bin/python3
      2 #author Bruce
      3 
      4 import RPi.GPIO as GPIO
      5 import time
      6 import json
      7 import datetime
      8 import requests
      9 
     10 APIKEY = '=xxxxxxxxxxxxxxxxxxxx='
     11 apiurl = 'http://api.heclouds.com/devices/11111111/datapoints'
     12 apiheaders = {'api-key': APIKEY, 'Content-Length': '120'}
     13 
     14 def getTemp():
     15     channel = 17
     16     data = []
     17     j = 0
     18 
     19     GPIO.setmode(GPIO.BCM)
     20 
     21     time.sleep(1)
     22 
     23     GPIO.setup(channel, GPIO.OUT)
     24 
     25     GPIO.output(channel, GPIO.LOW)
     26     time.sleep(0.02)
     27     GPIO.output(channel, GPIO.HIGH)
     28 
     29     GPIO.setup(channel, GPIO.IN)
     30 
     31     while GPIO.input(channel) == GPIO.LOW:
     32         continue
     33 
     34     while GPIO.input(channel) == GPIO.HIGH:
     35         continue
     36 
     37     while j < 40:
     38         k = 0
     39         while GPIO.input(channel) == GPIO.LOW:
     40             continue
     41     
     42         while GPIO.input(channel) == GPIO.HIGH:
     43             k += 1
     44             if k > 100:
     45                 break
     46     
     47         if k < 8:
     48             data.append(0)
     49         else:
     50             data.append(1)
     51 
     52         j += 1
     53 
     54     print("sensor is working.")
     55     print(data)
     56 
     57     humidity_bit = data[0:8]
     58     humidity_point_bit = data[8:16]
     59     temperature_bit = data[16:24]
     60     temperature_point_bit = data[24:32]
     61     check_bit = data[32:40]
     62 
     63     humidity = 0
     64     humidity_point = 0
     65     temperature = 0
     66     temperature_point = 0
     67     check = 0
     68 
     69     for i in range(8):
     70         humidity += humidity_bit[i] * 2 ** (7 - i)
     71         humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
     72         temperature += temperature_bit[i] * 2 ** (7 - i)
     73         temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
     74         check += check_bit[i] * 2 ** (7 - i)
     75 
     76     tmp = humidity + humidity_point + temperature + temperature_point
     77 
     78     tmp_output = open('/home/pi/Desktop/Projects/DHT11/log.txt', 'a+')
     79     csv=open('/home/pi/Desktop/Projects/DHT11/log.csv', 'a+')
     80 
     81     if check == tmp:
     82         print("temperature : ", temperature, ", humidity : " , humidity)
     83     
     84         tmp_output.write(',
    {"Time":' + time.strftime("%Y/%m/%d %H:%M:%S",time.localtime()) + ",")
     85         tmp_output.write('"temperate":')
     86         tmp_output.write(str(temperature))
     87         tmp_output.write(',')
     88         tmp_output.write('"Humidity":' + str(humidity))
     89         tmp_output.write('}')
     90         
     91         csv.write(time.strftime("%Y/%m/%d %H:%M:%S",time.localtime()) + ",")
     92         csv.write(str(temperature))
     93         csv.write(',')
     94         csv.write(str(humidity))
     95         csv.write('
    ')
     96     else:
     97         print("wrong")
     98         print("temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmp)
     99 
    100     tmp_output.close()
    101     csv.close()
    102 
    103     CurTime = datetime.datetime.now()
    104     payload = {'datastreams':[{"id":"DHT11a","datapoints":[{"at":CurTime.isoformat(),"value":temperature}]}]}
    105     print("Current time: %s" % CurTime.isoformat())
    106     print("Upload value: %.3f" % temperature)
    107 
    108     jdata = json.dumps(payload)
    109 
    110     r=requests.post(apiurl,headers=apiheaders,data=json.dumps(payload))
    111 
    112     return r
    113 
    114 if __name__ == "__main__":
    115     try:
    116         while 1:
    117             resp = getTemp()
    118             print("OneNET request result: 
     %s" %resp)
    119             time.sleep(55)
    120     finally:
    121         GPIO.cleanup()
    View Code
  • 相关阅读:
    第一章 快速入门
    增量式PID控制算法
    第二章 变量和基本类型
    位置式PID和增量式PID区别?
    I2C
    Linux系统进程调用列表
    Linux下I/O模型
    Linux下多路复用接口
    新浪博客网页编辑器PHP版带有上传图片功能
    晕死!博客园把我搞晕了!
  • 原文地址:https://www.cnblogs.com/keepee/p/9582545.html
Copyright © 2011-2022 走看看