summaryrefslogtreecommitdiff
path: root/minion/src/gymnasiearbete.cpp
blob: b31cf814f6760fac2d878a73e82d109cd0beafee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "http/request.hpp"
#include "secrets.hpp"
#include "wifi_module.hpp"

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <string.h>

constexpr auto HTTP_PORT = 80U;

constexpr auto BAUDRATE = 9600U;

constexpr auto NETWORK_MODULE_RX_PIN = 13U;
constexpr auto NETWORK_MODULE_TX_PIN = 11U;

constexpr auto SECOND_IN_MILLIS = 1000U;

auto wifi_module = WiFiModule({ NETWORK_MODULE_RX_PIN, NETWORK_MODULE_TX_PIN });

void setup()
{
	Serial.begin(BAUDRATE);

	pinMode(NETWORK_MODULE_RX_PIN, INPUT);
	pinMode(NETWORK_MODULE_TX_PIN, OUTPUT);

	wifi_module.begin(BAUDRATE);

	wifi_module.reset();

	delay(SECOND_IN_MILLIS);

	auto wifi_module_connected = wifi_module.test();

	Serial.print("Wifi module connected: ");
	Serial.println(wifi_module_connected ? "Yes" : "No");

	if (!wifi_module_connected)
	{
		while (true)
		{
		}
	}

	wifi_module.set_echo_enabled(false);

	delay(SECOND_IN_MILLIS);

	const auto wifi_connect_success =
		wifi_module.connect_to_wifi(WIFI_SSID, WIFI_PASSWORD);

	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(SECOND_IN_MILLIS * 3U);

	char local_ip[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	Serial.print("IP address: ");
	Serial.println(wifi_module.get_local_ip(local_ip));

	wifi_module.set_multiple_connections_enabled(true);

	delay(SECOND_IN_MILLIS);

	wifi_module.create_tcp_server(HTTP_PORT);
}

void loop()
{
	auto request = wifi_module.read_incoming_request();

	auto connection = request.connection();

	if (connection.id() == -1)
	{
		return;
	}

	Serial.print("Connection ID: ");
	Serial.println(connection.id());

	Serial.print("Connection closed: ");
	Serial.println(connection.is_closed() ? "true" : "false");

	Serial.print("Request method: ");
	Serial.println(http_request_method_strs[static_cast<size_t>(request.method())]);

	Serial.print("Request HTTP version: ");
	Serial.println(request.http_version());

	Serial.print("Request path: ");
	Serial.println(request.path());

	Serial.print("\nData: ");
	Serial.println(request.data());

	wifi_module.send_response(connection, 200U, "hello there!");

	wifi_module.close_connection(connection);

	Serial.print("Connection closed: ");
	Serial.println(connection.is_closed() ? "true" : "false");

	delay(SECOND_IN_MILLIS);
}