mqtt2.py 2.71 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
Spencer Du's avatar
Spencer Du committed
6
from pynq.lib import Pmod_ADC, Pmod_DAC
7
import time
8
import asyncio
Spencer Du's avatar
Spencer Du committed
9

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

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

38
        if message.topic == "microscope/light_sheet_microscope/UI/laser/515nm":
Spencer Du's avatar
Spencer Du committed
39 40 41 42 43 44
        	print("LED 0 flashing")
        	base = BaseOverlay("base.bit")
        	led0 = base.leds[0]
        	for i in range(20):
        		led0.toggle()
        		time.sleep(.1)
45

46 47 48 49 50 51
        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)
52

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

61
        if message.topic == "microscope/light_sheet_microscope/UI/laser/638nm":
Spencer Du's avatar
Spencer Du committed
62
            print("LED 0 flashing")
63 64 65 66 67
            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
68

Spencer Du's avatar
Spencer Du committed
69 70 71 72 73 74 75 76
        if message.topic == "microscope/light_sheet_microscope/UI/laser" and m_in["payload"]["cmd"] == "device turning off":
        	print("Laser turning off")
        	client.loop_stop()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/445nm" and m_in["payload"]["cmd"] == "set intensity of laser":
            dac = Pmod_DAC(base.PMODA)
            dac.write(0.6)

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