summaryrefslogtreecommitdiff
path: root/minion
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-05-30 13:06:33 +0200
committerHampusM <hampus@hampusmat.com>2022-05-30 13:06:33 +0200
commit7da6fc9f7be6c732aeae09cdcc338e1af2b5ad4c (patch)
tree6363660972ccfcc48dc4d2068ad174214e9f38b5 /minion
parent1452435a8d662bbd39f0c812cdc24ddd4ec226d3 (diff)
refactor(minion): store string constants in flash memory
Diffstat (limited to 'minion')
-rw-r--r--minion/src/gymnasiearbete.cpp23
-rw-r--r--minion/src/wifi_module.cpp81
-rw-r--r--minion/src/wifi_module.hpp3
3 files changed, 71 insertions, 36 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp
index 5c4f725..41be0ea 100644
--- a/minion/src/gymnasiearbete.cpp
+++ b/minion/src/gymnasiearbete.cpp
@@ -7,6 +7,7 @@
#include <Arduino.h>
#include <SoftwareSerial.h>
+#include <avr/pgmspace.h>
#include <string.h>
constexpr auto HTTP_PORT = 80U;
@@ -43,8 +44,8 @@ void setup()
auto wifi_module_connected = wifi_module.test();
- Serial.print("Wifi module connected: ");
- Serial.println(wifi_module_connected ? "Yes" : "No");
+ Serial.print(F("Wifi module connected: "));
+ Serial.println(wifi_module_connected ? F("Yes") : F("No"));
if (!wifi_module_connected)
{
@@ -59,8 +60,8 @@ void setup()
wifi_module.connect_to_wifi(WIFI_SSID, WIFI_PASSWORD);
Serial.print(
- wifi_connect_success ? "Connected to wifi network '"
- : "Failed to connect to wifi network '"
+ wifi_connect_success ? F("Connected to wifi network '")
+ : F("Failed to connect to wifi network '")
);
Serial.print(WIFI_SSID);
Serial.println("'");
@@ -76,7 +77,7 @@ void setup()
char local_ip[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- Serial.print("IP address: ");
+ Serial.print(F("IP address: "));
Serial.println(wifi_module.get_local_ip(local_ip));
wifi_module.set_multiple_connections_enabled(true);
@@ -102,16 +103,16 @@ void loop()
return;
}
- Serial.print("Connection ID: ");
+ Serial.print(F("Connection ID: "));
Serial.println(connection.id());
- Serial.print("Request method: ");
+ Serial.print(F("Request method: "));
Serial.println(http_request_method_strs[static_cast<size_t>(request.method())]);
- Serial.print("Request HTTP version: ");
+ Serial.print(F("Request HTTP version: "));
Serial.println(request.http_version());
- Serial.print("Request path: ");
+ Serial.print(F("Request path: "));
Serial.println(request.path());
const auto temperature_sensor_status = temperature_sensor.read_temperature();
@@ -124,7 +125,7 @@ void loop()
if (temperature_sensor_status != TemperatureSensorStatus::OK)
{
- Serial.print("Error: ");
+ Serial.print(F("Error: "));
Serial.println(static_cast<int>(temperature_sensor_status));
wifi_module.send_response(
@@ -161,7 +162,7 @@ void loop()
if (!send_response_ok)
{
- Serial.println("Failed to send response");
+ Serial.println(F("Failed to send response"));
wifi_module.close_connection(connection);
return;
}
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp
index 6c989f9..5174168 100644
--- a/minion/src/wifi_module.cpp
+++ b/minion/src/wifi_module.cpp
@@ -23,21 +23,27 @@ auto WiFiModule::get_available() noexcept -> int
void WiFiModule::reset() noexcept
{
- _send_serial("AT+RST");
+ const auto send_success = _send_serial("AT+RST");
+
+ if (!send_success)
+ {
+ Serial.println(F("Failed to send command to reset wifi module"));
+ return;
+ }
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
const auto response_status = _read(2000U, response);
Serial.println(
- response_status == WiFiModuleResponseStatus::OK ? "Reset wifi module"
- : "Failed to reset wifi module"
+ response_status == WiFiModuleResponseStatus::OK ? F("Reset wifi module")
+ : F("Failed to reset wifi module")
);
}
auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcept -> bool
{
- const char cmd[] = "AT+CWJAP";
+ const auto cmd = "AT+CWJAP_CUR";
auto command_length =
WIFI_CONNECT_COMMAND_BASE_LENGTH + strlen(cmd) + strlen(ssid) + strlen(password);
@@ -46,7 +52,9 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep
if (command == nullptr)
{
- Serial.println("Memory allocation failed for creating wifi connection command");
+ Serial.println(
+ F("Heap memory allocation failed for creating a wifi connection command")
+ );
return false;
}
@@ -56,7 +64,8 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep
if (!send_success)
{
- Serial.println("Failed to send connection command");
+ Serial.println(F("Failed to send wifi connect command"));
+ free(command);
return false;
}
@@ -64,7 +73,7 @@ auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcep
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- const auto response_status = _read(20000U, response);
+ const auto response_status = _read(40000U, response);
return response_status == WiFiModuleResponseStatus::OK;
}
@@ -77,6 +86,11 @@ void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
auto *command = util::malloc<char>(command_length);
+ if (command == nullptr)
+ {
+ return;
+ }
+
snprintf(command, command_length, "%s=%u", cmd, static_cast<int>(wifi_mode));
_send_serial(command);
@@ -96,6 +110,11 @@ void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
auto *command = util::malloc<char>(command_length);
+ if (command == nullptr)
+ {
+ return;
+ }
+
snprintf(command, command_length, "%s=%u", cmd, is_enabled ? 1U : 0U);
_send_serial(command);
@@ -115,6 +134,11 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept
auto *command = util::malloc<char>(command_length);
+ if (command == nullptr)
+ {
+ return;
+ }
+
snprintf(command, command_length, "%s%u", cmd, is_enabled ? 1U : 0U);
_send_serial(command);
@@ -126,10 +150,11 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept
const auto response_status = _read(1500U, response);
Serial.print(
- response_status == WiFiModuleResponseStatus::OK ? "Turned " : "Failed to turn "
+ response_status == WiFiModuleResponseStatus::OK ? F("Turned ")
+ : F("Failed to turn ")
);
- Serial.print(is_enabled ? "on" : "off");
- Serial.println(" AT commands echo");
+ Serial.print(is_enabled ? F("on") : F("off"));
+ Serial.println(F(" AT commands echo"));
}
void WiFiModule::create_tcp_server(size_t port) noexcept
@@ -140,6 +165,11 @@ void WiFiModule::create_tcp_server(size_t port) noexcept
auto *command = util::malloc<char>(command_length);
+ if (command == nullptr)
+ {
+ return;
+ }
+
snprintf(command, command_length, "%s=1,%u", cmd, port);
_send_serial(command);
@@ -162,7 +192,7 @@ auto WiFiModule::test() noexcept -> bool
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- const auto response_status = _read(5000U, response);
+ const auto response_status = _read(8000U, response);
if (response_status == WiFiModuleResponseStatus::TIMEOUT)
{
@@ -174,9 +204,7 @@ auto WiFiModule::test() noexcept -> bool
auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char *
{
- const auto *command = "AT+CIFSR";
-
- const auto send_success = _send_serial(command);
+ const auto send_success = _send_serial("AT+CIFSR");
if (!send_success)
{
@@ -300,7 +328,9 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo
if (command == nullptr)
{
- Serial.println("Memory allocation failed for creating close connection command");
+ Serial.println(
+ F("Heap memory allocation failed for creating close connection command")
+ );
return false;
}
@@ -316,14 +346,15 @@ auto WiFiModule::close_connection(NetworkConnection &connection) noexcept -> boo
if (response_status != WiFiModuleResponseStatus::OK)
{
- Serial.print("Failed to close connection to ");
+ Serial.print(F("Failed to close connection to "));
Serial.println(connection.id());
+ Serial.println(response);
return false;
}
connection.set_is_closed(true);
- Serial.print("Closed connection to ");
+ Serial.print(F("Closed connection to "));
Serial.println(connection.id());
return true;
@@ -346,7 +377,7 @@ auto WiFiModule::send_response(
tot_headers_lengths += strlen(headers[index]) + 2U;
}
- const auto data_length = strlen(RESPONSE_HTTP_VERSION) + 1 +
+ const auto data_length = strlen_P(RESPONSE_HTTP_VERSION) + 1 +
RESPONSE_STATUS_CODE_LENGTH + 4 + strlen(body) +
tot_headers_lengths;
@@ -356,7 +387,9 @@ auto WiFiModule::send_response(
if (command == nullptr)
{
- Serial.println("Memory allocation failed for creating send response command");
+ Serial.println(
+ F("Heap memory allocation failed for creating send response command")
+ );
return false;
}
@@ -370,19 +403,19 @@ auto WiFiModule::send_response(
_read(TIMEOUT_MEDIUM, response);
- _serial.print(RESPONSE_HTTP_VERSION);
- _serial.print(" ");
+ _serial.print(reinterpret_cast<const __FlashStringHelper *>(RESPONSE_HTTP_VERSION));
+ _serial.print(F(" "));
_serial.print(status_code);
- _serial.print("\r\n");
+ _serial.print(F("\r\n"));
// Print headers
for (size_t index = 0U; index < headers_cnt; index++)
{
_serial.print(headers[index]);
- _serial.print("\r\n");
+ _serial.print(F("\r\n"));
}
- _serial.print("\r\n");
+ _serial.print(F("\r\n"));
_serial.print(body);
strcpy(response, "");
diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp
index 963a1de..9bc2c20 100644
--- a/minion/src/wifi_module.hpp
+++ b/minion/src/wifi_module.hpp
@@ -4,6 +4,7 @@
#include "network_connection.hpp"
#include <SoftwareSerial.h>
+#include <avr/pgmspace.h>
#include <stddef.h>
#include <stdint.h>
@@ -25,7 +26,7 @@ constexpr auto TIMEOUT_SHORT = 1500U;
constexpr auto TIMEOUT_MEDIUM = 4000U;
constexpr auto TIMEOUT_LONG = 10000U;
-constexpr auto RESPONSE_HTTP_VERSION = "HTTP/1.1";
+const char RESPONSE_HTTP_VERSION[] PROGMEM = { "HTTP/1.1" };
constexpr auto RESPONSE_STATUS_CODE_LENGTH = 3U;
constexpr auto NUMBER_BASE = 10U;