ESP8266 freezing up

Hey team. I built a “Doggie hotel” that controls a couple of servos. One to open a gate and another the dispenses food. I’m using a simple webserver to control both of these with the addition of a button to dispense foot and it was working great… but as of late it doesn’t seem to keep looping. I lose the functionality of the webserver and even the button quits working. Most the time doing a reset fixes it but that is such a pain. I was hoping someone could look over my code and let me know if I have something that could be causing me issues. I’m using a ESP8266 board. This is hooked to a breadboard and powered on the VIN (9volts). In addition the servos are getting their power from the same power supply.

Here is my code:

#include <ESP8266WiFi.h>
#include <Servo.h>
Servo feederservo; // create servo object to control a servo for the feeder
Servo gateservo; // create servo object to control a servo for the gate
int pos = 0; // variable to store the servo position
#define WIFI_SSID “WiFi Network”
#define WIFI_PASS “1234567890”
WiFiServer server(80);

void wifiSetup() {

// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);

// Connect
Serial.printf("[WIFI] Connecting to %s “, WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(”.");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());

}

void setup(){
//start serial connection
Serial.begin(115200);
delay(10);
pinMode(D3, INPUT_PULLUP); //Gpio-0(D3) Button input - Feed Crunchers
feederservo.attach(D4); //Gpio-2(D4) of nodemcu with pwm pin of servo motor
gateservo.attach(D1); //Gpio-5(D1) of nodemcu with pwm pin of servo motor
wifiSetup();
server.begin();
Serial.println(“Server started”);
feederservo.write(180);
gateservo.write(180);
}

void loop() {
int sensorVal = digitalRead(0);

if (sensorVal == LOW) {
digitalWrite(1, HIGH);
feederservo.write(180);
delay(300);
feederservo.write(0);
delay(900);
feederservo.write(180);
delay(300);
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

Serial.println(“new client”);
while(!client.available()){
delay(1);
}

String request = client.readStringUntil(’\r’);
Serial.println(request);
client.flush();

int value = 0;

if (request.indexOf("/Req=0") != -1) {
digitalWrite(1, HIGH);
feederservo.write(180);
delay(300);
feederservo.write(0);
delay(900);
feederservo.write(180);
delay(300);
}

if (request.indexOf("/Req=1") != -1) {
digitalWrite(1, HIGH);
gateservo.write(180);
delay(300);
gateservo.write(90);
delay(3000);
gateservo.write(180);
delay(300);
}
else {
digitalWrite(1, LOW);

}

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println("");
client.println("");
client.println("");
client.println("

Cocoa Hotel

“);
client.println(”

“);
client.println(”<a href="/Req=0"">Send the Crunchers");
client.println("<a href="/Req=1"">Free Cocoa");
client.println("");
delay(1);
Serial.println(“Client disonnected”);
Serial.println("");

}

  1. Please go read the forum guide in the sticky post a the top of any section of the forum.
  2. Modify your post above and correct it for the forum rules you broke and add the information we are going to need in order to help you. If you don't know what those things are, see 1) above.

And let 'us' know if the ESP8266 has the freeRTOS OS?

You don't say which ESP8266 board you are using.
I'd be concerned about using any ESP8266 board from a 9v supply. It may be frying rather than freezing!?
Check it with your finger - you may find the voltage regulator is HOT

If you feed a ESP8266-board with 9V 9V - 3.3V = 5.7V * ampere must be disipated as heat in the voltage-regulater
It will be much better if you use 5V. But the voltage-regulator should be able to deliver 1A minimum and you should add a big electrolytic capacitor (2200 µF in parallel to the 3.3V and ground-pin.

ESP8266 have current-spikes when WLAN starts sending. This can cause voltage-drops if the power-supply is too weak.

if the ESP8266 freezes or not can be easily checked by a serial-output that is added to your loop
This serial output should be called regularly but not in every single execution of the loop.

Another source of bugs is using variables of type String.

variables of type String eat up all memory over time and start overwriting other variables.
This might cause the freezing.

Replace the variable request through directly printing the result
instead

Serial.println(client.readStringUntil('\r'));

or with a char-array or use a PString. PStrings are safe to use.
Even if you try to store a longer string than the PString can hold it just gets truncated
But a PString will never eat up memory or overwrite other variables

best regards Stefan

This is a bad idea:

while(!client.available()){
    delay(1);
  }

If you are using a ESP8266 with freeRTOS then the Arduino String thing issues is not an ESP8266 String thing issue as freeRTOS does cleanup of the String things.

Hi idaho-walker can you explain a bit more about the FreeRTOS?

Does this mean FreeTROS is a firmware for ESP8266 that enables uploading any kind of Arduino-sketch in combination with realtime-Operating-System capabilities like a guaranted maximum latency-time?

best regards Stefan

There are two flavors of ESP8266, one with and one without OS.

ESP8266 NON-OS API https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf

ESP8266 OS API API Reference — ESP8266 RTOS SDK Programming Guide documentation

freeRTOS API: FreeRTOS API categories

I only use ESP32’s. For the ESP32, that means being able to upload a properly coded Arduino-sketch in combination with a realtime-OS that ‘guarantees’ their being a maximum latency time. Latency time with free RTOS can be defined in several ways as well as using “equal to portMaxDelay”. On the ESP32 freeRTOS is built into the firmware and does not consume any program memory space from loading a library.

I was checking this morning a similar problem with my WiFi Nina WiFi module on the Uno-Rev2.
This link https://github.com/bblanchon/cpp4arduino/blob/master/HeapFragmentation/HeapFragmentation.ino
lets you check if you really have a heap fragmentation issue.
Second thing I would check (on top of the very good advice given by other forum members) : add a blinking LED to your sketch to know if you have a blocked processor something else.
The point in these kind of problems is, as long as you cannot narrow down the area’s where the problems is situated it can be looking for a needle in a hay stack. Some breadboards are in many cases not very reliable. Are you sure that the connections are still ok?

Best Regards,
Johi
www.SylvesterSolutions.com.

All of those long delay() calls are a REALLY bad idea on any 8266. They WILL trigger watchdog resets. You have to learn to avoid using delay, unless for VERY short periods (like single digit mSec).

If one is using the ESP8266 OS freeRTOS then delays, using vTaskDelay(x) is non-blocking. freeRTOS will switch to another task during the vTaskDelay(x) time.