#include "http/request.hpp" #include "http/response_status.hpp" #include "secrets.hpp" #include "temperature.hpp" #include "wifi_module.hpp" #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 SECOND_IN_MILLIS = 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); 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(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()); const auto temperature_sensor_status = temperature_sensor.read_temperature(); if (temperature_sensor_status != TemperatureSensorStatus::OK) { Serial.print("Error: "); Serial.println(static_cast(temperature_sensor_status)); wifi_module.send_response( connection, HTTP_RESPONSE_STATUS_INTERNAL_SERVER_ERROR, R"({"error": "Internal server error"})" ); wifi_module.close_connection(connection); delay(SECOND_IN_MILLIS); return; } char response_data[RESPONSE_DATA_MAX_LENGTH] = ""; snprintf( response_data, RESPONSE_DATA_MAX_LENGTH, "%u", temperature_sensor.temperature() ); const auto send_response_ok = wifi_module.send_response(connection, HTTP_RESPONSE_STATUS_OK, response_data); if (!send_response_ok) { Serial.println("Failed to send response"); } wifi_module.close_connection(connection); delay(SECOND_IN_MILLIS); }