[TH] PyTk

บทความนี้กล่าวถึง pytk อันเป็นไลบรารีเชื่อมประสานกับผู้ใช้แบบกราฟิกส์ หรือ GUI (Graphics User Interface) ของภาษาไพธอนที่เรียกใช้ tkinter (TK Interface) ทำให้สามารถเขียนโปรแกรม GUI ที่ทำงานได้ทั้งระบบปฏิบัติการ Windows, Linux และ macOS โดยการทำงานของ tkinter ของไพธอนเป็นการเชื่อมประสานกับไลบรารี Tcl/Tk อีกชั้นหนึ่ง และบทความนี้ใช้ Thonny เป็น IDE ในการเขียนโปรแกรมตัวอย่าและติดตั้งไลบรารี (อ่านบทความการใช้ Thonny)

การใช้งานจะต้องมีไลบรารี pytk ซึ่งสามารถเขียนโค้ดเพื่อทดสอบการติดตั้งไลบรารีด้วยโค้ดโปรแกรมภาษาไพธอนดังต่อไปนี้

try:
    import tkinter as tk
except ImportError:
    import sys
    print("ไม่พบไลบรารี tkinter!!!")
    sys.exit(0)
print("พร้อมสำหรับ tkinter")
Read More

[TH] Thonny 4

Thonny เป็นเครื่องมือประเภทสภาพแวดล้อมแบบบูรณาการ หรือ IDE (Integrated development environment) ที่พัฒนาโดยชาวอสโตเนียชื่อ Aivar Annamaa ในปี ค.ศ. 2015 ภายใต้สิทธิ์การใช้งานแบบเปิดเผยรหัสโปรแกรม (Open Source) แบบ MIT และคุณสมบัติของโปรแกรมนั้นครอบคลุมการเขียนโค้ด ตรวจสอบโค้ด บริหารจัดการไลบรารีของภาษาไพธอน และรันโปรแกรมภาษาไพธอนที่เขียนขึ้นทำให้เป็นซอฟต์แวร์ที่ทำงานในส่วนของการเขียนและทดสอบการทำงานที่ใช้งานง่ายเหมาะสมกับผู้เริ่มต้น

          การติดตั้ง Thonny ทำได้โดยดาวน์โหลดไฟล์ชื่อ thonny-xxl-4.1.1.exe จากเว็บไซต์ thonny.org ดังภาพที่ ก-1

ภาพที่ ก-1 หน้าจอเว็บไซต์ thonny.org

Read More

[EN]Calculate Regression with NumPy.


This article is to convert the implementation of the regression model from the article of ESP32-C3 to NumPy on Raspberry Pi and PC and use the display as Matplotlib, as shown in Figure 1, which can be found from the previous article. The temperature and humidity from the 1-day data were obtained from parameters a and b of the regression equation and the resulting equation or model was used to determine the possible temperature values over the 1 day.

Figure 1 Result of the program
Read More

[EN] Draw an analog clock using MicroPython.

The previous article discussed how to improve the display speed by using the double buffer technique. It is applied for the analog clock display as shown in Figure 1. We used Trigonometric calculations to determine the coordinates (x,y) of the tips of the seconds, minutes and hours. Each second operation uses a timer to make the operation closer to real-time than the Loop or Delay.

Figure 1 Result of drawing Analog clock
Read More

[EN] List the serial ports connected to the RPi with pySerial and PyQt5.

In the previous article, we have read the list of devices connected to the serial port of the Raspberry Pi board or RPi with the pySerial library in text mode as shown in Figure 1. This article combines the previous working principles with the use of a graphical user interface via the PyQt5 library, listed in the combobox for users to choose from. If no serial port connected to the board is found, the RPi disables the combobox from user selection. Therefore, this article discusses the implementation of pySerial with the QLabel and QComboBox libraries. PyQt5.

Read More

[EN] Binary Search Tree data structure programming with Python.

In the previous article, programming to implement queue-based data structures was introduced. In this article, we introduce programming to manage another type of data structure which has different storage and management methods called BST tree or Binary Search Tree, as shown in Figure 1, which is a structure that can be applied to data collection with attributes in which the data in the left branch is less than itself and the right branch is greater than itself or the opposite, i.e. the left branch has a greater value and the right branch is less. It enables searching for data in cases where the tree is balanced on the left and right in the BST structure, saving half the time or number of search times per round of available data, e.g. 100 data sets in the first round if it is not the information you are looking for will be left with a choice to find from the left or right branches which the selection causes the information of the other side is not considered or cut off approximately half. However, if the Binary Search Tree is out of balance, the search speed is not much different from the sequential search.

In this article, we use Python that works on either a Python 3 or MicroPython interpreter to store the data, adding information ,searching for information as an example of further development.

BST : Binary Search Tree
Figure 1 BST
Read More

[EN] List the serial ports connected to the RPi with pySerial.

This article discusses the use of Python language pySerial library on Raspberry Pi or RPi both 3rd and 4th generation to connect to the serial port. It can be done in 2 ways, the first is to use hardware like ET-CONV10/RS232 HAT that has been written in the book and with the use of a USB port connected to a converter to be a serial communication port (USB to Serial Port) as shown in Figure 1. This article uses the second method to run pySerial to check how many serial ports are connected and what are their names as an example in Figure 8.

Figure 1 Connect ET-USB/RS232 Mini to RPi’s USB
Read More

[TH] Calculate Regression with NumPy.

บทความนี้เป็นการแปลงการใช้งานโมเดลรีเกรสชันจากบทความของ ESP32-C3 มาเป็น NumPy บน Raspberry Pi และ PC พร้อมทั้งใช้การแสดงผลเป็น Matplotlib ดังภาพที่ 1 ซึ่งจะพบว่า จากบทความก่อนหน้านี้ได้นำค่าอุณหภูมิและความชื้นจากข้อมูล 1 วันมาหาพารามิเตอร์ a และ b ของสมการถดถอยหรือรีเกรสชัน (regression) และนำสมการหรือโมเดลที่ได้นี้มาหาค่าของอุณหภูมิที่เป็นไปได้ในช่วงเวลา 1 วัน

ภาพที่ 1 ผลลัพธ์ของตัวอย่างโปรแกรม
Read More

[EN] Queue Data Structure

This article introduces the use of the list class in Micropython as a queue data structure with a limited number of members. It works according to the FIFO (First-In-First-Out) principle, which can be applied in a variety of applications, such as being used as a storage, and when the data is full but we need to insert new data, the old data must be pop out. The example in this article uses the dCore-miniML board (Figure 1) to read the temperature of the chip and store it in a Queue structure and display it in a bar graph and Micropython implemented firmware version 1.16 (2021-06-23) for the ESP Module (SPIRAM).

(Figure. 1 An example of drawing a graph with data stored in a queued data structure)
Read More

[EN] PyGlet

pyglet is a Python library to create windows and cross-platform multimedia on Windows (Windows), MacOS (macOS) and Linux (Linux) for developing games or visualization applications. The library itself supports creating windows, integration with users through an event-based system, support OpenGL graphics, image/video loading support, and playing music. This article discusses installing and using pyglet on Raspberry Pi 3 B+ and Raspberry Pi 4 as a test device.

(Figure. 1 Example from 1-6)
Read More