mqtt2.py 5.54 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)
Spencer Du's avatar
Spencer Du committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

        if message.topic == "microscope/light_sheet_microscope/UI/laser/445nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.on()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/445nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.off()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/488nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
37 38
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
Spencer Du's avatar
Spencer Du committed
39
            led0.on()
40

Spencer Du's avatar
Spencer Du committed
41 42
        if message.topic == "microscope/light_sheet_microscope/UI/laser/488nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
43 44
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
Spencer Du's avatar
Spencer Du committed
45
            led0.off()
46

Spencer Du's avatar
Spencer Du committed
47 48
        if message.topic == "microscope/light_sheet_microscope/UI/laser/515nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
49 50
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
Spencer Du's avatar
Spencer Du committed
51
            led0.on()
Spencer Du's avatar
Spencer Du committed
52

Spencer Du's avatar
Spencer Du committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        if message.topic == "microscope/light_sheet_microscope/UI/laser/515nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.off()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/561nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.on()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/561nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.off()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/594nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.on()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/594nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.off()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/638nm" and m_in["payload"]["cmd"] == "turning on laser":
            print("Laser LED turning on")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.on()

        if message.topic == "microscope/light_sheet_microscope/UI/laser/638nm" and m_in["payload"]["cmd"] == "turning off laser":
            print("Laser LED turning off")
            base = BaseOverlay("base.bit")
            led0 = base.leds[0]
            led0.off()              
Spencer Du's avatar
Spencer Du committed
94 95

        if message.topic == "microscope/light_sheet_microscope/UI/laser/445nm" and m_in["payload"]["cmd"] == "set intensity of laser":
Spencer Du's avatar
Spencer Du committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            print(m_in["payload"]["voltage"])
            dac = Pmod_DAC(base.PMODA)
            dac.write(m_in["payload"]["voltage"])

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

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

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

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

        if message.topic == "microscope/light_sheet_microscope/UI/laser/638nm" and m_in["payload"]["cmd"] == "set intensity of laser":
            print(m_in["payload"]["voltage"])
Spencer Du's avatar
Spencer Du committed
122
            dac = Pmod_DAC(base.PMODA)
Spencer Du's avatar
Spencer Du committed
123
            dac.write(m_in["payload"]["voltage"])
Spencer Du's avatar
Spencer Du committed
124

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