#include "http/request.hpp" #include "http/response_status.hpp" #include "secrets.hpp" #include "temperature.hpp" #include "util.hpp" #include "wifi_module.hpp" #include #include #include #include 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); wifi_module.set_wifi_mode(WifiMode::Station); 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(); } 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)); char mac_address[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; Serial.print(F("MAC address: ")); Serial.println(wifi_module.get_mac_address(mac_address)); 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(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(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); }