怎么用树莓派实现数据的上传

作者:zxc5230 | 更新时间:2017-06-19 | 浏览量:2620

在贝壳物联的通讯协议上有发送实时数据,有没有大神发一段代码的,本人树莓派小白,想实时上传树莓派CPU的数据


评论:共10条

贝壳物联 评论于:2017-06-19 15:01:00
网站底部的代码连接里有。
zxc5230 回复于:2017-06-19 16:32:41
回复 @贝壳物联:我没找到呀,只看到有ESP8266的数据上传并没有找到树莓派的
zxc5230 评论于:2017-06-19 17:07:43
这个是我在代码里面根据那LED程序改的,有人能帮我下不
#!/usr/bin/python3
import socket
import time
import json

#must be modified===
DEVICEID='2508'
APIKEY='18a424b7b'
#modify end=========
host="www.bigiot.net"
port=8181

#connect bigiot
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(0)
while True:
try:
s.connect((host,port))
break
except:
print('waiting for connect bigiot.net...')
time.sleep(2)

#check in bigiot
checkinBytes=bytes('{\"M\":\"checkin\",\"ID\":\"'+DEVICEID+'\",\"K\":\"'+APIKEY+'\"}\n',encoding='utf8')
s.sendall(checkinBytes)

#keep online with bigiot function
data=b''
flag=1
t=time.time()
def keepOnline(t):
if time.time()-t>40:
s.sendall(b'{\"M\":\"status\"}\n')
print('check status')
return time.time()
else:
return t

#say something to other device function
def say(s,id,content):
sayBytes=bytes('{\"M\":\"say\",\"ID\":\"'+id+'\",\"C\":\"'+content+'\"}\n',encoding='utf8')
s.sendall(sayBytes)

#deal with message coming in
def process(msg,s,checkinBytes):
msg=json.loads(msg)
if msg['M'] == 'connected':
s.sendall(checkinBytes)
if msg['M'] == 'login':
say(s,msg['ID'],'Welcome! Your public ID is '+msg['ID'])
if msg['M'] == 'say':
say(s,msg['ID'],'You have send to me:{'+msg['C']+'}')

#for key in msg:
#print(key,msg[key])
#print('msg',type(msg))


#main while
while True:
try:
d=s.recv(1)
flag=True

except:
flag=False
time.sleep(1)
t = keepOnline(t)
if flag:
if d!=b'\n':
data+=d
else:
#get cpu temp
file = open("/sys/class/thermal/thermal_zone0/temp")
temp = float(file.read())/1000
file.close
sendmessage={"M":"update","ID":"2508","V":{"2383":"temp"}}
s.sendall(b'{\"sendmessage\"}\n')
data=b''
贝壳物联 回复于:2017-06-19 17:25:12
回复 @zxc5230:就是根据这个示例改一下。
zxc5230 回复于:2017-06-20 12:31:16
回复 @贝壳物联:能加下你们的q群不
贝壳物联 回复于:2017-06-21 13:50:23
回复 @zxc5230:可以,尽管加
zxc5230 评论于:2017-06-20 09:16:49
我的程序运行后可以在网页上看到树莓派上线但是在数据接口里面看却没有看到数据的上传
qiuqiudong 回复于:2017-08-02 23:12:04
回复 @zxc5230:你还有别的树莓派的代码吗? 比如说上传温度数据 、GPIO接继电器啥的
little_elephant 评论于:2018-04-13 16:40:37
格式不对,具体参考sayBytes=bytes('{\"M\":\"say\",\"ID\":\"'+id+'\",\"C\":\"'+content+'\"}\n',encoding='utf8')
SEU_XXL 评论于:2020-07-08 22:55:21
要想上传数据的话,需要使用到下面的格式,其中id、id_value都是变量
bytes('{\"M\":\"update\",\"ID\":\"'+id+'\",\"V\":{\"'+id_value[0]+'\":\"'+id_value[1]+'\"}}\n',encoding='utf8')
返回顶部