#include "hal.h" #include "tcpip_adapter.h" #include "esp_wifi.h" #include "esp_event_loop.h" #include #include "format.h" #include "socket.h" #include "proc.h" #ifdef WITH_APRS #include "wifi.h" #include "aprs.h" #define DEBUG_PRINT // ====================================================================================================== static wifi_ap_record_t AP[8]; static uint16_t APs=0; const int MaxLineLen=512; static char Line[MaxLineLen]; static void AP_Print(void (*Output)(char)) { for(uint16_t Idx=0; Idx "); Format_String(CONS_UART_Write, Msg); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif return 0; } extern "C" void vTaskAPRS(void* pvParameters) { esp_err_t Err; vTaskDelay(1000); Err=WIFI_Init(); #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "WIFI_Init() => "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif for( ; ; ) // main (endless) loop { vTaskDelay(5000); Err=WIFI_Start(); // start WiFi #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "WIFI_Start() => "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif /* vTaskDelay(5000); APs=8; Err=WIFI_ActiveScan(AP, APs); #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "WIFI_ActiveScan() => "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); CONS_UART_Write('/'); Format_UnsDec(CONS_UART_Write, APs); Format_String(CONS_UART_Write, "\n"); if(Err==ESP_OK) AP_Print(CONS_UART_Write); xSemaphoreGive(CONS_Mutex); #endif */ vTaskDelay(1000); APs=8; Err=WIFI_PassiveScan(AP, APs); // perform a passive scan: find Access Points around #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "WIFI_PassiveScan() => "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); CONS_UART_Write('/'); Format_UnsDec(CONS_UART_Write, APs); Format_String(CONS_UART_Write, "\n"); if(Err==ESP_OK) AP_Print(CONS_UART_Write); xSemaphoreGive(CONS_Mutex); #endif if(Err==ESP_OK) // if WiFi scan went well { for(uint16_t Idx=0; Idx "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif if(Err) continue; // if connection failed then give up WIFI_IP.ip.addr = 0; for(uint8_t Idx=0; Idx<10; Idx++) // wait to obtain local IP from DHCP { vTaskDelay(1000); if(WIFI_State.isConnected==1) break; if(WIFI_getLocalIP()) break; } #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "Local IP: "); IP_Print(CONS_UART_Write, WIFI_IP.ip.addr); Format_String(CONS_UART_Write, " GW: "); IP_Print(CONS_UART_Write, WIFI_IP.gw.addr); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif if(WIFI_IP.ip.addr==0) { WIFI_Disconnect(); continue; } // if getting local IP failed then give up int ConnErr=APRS_Socket.Connect(APRS_Host, APRS_Port); // connect to the APRS server if(ConnErr>=0) // if connection succesfull { uint8_t LoginLen = LoginLine(Line, 5); #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "Connected to "); IP_Print(CONS_UART_Write, APRS_Socket.getIP()); Format_String(CONS_UART_Write, "\nAPRS <- "); Format_String(CONS_UART_Write, Line); xSemaphoreGive(CONS_Mutex); #endif APRS_Socket.setReceiveTimeout(1); // [sec] receive timeout // APRS_Socket.setBlocking(0); int Write=APRS_Socket.Send(Line, LoginLen); // send login to the APRS server int LinePtr=0; for(int Idx=0; Idx<120; Idx++) // wait for some time and watch what the server sends to us { int Left = MaxLineLen-LinePtr; if(Left<1) break; int Read=APRS_Socket.Receive(Line+LinePtr, Left-1); #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "APRS: "); Format_SignDec(CONS_UART_Write, Read); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif if(Read<0) break; // if an error reading from the APRS server if(Read==0) continue; // no more bytes: keep going int LineLen = LinePtr+Read; int MsgPtr = 0; Line[LineLen]=0; for(int Idx=LinePtr; Idx0) { int Copy=LineLen-MsgPtr; if(Copy>0) { memcpy(Line, Line+MsgPtr, Copy); LinePtr=0; } } } } #ifdef DEBUG_PRINT else // if connection failed to the APRS server { xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "Failed to connect to APRS -> "); Format_SignDec(CONS_UART_Write, ConnErr); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); } #endif APRS_Socket.Disconnect(); vTaskDelay(5000); WIFI_Disconnect(); WIFI_IP.ip.addr=0; vTaskDelay(2000); } } vTaskDelay(2000); Err=WIFI_Stop(); #ifdef DEBUG_PRINT xSemaphoreTake(CONS_Mutex, portMAX_DELAY); Format_String(CONS_UART_Write, "WIFI_Stop() => "); if(Err>=ESP_ERR_WIFI_BASE) Err-=ESP_ERR_WIFI_BASE; Format_SignDec(CONS_UART_Write, Err); Format_String(CONS_UART_Write, "\n"); xSemaphoreGive(CONS_Mutex); #endif } } #endif // WITH_APRS