示例#1
0
    def __init__(self, config, callback=None):
        self.config = config
        self.server = config["mqtt_server"]
        self.port = config["mqtt_port"]
        self.clientid = config["mqtt_clientid"]
        self.userid = config["userid"]
        self.password = config["password"]
        self.topicprefix = config["topicprefix"]
        self.receiver_callback = None
        self.client = None

        self.client = MQTTClient(self.clientid, server=self.server, port=self.port,
            user=self.userid, password=self.password)
        if callback == None :
            #set to default callback
            self.client.set_callback(self.received_cb)
        else:
            self.client.set_callback(callback)
        self.client.connect()

        topic = self.topicprefix  + 'cmnd/+/+'
        #topic = self.topicprefix  + 'cmnd/#'
        topic = topic.encode()
        self.client.subscribe(topic)
        print ("mqtt setup listening to ", topic)
示例#2
0
def publish(client='umqtt_client', broker='127.0.0.1', topic='/mytopic/foo', message='', username=None, password=None):
    c = MQTTClient(client, broker, user=username, password=password)
    try:
        c.connect()
    except OSError as e:
        print('unable to connect to mqtt {} with error {}'.format(broker, e))
    else:
        # has to be bytes
        c.publish(str(topic).encode(), str(message).encode())
        c.disconnect()
def main(server=SERVER):
    c = MQTTClient(CLIENT_ID, server)
    c.connect()
    print("Connected to %s, waiting for button presses" % server)
    while True:
        while True:
            if button.value() == 0:
                break
            time.sleep_ms(20)
        print("Button pressed")
        c.publish(TOPIC, b"toggle")
        time.sleep_ms(200)

    c.disconnect()
示例#4
0
文件: main.py 项目: snenyl/Pycom
def mainMQTT(server="broker.hivemq.com"):
    print("Connecting to MQTT server")
    c = MQTTClient("umqtt_client", server)
    c.connect()
    for x in range(0, len(A)):
        print("Publishing")
        # PublishThis_ba = bytearray(struct.pack("b", A[x][0])) + bytearray(A[x][1]) + bytearray(A[x][2]) + bytearray(A[x][3])
        PublishThis_ba = bytearray(struct.pack("b", A[x][0])) + bytearray(
            struct.pack("f", A[x][1])) + bytearray(struct.pack(
                "f", A[x][2])) + bytearray(struct.pack("f", A[x][3]))
        # PublishThis_ba = T[x]
        c.publish(b"IKT520_LAB1", PublishThis_ba)
        pass

    c.disconnect()
示例#5
0
def main(server=SERVER):
    c = MQTTClient(CLIENT_ID, server)
    # Subscribed messages will be delivered to this callback
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(TOPIC)
    print("Connected to %s, subscribed to %s topic" % (server, TOPIC))

    try:
        while 1:
            # micropython.mem_info()
            c.wait_msg()
    finally:
        c.disconnect()
示例#6
0
class MQTTHandler(TransportHandler):
    def __init__(self, config, callback=None):
        self.config = config
        self.server = config["mqtt_server"]
        self.port = config["mqtt_port"]
        self.clientid = config["mqtt_clientid"]
        self.userid = config["userid"]
        self.password = config["password"]
        self.topicprefix = config["topicprefix"]
        self.receiver_callback = None
        self.client = None

        self.client = MQTTClient(self.clientid, server=self.server, port=self.port,
            user=self.userid, password=self.password)
        if callback == None :
            #set to default callback
            self.client.set_callback(self.received_cb)
        else:
            self.client.set_callback(callback)
        self.client.connect()

        topic = self.topicprefix  + 'cmnd/+/+'
        #topic = self.topicprefix  + 'cmnd/#'
        topic = topic.encode()
        self.client.subscribe(topic)
        print ("mqtt setup listening to ", topic)

    def received_cb(self, topic, msg, retain, dup):
        print((topic, msg, retain, dup))
        param={}
        param["topic"]=topic
        param["msg"]=msg
        param["retain"]=retain
        param["dup"]=dup
    def publish(self, devicename, action, msg):

        pub_topic= self.topicprefix + "stat/" + devicename + "/" + action
        msg=json.dumps(msg)
        msg_enc=msg.encode()
        pub_topic=pub_topic.encode()
        print ("pub topic ", pub_topic, msg_enc)
        self.client.publish(pub_topic, msg_enc)
    def start(self):
        blocking_method=True
        while True:
            if blocking_method:
                # Blocking wait for message
                self.client.wait_msg()
            else:
                # Non-blocking wait for message
                self.client.check_msg()
                # Then need to sleep to avoid 100% CPU usage (in a real
                # app other useful actions would be performed instead)
                time.sleep(1)
if __name__ == '__main__':

    wlan = wifimgr.get_connection()

    if wlan is None:
        print("Could not initialize the network connection.")
        while True:
            pass  # you shall not pass :D

    print("Starting Server")

    server = 'test.mosquitto.org'
    client_id = 'composteira1'
    topic = 'ecomposteira/composter/measurements'
    topic = topic.encode()

    client = MQTTClient(client_id, server, port=1883)
    client.connect()

    for data in data_gen_loop(sleeptime=60):

        data['macAddress'] = ubinascii.hexlify(wlan.config('mac'),
                                               ':').decode()
        data = json.dumps(data)
        print("Publicando %s" % data)

        client.publish(topic, data)

    client.disconnect()
示例#8
0
def main(server="localhost", blocking_method=False):
    c = MQTTClient("umqtt_client", server)
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(b"foo_topic")
    while True:
        if blocking_method:
            # Blocking wait for message
            c.wait_msg()
        else:
            # Non-blocking wait for message
            c.check_msg()
            # Then need to sleep to avoid 100% CPU usage (in a real
            # app other useful actions would be performed instead)
            time.sleep(1)

    c.disconnect()
示例#9
0
def main(server="localhost"):
    c = MQTTClient("umqtt_client", server)
    c.connect()
    c.publish(b"foo_topic", b"hello")
    c.disconnect()
示例#10
0
wait_ms = 5000 #Sec between photos

#MQTT Configurations
mqtt_server = '192.168.*.*'
mqtt_client_id = 'esp32-camera'
mqtt_topic = b'Camera/Images'
mqtt_user = b'***'
mqtt_pass = b'***'

try:
    # camera init
    led = machine.Pin(4, machine.Pin.OUT) #Pin for LED

    camera.init(0, format=camera.JPEG)  # ESP32-CAM
    
    c = MQTTClient(mqtt_client_id, mqtt_server, user = mqtt_user, password = mqtt_pass)
    c.connect()

    # ntp sync for date
    ntptime.settime()
    rtc = machine.RTC()

except Exception as e:
    print("Error ocurred: " + str(e))
    time.sleep_ms(5000)
    machine.reset()

#error_counter = 0
while True:
    try:
        # prepare for photo