由于micropython for esp8266板子没有串口,无奈之下想到用pc机通过usb转ttl模块直接读取数值并上传到onenet平台。
效果图:
接线图:
pm2.5模块 usb转tll模块
vcc 5v
gnd gnd
Tx Rx
源码:
import serial
import time
def get_pm(
com)
:
ser
= serial.Serial(
port=com,
baudrate=2400)
time.sleep(
0.1)
data
=ser.read_all()
ser.close()
data_all
=data.hex()
R
= data_all.find(
'aa')
if R
>-1 and len(data_all)
>(R
+6)
:
P
="0x"+data_all[R
+2:R
+4]
L
="0x"+data_all[R
+4:R
+6]
P_10
=int(
str(P),
16)
L_10
=int(
str(L),
16)
SHI
=P_10
*256+L_10
SHUCHU
=SHI
/(
1024/5)
*800
return int(SHUCHU)
from urllib
import request,parse
import json
DEVICE_ID
='xxxxx'
API_KEY
='xxxxxxxxx'
def Http_Pot_OnetNet(
data)
:
url
='http://api.heclouds.com/devices/'+DEVICE_ID
+'/datapoints'
values
={
'datastreams':[{
"id":"pm2.5",
"datapoints":[{
"value":data}]}]}
headers
={
"api-key":API_KEY}
d
= json.dumps(values).encode(
"utf-8")
r
= request.Request(url,d,
headers=headers)
res
= request.urlopen(r)
print(json.loads(res.read())[
"error"])
while 1:
Http_Pot_OnetNet(get_pm(
'com5'))
time.sleep(
60)
此内容由EEWORLD论坛网友youxinweizhi原创,如需转载或用于商业用途需征得作者同意并注明出处
本文来自论坛,点击查看完整帖子内容。