From dd1d0cf34a44f70793dcb52fd9ef90d9274b55d1 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 27 Oct 2022 17:42:54 +0200 Subject: move esp setup to setup.c --- stm32f091/esp8266.c | 205 +++++++-------------------------------------------- stm32f091/esp8266.h | 23 +++--- stm32f091/main.c | 5 +- stm32f091/server.c | 4 +- stm32f091/setup.c | 10 +++ stm32f091/test.c | 18 ----- stm32f091/test.h | 3 - stm32f091/wifi.def.h | 2 + 8 files changed, 54 insertions(+), 216 deletions(-) delete mode 100644 stm32f091/test.c delete mode 100644 stm32f091/test.h (limited to 'stm32f091') diff --git a/stm32f091/esp8266.c b/stm32f091/esp8266.c index c98528e..d45954e 100644 --- a/stm32f091/esp8266.c +++ b/stm32f091/esp8266.c @@ -10,6 +10,12 @@ #include "server.h" #include "util.h" +// macro for concise sending of multiple commands +#define ws_esp8266_send_seq(cmd) { \ + uint8_t _cmd[] = cmd; \ + ws_server_send(_cmd, sizeof(_cmd)); \ +} + uint8_t g_ws_esp8266_dma_rx_buffer[WS_DMA_RX_BUFFER_SIZE]; uint8_t g_ws_esp8266_dma_tx_buffer[WS_DMA_TX_BUFFER_SIZE]; @@ -53,6 +59,28 @@ void ws_esp8266_start_receive() { __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT); } +void ws_esp8266_connect() { + ws_esp8266_send_seq("AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n"); +} + +void ws_esp8266_ap_client_mode() { + ws_esp8266_send_seq("AT+CWMODE=1\r\n"); +} + +void ws_esp8266_start_tcp_server() { + ws_esp8266_send_seq("AT+CIPSERVER=0\r\n"); + ws_esp8266_send_seq("AT+CIPMUX=1\r\n"); + ws_esp8266_send_seq("AT+CIPSERVER=1," WS_SERVER_PORT "\r\n"); +} + +void ws_esp8266_set_mac() { + ws_esp8266_send_seq("AT+CIPSTAMAC=\"" WS_ESP8266_WLAN_MAC "\"\r\n"); +} + +void ws_esp8266_set_ip() { + ws_esp8266_send_seq("AT+CIPSTA=\"" WS_ESP8266_WLAN_IP "\"\r\n"); +} + // TODO: refactor code from here to EOF void ws_esp8266_ATsendCommand(uint8_t* data){ char dataChar[20]; @@ -68,183 +96,6 @@ void ws_esp8266_ATsendCommand(uint8_t* data){ HAL_UART_Transmit(&huart2, data, strlen((char*)data),1000); HAL_Delay(5000); } -int ws_esp8266_checkOK(uint8_t *receiveData,int length){ - char *ret=""; - // char *ret1=""; - HAL_UART_Transmit(&huart2, receiveData,length,1000); - ret = strstr((char*)receiveData,"OK"); - // ret = strstr((char*)receiveData,"change"); - // memset(receiveData,0,30); - if((ret[0]='O') && (ret[1]=='K')){ - //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100); - return 1; - - } -// else if((ret1[0]='c') && (ret1[1]=='h')){ -// //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100); -// return 1; -// -// } - else{ - return 0; - } - -} -int ws_esp8266_receivingMsg(uint8_t *receiveData,int length){ - char *ret=""; - HAL_UART_Transmit(&huart2, receiveData,length,1000); - ret = strstr((char*)receiveData,"+IPD"); - // memset(receiveData,0,30); - - if((ret[0]='+') && (ret[1]=='I')){ - //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100); - return 1; - - } - else{ - return 0; - } - -} -int ws_esp8266_unlink(uint8_t *receiveData,int length){ - char *ret=""; - HAL_UART_Transmit(&huart2, receiveData,length,1000); - ret = strstr((char*)receiveData,"UNLINK"); - // memset(receiveData,0,30); - if((ret[0]='U') && (ret[1]=='N')){ - //HAL_UART_Transmit(&huart2, (uint8_t*)ret, sizeof(ret), 100); - return 1; - - } - else{ - return 0; - } - -} - -void ws_esp8266_StartEsp(){ - uint8_t Tx_AT[]="AT\r\n"; - uint8_t Rx_buffer[10]={0}; - for(int i=0;i<3;i++){ - // HAL_UART_Transmit(&huart2, hier,sizeof(hier),100); - HAL_UART_Transmit(&huart1, Tx_AT,strlen((char*)Tx_AT), 100); - HAL_UART_Receive(&huart1, Rx_buffer, 10, 100); - - - HAL_UART_Transmit(&huart2, Rx_buffer,10,100); - HAL_Delay(5000); - //memset(Rx_buffer,0,sizeof(Rx_buffer)); - } - -} -void ws_esp8266_disconnect(){ - int ret; - uint8_t Tx_disconnect[]="AT+CWQAP\r\n";uint8_t buffer[17]={0}; - while(ret!=1){ - - HAL_UART_Transmit(&huart1, Tx_disconnect,strlen((char*)Tx_disconnect), 100); - HAL_UART_Receive(&huart1, buffer, 17, 100); - HAL_Delay(2000); - - if(ws_esp8266_checkOK(buffer,17)==1){ - ret=1; - } - - } - - HAL_Delay(5000); -} -void ws_esp8266_mode(){ - int ret; - uint8_t buffer1[20]={0}; uint8_t Tx_mode[]="AT+CWMODE=1\r\n"; - - while(ret!=1){ - - HAL_UART_Transmit(&huart1, Tx_mode,strlen((char*)Tx_mode), 100); - HAL_UART_Receive(&huart1, buffer1, 20, 100); - HAL_Delay(1000); - - if(ws_esp8266_checkOK(buffer1,20)==1){ - ret=1; - - } - - } - - HAL_Delay(1000); -} -void ws_esp8266_connect(){ - uint8_t Tx_network[]="AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n"; - - - HAL_UART_Transmit(&huart1, Tx_network,strlen((char*)Tx_network),1000); - HAL_Delay(10000); -// HAL_UART_Transmit(&huart1, Tx_network,sizeof(Tx_network),1000); -// HAL_Delay(10000); - - - - - -} -void ws_esp8266_serveraan(){ - int ret; - uint8_t buffer1[30]={0}; uint8_t Tx_server[]="AT+CIPSERVER=1," WS_SERVER_PORT "\r\n"; - - while(ret!=1){ - - HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100); - HAL_UART_Receive(&huart1, buffer1, 30, 100); - HAL_Delay(2000); - - if(ws_esp8266_checkOK(buffer1,30)==1){ - ret=1; - - } - - } - - HAL_Delay(1000); -} -void ws_esp8266_serveruit(){ - //int ret; - //uint8_t buffer1[27]={0}; - uint8_t Tx_server[]="AT+CIPSERVER=0\r\n"; -// -// while(ret!=1){ - - HAL_UART_Transmit(&huart1, Tx_server,strlen((char*)Tx_server), 100); -// HAL_UART_Receive(&huart1, buffer1, 27, 100); - HAL_Delay(3000); - -// if(unlink(buffer1,27)==1){ -// ret=1; -// -// } -// -// } - - HAL_Delay(1000); -} -void ws_esp8266_mux(){ - int ret; - uint8_t buffer2[20]={0}; uint8_t Tx_mux[]="AT+CIPMUX=1\r\n"; - - while(ret!=1){ - - HAL_UART_Transmit(&huart1, Tx_mux,strlen((char*)Tx_mux), 100); - HAL_UART_Receive(&huart1, buffer2, 20, 100); - HAL_Delay(2000); - - if(ws_esp8266_checkOK(buffer2,20)==1){ - ret=1; - - } - - } - - HAL_Delay(5000); -} void ws_esp8266_close(){ uint8_t Tx_close[]="AT+CIPCLOSE=0\r\n"; diff --git a/stm32f091/esp8266.h b/stm32f091/esp8266.h index 64910fd..66ccfba 100644 --- a/stm32f091/esp8266.h +++ b/stm32f091/esp8266.h @@ -25,18 +25,17 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart); /** @brief send data to esp over uart with dma */ void ws_esp8266_send(uint8_t* data, size_t size); +/** @brief start dma receive and reset half-transfer interrupt flag */ void ws_esp8266_start_receive(); -// TODO: remove/update/document/refactor these functions -void ws_esp8266_ATsendCommand(uint8_t* data); -int ws_esp8266_checkOK(uint8_t *receiveData,int length); -int ws_esp8266_receivingMsg(uint8_t *receiveData,int length); -int ws_esp8266_unlink(uint8_t *receiveData,int length); -void ws_esp8266_StartEsp(); -void ws_esp8266_disconnect(); -void ws_esp8266_mode(); +/** @brief connect to access point using wifi.h credentials */ void ws_esp8266_connect(); -void ws_esp8266_serveraan(); -void ws_esp8266_serveruit(); -void ws_esp8266_mux(); -void ws_esp8266_close(); +/** @brief set esp to access point client mode (connect to AP, not become one) */ +void ws_esp8266_ap_client_mode(); +/** @brief initialize and configure the tcp server */ +void ws_esp8266_start_tcp_server(); + +/** @brief set mac address of the esp client */ +void ws_esp8266_set_mac(); +/** @brief set static ip address of the esp client */ +void ws_esp8266_set_ip(); diff --git a/stm32f091/main.c b/stm32f091/main.c index 80eb5a7..67c0a8d 100644 --- a/stm32f091/main.c +++ b/stm32f091/main.c @@ -6,11 +6,8 @@ #include "setup.h" #include "sensor.h" -#include "test.h" - int main() { ws_io_setup(); - // xTaskCreate(ws_sensor_read_task, "sensor", 64, NULL, 1, NULL); - xTaskCreate(ws_test_write_task, "test", 16, NULL, 2, NULL); + xTaskCreate(ws_sensor_read_task, "sensor", 64, NULL, 1, NULL); vTaskStartScheduler(); } diff --git a/stm32f091/server.c b/stm32f091/server.c index a06727c..1b677c2 100644 --- a/stm32f091/server.c +++ b/stm32f091/server.c @@ -77,8 +77,8 @@ void ws_server_req_incoming(uint8_t* data, size_t size) { break; } case WS_SERVER_LM_IDLE: { - if (next_few_bytes_are("+IPD")) { - i += 3; // skip I, P, and D + if (next_few_bytes_are("+IPD,")) { + i += 4; // skip I, P, D, and comma g_ws_server_parser.mode = WS_SERVER_LM_IPD_LISTENING; } break; diff --git a/stm32f091/setup.c b/stm32f091/setup.c index 9ed4174..2175ab3 100644 --- a/stm32f091/setup.c +++ b/stm32f091/setup.c @@ -91,6 +91,16 @@ void ws_io_setup() { }); ws_backlog_alloc(24 * 60); + +#ifdef WS_ESP8266_WLAN_MAC + ws_esp8266_set_mac(); +#endif +#ifdef WS_ESP8266_WLAN_IP + ws_esp8266_set_ip(); +#endif + ws_esp8266_ap_client_mode(); + ws_esp8266_connect(); + ws_esp8266_start_tcp_server(); } static void ws_io_clock_setup() { diff --git a/stm32f091/test.c b/stm32f091/test.c deleted file mode 100644 index ae48ef7..0000000 --- a/stm32f091/test.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include - -#include "server.h" -#include "setup.h" - -void ws_test_write_task() { - uint8_t cmd1[] = "AT+CIPMUX=1\r\n"; - uint8_t cmd2[] = "AT+CWMODE=1\r\n"; - uint8_t cmd3[] = "AT+CWJAP=\"" WS_ESP8266_WLAN_SSID "\",\"" WS_ESP8266_WLAN_PASSWD "\"\r\n"; - while (1) { - ws_server_send(cmd1, sizeof(cmd1)); - ws_server_send(cmd2, sizeof(cmd2)); - ws_server_send(cmd3, sizeof(cmd3)); - vTaskDelay(portTICK_PERIOD_MS * 1000 * 1); - } -} diff --git a/stm32f091/test.h b/stm32f091/test.h deleted file mode 100644 index dffa516..0000000 --- a/stm32f091/test.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void ws_test_write_task(); diff --git a/stm32f091/wifi.def.h b/stm32f091/wifi.def.h index f0748d2..61e1706 100644 --- a/stm32f091/wifi.def.h +++ b/stm32f091/wifi.def.h @@ -2,4 +2,6 @@ #define WS_ESP8266_WLAN_SSID "Test" #define WS_ESP8266_WLAN_PASSWD "12345678" +// #define WS_ESP8266_WLAN_MAC "f2:9b:89:47:c4:f3" +// #define WS_ESP8266_WLAN_IP "192.168.2.69" -- cgit v1.2.3