Skip to content

Commit 6290254

Browse files
committed
[feature]: Add board temperature monitoring and logging.
- Introduced readBoardTemperature() and formatTemperatureMessage() utilities. - Added print_board_temperature() to log formatted temperature readings via SerialPrinter. - Registered board temperature as a scheduled entry in scheduler1 and print it periodically in loop1. - Adjusted scheduler intervals and initialised LED_BUILTIN as output. Signed-off-by: Goran Mišković <[email protected]>
1 parent 81d8be7 commit 6290254

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/main.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "secrets.h" // Contains STASSID, STAPSK, QOTD_HOST, ECHO_HOST, QOTD_PORT, ECHO_PORT
3333
#include <WiFi.h>
3434
#include <algorithm>
35+
#include <cmath>
3536

3637
using namespace async_tcp;
3738
/**
@@ -83,6 +84,26 @@ static const std::string echo = "echo";
8384
static const std::string stack_0 = "stack_0";
8485
static const std::string stack_1 = "stack_1";
8586
static const std::string heap = "heap";
87+
static const std::string board_temperature = "temperature";
88+
/**
89+
* Reads the board temperature from internal temperature sensor
90+
* @return Temperature value in Celsius
91+
*/
92+
float readBoardTemperature() {
93+
return analogReadTemp();
94+
}
95+
96+
/**
97+
* Formats temperature reading into a display string
98+
* @param temperature Temperature value in Celsius
99+
* @return Formatted string with temperature reading and units
100+
*/
101+
std::string formatTemperatureMessage(const float temperature) {
102+
const std::string prefix = "Temperature in The Factory: ";
103+
const std::string suffix = "°C.\n";
104+
const auto tempStr = std::to_string(lround(temperature));
105+
return prefix + tempStr + suffix;
106+
}
86107

87108
/**
88109
* @brief Connects to the "quote of the day" server and initiates a connection.
@@ -164,6 +185,16 @@ void print_stack_stats() {
164185
serial_printer.print(std::move(stack_stats));
165186
}
166187

188+
void print_board_temperature() {
189+
// Read the board temperature
190+
const float temperature = readBoardTemperature();
191+
// Format the message
192+
auto temperature_message = std::make_unique<std::string>(
193+
formatTemperatureMessage(temperature));
194+
// Print the message using SerialPrinter
195+
serial_printer.print(std::move(temperature_message));
196+
}
197+
167198
/**
168199
* @brief Initializes the Wi-Fi connection and asynchronous context on Core 0.
169200
*/
@@ -221,9 +252,11 @@ void setup() {
221252
qotd_closed_handler->initialisePerpetualBridge();
222253
qotd_client.setOnClosedCallback(std::move(qotd_closed_handler));
223254

224-
scheduler0.setEntry(qotd, 1010);
255+
scheduler0.setEntry(qotd, 505);
225256
scheduler0.setEntry(echo, 101);
226-
scheduler0.setEntry(stack_0, 707070);
257+
scheduler0.setEntry(stack_0, 70707);
258+
259+
pinMode(LED_BUILTIN, OUTPUT);
227260

228261
operational = true;
229262
}
@@ -243,6 +276,7 @@ void setup1() {
243276

244277
scheduler1.setEntry(stack_1, 909090);
245278
scheduler1.setEntry(heap, 808080);
279+
scheduler1.setEntry(board_temperature, 606060);
246280
ctx1_ready = true;
247281
}
248282

@@ -269,4 +303,5 @@ void loop() {
269303
void loop1() {
270304
if (scheduler1.timeToRun(stack_1)) print_stack_stats();
271305
if (scheduler1.timeToRun(heap)) print_heap_stats();
306+
if (scheduler1.timeToRun(board_temperature)) print_board_temperature();
272307
}

0 commit comments

Comments
 (0)