× Home Weekly activities My Project Agreement About me

Eric NDAYISHIMIYE

Week 14: Interface and Application Programming

Group assignment

  1. * Compare as many tool options as possible

  • The group assignment page is here on FabLab Rwanda web page

  • Individual assignment:
    Write an application that interfaces a user with an input &/or output device that you made

    This week, I choose to use my microcontroller board, which I designed in Week 9's Output_Devices, for creating an interface application to turn LED on and off over serial communication on ESP32 using Tkinter in Python.

    My objective is to establish a connection between the ESP32 and the created Tkinter Graphical User interfaces (Tk GUI) by programming it through Python. With the built-in circuit, communication between the ESP32 and LED is possible. Therefore, I will utilize the application's button to control the LED and toggle its state.

    What is Serial Communication ?

    Serial communication is a method of transmitting data between two devices, typically a computer and a peripheral device, using a serial communication protocol. In serial communication, data is sent one bit at a time over a single communication line, which can be a physical wire or a wireless connection.

    What is tkinter ?

    Tkinter is a built-in Python module for creating graphical user interfaces (GUIs). It provides a set of tools and widgets that can be used to create windows, buttons, text boxes, menus, and other graphical elements that users can interact with. Tkinter is based on the Tk toolkit, which is a popular GUI toolkit for the Tcl programming language.

    Installation of Python

    Python is a high-level, interpreted programming language that is used for a wide range of applications, including web development, data analysis, artificial intelligence, and scientific computing. It was first released in 1991 by Guido van Rossum, and it has since become one of the most popular programming languages in use today.

    1. 1. Go to the official Python website's Windows downloads page and download one of the Stable Releases of Python.
    2. 2. Once the download is complete, double click the installer to launch it.
    3. 3. When you are ready to being the installation you can click Install Now.

    PIP (Python Package Installer)

    This is is the default package manager for Python packages. It is used to install and manage Python packages, also known as modules, that are published on the Python Package Index (PyPI).

    The communication between PC and esp32 via serial communication

    Serial communication is a method of exchanging data between two devices over a serial communication link. In the case of communication between a PC and an ESP32, the serial communication link is typically established using a USB-to-serial converter that is built into the ESP32 development board.

    Serial Communication between a PC and an ESP32 is a simple and effective way to exchange data between the two devices. It can be used for a wide range of applications, such as controlling the ESP32 from a PC, or sending sensor data from the ESP32 to the PC for analysis.

    Installing Tkinter

    Creating python app (app.py)

    Under here is the code I used to provide my interface to control the LED by turning it ON and/or OFF.

                
                   import serial
    import tkinter as tk
    
    serialPort = 'COM7' # change this to match your Arduino serial port
    serialBaud = 9600
    
    def sendChar(char):
        ser = serial.Serial(serialPort,  serialBaud)
        ser.write(char.encode())
        ser.close()
    
    root = tk.Tk()
    root.title("CONTROL LED WITH TKINTER UI AND ESP32")
    
    buttonOn = tk.Button(root, text="TURN ON LED", command=lambda: sendChar('H'))
    buttonOn.pack()
    
    buttonOff = tk.Button(root, text="TURN OFF  LED", command=lambda: sendChar('L'))
    buttonOff.pack()
    root.mainloop()
                
             

    Interface visualization, also known as graphical user interface (GUI) visualization

    is the process of creating a visual representation of an interface that allows users to interact with a software application. GUIs use graphical elements such as icons, buttons, and menus to represent different functions and options within the application, making it easier for users to navigate and interact with the software.

    Below here is the used Arduino code

                
                   const int LED = 2;
    int incomingByte;      // a variable to read incoming serial data into
    
    void setup() {
      // initialize serial communication:
      Serial.begin(9600);
      // initialize the LED pin as an output:
      pinMode(LED , OUTPUT);
    }
    
    void loop() {
      // see if there's incoming serial data:
      if (Serial.available() > 0) {
        // read the oldest byte in the serial buffer:
        incomingByte = Serial.read();
        // if it's a capital H (ASCII 72), turn on the LED:
        if (incomingByte == 'H') {
           digitalWrite(LED, HIGH);
           Serial.println(" H received");
        }
        // if it's an L (ASCII 76) turn off the LED:
        if (incomingByte == 'L') {
            digitalWrite(LED, LOW);
            Serial.println(" L received");
        }
      }
    }
                
             

    Running the application

    below is the hero shot

    • Used file can be found here

    © 2023 | Eric NDAYISHIMIYE | All Rights Reserved