This code is a temperature logging and monitoring system using...
This code is a temperature logging and monitoring system using an embedded device like an mbed microcontroller. Here's an explanation of what it does:
Overview:
The code sets up a multi-threaded system for measuring temperature using a temperature sensor connected to the mbed microcontroller. It processes and logs temperature data, interfaces with an external USB flash drive for saving logs, communicates with an external application over Ethernet, and displays relevant information on an LCD screen. Here's the main functionality broken down by components:
Functionality:
-
Temperature Sensor Setup:
- Reads analog voltage from the temperature sensor (
p17
) and converts it to temperature values in Celsius (tempC
) and Fahrenheit (tempF
).
- Reads analog voltage from the temperature sensor (
-
Temperature Logging and Computation:
- Calculates and logs temperature readings in real-time.
- Computes the change/average rate of temperature over time for Celsius and Fahrenheit:
- AvgC = Average change in Celsius per second.
- AvgF = Average change in Fahrenheit per second.
- Keeps track of the total number of readings, total temperature, and time elapsed.
-
User Interaction via LCD:
- Displays dynamic data (temperature, time elapsed, average rates, etc.) on an LCD screen (
TextLCD
setup). - Includes a final display of average temperature values and rates at the end of execution.
- Displays dynamic data (temperature, time elapsed, average rates, etc.) on an LCD screen (
-
USB Flash Drive Logging:
- Logs temperature data in real-time to a
Temperature_Log.txt
file on an external USB flash drive connected to the system. - Ensures clean and organized logging with a timestamp, temperature readings, and average rates.
- Logs temperature data in real-time to a
-
Ethernet Communication:
- Sets up an Ethernet connection to communicate with an external client (e.g., a Java application).
- Waits for commands from the client:
Pause
,Resume
,Stopn
(stop the application). These commands control the system behavior:Pause
: Halts certain functionalities temporarily (keeps temperature reading thread running).Resume
: Resumes the paused functionalities.Stopn
: Terminates all the threads and operations.
-
LED and Speaker Notifications:
- Activates all four LEDs (
LED1
toLED4
) and produces audio feedback from the connected speaker each time a temperature reading is taken. - Turns off the LEDs and sound after completing each cycle.
- Activates all four LEDs (
-
Multi-Threading:
- The program leverages multiple threads to handle different tasks simultaneously, including:
- Reading temperature (
readTemp
). - Logging data to USB (
USBWrite
). - Updating the LCD (
TextLCD
). - Managing speaker/LED notifications (
speaker
). - Responding to Ethernet-based commands (
Pause
).
- Reading temperature (
- The program leverages multiple threads to handle different tasks simultaneously, including:
-
Thread Communication via Mailbox:
- Uses an RTOS-based mailbox (FIFO buffer) to share temperature readings and metadata between threads without conflicts.
-
Timer Management:
- Precise timing is handled via an onboard timer to measure elapsed time and ensure consistent intervals between actions.
-
Finalization:
- When the defined application runtime ends (or a stop command is received):
- The system finalizes logging.
- Displays final average statistics on the LCD.
- Plays audio and flashes LEDs to signal completion.
- When the defined application runtime ends (or a stop command is received):
Initialization:
- The
main
function initializes various components like the USB file system, Ethernet interface, LEDs, LCD, temperature sensor, mailboxes, etc. - It also sets up the embedded device as an Ethernet server, waiting for a client to connect and send runtime configuration (such as total runtime and time intervals).
Summary:
In summary, this code implements a sophisticated real-time temperature monitoring and logging solution. It:
- Measures and logs temperature from a sensor.
- Provides user feedback on an LCD.
- Alerts users with LED flashes and sound.
- Stores organized logs on a USB flash drive.
- Interfaces with an external application over Ethernet.
- Uses multi-threading and mailbox communication for efficiency and parallelism.