summaryrefslogtreecommitdiff
path: root/minion/src/gymnasiearbete.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'minion/src/gymnasiearbete.cpp')
-rw-r--r--minion/src/gymnasiearbete.cpp47
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);
}