diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-16 12:44:58 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-05-16 13:50:22 +0200 |
commit | 3ede86caf948d6e989fd056ea4f8d8c588939971 (patch) | |
tree | a3226c6b9005ddc24a4af7a3c38722eb113963d4 /minion/src/gymnasiearbete.cpp | |
parent | 6aa69cdcd8720d835e922323cdcf79147c9b3154 (diff) |
feat(minion): add reading temperature sensor
Diffstat (limited to 'minion/src/gymnasiearbete.cpp')
-rw-r--r-- | minion/src/gymnasiearbete.cpp | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index a1f6d51..fada1e2 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -1,6 +1,7 @@ #include "http/request.hpp" #include "http/response_status.hpp" #include "secrets.hpp" +#include "temperature.hpp" #include "wifi_module.hpp" #include <Arduino.h> @@ -14,10 +15,16 @@ constexpr auto BAUDRATE = 9600U; constexpr auto NETWORK_MODULE_RX_PIN = 13U; constexpr auto NETWORK_MODULE_TX_PIN = 11U; +constexpr auto TEMPERATURE_SENSOR_PIN = 9U; + constexpr auto SECOND_IN_MILLIS = 1000U; +constexpr auto RESPONSE_DATA_MAX_LENGTH = 64U; + auto wifi_module = WiFiModule({ NETWORK_MODULE_RX_PIN, NETWORK_MODULE_TX_PIN }); +auto temperature_sensor = TemperatureSensor(TEMPERATURE_SENSOR_PIN); + void setup() { Serial.begin(BAUDRATE); @@ -102,12 +109,44 @@ void loop() Serial.print("\nData: "); Serial.println(request.data()); - wifi_module.send_response(connection, HTTP_RESPONSE_STATUS_OK, "hello there!"); + const auto temperature_sensor_status = temperature_sensor.read_temperature(); - wifi_module.close_connection(connection); + if (temperature_sensor_status != TemperatureSensorStatus::OK) + { + Serial.print("Error: "); + Serial.println(static_cast<int>(temperature_sensor_status)); - Serial.print("Connection closed: "); - Serial.println(connection.is_closed() ? "true" : "false"); + wifi_module.send_response( + connection, + HTTP_RESPONSE_STATUS_INTERNAL_SERVER_ERROR, + R"({"error": "Internal server error"})" + ); + + wifi_module.close_connection(connection); + + delay(SECOND_IN_MILLIS); + + return; + } + + char response_data[RESPONSE_DATA_MAX_LENGTH] = ""; + + snprintf( + response_data, + RESPONSE_DATA_MAX_LENGTH, + "%u", + temperature_sensor.temperature() + ); + + const auto send_response_ok = + wifi_module.send_response(connection, HTTP_RESPONSE_STATUS_OK, response_data); + + if (!send_response_ok) + { + Serial.println("Failed to send response"); + } + + wifi_module.close_connection(connection); delay(SECOND_IN_MILLIS); } |