diff options
author | HampusM <hampus@hampusmat.com> | 2022-05-17 12:49:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-05-17 14:02:12 +0200 |
commit | 76bc59f8548bb3388747cc1c54326fb7236dc1eb (patch) | |
tree | 5ebd41472c86f55cd17551a1906e5a4618d08a5a /minion/src | |
parent | 0529ac97653bc535c5c1db832d258f661d69309c (diff) |
refactor(minion): add quit function & improve in loop
Diffstat (limited to 'minion/src')
-rw-r--r-- | minion/src/gymnasiearbete.cpp | 35 | ||||
-rw-r--r-- | minion/src/util.cpp | 6 | ||||
-rw-r--r-- | minion/src/util.hpp | 2 |
3 files changed, 31 insertions, 12 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index fada1e2..401722c 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -2,6 +2,7 @@ #include "http/response_status.hpp" #include "secrets.hpp" #include "temperature.hpp" +#include "util.hpp" #include "wifi_module.hpp" #include <Arduino.h> @@ -17,7 +18,7 @@ constexpr auto NETWORK_MODULE_TX_PIN = 11U; constexpr auto TEMPERATURE_SENSOR_PIN = 9U; -constexpr auto SECOND_IN_MILLIS = 1000U; +constexpr auto MILLIS_IN_SECOND = 1000U; constexpr auto RESPONSE_DATA_MAX_LENGTH = 64U; @@ -25,6 +26,8 @@ auto wifi_module = WiFiModule({ NETWORK_MODULE_RX_PIN, NETWORK_MODULE_TX_PIN }); auto temperature_sensor = TemperatureSensor(TEMPERATURE_SENSOR_PIN); +auto last_sent_response_time = 0UL; + void setup() { Serial.begin(BAUDRATE); @@ -36,7 +39,7 @@ void setup() wifi_module.reset(); - delay(SECOND_IN_MILLIS); + delay(MILLIS_IN_SECOND); auto wifi_module_connected = wifi_module.test(); @@ -45,14 +48,12 @@ void setup() if (!wifi_module_connected) { - while (true) - { - } + util::quit(); } wifi_module.set_echo_enabled(false); - delay(SECOND_IN_MILLIS); + delay(MILLIS_IN_SECOND); const auto wifi_connect_success = wifi_module.connect_to_wifi(WIFI_SSID, WIFI_PASSWORD); @@ -64,9 +65,14 @@ void setup() Serial.print(WIFI_SSID); Serial.println("'"); + if (!wifi_connect_success) + { + util::quit(); + } + wifi_module.set_wifi_mode(WifiMode::Station); - delay(SECOND_IN_MILLIS * 3U); + delay(MILLIS_IN_SECOND * 3U); char local_ip[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; @@ -75,13 +81,18 @@ void setup() wifi_module.set_multiple_connections_enabled(true); - delay(SECOND_IN_MILLIS); + delay(MILLIS_IN_SECOND); wifi_module.create_tcp_server(HTTP_PORT); } void loop() { + // Wait until a second has passed since the last sent response + while ((last_sent_response_time + MILLIS_IN_SECOND) > millis()) + { + } + auto request = wifi_module.read_incoming_request(); auto connection = request.connection(); @@ -124,8 +135,6 @@ void loop() wifi_module.close_connection(connection); - delay(SECOND_IN_MILLIS); - return; } @@ -144,9 +153,11 @@ void loop() if (!send_response_ok) { Serial.println("Failed to send response"); + wifi_module.close_connection(connection); + return; } - wifi_module.close_connection(connection); + last_sent_response_time = millis(); - delay(SECOND_IN_MILLIS); + wifi_module.close_connection(connection); } diff --git a/minion/src/util.cpp b/minion/src/util.cpp index 540a20c..15cf840 100644 --- a/minion/src/util.cpp +++ b/minion/src/util.cpp @@ -37,4 +37,10 @@ auto streq(const char *str_one, const char *str_two) noexcept -> bool return strcmp(str_one, str_two) == 0; } +void quit() noexcept +{ + while (true) + ; +} + } // namespace util diff --git a/minion/src/util.hpp b/minion/src/util.hpp index 4cc3967..0dbb03f 100644 --- a/minion/src/util.hpp +++ b/minion/src/util.hpp @@ -36,4 +36,6 @@ void substr(const char *str, const char *end, char *dest) noexcept; */ auto streq(const char *str_one, const char *str_two) noexcept -> bool; +void quit() noexcept; + } // namespace util |