コード例 #1
0
ファイル: pub_sub.py プロジェクト: sevenwang/SIoT
'''
# 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:
コード例 #2
0
    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)
            sleep(2)
コード例 #3
0
ファイル: 10_vvboard_TDS.py プロジェクト: snowcity007/SIoT
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)