利用樹霉派採集溫濕度上傳到OneNET(非完整,僅參考)
阅读原文时间:2023年07月17日阅读:1

看圖:

Python代碼:

#env /usr/bin/python3
#author Bruce

import RPi.GPIO as GPIO
import time
import json
import datetime
import requests

APIKEY = '=xxxxxxxxxxxxxxxxxxxx='
apiurl = 'http://api.heclouds.com/devices/11111111/datapoints'
apiheaders = {'api-key': APIKEY, 'Content-Length': ''}

def getTemp():
channel = 17
data = []
j = 0

 GPIO.setmode(GPIO.BCM)

 time.sleep(1)

 GPIO.setup(channel, GPIO.OUT)

 GPIO.output(channel, GPIO.LOW)  
 time.sleep(0.02)  
 GPIO.output(channel, GPIO.HIGH)

 GPIO.setup(channel, GPIO.IN)

 while GPIO.input(channel) == GPIO.LOW:  
     continue

 while GPIO.input(channel) == GPIO.HIGH:  
     continue

 while j < 40:  
     k = 0  
     while GPIO.input(channel) == GPIO.LOW:  
         continue

     while GPIO.input(channel) == GPIO.HIGH:  
         k += 1  
         if k > 100:  
             break

     if k < 8:  
         data.append(0)  
     else:  
         data.append(1)

     j += 1

 print("sensor is working.")  
 print(data)

 humidity\_bit = data\[0:8\]  
 humidity\_point\_bit = data\[8:16\]  
 temperature\_bit = data\[16:24\]  
 temperature\_point\_bit = data\[24:32\]  
 check\_bit = data\[32:40\]

 humidity = 0  
 humidity\_point = 0  
 temperature = 0  
 temperature\_point = 0  
 check = 0

 for i in range(8):  
     humidity += humidity\_bit\[i\] \* 2 \*\* (7 - i)  
     humidity\_point += humidity\_point\_bit\[i\] \* 2 \*\* (7 - i)  
     temperature += temperature\_bit\[i\] \* 2 \*\* (7 - i)  
     temperature\_point += temperature\_point\_bit\[i\] \* 2 \*\* (7 - i)  
     check += check\_bit\[i\] \* 2 \*\* (7 - i)

 tmp = humidity + humidity\_point + temperature + temperature\_point

 tmp\_output = open('/home/pi/Desktop/Projects/DHT11/log.txt', 'a+')  
 csv=open('/home/pi/Desktop/Projects/DHT11/log.csv', 'a+')

 if check == tmp:  
     print("temperature : ", temperature, ", humidity : " , humidity)

     tmp\_output.write(',\\n{"Time":' + time.strftime("%Y/%m/%d %H:%M:%S",time.localtime()) + ",")  
     tmp\_output.write('"temperate":')  
     tmp\_output.write(str(temperature))  
     tmp\_output.write(',')  
     tmp\_output.write('"Humidity":' + str(humidity))  
     tmp\_output.write('}')

     csv.write(time.strftime("%Y/%m/%d %H:%M:%S",time.localtime()) + ",")  
     csv.write(str(temperature))  
     csv.write(',')  
     csv.write(str(humidity))  
     csv.write('\\n')  
 else:  
     print("wrong")  
     print("temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmp)

 tmp\_output.close()  
 csv.close()

 CurTime = datetime.datetime.now()  
 payload = {'datastreams':\[{"id":"DHT11a","datapoints":\[{"at":CurTime.isoformat(),"value":temperature}\]}\]}  
 print("Current time: %s" % CurTime.isoformat())  
 print("Upload value: %.3f" % temperature)

 jdata = json.dumps(payload)

 r=requests.post(apiurl,headers=apiheaders,data=json.dumps(payload))

 return r

if __name__ == "__main__":
try:
while 1:
resp = getTemp()
print("OneNET request result: \n %s" %resp)
time.sleep(55)
finally:
GPIO.cleanup()