/* * Wifi.c * * Created on: Nov 29, 2020 * Author: rpaz */ #include "WebServer.h" extern float temperature; /* Update SSID and PASSWORD with own Access point settings */ #define SSID "Pixelito" #define PASSWORD "bombardeenlaus" #define PORT 80 #define TERMINAL_USE #define WIFI_WRITE_TIMEOUT 10000 #define WIFI_READ_TIMEOUT 10000 #define SOCKET 0 #ifdef TERMINAL_USE #define LOG(a) printf a #else #define LOG(a) #endif /* Private defines -----------------------------------------------------------*/ static uint8_t http[1024]; static uint8_t IP_Addr[4]; static int WifiStart(void) { uint8_t MAC_Addr[6]; /*Initialize and use WIFI module */ if (WIFI_Init() == WIFI_STATUS_OK) { LOG(("ES-WIFI Initialized.\n")); if (WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { LOG( ("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", MAC_Addr[0], MAC_Addr[1], MAC_Addr[2], MAC_Addr[3], MAC_Addr[4], MAC_Addr[5])); } else { LOG(("> ERROR : CANNOT get MAC address\n")); return -1; } } else { return -1; } return 0; } int WifiConnect(void) { WifiStart(); LOG(("\nConnecting to %s , %s\n",SSID,PASSWORD)); if (WIFI_Connect(SSID, PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { if (WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { LOG( ("> es-wifi module connected: got IP Address : %d.%d.%d.%d\n", IP_Addr[0], IP_Addr[1], IP_Addr[2], IP_Addr[3])); } else { LOG((" ERROR : es-wifi module CANNOT get IP address\n")); return -1; } } else { LOG(("ERROR : es-wifi module NOT connected\n")); return -1; } return 0; } int WifiServer(void) { bool StopServer = false; LOG(("\nRunning HTML Server test\n")); if (WifiConnect() != 0) return -1; if (WIFI_STATUS_OK != WIFI_StartServer(SOCKET, WIFI_TCP_PROTOCOL, 1, "", PORT)) { LOG(("ERROR: Cannot start server.\n")); } LOG( ("Server is running and waiting for an HTTP Client connection to %d.%d.%d.%d\n",IP_Addr[0],IP_Addr[1],IP_Addr[2],IP_Addr[3])); do { uint8_t RemoteIP[4]; uint16_t RemotePort; while (WIFI_STATUS_OK != WIFI_WaitServerConnection(SOCKET, 1000, RemoteIP, &RemotePort)) { LOG( ("Waiting connection to %d.%d.%d.%d\n",IP_Addr[0],IP_Addr[1],IP_Addr[2],IP_Addr[3])); } LOG( ("Client connected %d.%d.%d.%d:%d\n",RemoteIP[0],RemoteIP[1],RemoteIP[2],RemoteIP[3],RemotePort)); StopServer = WebServerProcess(); if (WIFI_CloseServerConnection(SOCKET) != WIFI_STATUS_OK) { LOG(("ERROR: failed to close current Server connection\n")); return -1; } } while (StopServer == false); if (WIFI_STATUS_OK != WIFI_StopServer(SOCKET)) { LOG(("ERROR: Cannot stop server.\n")); } LOG(("Server is stop\n")); return 0; } static bool WebServerProcess(void) { uint8_t LedState = 1; SensorData_t aux; uint16_t respLen; static uint8_t resp[1024]; bool stopserver = false; if (WIFI_STATUS_OK == WIFI_ReceiveData(SOCKET, resp, 1000, &respLen, WIFI_READ_TIMEOUT)) { LOG(("get %d byte from server\n",respLen)); if (respLen > 0) { if (strstr((char*) resp, "GET")) /* GET: put web page */ { aux = GetSensors(); //BSP_TSENSOR_ReadTemp(); if (SendWebPage(LedState, aux) != WIFI_STATUS_OK) { LOG(("> ERROR : Cannot send web page\n")); } else { LOG(("Send page after GET command\n")); } } else if (strstr((char*) resp, "POST"))/* POST: received info */ { LOG(("Post request\n")); if (strstr((char*) resp, "radio")) { if (strstr((char*) resp, "radio=0")) { LedState = 0; QueueLed(LedState); } else if (strstr((char*) resp, "radio=1")) { LedState = 1; QueueLed(LedState); } aux = GetSensors(); //BSP_TSENSOR_ReadTemp(); } if (strstr((char*) resp, "stop_server")) { if (strstr((char*) resp, "stop_server=0")) { stopserver = false; } else if (strstr((char*) resp, "stop_server=1")) { stopserver = true; } } aux = GetSensors(); //BSP_TSENSOR_ReadTemp(); if (SendWebPage(LedState, aux) != WIFI_STATUS_OK) { LOG(("> ERROR : Cannot send web page\n")); } else { LOG(("Send Page after POST command\n")); } } } } else { LOG(("Client close connection\n")); } return stopserver; } /** * @brief Send HTML page * @param None * @retval None */ static WIFI_Status_t SendWebPage(uint8_t ledIsOn, SensorData_t payload) { uint8_t temp[50]; uint16_t SentDataLength; WIFI_Status_t ret; /* construct web page content */ strcpy((char*) http, (char*) "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"); strcat((char*) http, (char*) "\r\n
\r\n"); strcat((char*) http, (char*) "