LoRa_APRS_iGate/src/wifi_utils.cpp

102 lines
3.7 KiB
C++
Raw Normal View History

2023-06-06 16:43:04 +02:00
#include <WiFi.h>
#include "configuration.h"
2023-06-10 02:20:39 +02:00
#include "pins_config.h"
2023-06-08 06:58:10 +02:00
#include "wifi_utils.h"
2023-06-06 17:21:59 +02:00
#include "display.h"
2023-06-06 16:43:04 +02:00
extern Configuration Config;
extern WiFi_AP *currentWiFi;
extern int myWiFiAPIndex;
extern int myWiFiAPSize;
2023-06-08 23:09:05 +02:00
extern int stationMode;
2023-06-09 07:12:13 +02:00
extern uint32_t previousWiFiMillis;
2023-06-06 16:43:04 +02:00
namespace WIFI_Utils {
2024-01-03 02:12:10 +01:00
void checkWiFi() {
if ((WiFi.status() != WL_CONNECTED) && ((millis() - previousWiFiMillis) >= 30*1000)) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
previousWiFiMillis = millis();
}
2023-06-09 07:12:13 +02:00
}
2024-01-03 02:12:10 +01:00
void startWiFi() {
int wifiCounter = 0;
2024-01-12 05:36:04 +01:00
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(500);
2024-01-03 02:12:10 +01:00
unsigned long start = millis();
show_display("", "", "Connecting to Wifi:", "", currentWiFi->ssid + " ...", 0);
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
while (WiFi.status() != WL_CONNECTED && wifiCounter<2) {
delay(500);
2024-01-05 11:44:55 +01:00
#if defined(TTGO_T_LORA32_V2_1) || defined(HELTEC_V2) || defined(HELTEC_V3) || defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
2024-01-03 02:12:10 +01:00
digitalWrite(internalLedPin,HIGH);
#endif
2024-01-03 02:12:10 +01:00
Serial.print('.');
delay(500);
2024-01-05 11:44:55 +01:00
#if defined(TTGO_T_LORA32_V2_1) || defined(HELTEC_V2) || defined(HELTEC_V3) || defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
2024-01-03 02:12:10 +01:00
digitalWrite(internalLedPin,LOW);
#endif
2024-01-03 02:12:10 +01:00
if ((millis() - start) > 10000){
delay(1000);
if(myWiFiAPIndex >= (myWiFiAPSize-1)) {
myWiFiAPIndex = 0;
2024-01-12 05:36:04 +01:00
if (stationMode==5) {
2024-01-03 02:12:10 +01:00
wifiCounter++;
}
} else {
myWiFiAPIndex++;
2023-07-31 00:57:31 +02:00
}
2024-01-03 02:12:10 +01:00
currentWiFi = &Config.wifiAPs[myWiFiAPIndex];
start = millis();
Serial.print("\nConnecting to WiFi '"); Serial.print(currentWiFi->ssid); Serial.println("' ...");
show_display("", "", "Connecting to Wifi:", "", currentWiFi->ssid + " ...", 0);
WiFi.disconnect();
WiFi.begin(currentWiFi->ssid.c_str(), currentWiFi->password.c_str());
}
2024-01-03 02:12:10 +01:00
}
2024-01-05 11:44:55 +01:00
#if defined(TTGO_T_LORA32_V2_1) || defined(HELTEC_V2) || defined(HELTEC_V3) || defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
2024-01-03 02:12:10 +01:00
digitalWrite(internalLedPin,LOW);
#endif
2024-01-03 02:12:10 +01:00
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected as ");
Serial.println(WiFi.localIP());
show_display("", "", " Connected!!", "" , " loading ...", 1000);
} else if (WiFi.status() != WL_CONNECTED && stationMode==5) {
Serial.println("\nNot connected to WiFi! (DigiRepeater Mode)");
show_display("", "", " WiFi Not Connected!", " DigiRepeater MODE" , " loading ...", 2000);
}
}
2024-01-03 02:12:10 +01:00
void setup() {
2023-11-28 04:57:15 +01:00
if (stationMode==1 || stationMode==2) {
2024-01-03 02:12:10 +01:00
if (stationMode==1) {
Serial.println("stationMode ---> iGate (only Rx)");
} else {
Serial.println("stationMode ---> iGate (Rx + Tx)");
}
startWiFi();
btStop();
2023-11-28 04:57:15 +01:00
} else if (stationMode==3 || stationMode==4) {
2024-01-03 02:12:10 +01:00
if (stationMode==3) {
Serial.println("stationMode ---> DigiRepeater (Rx freq == Tx freq)");
} else {
Serial.println("stationMode ---> DigiRepeater (Rx freq != Tx freq)");
}
WiFi.mode(WIFI_OFF);
btStop();
2023-11-28 04:57:15 +01:00
} else if (stationMode==5) {
Serial.println("stationMode ---> iGate when Wifi/APRS available (DigiRepeater when not)");
2023-06-08 01:34:18 +02:00
} else {
Serial.println("stationMode ---> NOT VALID, check '/data/igate_conf.json'");
show_display("------- ERROR -------", "stationMode Not Valid", "change it on : /data/", "igate_conf.json", 0);
while (1);
2023-06-08 01:34:18 +02:00
}
2024-01-03 02:12:10 +01:00
}
2023-06-08 01:34:18 +02:00
2023-06-06 16:43:04 +02:00
}