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()
font = ImageFont.truetype('/home/pi/workspace/Oled/fonts/Font.ttc', 16) def handler(signum, frame): print(f'Signal handler called with signal {signum}') oled.teardown() exit(0) if __name__ == "__main__": signal.signal(signal.SIGTERM, handler) signal.signal(signal.SIGINT, handler) oled = oled.Oled() ip = get_host_ip() oled.setup() oled.clear() while True: image = Image.new('1', (config.WIDTH, config.HEIGHT), "WHITE") draw = ImageDraw.Draw(image) draw.line([(0, 0), (127, 0)], fill=0) draw.line([(0, 0), (0, 31)], fill=0) draw.line([(0, 31), (127, 31)], fill=0) draw.line([(127, 0), (127, 31)], fill=0) draw.text((4, 7), ip, font=font, fill=0) oled.show(image) oled.delay(10000) ip = get_host_ip()
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)