mqtt2.py 2.18 KB
Newer Older
Spencer Du's avatar
Spencer Du committed
1 2
import logging
import paho.mqtt.client as mqtt
Spencer Du's avatar
Spencer Du committed
3
import json
4 5
from pynq.overlays.base import BaseOverlay
from pynq.lib import LED, Switch, Button
6
import time
7
import asyncio
Spencer Du's avatar
Spencer Du committed
8

Spencer Du's avatar
Spencer Du committed
9
class embedded(mqtt.Client):
Spencer Du's avatar
Spencer Du committed
10 11
    def on_connect(self, mqtt, obj, flags, rc):
        pass
Spencer Du's avatar
Spencer Du committed
12

Spencer Du's avatar
Spencer Du committed
13
    def on_message(self, mqtt, userdata, message):
14
        asyncio.set_event_loop(asyncio.new_event_loop())
Spencer Du's avatar
Spencer Du committed
15
        m_decode = str(message.payload.decode("utf-8"))
Spencer Du's avatar
Spencer Du committed
16
        print("\n" + "message recieved= " + m_decode)
Spencer Du's avatar
Spencer Du committed
17 18 19 20
        print("message topic=", message.topic)
        print("message qos=", message.qos)
        print("message retain flag=", message.retain)
        m_in = json.loads(m_decode)
21 22 23 24 25 26
        if message.topic == "microscope/light_sheet_microscope/UI/laser/445nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
27
        
28 29 30 31 32 33
        if message.topic == "microscope/light_sheet_microscope/UI/laser/488nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
34

35 36 37 38 39 40
        if message.topic == "microscope/light_sheet_microscope/UI/laser/515nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
41

42 43 44 45 46 47
        if message.topic == "microscope/light_sheet_microscope/UI/laser/561nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
48

49 50 51 52 53 54
        if message.topic == "microscope/light_sheet_microscope/UI/laser/594nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
55

56 57 58 59 60 61
        if message.topic == "microscope/light_sheet_microscope/UI/laser/638nm":
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            for i in range(20):
                led0.toggle()
                time.sleep(.1)
Spencer Du's avatar
Spencer Du committed
62

Spencer Du's avatar
Spencer Du committed
63
    def run(self):
Spencer Du's avatar
Spencer Du committed
64
        self.connect("broker.hivemq.com", 1883, 60)