Example #1
0
        global is_close_unknown  #如果是未知原因关闭的话 订阅会丢失 重新订阅即可
        if (is_close_unknown):
            all_subscribe()
            is_close_unknown = False
            print("非正常关闭,所以需要重新初始化订阅相关代码")


def on_disconnect(client, userdata, rc):  #siot关闭连接时回调
    rc = int(rc)
    if (rc != 0):  #0是正常关闭 非0即表示非正常关闭
        print("说明连接已经关闭了....")
        global is_close_unknown
        is_close_unknown = True


siot.init(projectid, iot_server, user=iot_user, password=iot_pwd)
siot.client.on_connect = on_connect
siot.client.on_disconnect = on_disconnect
siot.connect()
all_subscribe()
siot.loop()
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
Example #2
0
topic_msg_map = {}


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)

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':
    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]
        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:
Example #5
0
'''
# 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:
Example #6
0
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)

Example #7
0
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秒