don’t post snippets…
how is SerialDebug defined?
I wanted to show the code that is actually executed for STA mode to keep things simple.
But below please find the complete setup() and StartSerialServer() functions.
The total sketch code is pretty big and it contains settings for STA and AP operation.
ESPConf is a struct read from and written to EEPROM. It is used on startup to set up the device parameters inside InitConfig(), which is called at the top of setup().
This works pretty well since about 6 months.
But I want to weed out all irregularities and now I came to the debug printing.
The defines are made in a header file so I could change them easily during debugging:
#define SerialDebug Serial1 // Debug goes out on GPIO02
#define SerialSS Serial // Device connected to the ESP UART
Notice that inside StartSerialServer() I am swapping the SerialSS pins to the ones actually connected to the controlled device which cannot have spurious data sent to it during WiFi board startup…
void setup()
{
String MsgDbg = ""; //Used for debug messages
IPAddress local_ip;
IPAddress gateway;
IPAddress netmask (255,255,255,0);
IPAddress opendns1 (208,67,220,220);
IPAddress opendns2 (208,67,222,222);
int sta_connected = 0;
memset (&cnftxbuf, 0, sizeof(cnftxbuf));
// !!! Debug output goes to GPIO02 unless we swap pins for Tx between Serial0 and Serial1!!!
SerialDebug.begin(115200);
InitConfig(); //Initialize handling of configuration via EEPROM
if (ESPConf.mode == WIFI_STA)
{
WiFi.mode((WiFiMode)ESPConf.mode); //WIFI_AP, WIFI_STA, WIFI_AP_STA, WIFI_OFF
if (ESPConf.fixedaddress == 1) //Do not use DHCP, set fixed address
{
//The 4 addresses below are (ip, gateway, subnet, DNS1, DNS2) and must be calculated from the used IP address
local_ip = ESPConf.addr;
gateway = ESPConf.addr;
gateway[3] = 1; //Force fake gateway as the first address in the network and use opendns
WiFi.config(local_ip, gateway, netmask, opendns1, opendns2);
}
else
{
local_ip = IPAddress(0,0,0,0);
WiFi.config(local_ip, local_ip, local_ip); //Reset to use DHCP
}
WiFi.hostname(ESPConf.host); //This is the hostname that should be supplied to the DHCP server
WiFi.begin(ESPConf.sta_ssid, ESPConf.sta_passwd);
if(WiFi.waitForConnectResult() == WL_CONNECTED)
{
MsgDbg = "STA ADDR = " + WiFi.localIP();
sta_connected = 1;
}
else
MsgDbg = "WiFi Connection to " + String(ESPConf.sta_ssid) + " failed! Starting SoftAP instead";
SerialDebug.println(MsgDbg);
}
if ((ESPConf.mode == WIFI_AP) || (!sta_connected)) //Mode WIFI_AP or failed to connect WIFI_STA so start mode WIFI_AP
{
WiFi.mode(WIFI_AP); //WIFI_OFF, WIFI_STA, WIFI_AP, WIFI_AP_STA
WiFi.softAPConfig((IPAddress)ESPConf.addr, (IPAddress)ESPConf.addr, netmask); //IPAddress(255, 255, 255, 0));
if (ESPConf.wichannel == 0)
WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd);
else if (ESPConf.hidden == 0)
WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd, ESPConf.wichannel);
else
WiFi.softAP(ESPConf.ap_ssid, ESPConf.ap_passwd, ESPConf.wichannel, true);
}
StartSerialServer();
StartConfigServer();
if (ESPConf.mode == WIFI_STA)
local_ip = WiFi.localIP();
else
local_ip = WiFi.softAPIP();
SerialDebug.print("Use ");
SerialDebug.print(local_ip);
SerialDebug.print(':');
SerialDebug.print(ESPConf.tcpport);
SerialDebug.println(" to connect serial");
}
void StartSerialServer()
{
//start UART. Be sure to set the speed to match the speed of whatever is
//connected to the UART.
SerialSS.begin(ESPConf.baud);
SerialDebug.println("Swapping pins for Serial, use 7/10 for Rx/Tx!");
SerialSS.swap(); //Put the serial pins on 7 (Rx) and 10 (Tx) to avoid boot data to reach SS
// Instantiate and start TCP serial server on port TCP_PORT
tcpServer = new WiFiServer(ESPConf.tcpport);
tcpServer->begin();
tcpServer->setNoDelay(true);
SerialDebug.println("Serialserver ready!");
}