利用樹霉派採集溫濕度上傳到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(',\n{"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('\n') 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: \n %s" %resp) 119 time.sleep(55) 120 finally: 121 GPIO.cleanup()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义