summaryrefslogtreecommitdiff
path: root/minion
diff options
context:
space:
mode:
Diffstat (limited to 'minion')
-rw-r--r--minion/src/gymnasiearbete.cpp42
-rw-r--r--minion/src/wifi_module.cpp80
-rw-r--r--minion/src/wifi_module.hpp33
3 files changed, 76 insertions, 79 deletions
diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp
index b3c543a..927cd1c 100644
--- a/minion/src/gymnasiearbete.cpp
+++ b/minion/src/gymnasiearbete.cpp
@@ -5,12 +5,18 @@
#include <SoftwareSerial.h>
#include <string.h>
+constexpr auto HTTP_PORT = 80U;
+
constexpr auto BAUDRATE = 9600U;
constexpr auto NETWORK_MODULE_RX_PIN = 3;
constexpr auto NETWORK_MODULE_TX_PIN = 2;
-auto wifi_module = WiFiModule(NETWORK_MODULE_RX_PIN, NETWORK_MODULE_TX_PIN);
+constexpr auto SECOND_IN_MILLIS = 1000U;
+
+constexpr auto MAX_HTTP_REQUEST_SIZE = 200U;
+
+auto wifi_module = WiFiModule({ NETWORK_MODULE_RX_PIN, NETWORK_MODULE_TX_PIN });
void setup()
{
@@ -23,7 +29,7 @@ void setup()
wifi_module.reset();
- delay(1000);
+ delay(SECOND_IN_MILLIS);
auto wifi_module_connected = wifi_module.test();
@@ -39,26 +45,20 @@ void setup()
wifi_module.set_echo_enabled(false);
- delay(1000);
+ delay(SECOND_IN_MILLIS);
const auto wifi_connect_success = wifi_module.connect(WIFI_SSID, WIFI_PASSWORD);
- if (wifi_connect_success)
- {
- Serial.print("Connected to wifi network '");
- Serial.print(WIFI_SSID);
- Serial.println("' successfully");
- }
- else
- {
- Serial.print("Failed to connect to wifi network '");
- Serial.print(WIFI_SSID);
- Serial.println("'");
- }
+ Serial.print(
+ wifi_connect_success ? "Connected to wifi network '"
+ : "Failed to connect to wifi network '"
+ );
+ Serial.print(WIFI_SSID);
+ Serial.println("'");
wifi_module.set_wifi_mode(WifiMode::Station);
- delay(3000);
+ delay(SECOND_IN_MILLIS * 3U);
char local_ip[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
@@ -67,21 +67,21 @@ void setup()
wifi_module.set_multiple_connections_enabled(true);
- delay(1000);
+ delay(SECOND_IN_MILLIS);
- wifi_module.create_tcp_server(80U);
+ wifi_module.create_tcp_server(HTTP_PORT);
}
void loop()
{
if (wifi_module.has_incoming_request())
{
- char raw_request[200] = "";
+ char raw_request[MAX_HTTP_REQUEST_SIZE] = "";
const auto connection_id = wifi_module.read_incoming_request(raw_request);
Serial.print("Connection ID: ");
- Serial.println(connection_id);
+ Serial.println(static_cast<unsigned int>(connection_id));
Serial.print("\nRaw request: ");
Serial.println(raw_request);
@@ -90,6 +90,6 @@ void loop()
wifi_module.close_connection(connection_id);
- delay(1000);
+ delay(SECOND_IN_MILLIS);
}
}
diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp
index 718065d..70fd6f9 100644
--- a/minion/src/wifi_module.cpp
+++ b/minion/src/wifi_module.cpp
@@ -5,8 +5,8 @@
#include <Arduino.h>
#include <string.h>
-WiFiModule::WiFiModule(uint8_t receive_pin, uint8_t transmit_pin) noexcept
- : _serial(receive_pin, transmit_pin)
+WiFiModule::WiFiModule(const WiFiModuleOptions &options) noexcept
+ : _serial(options.receive_pin, options.transmit_pin)
{
}
@@ -15,7 +15,7 @@ void WiFiModule::begin(size_t baudrate) noexcept
_serial.begin(baudrate);
}
-int WiFiModule::get_available() noexcept
+auto WiFiModule::get_available() noexcept -> int
{
return _serial.available();
}
@@ -34,7 +34,7 @@ void WiFiModule::reset() noexcept
);
}
-bool WiFiModule::connect(const char *ssid, const char *password) noexcept
+auto WiFiModule::connect(const char *ssid, const char *password) noexcept -> bool
{
const char cmd[] = "AT+CWJAP";
@@ -64,21 +64,14 @@ bool WiFiModule::connect(const char *ssid, const char *password) noexcept
const auto response_status = _read(20000U, response);
- if (response_status != ResponseStatus::OK)
- {
- Serial.print("Connection command responded with status ");
- Serial.println(response_status);
- return false;
- }
-
- return true;
+ return response_status == ResponseStatus::OK;
}
void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
{
const char cmd[] = "AT+CWMODE_CUR";
- auto command_length = strlen(cmd) + 2U + 1U;
+ const auto command_length = strlen(cmd) + 2U + 1U;
auto *command = util::malloc<char>(command_length);
@@ -90,7 +83,7 @@ void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(1500U, response);
+ _read(TIMEOUT_SHORT, response);
}
void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
@@ -109,7 +102,7 @@ void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(1500U, response);
+ _read(TIMEOUT_SHORT, response);
// Serial.println(response);
}
@@ -132,23 +125,14 @@ void WiFiModule::set_echo_enabled(bool is_enabled) noexcept
const auto response_status = _read(1500U, response);
- if (response_status == ResponseStatus::OK)
- {
- Serial.print("Turned ");
- Serial.print(is_enabled ? "on" : "off");
- Serial.println(" AT commands echo");
- }
- else
- {
- Serial.print("Failed to turn ");
- Serial.print(is_enabled ? "on" : "off");
- Serial.println(" AT commands echo");
- }
+ Serial.print(response_status == ResponseStatus::OK ? "Turned " : "Failed to turn ");
+ Serial.print(is_enabled ? "on" : "off");
+ Serial.println(" AT commands echo");
}
void WiFiModule::create_tcp_server(size_t port) noexcept
{
- const auto cmd = "AT+CIPSERVER";
+ const auto *cmd = "AT+CIPSERVER";
auto command_length = strlen(cmd) + 7U + 1U;
@@ -162,12 +146,12 @@ void WiFiModule::create_tcp_server(size_t port) noexcept
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
// Serial.println(response);
}
-bool WiFiModule::test() noexcept
+auto WiFiModule::test() noexcept -> bool
{
const auto send_success = _send_serial("AT");
@@ -188,9 +172,9 @@ bool WiFiModule::test() noexcept
return strcmp(response, "") != 0;
}
-const char *WiFiModule::get_local_ip(char *local_ip_out) noexcept
+auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char *
{
- const auto command = "AT+CIFSR";
+ const auto *command = "AT+CIFSR";
const auto send_success = _send_serial(command);
@@ -240,15 +224,19 @@ const char *WiFiModule::get_local_ip(char *local_ip_out) noexcept
return local_ip_out;
}
-bool WiFiModule::has_incoming_request() noexcept
+auto WiFiModule::has_incoming_request() noexcept -> bool
{
- return get_available() != 0 && _serial.find("+IPD,");
+ char request_prefix[] = "+IPD,";
+
+ return get_available() != 0 && _serial.find(request_prefix);
}
-size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
+auto WiFiModule::read_incoming_request(char *raw_request_out) noexcept -> size_t
{
+ const auto min_available_bytes = 5;
+
// Wait for the data buffer a bit
- while (get_available() < 5)
+ while (get_available() < min_available_bytes)
{
}
@@ -259,7 +247,7 @@ size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
Serial.print("Data length: ");
Serial.print(data_length);
- auto read_bytes = 0U;
+ auto read_bytes = 0;
const auto start_time = millis();
while (read_bytes != data_length && (start_time + 10000) > millis())
@@ -282,9 +270,9 @@ size_t WiFiModule::read_incoming_request(char *raw_request_out) noexcept
return connection_id;
}
-bool WiFiModule::close_connection(size_t connection_id) noexcept
+auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool
{
- const auto cmd = "AT+CIPCLOSE";
+ const auto *cmd = "AT+CIPCLOSE";
auto command_length = strlen(cmd) + 50U + 1U;
@@ -321,9 +309,9 @@ bool WiFiModule::close_connection(size_t connection_id) noexcept
return true;
}
-bool WiFiModule::send(size_t connection_id, const char *data)
+auto WiFiModule::send(size_t connection_id, const char *data) noexcept -> bool
{
- const auto cmd = "AT+CIPSEND";
+ const auto *cmd = "AT+CIPSEND";
const auto data_length = strlen(data);
@@ -345,18 +333,18 @@ bool WiFiModule::send(size_t connection_id, const char *data)
char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
_serial.print(data);
strcpy(response, "");
- _read(4000U, response);
+ _read(TIMEOUT_LONG, response);
return true;
}
-bool WiFiModule::_send_serial(const char *command) noexcept
+auto WiFiModule::_send_serial(const char *command) noexcept -> bool
{
auto full_command_length = strlen(command) + 2U + 1U;
@@ -379,7 +367,7 @@ bool WiFiModule::_send_serial(const char *command) noexcept
return true;
}
-ResponseStatus WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
+auto WiFiModule::_read(uint64_t timeout, char *response_out) noexcept -> ResponseStatus
{
const auto start_time = millis();
@@ -422,7 +410,7 @@ ResponseStatus WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
return status;
}
-char WiFiModule::_read_byte() noexcept
+auto WiFiModule::_read_byte() noexcept -> char
{
return static_cast<char>(_serial.read());
}
diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp
index 3621229..5de898e 100644
--- a/minion/src/wifi_module.hpp
+++ b/minion/src/wifi_module.hpp
@@ -8,6 +8,9 @@ constexpr auto MAX_NETWORK_MODULE_RESPONSE_LENGTH = 128U;
constexpr auto ASCII_TO_CHAR = 48U;
+constexpr auto TIMEOUT_SHORT = 1500U;
+constexpr auto TIMEOUT_LONG = 4000U;
+
enum WifiMode
{
Station = 1,
@@ -23,14 +26,20 @@ enum ResponseStatus
TIMEOUT
};
+struct WiFiModuleOptions
+{
+ uint8_t receive_pin;
+ uint8_t transmit_pin;
+};
+
class WiFiModule
{
public:
- WiFiModule(uint8_t receive_pin, uint8_t transmit_pin) noexcept;
+ explicit WiFiModule(const WiFiModuleOptions &options) noexcept;
void begin(size_t baudrate) noexcept;
- int get_available() noexcept;
+ auto get_available() noexcept -> int;
void reset() noexcept;
@@ -42,7 +51,7 @@ public:
*
* @returns Whether or not it succeeded.
*/
- bool connect(const char *ssid, const char *password) noexcept;
+ auto connect(const char *ssid, const char *password) noexcept -> bool;
void set_wifi_mode(WifiMode wifi_mode) noexcept;
@@ -57,7 +66,7 @@ public:
*
* @returns Whether or not the test succeeded.
*/
- bool test() noexcept;
+ auto test() noexcept -> bool;
/**
* Gets local IP address of the wifi module.
@@ -66,9 +75,9 @@ public:
*
* @returns A pointer to the local IP output buffer.
*/
- const char *get_local_ip(char *local_ip_out) noexcept;
+ auto get_local_ip(char *local_ip_out) noexcept -> const char *;
- bool has_incoming_request() noexcept;
+ auto has_incoming_request() noexcept -> bool;
/**
* Reads a incoming HTTP request.
@@ -77,11 +86,11 @@ public:
*
* @returns The connection ID.
*/
- size_t read_incoming_request(char *raw_request_out) noexcept;
+ auto read_incoming_request(char *raw_request_out) noexcept -> size_t;
- bool close_connection(size_t connection_id) noexcept;
+ auto close_connection(size_t connection_id) noexcept -> bool;
- bool send(size_t connection_id, const char *data);
+ auto send(size_t connection_id, const char *data) noexcept -> bool;
private:
SoftwareSerial _serial;
@@ -93,7 +102,7 @@ private:
*
* @returns Whether or not it succeeded.
*/
- bool _send_serial(const char *command) noexcept;
+ auto _send_serial(const char *command) noexcept -> bool;
/**
* Reads from the wifi module until it responds with a status.
@@ -104,7 +113,7 @@ private:
* @returns The response status.
*
*/
- ResponseStatus _read(uint64_t timeout, char *response_out) noexcept;
+ auto _read(uint64_t timeout, char *response_out) noexcept -> ResponseStatus;
- char _read_byte() noexcept;
+ auto _read_byte() noexcept -> char;
};