mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-22 23:56:13 +01:00
Wifi STA or AP mode
This commit is contained in:
parent
cf0c818138
commit
0bb6b577fa
62
WifiESP.cpp
62
WifiESP.cpp
|
@ -22,6 +22,7 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESPAsyncTCP.h>
|
#include <ESPAsyncTCP.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "WifiESP.h"
|
#include "WifiESP.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
|
@ -137,25 +138,62 @@ void hw_wdt_enable(){
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool WifiESP::setup(const char *wifiESSID,
|
bool WifiESP::setup(const char *SSid,
|
||||||
const char *wifiPassword,
|
const char *password,
|
||||||
const char *hostname,
|
const char *hostname,
|
||||||
int port,
|
int port,
|
||||||
const byte channel) {
|
const byte channel) {
|
||||||
|
bool havePassword = true;
|
||||||
|
bool haveSSID = true;
|
||||||
|
bool wifiUp = false;
|
||||||
|
|
||||||
// We are server and should not sleep
|
// We are server and should not sleep
|
||||||
wifi_set_sleep_type(NONE_SLEEP_T);
|
wifi_set_sleep_type(NONE_SLEEP_T);
|
||||||
// connects to access point
|
// connects to access point
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
WiFi.setAutoReconnect(true);
|
const char *yourNetwork = "Your network ";
|
||||||
WiFi.begin(wifiESSID, wifiPassword);
|
if (strncmp(yourNetwork, SSid, 13) == 0 || strncmp("", SSid, 13) == 0)
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
haveSSID = false;
|
||||||
Serial.print('.');
|
if (strncmp(yourNetwork, password, 13) == 0 || strncmp("", password, 13) == 0)
|
||||||
delay(500);
|
havePassword = false;
|
||||||
|
|
||||||
|
if (haveSSID && havePassword) {
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.setAutoReconnect(true);
|
||||||
|
WiFi.begin(SSid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
Serial.print('.');
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
|
||||||
|
wifiUp = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECTED)
|
if (!haveSSID) {
|
||||||
DIAG(F("Wifi IP %s"),WiFi.localIP().toString().c_str());
|
// prepare all strings
|
||||||
else {
|
String strSSID("DCC_");
|
||||||
DIAG(F("Wifi fail"));
|
String strPass("PASS_");
|
||||||
|
String strMac = WiFi.macAddress();
|
||||||
|
strMac.remove(0,9);
|
||||||
|
strMac.replace(":","");
|
||||||
|
strMac.replace(":","");
|
||||||
|
strSSID.concat(strMac);
|
||||||
|
strPass.concat(strMac);
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
if (WiFi.softAP(strSSID.c_str(),
|
||||||
|
havePassword ? password : strPass.c_str(),
|
||||||
|
channel, false, 8)) {
|
||||||
|
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
|
||||||
|
DIAG(F("Wifi AP IP %s"),WiFi.softAPIP().toString().c_str());
|
||||||
|
wifiUp = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (!wifiUp) {
|
||||||
|
DIAG(F("Wifi all fail"));
|
||||||
// no idea to go on
|
// no idea to go on
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user