Hi, i am busy porting some projects to work with an ESP32 instead of an ESP8266, and i am encountering some Serial debug message from an unknown source.
the code
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
WebServer server(80);
#endif
#include <PatchDNSServer.h>
DNSServer dnsServer;
#define DNS_PORT 53
const char *ssid = "XXX";
const char *password = "xxxxxxxx";
const char *apname = "ESPLed";
const char *appass = "password"; // minimum length 8 characters
const int ledpin = 2; // I'm running this on an ESP-01
// i have a led connected to this active LOW
// i can't use the internal (pin 1) for the use of Serial
uint8_t ledstatus = 0; // this is keeping track of the state (of our statemachine)
const char // like this these lines are statically declared and const, so we can't change them at all
*pageheader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
*htmlhead = "<html><head><title>ESPwebserver</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></head>",
*bodystyle = "<body style=\"color: wheat; background-color: teal; font-size: 12pt; font-family: sans-serif;\">",
*accessIP = "http://192.168.4.1",
*htmlclose = "</body></html>";
String webIP;
void setup() {
webIP.reserve(30); // prevent fragments
digitalWrite(ledpin, HIGH);
pinMode(ledpin, OUTPUT);
Serial.begin(115200);
WiFi.softAP(apname, appass); // start AP mode
webIP = accessIP;
Serial.print("Started Access Point \"");
Serial.print(apname);
Serial.println("\"");
Serial.print("With password \"");
Serial.print(appass);
Serial.println("\"");
WiFi.begin(ssid, password); // attempt starting STA mode
Serial.println("Attempting to start Station mode");
uint32_t moment = millis();
while ((WiFi.status() != WL_CONNECTED) && (millis() < moment + 8000)) {
delay(500);
Serial.print(".");
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("espled")) { // type esp8266.local/ in your browser
Serial.println("MDNS responder started, type espled.local/ in your browser");
}
webIP = StationIP();
}
else if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" Unsuccessful.");
}
else if (WiFi.status() == WL_NO_SSID_AVAIL) {
Serial.print("Network ");
Serial.print(ssid);
Serial.println(" not available.");
}
WiFi.reconnect(); // reconnect AP after attempting to connect STA
dnsServer.start(DNS_PORT, "lab.com", IPAddress(192, 168, 4, 1));
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
dnsServer.processNextRequest();
checkLedStatus();
}
void checkLedStatus() { // our statemachine
switch (ledstatus) {
case 0: {
digitalWrite(ledpin, HIGH);
return;
}
case 1: {
digitalWrite(ledpin, LOW);
return;
}
case 2: {
if (ledBlink(500)) ; // here the return value (of ledBlink() ) gets discarded
return;
}
case 3: {
modulateLed();
return;
}
}
}
void modulateLed() {
static uint16_t ms = 100;
static bool increase = true;
if (!ledBlink(ms)) return;
if (ms > 250) increase = false;
if (ms < 20) increase = true;
if (increase) ms = (ms * 10) / 9;
else ms = (ms * 9) / 10;
}
bool ledBlink(uint32_t wait) {
static bool pinstate = false;
static uint32_t moment = millis();
if (millis() > moment + wait) {
pinstate = !pinstate;
moment = millis();
if (pinstate) digitalWrite(ledpin, LOW);
else digitalWrite(ledpin, HIGH);
return pinstate; // if pinstate is true and the pinstate has changed, the modulator will change speed
}
return false;
}
void handleRoot() {
String ledstatusupdate;
if (server.hasArg("led")) {
if (server.arg("led") == "off") {
ledstatus = 0;
ledstatusupdate = "The LED has been turned Off<br>";
}
else if (server.arg("led") == "on") {
ledstatus = 1;
ledstatusupdate = "The LED has been turned On<br>";
}
else if (server.arg("led") == "blink") {
ledstatus = 2;
ledstatusupdate = "The LED has been set to Blink<br>";
}
else if (server.arg("led") == "modulate") {
ledstatus = 3;
ledstatusupdate = "The LED has been set to Modulate<br>";
}
}
String s = "";
s += pageheader;
s += htmlhead;
s += bodystyle;
s += "<h1>Welcome to ESP webserver</h1><p>From here you can control your LED making it blink or just turn on or off. ";
s += "</p>";
s += ledstatusupdate;
s += "<br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"on\">"; // the hidden parameter gets included
s += "<input type=\"submit\" value=\" LED ON \"></form><br>"; // the button simply submits the form
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"off\">";
s += "<input type=\"submit\" value=\" LED OFF\"></form><br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"blink\">";
s += "<input type=\"submit\" value=\" BLINK \"></form><br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"modulate\">";
s += "<input type=\"submit\" value=\"MODULATE\"></form><br>";
s += htmlclose;
yield(); // not stricktly neccesary, though the String class can be slow
server.send(200, "text/html", s); //Send web page
}
String StationIP() {
String stationIP = "http://";
stationIP += WiFi.localIP().toString();
return stationIP;
}
and what the results into
Started Access Point "ESPLed"
With password "password"
Attempting to start Station mode
......
Connected to LAB
IP address: 192.168.0.117
MDNS responder started, type espled.local/ in your browser
HTTP server started
dhcps: send_nak>>udp_sendto result 0
dhcps: send_nak>>udp_sendto result 0
dhcps: send_nak>>udp_sendto result 0
dhcps: send_nak>>udp_sendto result 0
Anybody know where these messages come from and / or how to turn them off. I intend to also use all UARTs in some applications and i am not interested in this information.
Yep i found that topic on google , but it refers to the esp8266, on which the issue actually does not occur in this situation. It only shows up on an ESP32. It must be somewhere in the WebServer.h or WiFiClient.h or files they depend on, i'll keep looking.
It is something i found somewhere to make it easier to find the root page of the webserver when connecting to the ESP as an AP.
anyway, in PatchDNSServer.cpp there are 2 lines
#define DEBUG
#define DEBUG_OUTPUT Serial
and commenting those out should do the trick i will scan through some more of the files just to make sure.
In all other implementations i never had Serial enabled i think, though it may even be the cause of some things not functioning properly, for which i could not find a cause. All in all a great find i hope.
That got me. Then i thought that maybe i should have a look to see if there is a newer core available (i was on 1.0.6) and installed 2.0.11 which seems to not squirt out the debug message anymore.
a different issue showed up, where in the ESP8266 core (and esp32 1.0.6) the IP that is available thru station mode is also useable in AP mode, that does not seem to be the case anymore, but a simple '/' in the form has always worked, and i guess was better anyway. I had switched to that recently when creating something where ESP's are actually connecting to each other and that was creating some issue because that station IP address did not work in AP mode. anyway, this seems to all work as expected now, this is the latest code
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
ESP8266WebServer server(80);
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
WebServer server(80);
#endif
#include <PatchDNSServer.h>
DNSServer dnsServer;
#define DNS_PORT 53
const char *ssid = "LAB";
const char *password = "Page22Air";
const char *apname = "ESPLed";
const char *appass = "password"; // minimum length 8 characters
const int ledpin = 2; // I'm running this on an ESP-01
// i have a led connected to this active LOW
// i can't use the internal (pin 1) for the use of Serial
uint8_t ledstatus = 0; // this is keeping track of the state (of our statemachine)
const char // like this these lines are statically declared and const, so we can't change them at all
*pageheader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
*htmlhead = "<html><head><title>ESPwebserver</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></head>",
*bodystyle = "<body style=\"color: wheat; background-color: teal; font-size: 12pt; font-family: sans-serif;\">",
*accessIP = "http://192.168.4.1",
*htmlclose = "</body></html>";
String webIP;
void setup() {
webIP.reserve(30); // prevent fragments
digitalWrite(ledpin, HIGH);
pinMode(ledpin, OUTPUT);
Serial.begin(115200);
WiFi.softAP(apname, appass); // start AP mode
webIP = accessIP;
Serial.print("Started Access Point \"");
Serial.print(apname);
Serial.println("\"");
Serial.print("With password \"");
Serial.print(appass);
Serial.println("\"");
WiFi.begin(ssid, password); // attempt starting STA mode
Serial.println("Attempting to start Station mode");
uint32_t moment = millis();
while ((WiFi.status() != WL_CONNECTED) && (millis() < moment + 8000)) {
delay(500);
Serial.print(".");
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("espled")) { // type esp8266.local/ in your browser
Serial.println("MDNS responder started, type espled.local/ in your browser");
}
webIP = "/"; //StationIP();
}
else if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" Unsuccessful.");
}
else if (WiFi.status() == WL_NO_SSID_AVAIL) {
Serial.print("Network ");
Serial.print(ssid);
Serial.println(" not available.");
}
WiFi.reconnect(); // reconnect AP after attempting to connect STA
dnsServer.start(DNS_PORT, "lab.com", IPAddress(192, 168, 4, 1));
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
dnsServer.processNextRequest();
checkLedStatus();
}
void checkLedStatus() { // our statemachine
switch (ledstatus) {
case 0: {
digitalWrite(ledpin, HIGH);
return;
}
case 1: {
digitalWrite(ledpin, LOW);
return;
}
case 2: {
if (ledBlink(500)) ; // here the return value (of ledBlink() ) gets discarded
return;
}
case 3: {
modulateLed();
return;
}
}
}
void modulateLed() {
static uint16_t ms = 100;
static bool increase = true;
if (!ledBlink(ms)) return;
if (ms > 250) increase = false;
if (ms < 20) increase = true;
if (increase) ms = (ms * 10) / 9;
else ms = (ms * 9) / 10;
}
bool ledBlink(uint32_t wait) {
static bool pinstate = false;
static uint32_t moment = millis();
if (millis() > moment + wait) {
pinstate = !pinstate;
moment = millis();
if (pinstate) digitalWrite(ledpin, LOW);
else digitalWrite(ledpin, HIGH);
return pinstate; // if pinstate is true and the pinstate has changed, the modulator will change speed
}
return false;
}
void handleRoot() {
String ledstatusupdate;
if (server.hasArg("led")) {
if (server.arg("led") == "off") {
ledstatus = 0;
ledstatusupdate = "The LED has been turned Off<br>";
}
else if (server.arg("led") == "on") {
ledstatus = 1;
ledstatusupdate = "The LED has been turned On<br>";
}
else if (server.arg("led") == "blink") {
ledstatus = 2;
ledstatusupdate = "The LED has been set to Blink<br>";
}
else if (server.arg("led") == "modulate") {
ledstatus = 3;
ledstatusupdate = "The LED has been set to Modulate<br>";
}
}
String s = "";
s += pageheader;
s += htmlhead;
s += bodystyle;
s += "<h1>Welcome to ESP webserver</h1><p>From here you can control your LED making it blink or just turn on or off. ";
s += "</p>";
s += ledstatusupdate;
s += "<br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"on\">"; // the hidden parameter gets included
s += "<input type=\"submit\" value=\" LED ON \"></form><br>"; // the button simply submits the form
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"off\">";
s += "<input type=\"submit\" value=\" LED OFF\"></form><br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"blink\">";
s += "<input type=\"submit\" value=\" BLINK \"></form><br>";
s += "<form action=\"";
s += webIP;
s += "\" method=\"get\" name=\"button\">";
s += "<input type=\"hidden\" name=\"led\" value=\"modulate\">";
s += "<input type=\"submit\" value=\"MODULATE\"></form><br>";
s += htmlclose;
yield(); // not stricktly neccesary, though the String class can be slow
server.send(200, "text/html", s); //Send web page
}
String StationIP() {
String stationIP = "http://";
stationIP += WiFi.localIP().toString();
return stationIP;
}
Something like that, looking to find the file now, but i suspect that this may all be pre-compiled as a part of the core.
Did find it in the esp8266 core though.