global topic_msg_map topic_msg_map[str(msg.topic)] = str(msg.payload.decode()) def on_topic_read(topic): global topic_msg_map result = topic_msg_map.get(topic, None) del site[topic] return result siot.init('', '127.0.0.1', user='******', password='******') siot.connect() _siot_connected = True if _siot_connected: siot.subscribe('af/d2', on_topic_subscribe) siot.loop() while True: a0 = Pin("A0", Pin.ANALOG).read_analog() siot.publish('af/a0', a0) sleep(0.01) a1 = Pin("A1", Pin.ANALOG).read_analog() siot.publish('af/a1', a1) sleep(5) if '1' == on_topic_read('af/d2'): Pin(2, Pin.OUT).write_digital(1) sleep(5) else: Pin(2, Pin.OUT).write_digital(0) if 380 > a1: Pin(2, Pin.OUT).write_digital(1)
def on_topic_subscribe(client, userdata, msg): global topic_msg_map topic_msg_map[str(msg.topic)] = str(msg.payload.decode()) def on_topic_read(topic): global topic_msg_map result = topic_msg_map.get(topic, None) if result: del topic_msg_map[topic] return str(result) siot.init('', iot_server, user=iot_user, password=iot_pwd) siot.connect() siot.subscribe(projectid + '/d2', on_topic_subscribe) siot.loop() start_time = time.time() - t1 water = False while True: a0 = Pin("A0", Pin.ANALOG).read_analog() a1 = Pin("A1", Pin.ANALOG).read_analog() if soil < a1: water = True print("超出阈值,当前为:%d" % a1) getcmd = on_topic_read(projectid + '/d2') if getcmd: print("收到指令:内容为:%s" % getcmd) if getcmd == '1': water = True if water:
def all_subscribe(): #所有的订阅相关集合 siot.subscribe(projectid + '/relay', on_topic_subscribe) siot.subscribe(projectid + '/info', on_topic_subscribe)
del topic_msg_map[topic] return str(result) data = 'http://' + get_host_ip() + ':8001' img = qrcode.make(data) out = io.BytesIO() img.save(out, 'PNG') base64_data = base64.b64encode(out.getvalue()) s = base64_data.decode() data = 'data:image/jpeg;base64,%s' % s siot.init('remi', iot_server, user=iot_user, password=iot_pwd) siot.connect() for item in project_list: siot.subscribe(item + '/light', on_topic_subscribe) siot.subscribe(item + '/soil', on_topic_subscribe) siot.loop() # Web主程序 class MyApp(App): global i, project_list, id def __init__(self, *args): super(MyApp, self).__init__(*args) def main(self): container = gui.VBox(width=400, height=340, style={'margin': '0px auto'})
# 读取后删除 def topic_read(topic): global msg_map result=msg_map.get(topic,None) if result: re = result[0] del msg_map[topic] return re return result siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) siot.connect() # 订阅消息 for item in project_dict.keys(): siot.subscribe(item +"/"+ topic_list[0], on_subscribe) siot.loop() # 通过循环读出数值,和阈值比较后判断是否发送控制指令 temp_id = '' while True: for item in project_dict.keys(): temp_id = item +'/'+ topic_list[0] val = topic_read(temp_id) if val: default_val = project_dict.get(item,None) if int(val) > int(default_val): # 发送控制指令 siot.publish(item +'/'+ topic_list[1],"1") print(('项目:%s的监测数据为%s,超过预设值%s,已经发送控制指令!')%(item,val,default_val))
# 绘图 import matplotlib.pyplot as plt fig, ax = plt.subplots() def draw(): global x, y, flag, w plt.axis([0, 100, 0, 1]) plt.ion() x.append(len(y)) if len(x) > w: x.pop(0) y.pop(0) ax.cla() plt.plot(x, y, 'r--') plt.pause(0.0001) plt.show() flag = False siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) siot.connect() siot.subscribe(topic_id, on_subscribe) siot.loop() while True: if flag: draw() time.sleep(1)
''' import siot import time SERVER = "127.0.0.1" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'xzr/001' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 def sub_cb(client, userdata, msg): print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload)) # msg.payload中是消息的内容,类型是bytes,需要用解码。 s = msg.payload.decode() print(s) siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) siot.connect() siot.subscribe(IOT_pubTopic, sub_cb) siot.loop() try: while True: pass except: siot.stop() print("disconnect seccused")
global msg_map t = time.asctime(time.localtime(time.time())) print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload) + " Time:" + t) msg_map[str(msg.topic)] = [str(msg.payload.decode()), time.time()] # 读取后不删除 def topic_read(topic): global msg_map result = msg_map.get(topic, None) if result: re = result[0] return re def topic_read_time(topic): global msg_map result = msg_map.get(topic, None) if result: re = time.asctime(time.localtime(result[1])) return re siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) siot.connect() for item1 in project_list: for item2 in topic_list: siot.subscribe(item1 + '/' + item2, on_subscribe) siot.loop()
# 处理订阅消息 def on_topic_subscribe(client,userdata,msg): global topic_msg_map topic_msg_map[str(msg.topic)]=str(msg.payload.decode()) def on_topic_read(topic): global topic_msg_map result=topic_msg_map.get(topic,None) if result: del topic_msg_map[topic] return str(result) siot.init('',iot_server,user=iot_user,password=iot_pwd) siot.connect() siot.subscribe(projectid + '/light',on_topic_subscribe) siot.subscribe(projectid + '/soil',on_topic_subscribe) siot.loop() # Web主程序 class MyApp(App): def __init__(self, *args): super(MyApp, self).__init__(*args) def main(self): container = gui.VBox(width=400, height=240,style={'margin':'0px auto'}) self.lbl_01 = gui.Label('开源智慧农场监控页面',style={'font-size': '25px'}) self.lbl_02 = gui.Label('(设备编号:' + projectid + ')',style={'font-size': '10px'}) self.lbl_1 = gui.Label('环境光照值:等待数据中') self.lbl_2 = gui.Label('土壤湿度值:等待数据中') self.lbl_9 = gui.Label(' ')
''' # file sub.py # brief Set 'SERVER','CLIENT_ID'(this can be null),'IOT_pubTopic','IOT_UserName','IOT_PassWord' # download into pc or raspberryPi and run the file # You receive the message from server # Copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com) # licence The MIT License (MIT) # author [LuoYufeng]([email protected]) # version V1.0 # date 2019-10-8 ''' import siot import time SERVER = "127.0.0.1" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'xzr/001' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 def sub_cb(client, userdata, msg): print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload)) siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) siot.connect() siot.subscribe(IOT_pubTopic, sub_cb) siot.loop() try: while True: pass except:
def all_subscribe(): #所有的订阅相关集合 siot.subscribe(projectid + '/d2', on_topic_subscribe)
topic_msg_map[str(msg.topic)] = str(msg.payload.decode()) def on_topic_read(topic): global topic_msg_map result = topic_msg_map.get(topic, None) if result: del topic_msg_map[topic] return result siot.init('', '127.0.0.1', user='******', password='******') siot.connect() _siot_connected = True if _siot_connected: siot.subscribe('sf101/d2', on_topic_subscribe) siot.loop() while True: a0 = Pin("A0", Pin.ANALOG).read_analog() siot.publish('sf101/a0', a0) sleep(0.01) a1 = Pin("A1", Pin.ANALOG).read_analog() siot.publish('sf101/a1', a1) sleep(5) if '1' == on_topic_read('sf/d2'): Pin(2, Pin.OUT).write_digital(1) sleep(5) else: Pin(2, Pin.OUT).write_digital(0) if 380 < a1: Pin(2, Pin.OUT).write_digital(1)