summaryrefslogtreecommitdiff
path: root/minion/src/gymnasiearbete.cpp
blob: 41be0eaea3efb93ca0afe30c667947dc737d52c2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "http/request.hpp"
#include "http/response_status.hpp"
#include "secrets.hpp"
#include "temperature.hpp"
#include "util.hpp"
#include "wifi_module.hpp"

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <avr/pgmspace.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 TEMPERATURE_SENSOR_PIN = 9U;

constexpr auto MILLIS_IN_SECOND = 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);

auto last_sent_response_time = 0UL;

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(MILLIS_IN_SECOND);

	auto wifi_module_connected = wifi_module.test();

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

	if (!wifi_module_connected)
	{
		util::quit();
	}

	wifi_module.set_echo_enabled(false);

	delay(MILLIS_IN_SECOND);

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

	Serial.print(
		wifi_connect_success ? F("Connected to wifi network '")
							 : F("Failed to connect to wifi network '")
	);
	Serial.print(WIFI_SSID);
	Serial.println("'");

	if (!wifi_connect_success)
	{
		util::quit();
	}

	wifi_module.set_wifi_mode(WifiMode::Station);

	delay(MILLIS_IN_SECOND * 3U);

	char local_ip[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

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

	wifi_module.set_multiple_connections_enabled(true);

	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();

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

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

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

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

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

	const auto temperature_sensor_status = temperature_sensor.read_temperature();

	const char *response_headers[] = { "Access-Control-Allow-Origin: *",
									   "Content-Type: application/json" };

	const auto response_header_cnt =
		sizeof(response_headers) / sizeof(response_headers[0]);

	if (temperature_sensor_status != TemperatureSensorStatus::OK)
	{
		Serial.print(F("Error: "));
		Serial.println(static_cast<int>(temperature_sensor_status));

		wifi_module.send_response(
			connection,
			HTTP_RESPONSE_STATUS_INTERNAL_SERVER_ERROR,
			response_headers,
			response_header_cnt,
			R"({"error": "Internal server error"})"
		);

		wifi_module.close_connection(connection);

		return;
	}

	char response_data[RESPONSE_DATA_MAX_LENGTH] = "";

	snprintf(
		response_data,
		RESPONSE_DATA_MAX_LENGTH,
		"{ %s: { %s: %u} }",
		R"("data")",
		R"("temperature")",
		temperature_sensor.temperature()
	);

	const auto send_response_ok = wifi_module.send_response(
		connection,
		HTTP_RESPONSE_STATUS_OK,
		response_headers,
		response_header_cnt,
		response_data
	);

	if (!send_response_ok)
	{
		Serial.println(F("Failed to send response"));
		wifi_module.close_connection(connection);
		return;
	}

	last_sent_response_time = millis();

	wifi_module.close_connection(connection);
}