Created
November 28, 2023 19:11
-
-
Save Lokno/cbc67226ab5cb729a01434d8e2d92909 to your computer and use it in GitHub Desktop.
Combines the Telemetrix-AIO library with tk interface to blink an LED on pin 2 of a microcontroller at a specified ip address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/MrYsLab/telemetrix-esp32 | |
import asyncio | |
import tkinter as tk | |
from telemetrix_aio_esp32 import telemetrix_aio_esp32 | |
DIGITAL_PIN = 2 | |
class App: | |
def __init__(self): | |
self.root = tk.Tk() | |
self.root.title("TelemetrixAioEsp32 Blink Example") | |
self.board = None | |
self.ip_address_frame = tk.Frame(self.root) | |
self.ip_address_frame.pack() | |
self.ip_address_label = tk.Label(self.ip_address_frame, text="IP Address:") | |
self.ip_address_label.pack(side="left") | |
self.ip_address_entry = tk.Entry(self.ip_address_frame) | |
self.ip_address_entry.pack(side="left", padx=5) | |
self.ip_address_entry.insert("end",'') | |
self.button_frame = tk.Frame(self.root) | |
self.button_frame.pack() | |
self.connect_button = tk.Button(self.button_frame, text="Connect", command=self.start_connect) | |
self.connect_button.pack(side="left", padx=5) | |
self.start_button = tk.Button(self.button_frame, text="On", command=self.start_blink) | |
self.start_button.pack(side="left") | |
self.start_button['state'] = tk.DISABLED | |
self.request_blink = False | |
self.request_connect = False | |
self.event_loop = asyncio.get_event_loop() | |
asyncio.set_event_loop(self.event_loop) | |
self.showing = True | |
self.root.protocol("WM_DELETE_WINDOW", self.on_closing) | |
self.led_on = False | |
async def connect(self): | |
if self.board is None: | |
ip_address = self.ip_address_entry.get() | |
self.board = telemetrix_aio_esp32.TelemetrixAioEsp32(transport_address=ip_address, loop=self.event_loop, autostart=False) | |
await self.board.start_aio() | |
await self.board.set_pin_mode_digital_output(DIGITAL_PIN) | |
await self.board.digital_write(DIGITAL_PIN, 0) | |
self.led_on = False | |
self.start_button["text"] = "On" | |
self.connect_button["text"] = "Disconnect" | |
self.start_button['state'] = tk.NORMAL | |
else: | |
await self.board.shutdown() | |
self.board = None | |
if self.showing: | |
self.connect_button["text"] = "Connect" | |
self.start_button['state'] = tk.DISABLED | |
if self.showing: | |
self.connect_button['state'] = tk.NORMAL | |
async def show(self): | |
while self.showing: | |
self.root.update() | |
if self.request_blink: | |
self.request_blink = False | |
if self.led_on: | |
await self.board.digital_write(DIGITAL_PIN, 0) | |
await asyncio.sleep(0.001) | |
self.start_button["text"] = "On" | |
self.led_on = False | |
else: | |
await self.board.digital_write(DIGITAL_PIN, 1) | |
await asyncio.sleep(0.001) | |
self.start_button["text"] = "Off" | |
self.led_on = True | |
self.start_button['state'] = tk.NORMAL | |
if self.request_connect: | |
self.request_connect = False | |
await self.connect() | |
if self.board is not None: | |
await self.connect() | |
def start_blink(self): | |
self.request_blink = True | |
self.start_button['state'] = tk.DISABLED | |
def start_connect(self): | |
self.request_connect = True | |
self.connect_button['state'] = tk.DISABLED | |
def run(self): | |
self.event_loop.run_until_complete(self.show()) | |
def on_closing(self): | |
self.showing = False | |
self.root.destroy() | |
if __name__ == "__main__": | |
app = App() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment