start_time = time.time() - t1 water = False Board("uno").begin() while True: a0 = Pin(Pin.A0, Pin.ANALOG).read_analog() a1 = Pin(Pin.A1, Pin.ANALOG).read_analog() if soil < a1: water = True print("超出阈值,当前为:%d" % a1) getcmd = on_topic_read(projectid + '/info') if getcmd: print("收到互动指令:内容为:%s" % getcmd) if getcmd.lower() == 'ip': siot.publish(projectid + '/info', 'ip=' + get_host_ip()) getcmd = on_topic_read(projectid + '/relay') if getcmd: print("收到浇水指令:内容为:%s" % getcmd) if getcmd == '1': water = True if water: Pin(Pin.D2, Pin.OUT).write_digital(1) time.sleep(t2) Pin(Pin.D2, Pin.OUT).write_digital(0) water = not water end_time = time.time() if end_time - start_time > t1: siot.publish(projectid + '/light', a0) time.sleep(0.01)
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) sleep(2) Pin(2, Pin.OUT).write_digital(0)
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: Pin(2, Pin.OUT).write_digital(1) time.sleep(t2) Pin(2, Pin.OUT).write_digital(0) water = not water end_time = time.time() if end_time - start_time > t1: siot.publish(projectid + '/a0', a0) time.sleep(0.01) siot.publish(projectid + '/a1', a1) start_time = time.time() i = i + 1 print("发送数据:%d" % i) # In[ ]:
while True: a0 = Pin(Pin.A0,Pin.ANALOG).read_analog() a1 = Pin(Pin.A1,Pin.ANALOG).read_analog() # print("a0:%d,a1:%d"%(a0,a1)) if soil < a1: water = True print("超出阈值,当前为:%d"%a1) getcmd = on_topic_read(projectid + '/relay') if getcmd: print("收到浇水指令:内容为:%s"%getcmd) if getcmd == '1': water = True if water: Pin(Pin.D2,Pin.OUT).write_digital(1) time.sleep(t2); Pin(Pin.D2,Pin.OUT).write_digital(0) water = not water end_time = time.time() if end_time - start_time > t1: siot.publish(projectid + '/light',a0) time.sleep(0.01); siot.publish(projectid + '/soil',a1) time.sleep(0.01); d4 = [DHT11(Pin(Pin.D4)).temp_c(),DHT11(Pin(Pin.D4)).humidity()] siot.publish(projectid + '/air',str(d4)) start_time = time.time() i = i + 1 print("发送数据:%d"%i)
def on_button_pressed(self, widget): global i i = i + 1 siot.publish(project_list[id] + '/relay', '1') self.lbl_9.set_text('成功发送浇水指令!次数:' + str(i))
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)) time.sleep(1) #等待1秒钟继续
water = False Board("xugu").begin() while True: a0 = Pin(Pin.A0, Pin.ANALOG).read_analog() a1 = Pin(Pin.A1, Pin.ANALOG).read_analog() # print("a0:%d,a1:%d"%(a0,a1)) if soil < a1: water = True print("超出阈值,当前为:%d" % a1) getcmd = on_topic_read(projectid + '/relay') if getcmd: print("收到浇水指令:内容为:%s" % getcmd) if getcmd == '1': water = True if water: Pin(Pin.D2, Pin.OUT).write_digital(1) time.sleep(t2) Pin(Pin.D2, Pin.OUT).write_digital(0) water = not water end_time = time.time() if end_time - start_time > t1: siot.publish(projectid + '/light', a0) time.sleep(0.01) siot.publish(projectid + '/soil', a1) start_time = time.time() i = i + 1 print("发送数据:%d" % i)
''' # file pub_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 will send the message by seconds and 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 = '******' #密码 siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) def sub_cb(client, userdata, msg): print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload)) siot.connect() siot.set_callback(sub_cb) siot.getsubscribe(IOT_pubTopic) siot.loop() tick = 0 try: while True:
import siot import time from xugu import Pin # 从 xugu 库中导入 Pin类 p = Pin("A0", Pin.ANALOG) # 初始化 A0 引脚,设置为输入模式 SERVER = "192.168.0.101" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'DIY/TEST01' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) def sub_cb(client, userdata, msg): print("\nTopic:" + str(msg.topic) + " Message:" + str(msg.payload)) siot.connect() siot.set_callback(sub_cb) siot.getsubscribe(IOT_pubTopic) siot.loop() while True: TDS = p.read_analog() #读取 A0 引脚的模拟量 siot.publish(IOT_pubTopic, "%d" % TDS) time.sleep(1)
import siot #导入siot库 import time #导入time库 from xugu import Pin # 从 xugu 库中导入 Pin类 p = Pin("A0", Pin.ANALOG) # 初始化 A0 引脚,设置为输入模式 SERVER = "192.168.0.101" #MQTT服务器IP CLIENT_ID = "" #在SIoT上,CLIENT_ID可以留空 IOT_pubTopic = 'DIY/TEST01' #“topic”为“项目名称/设备名称” IOT_UserName = '******' #用户名 IOT_PassWord = '******' #密码 siot.init(CLIENT_ID, SERVER, user=IOT_UserName, password=IOT_PassWord) #将上述配置信息作为siot客户端参数 siot.connect() #连接siot服务器 while True: TDS = p.read_analog() #读取 A0 引脚的模拟量并赋值给变量TDS siot.publish(IOT_pubTopic, "%d" % TDS) #发布检测到的TDS值 time.sleep(1) #等待1秒
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) sleep(2) Pin(2, Pin.OUT).write_digital(0)