From da57f5ee4041fb6a099607a258327f573f8a0b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 11 Aug 2022 14:06:39 +0200 Subject: [PATCH] esp8266: add connected function --- lib/radio_esp8266.c | 24 +++++++++++++++++++++++- lib/radio_esp8266.h | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/radio_esp8266.c b/lib/radio_esp8266.c index eafe843..01befff 100644 --- a/lib/radio_esp8266.c +++ b/lib/radio_esp8266.c @@ -141,10 +141,32 @@ void radio_esp8266_reset(void) while(rx_used < 13 || memcmp((char*)&rx_buffer[rx_used - 13], "WIFI GOT IP\r\n", 13) != 0) { // wait to have IP __WFI(); // sleep until something happened } - radio_esp8266_transmit((uint8_t*)"ATE0\r\n", 6); // disable echoing +} + +bool radio_esp8266_connected(void) +{ + // verify if we ware connect to access point + radio_esp8266_transmit_at("+CWJAP_CUR?"); while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response __WFI(); // sleep until something happened } + if (rx_used < 5 || 0 == memcmp((char*)&rx_buffer[rx_used - 5], "No AP", 5)) { + rx_used = 0; // finished using the buffer + return false; + } + + // check if we have an IP + radio_esp8266_transmit_at("+CIPSTA_CUR?"); // verify if module is present + while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response + __WFI(); // sleep until something happened + } + if (rx_used < 17 || '0' == rx_buffer[16]) { // check for +CIPSTA_CUR:ip:"0.0.0.0" + rx_used = 0; + return false; + } + + rx_used = 0; // clear buffer + return true; } void radio_esp8266_tcp_open(char* host, uint16_t port) diff --git a/lib/radio_esp8266.h b/lib/radio_esp8266.h index ab822b6..d8060aa 100644 --- a/lib/radio_esp8266.h +++ b/lib/radio_esp8266.h @@ -22,6 +22,10 @@ extern uint16_t radio_esp8266_received_len; void radio_esp8266_setup(void); /** reset ESP */ void radio_esp8266_reset(void); +/** check if connect to the network + * @return if connected to network + */ +bool radio_esp8266_connected(void); /** establish TCP connection * @param[in] host host to connect to * @param[in] port TCP port to connect to