I am having a very strange problem that I cannot figure out and I am out of options of what even to search.
I am using a ESP8266 to connect to my wifi router, and act as a web server that executes one of two functions based on the request. The issue is it sometimes works perfectly, and sometimes it doesn’t execute the functions. When it doesn’t work, I make the request and see output in the Arduino Serial Monitor, which indicates that the internet connection works and the ESP8266 is receiving something from the internet, but it doesn’t execute the functions. It can go from working to not working within the same hour, with no change to anything except unplugging the board and plugging it in again.
What can I do to make it work consistently?
Arduino IDE Serial Monitor output:
- Failing case: When I make one network request and it doesn’t execute the function
WS:ac
:ref 1
WS:av
:ref 2
:ur 2
New client
:ref 2
:ur 2- Successful case: When I make the same network request and it does execute the function
WS:ac
:rn 247
:ref 1
WS:av
:ref 2
:ur 2
New client
:ref 2
:ur 2
request: GET /ledOff HTTP/1.1
method: GET url: /ledOff search:
headerName: Content-type
headerValue: application/json
headerName: host
headerValue: nataliaandandy.hopto.org:8880
headerName: x-newrelic-id
headerValue: VwAOU1RRGwAFUFZUAwQE
headerName: x-newrelic-transaction
headerValue: PxQCVANSXlVSVwJVBVJUVkYdUFIOFQZOElcNBwleUlAABg5TUgIDQEgUUQMDW1kEVQZDPw==
headerName: Connection
headerValue: close
:c0 1, 247
args:
args count: 0
args:
args count: 0
Request: /ledOff
Arguments:
final list of key/value pairs:
LED off page
:wr 83 0
:wrc 83 83 0
:wr 10 0
:wrc 10 10 0
:ack 83
:rcl
:abort
:ur 1
WS:dis
:del
- When I restart the board, I get this output on startup for both success and failing cases:
.....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
cnt
connected with MyWifiSSID, channel 1
ip:192.168.1.176,mask:255.255.255.0,gw:192.168.1.1
ip:192.168.1.176,mask:255.255.255.0,gw:192.168.1.1
wifi evt: 0
wifi evt: 3
.
WiFi connected
HTTP server started
192.168.1.176
pm open,type:2 0
My setup:
Hardware:
- ESP8266 NodeMCU CP2102 ESP-12E. Purchased at https://www.amazon.com/gp/product/B07HF ... UTF8&psc=1
- Mac running macOS Catalina
- Servo motor MG90S
Software:
- Arduino IDE v1.8.12
- Using SiLabsUSBDriverDisk for the Mac to discover the ESP8266 as described here: https://cityos-air.readme.io/docs/1-mac ... or-nodemcu
Code
// Import required libraries
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <Servo.h>
//Static IP address configuration
IPAddress staticIP(192, 168, 1, 176); //ESP static ip
IPAddress gateway(192, 168, 1, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(8, 8, 8, 8); //DNS
const char* deviceName = "deviceName.com";
//On board LED Connected to GPIO2
#define LED 2
// WiFi parameters
const char* ssid = "MyWifiSSID";
const char* password = "MyWifiPassword";
////Declare a global object variable from the ESP8266WebServer class.
ESP8266WebServer server(80); //Server on port 80
Servo servo;
void handleLEDon() {
Serial.println("LED on page");
digitalWrite(LED,LOW); //LED is connected in reverse
delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
servo.write(0);
server.send(200, "text/html", "LED is ON"); //Send ADC value only to client ajax request
}
void handleLEDoff() {
Serial.println("LED off page");
digitalWrite(LED,HIGH); //LED off
delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
servo.write(120);
server.send(200, "text/html", "LED is OFF"); //Send ADC value only to client ajax request
}
void setup(void)
{
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
// Start Serial
Serial.begin(115200);
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname(deviceName); // DHCP Hostname (useful for finding device for static lease)
WiFi.config(staticIP, subnet, gateway);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.on("/ledOn", handleLEDon); //as Per <a href="ledOn">, Subroutine to be called
server.on("/ledOff", handleLEDoff);
server.begin(); //Start server
Serial.println("HTTP server started");
// Print the IP address
Serial.println(WiFi.localIP());
servo.attach(14); //GPIO15 = D5
}
void loop() {
server.handleClient(); //Handle client requests
delay(20); // If the issue is caused by the watchdog timer, adding small delays fix the issue
}
Arduino IDE Settings:
- Preferences > Additional Boards Manager URLs > http://arduino.esp8266.com/stable/packa ... index.json
- Tools > Board > Board Manager > esp8266 by ESP8266 Community version 2.6.3 installed
- Tools > Board > NodeMCU 1.0 (ESP-12E Module)
- Tools > Upload Speed > 115200
- Tools > CPU Frequency > 80 MHz
- Tools > Flash Size > 4MB (FS:2MB OTA:~1019KB)
- Tools > Debug port > Serial
- Tools > Debug Level > SSL+TSL_MEM+HTTP_CLIENT+HTTP_SERVER+CORE+WIFI+HTTP…UPDATE+UPDATER+OTA+OOM+MDNS
- Tools > IwIP Variant > v2 Lower Memory
- Tools > VTables > Flash
- Tools > Exceptions > Legacy (new can return nullptr)
- Tools > Erase Flash > Only Sketch
- Tools > SSL Support > All SSL ciphers (most compatible)
- Tools > Port > /dev/cu.SLLAB_USBtoUART
Power:
I am using the usb input to power the board. I read that if there is not enough power supplied to the board, it could behave strangely. I have powered the board from my computer’s USB, and a usb power brick that outputs 5V/2A. With both these power sources, the board worked and didn’t work at different times, which makes me think power isn’t the issue. Isn’t 5V/2A enough?
Internet connection:
I don’t think this is the issue, since the board is logging something in the Serial Monitor when I make the request, indicating it is connected to the internet properly. However, I will add the setup here just in case.
I am setting a static IP for the ESP8266 in code. I setup port forwarding on my router and can see the port is open using https://www.portchecktool.com/. I then setup a DDNS using NoIp.com. I then use IFFF to trigger a request to the domain name on a “Button Widget” tap.