464

Dependencies:   DISCO_L475VG_IOT01A_wifi

Committer:
omareddash
Date:
Sun Oct 31 01:03:51 2021 +0000
Revision:
0:4935437a7008
464

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omareddash 0:4935437a7008 1 #include "mbed.h"
omareddash 0:4935437a7008 2 #include "wifi.h"
omareddash 0:4935437a7008 3 /*------------------------------------------------------------------------------
omareddash 0:4935437a7008 4 Hyperterminal settings: 115200 bauds, 8-bit data, no parity
omareddash 0:4935437a7008 5
omareddash 0:4935437a7008 6 This example
omareddash 0:4935437a7008 7 - connects to a wifi network (SSID & PWD to set in mbed_app.json)
omareddash 0:4935437a7008 8 - displays the IP address and creates a web page
omareddash 0:4935437a7008 9 - then connect on its IP address on the same wifi network with another device
omareddash 0:4935437a7008 10 - Now able to change the led status and read the temperature
omareddash 0:4935437a7008 11
omareddash 0:4935437a7008 12 This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13),
omareddash 0:4935437a7008 13 wifi_dataready pin (PE_1), wifi reset pin (PE_8)
omareddash 0:4935437a7008 14 ------------------------------------------------------------------------------*/
omareddash 0:4935437a7008 15
omareddash 0:4935437a7008 16 /* Private defines -----------------------------------------------------------*/
omareddash 0:4935437a7008 17 #define WIFI_WRITE_TIMEOUT 10000
omareddash 0:4935437a7008 18 #define WIFI_READ_TIMEOUT 10000
omareddash 0:4935437a7008 19 #define PORT 80
omareddash 0:4935437a7008 20
omareddash 0:4935437a7008 21 /* Private typedef------------------------------------------------------------*/
omareddash 0:4935437a7008 22 typedef enum
omareddash 0:4935437a7008 23 {
omareddash 0:4935437a7008 24 WS_IDLE = 0,
omareddash 0:4935437a7008 25 WS_CONNECTED,
omareddash 0:4935437a7008 26 WS_DISCONNECTED,
omareddash 0:4935437a7008 27 WS_ERROR,
omareddash 0:4935437a7008 28 } WebServerState_t;
omareddash 0:4935437a7008 29
omareddash 0:4935437a7008 30 /* Private macro -------------------------------------------------------------*/
omareddash 0:4935437a7008 31 static int wifi_sample_run(void);
omareddash 0:4935437a7008 32 static void WebServerProcess(void);
omareddash 0:4935437a7008 33 static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature);
omareddash 0:4935437a7008 34 /* Private variables ---------------------------------------------------------*/
omareddash 0:4935437a7008 35 Serial pc(SERIAL_TX, SERIAL_RX);
omareddash 0:4935437a7008 36 static uint8_t http[1024];
omareddash 0:4935437a7008 37 static uint8_t resp[1024];
omareddash 0:4935437a7008 38 uint16_t respLen;
omareddash 0:4935437a7008 39 uint8_t IP_Addr[4];
omareddash 0:4935437a7008 40 uint8_t MAC_Addr[6];
omareddash 0:4935437a7008 41 int32_t Socket = -1;
omareddash 0:4935437a7008 42 static WebServerState_t State = WS_ERROR;
omareddash 0:4935437a7008 43 char ModuleName[32];
omareddash 0:4935437a7008 44 DigitalOut led(LED2);
omareddash 0:4935437a7008 45 AnalogIn adc_temp(ADC_TEMP);
omareddash 0:4935437a7008 46
omareddash 0:4935437a7008 47 int main()
omareddash 0:4935437a7008 48 {
omareddash 0:4935437a7008 49 int ret = 0;
omareddash 0:4935437a7008 50 led = 0;
omareddash 0:4935437a7008 51 pc.baud(115200);
omareddash 0:4935437a7008 52 printf("\n");
omareddash 0:4935437a7008 53 printf("************************************************************\n");
omareddash 0:4935437a7008 54 printf("*** STM32 IoT Discovery kit for STM32L475 MCU ***\n");
omareddash 0:4935437a7008 55 printf("*** WIFI Web Server demonstration ***\n\n");
omareddash 0:4935437a7008 56 printf("*** Copy the IP address on another device connected ***\n");
omareddash 0:4935437a7008 57 printf("*** to the wifi network ***\n");
omareddash 0:4935437a7008 58 printf("*** Read the temperature and update the LED status ***\n");
omareddash 0:4935437a7008 59 printf("************************************************************\n");
omareddash 0:4935437a7008 60
omareddash 0:4935437a7008 61 /* Working application */
omareddash 0:4935437a7008 62 ret = wifi_sample_run();
omareddash 0:4935437a7008 63
omareddash 0:4935437a7008 64 if (ret != 0) {
omareddash 0:4935437a7008 65 return -1;
omareddash 0:4935437a7008 66 }
omareddash 0:4935437a7008 67
omareddash 0:4935437a7008 68
omareddash 0:4935437a7008 69 while(1) {
omareddash 0:4935437a7008 70 WebServerProcess ();
omareddash 0:4935437a7008 71 }
omareddash 0:4935437a7008 72
omareddash 0:4935437a7008 73 }
omareddash 0:4935437a7008 74
omareddash 0:4935437a7008 75
omareddash 0:4935437a7008 76 int wifi_sample_run(void)
omareddash 0:4935437a7008 77 {
omareddash 0:4935437a7008 78
omareddash 0:4935437a7008 79 /*Initialize and use WIFI module */
omareddash 0:4935437a7008 80 if(WIFI_Init() == WIFI_STATUS_OK) {
omareddash 0:4935437a7008 81 printf("ES-WIFI Initialized.\n");
omareddash 0:4935437a7008 82
omareddash 0:4935437a7008 83 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
omareddash 0:4935437a7008 84 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",
omareddash 0:4935437a7008 85 MAC_Addr[0],
omareddash 0:4935437a7008 86 MAC_Addr[1],
omareddash 0:4935437a7008 87 MAC_Addr[2],
omareddash 0:4935437a7008 88 MAC_Addr[3],
omareddash 0:4935437a7008 89 MAC_Addr[4],
omareddash 0:4935437a7008 90 MAC_Addr[5]);
omareddash 0:4935437a7008 91 } else {
omareddash 0:4935437a7008 92 printf("> ERROR : CANNOT get MAC address\n");
omareddash 0:4935437a7008 93 }
omareddash 0:4935437a7008 94
omareddash 0:4935437a7008 95 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
omareddash 0:4935437a7008 96 printf("> es-wifi module connected \n");
omareddash 0:4935437a7008 97
omareddash 0:4935437a7008 98 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
omareddash 0:4935437a7008 99 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",
omareddash 0:4935437a7008 100 IP_Addr[0],
omareddash 0:4935437a7008 101 IP_Addr[1],
omareddash 0:4935437a7008 102 IP_Addr[2],
omareddash 0:4935437a7008 103 IP_Addr[3]);
omareddash 0:4935437a7008 104
omareddash 0:4935437a7008 105 printf(">Start HTTP Server... \n");
omareddash 0:4935437a7008 106 printf(">Wait for connection... \n");
omareddash 0:4935437a7008 107 State = WS_IDLE;
omareddash 0:4935437a7008 108 } else {
omareddash 0:4935437a7008 109 printf("> ERROR : es-wifi module CANNOT get IP address\n");
omareddash 0:4935437a7008 110 return -1;
omareddash 0:4935437a7008 111 }
omareddash 0:4935437a7008 112 } else {
omareddash 0:4935437a7008 113 printf("> ERROR : es-wifi module NOT connected\n");
omareddash 0:4935437a7008 114 return -1;
omareddash 0:4935437a7008 115 }
omareddash 0:4935437a7008 116 } else {
omareddash 0:4935437a7008 117 printf("> ERROR : WIFI Module cannot be initialized.\n");
omareddash 0:4935437a7008 118 return -1;
omareddash 0:4935437a7008 119 }
omareddash 0:4935437a7008 120 return 0;
omareddash 0:4935437a7008 121 }
omareddash 0:4935437a7008 122
omareddash 0:4935437a7008 123 /**
omareddash 0:4935437a7008 124 * @brief Send HTML page
omareddash 0:4935437a7008 125 * @param None
omareddash 0:4935437a7008 126 * @retval None
omareddash 0:4935437a7008 127 */
omareddash 0:4935437a7008 128 static void WebServerProcess(void)
omareddash 0:4935437a7008 129 {
omareddash 0:4935437a7008 130 uint8_t LedState = 0;
omareddash 0:4935437a7008 131 float temp;
omareddash 0:4935437a7008 132 switch(State)
omareddash 0:4935437a7008 133 {
omareddash 0:4935437a7008 134 case WS_IDLE:
omareddash 0:4935437a7008 135 Socket = 0;
omareddash 0:4935437a7008 136 WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
omareddash 0:4935437a7008 137
omareddash 0:4935437a7008 138 if(Socket != -1)
omareddash 0:4935437a7008 139 {
omareddash 0:4935437a7008 140 printf("> HTTP Server Started \n");
omareddash 0:4935437a7008 141 State = WS_CONNECTED;
omareddash 0:4935437a7008 142 }
omareddash 0:4935437a7008 143 else
omareddash 0:4935437a7008 144 {
omareddash 0:4935437a7008 145 printf("> ERROR : Connection cannot be established.\n");
omareddash 0:4935437a7008 146 State = WS_ERROR;
omareddash 0:4935437a7008 147 }
omareddash 0:4935437a7008 148 break;
omareddash 0:4935437a7008 149
omareddash 0:4935437a7008 150 case WS_CONNECTED:
omareddash 0:4935437a7008 151
omareddash 0:4935437a7008 152 WIFI_ReceiveData(Socket, resp, 1200, &respLen, WIFI_READ_TIMEOUT);
omareddash 0:4935437a7008 153
omareddash 0:4935437a7008 154 if( respLen > 0)
omareddash 0:4935437a7008 155 {
omareddash 0:4935437a7008 156 if(strstr((char *)resp, "GET")) /* GET: put web page */
omareddash 0:4935437a7008 157 {
omareddash 0:4935437a7008 158 temp = (adc_temp.read()*100);
omareddash 0:4935437a7008 159 if(SendWebPage(LedState, temp) != WIFI_STATUS_OK)
omareddash 0:4935437a7008 160 {
omareddash 0:4935437a7008 161 printf("> ERROR : Cannot send web page\n");
omareddash 0:4935437a7008 162 State = WS_ERROR;
omareddash 0:4935437a7008 163 }
omareddash 0:4935437a7008 164 }
omareddash 0:4935437a7008 165 else if(strstr((char *)resp, "POST"))/* POST: received info */
omareddash 0:4935437a7008 166 {
omareddash 0:4935437a7008 167 if(strstr((char *)resp, "radio"))
omareddash 0:4935437a7008 168 {
omareddash 0:4935437a7008 169 if(strstr((char *)resp, "radio=0"))
omareddash 0:4935437a7008 170 {
omareddash 0:4935437a7008 171 LedState = 0;
omareddash 0:4935437a7008 172 led = 0;
omareddash 0:4935437a7008 173 }
omareddash 0:4935437a7008 174 else if(strstr((char *)resp, "radio=1"))
omareddash 0:4935437a7008 175 {
omareddash 0:4935437a7008 176 LedState = 1;
omareddash 0:4935437a7008 177 led = 1;
omareddash 0:4935437a7008 178 }
omareddash 0:4935437a7008 179
omareddash 0:4935437a7008 180 temp = (adc_temp.read()*100);
omareddash 0:4935437a7008 181 if(SendWebPage(LedState, temp) != WIFI_STATUS_OK)
omareddash 0:4935437a7008 182 {
omareddash 0:4935437a7008 183 printf("> ERROR : Cannot send web page\n");
omareddash 0:4935437a7008 184 State = WS_ERROR;
omareddash 0:4935437a7008 185 }
omareddash 0:4935437a7008 186 }
omareddash 0:4935437a7008 187 }
omareddash 0:4935437a7008 188 }
omareddash 0:4935437a7008 189 if(WIFI_StopServer(Socket) == WIFI_STATUS_OK)
omareddash 0:4935437a7008 190 {
omareddash 0:4935437a7008 191 WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT);
omareddash 0:4935437a7008 192 }
omareddash 0:4935437a7008 193 else
omareddash 0:4935437a7008 194 {
omareddash 0:4935437a7008 195 State = WS_ERROR;
omareddash 0:4935437a7008 196 }
omareddash 0:4935437a7008 197 break;
omareddash 0:4935437a7008 198 case WS_ERROR:
omareddash 0:4935437a7008 199 default:
omareddash 0:4935437a7008 200 break;
omareddash 0:4935437a7008 201 }
omareddash 0:4935437a7008 202 }
omareddash 0:4935437a7008 203
omareddash 0:4935437a7008 204
omareddash 0:4935437a7008 205 /**
omareddash 0:4935437a7008 206 * @brief Send HTML page
omareddash 0:4935437a7008 207 * @param None
omareddash 0:4935437a7008 208 * @retval None
omareddash 0:4935437a7008 209 */
omareddash 0:4935437a7008 210 static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature)
omareddash 0:4935437a7008 211 {
omareddash 0:4935437a7008 212 uint8_t temp[50];
omareddash 0:4935437a7008 213 uint16_t SentDataLength;
omareddash 0:4935437a7008 214 WIFI_Status_t ret;
omareddash 0:4935437a7008 215
omareddash 0:4935437a7008 216 /* construct web page content */
omareddash 0:4935437a7008 217 strcpy((char *)http, (char *)"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
omareddash 0:4935437a7008 218 strcat((char *)http, (char *)"<html>\r\n<body>\r\n");
omareddash 0:4935437a7008 219 strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n");
omareddash 0:4935437a7008 220 strcat((char *)http, (char *)"<h2>InventekSys : Web Server using Es-Wifi with STM32</h2>\r\n");
omareddash 0:4935437a7008 221 strcat((char *)http, (char *)"<br /><hr>\r\n");
omareddash 0:4935437a7008 222 strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temp: <input type=\"text\" size=2 value=\"");
omareddash 0:4935437a7008 223 sprintf((char *)temp, "%f", temperature);
omareddash 0:4935437a7008 224 strcat((char *)http, (char *)temp);
omareddash 0:4935437a7008 225 strcat((char *)http, (char *)"\"> <sup>O</sup>C");
omareddash 0:4935437a7008 226
omareddash 0:4935437a7008 227 if (ledIsOn)
omareddash 0:4935437a7008 228 {
omareddash 0:4935437a7008 229 strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" >LED off");
omareddash 0:4935437a7008 230 strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" checked>LED on");
omareddash 0:4935437a7008 231 }
omareddash 0:4935437a7008 232 else
omareddash 0:4935437a7008 233 {
omareddash 0:4935437a7008 234 strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" checked>LED off");
omareddash 0:4935437a7008 235 strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" >LED on");
omareddash 0:4935437a7008 236 }
omareddash 0:4935437a7008 237
omareddash 0:4935437a7008 238 strcat((char *)http, (char *)"</strong><p><input type=\"submit\"></form></span>");
omareddash 0:4935437a7008 239 strcat((char *)http, (char *)"</body>\r\n</html>\r\n");
omareddash 0:4935437a7008 240
omareddash 0:4935437a7008 241 ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT);
omareddash 0:4935437a7008 242
omareddash 0:4935437a7008 243 if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http)))
omareddash 0:4935437a7008 244 {
omareddash 0:4935437a7008 245 ret = WIFI_STATUS_ERROR;
omareddash 0:4935437a7008 246 }
omareddash 0:4935437a7008 247
omareddash 0:4935437a7008 248 return ret;
omareddash 0:4935437a7008 249 }