Programming the ESP32

After the setup of the system, the programming of the ESP32 is initiated in the Arduino IDE. The previously researched libraries are used to control the sensors. Initially, a separate functional program is written for each sensor to capture the data, and eventually, all the programs are combined to read all the sensors simultaneously. The program is then expanded to make the status LEDs light up in different colors based on specific ranges of the room climate values. The three colors chosen are red, yellow, and green, equivalent to the warning light. When the values are within acceptable range, the LEDs and the warning light will stay lit in green. If a value is exceeded, the LEDs of the Climate Cube or the warning light will blink in yellow, or in the case of a critical threshold being crossed, they will blink in red. However, the control of the warning light is done through the 4-relay module via the Raspberry Pi, so the information from the ESP32 needs to be transmitted to the Raspberry Pi. The abnormal condition can be acknowledged by pressing the illuminated push button, causing the signaling devices to stop blinking and remain lit continuously. After acknowledgment, appropriate measures should be taken to normalize the room climate, causing the signaling lights to stay lit in green again. Finally, the required program to transmit the data to the Raspberry Pi is added.

During the programming process, the ISO 25010 standard is followed to ensure the quality and efficiency of the program. This involves creating separate methods for subtasks such as controlling the LEDs or reading the sensor data. This approach keeps the main method (loop), which is ultimately executed in a continuous loop, organized. As a result, the program code is generally more readable and understandable. Additionally, individual program parts can be easily reused without redundancy. Furthermore, this approach greatly simplifies the process of troubleshooting, as the code is defined only in one place instead of being scattered throughout the entire program.

Furthermore, care was taken to avoid using the delay() function for cyclical tasks in the programming. During a delay, the ESP32 is blocked and cannot perform other tasks. Instead, the millis() function was utilized to realize time-based tasks such as LED blinking. The millis() function returns the number of milliseconds that have elapsed since the ESP32 started. By calculating the desired time interval, the subtask can be executed cyclically after that interval has passed. Avoiding delays generally improves performance and increases flexibility.

Finally, the program code has been extensively commented. This helps in making the code easier to read, understand, and facilitates future maintenance if required.


Programming the Raspberry Pi

Next, the Raspberry Pi is programmed in Python. A program is written to enable communication with the ESP32. Additionally, relevant lines of code are added to control the warning light based on the received sensor data. Finally, it is implemented to send the sensor data to the Influx database, in this case, that of BBS2 Wolfsburg, where they can be stored, visualized, and analyzed over an extended period. For the uploaded sensor data, a so-called "tag" is added to differentiate the measurement values. In this case, the room number where the Climate Cube is currently stationed is added as the tag. If the Climate Cube is to be used in a different room, the room number can be changed in the tag. This makes it easy to distinguish between the measurement values, and they can also be compared and displayed in the Influx database.

Similar to the programming of the ESP32, partial functions have been outsourced and defined in their own methods. However, unlike before, the sleep() function has been used for cyclical processes, which is equivalent to the delay() function in the ESP32. To ensure that the Raspberry Pi is not blocked and can perform all subtasks in parallel, multi-threading is used after installing the appropriate library. The different subprocesses, such as communication via MQTT, controlling the relays, and querying the acknowledgment button, are divided into individual threads that can be executed simultaneously.

Tastatur, Schreiben, ComputertastaturMaus, Rechner, Technologie, Internet

Figure: Keyboard and Mouse from Pixabay

Zuletzt geändert: Montag, 3. Juli 2023, 09:33