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) 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:
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)
''' # 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 #导入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秒