#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 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("Wifi module connected: "); Serial.println(wifi_module_connected ? "Yes" : "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 ? "Connected to wifi network '" : "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("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("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); 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_data); if (!send_response_ok) { Serial.println("Failed to send response"); wifi_module.close_connection(connection); return; } last_sent_response_time = millis(); wifi_module.close_connection(connection); }