def data_handler(data): oled.fill(0) oled.text("listen", 0, 0) oled.text("UDP Port", 0, 10) oled.text(str(UDP_PORT), 0, 20) oled.text(data.decode(), 0, 40) oled.show()
def onMQTTMessage(topic, msg): global newMessage global lastDisplay newMessage = msg print("Topic: %s, Message: %s" % (topic, msg)) oled.fill(0) oled.text('Pi4IoT', 0, 0) if msg == b"on": led.off() oled.text('LED: ON', 0, 20) oled.text(lastDisplay, 0, 30) elif msg == b"off": led.on() oled.text('LED: OFF', 0, 20) oled.text(lastDisplay, 0, 30) else: state = "LED: OFF" if led.value() else "LED: ON" lastDisplay = msg.decode() oled.text(state, 0, 20) oled.text(lastDisplay, 0, 30) oled.show()
def display_ip(): oled.text("IP:", 0, 0) oled.text("{:>3}.{:>3}.".format(*ip.split(".")[:2]), 0, 10) oled.text("{:>3}.{:>3}".format(*ip.split(".")[2:]), 0, 20)
def show_value(value): oled.fill(0) display_ip() oled.text(value, 0, 40) oled.show()
oled.show() def each_loop(client): global newMessage if newMessage in [b'on', b'off']: client.publish(MQTT_CONFIG['TOPIC'] + b'/state', newMessage) newMessage = None elif newMessage is not None: client.publish(MQTT_CONFIG['TOPIC'] + b'/display', newMessage) newMessage = None if mode == HTTP_MODE: from set_led_http import serve show_value("") serve(show_value) elif mode == UDP_MODE: oled.fill(0) oled.text("listen", 0, 0) oled.text("UDP Port", 0, 10) oled.text(str(UDP_PORT), 0, 20) oled.show() from udp_client import listen listen(data_handler, port=UDP_PORT) elif mode == MQTT_MODE: from mqtt import listen listen(onMQTTMessage, each_loop, MQTT_CONFIG)