#include "http/request.hpp" #include "http/response_status.hpp" #include "secrets.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 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(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, HTTP_RESPONSE_STATUS_OK, "hello there!"); wifi_module.close_connection(connection); Serial.print("Connection closed: "); Serial.println(connection.is_closed() ? "true" : "false"); delay(SECOND_IN_MILLIS); }