ESP8266 Do more than "handleClient" in loop

Hi! Move it if i posted this in the wrong section!

I am experimenting with the ESP8266, my current project contains a Web App and a microphone input. The problem i am having right now is that when i add more code to the loop method, the webserver goes offline. Is this because the chip is too slow, or is there some well known way to go about this?

You can have more stuff in loop as long as it doesn't block for too long. But IIRC there is a limit to how long you can block on an 8266 before it shuts down the Internet side of things.

The 8266 has a watchdog timer that is enabled by default. Spend too much time in blocking code that does not call delay() or some other "system" function, and the 8266 gets reset by the watchdog, and a stack trace, clearly labeled as resulting from a watchdog reset, will be printed to the Serial port when the reset occurs. Other than that, there is no reason you should not be able to do a LOT of processing in loop();

Regards, Ray L.

Hi, I'm also experimenting with a NodeMCU (also with an ESP8266) and I'm probably a novice/intermediate programmer.

I'm able to get the device to run sketches that are designed to do one thing, i.e. blink an LED, run SparkFun Battery Babysitter communication, read from an acceleratorometer, etc.

Where I'm stuck is how to I combine these different functions into one sketch?

I have yet to setup the NodeMCU with Wifi so I can OTA upload sketches while the serial port reader can show me the battery charge level.

Thanks for any help.

Where I'm stuck is how to I combine these different functions into one sketch?

Step 1 is to fully understand the code you have and how it works. Helps if you know the language a bit. That might require a little study. But a hack-and-paste coder will always have trouble trying to combine codes. If you're going to be a hack-and-poster then you're sort of limited to what you can hack-and-paste.

The ESP8266 will trip its watchdog timer after 250 ms iirc. It's easy to prevent by having your loop go fast enough, or by adding a delay(0) or yield() commands in your code, whenever you feel is a good point to allow some time for the network stack to run (note: it's not guaranteed how long this takes, so don't do this in time sensitive parts of your code).

To combine sketches: put the code of each sensor in its own function, then call those functions one by one from loop().

wvmarle: To combine sketches: put the code of each sensor in its own function, then call those functions one by one from loop().

Thanks! I forgot about the feature! For reference, here's the link to the tutorial: https://www.arduino.cc/en/Reference/FunctionDeclaration

feature? LOL

Juraj: feature? LOL

Well, wouldn't want to call it a bug, so it must be a feature!

Juraj: feature? LOL

Common guys, give a newbie a break. :)

I still can't get it to work :/ If i got it right i have to let the networking do things by calling 'delay(0)' or 'yield()'. But the network still goes offline almost imideatly. Here is my loop method.

void loop ( void ) {
    server.handleClient();

    unsigned long currentMillis = millis();
    sensorvalue = analogRead(A0);
    yield();

    if (sensorvalue >= sensorthreshold) {
        if (currentMillis > lastSampleTime + minTimeBetweenClaps) {
            lampOn = !lampOn;
            digitalWrite(lampPin, lampOn);

            lastSampleTime = currentMillis;
            yield();
        }
    }
}

Note: it still works perfectly if i don't have any code after the handleClient call.

Post the rest of the code. When it doesn't make any sense why this piece of code that should work doesn't, always consider the rest of the code.

The circuit too. What is this "lamp" and how are you powering it?

That's indeed the same thing I was thinking: what is this lamp? How much power does it draw? How does it get that power?

What happens if you do not connect the lamp, just run the code? If that runs fine, it's a clear power or interference issue.

The lamp is just a relay, the pin goes to a BC637 Transistor, which activates a relay. So the power draw shouldn't really be to big, i can of course test it anyway. Here's the rest of the code, it was too long to embed, sho here's a pastebin :[] https://pastebin.com/HhbMi0ne

The code that you posted: does that show the problem? If so, that's great, and there's no need for the rest of the code. It's the smallest example that demonstrates the problem, which is exactly what you should post.

For your connections: is there a resistor between the Arduino and the base of that transistor? If not, you're probably overloading the pin.

Where does the power for the relay come from? Same power supply as the Arduino? Is it rated to supply more than the Arduino + relay together? Do you have a flyback diode on the relay?

You can probably answer all this by simply posting a complete circuit diagram (so no Fritzy spaghetti diagram) of your circuit. That's where almost certainly the problem can be found.

Here’s the schematic.

jakobst:
Here's the schematic.

Inlined for convenience.
Nothing spectacularly wrong, there, though I do miss capacitors around the regulator. That may be the problem: the power dip when you switch on the relay may cause the ESP to misbehave. The capacitors are there to dampen those spikes.

A few minor issues:
GPIO2 connected to Vcc: not needed, the internal pull-up is enabled upon startup. Also it's much safer to add resistors here (like 10k) so you don't kill the port if you set it to output, low.

Likewise for GPIO15: this needs the pull-down, but better add a resistor, again for protection (in case you set it to output, high), and then you can also actually use this pin. It goes great as output for a MOSFET driver, as this normally also needs a pull-down.

I changed the "lampPin" to GPIO16, where nothing is connected. The problem still persists. So i suspect that it has nothing to do with the hardware?

wvmarle: Inlined for convenience. Nothing spectacularly wrong, there, though I do miss capacitors around the regulator. That may be the problem: the power dip when you switch on the relay may cause the ESP to misbehave. The capacitors are there to dampen those spikes.

A few minor issues: GPIO2 connected to Vcc: not needed, the internal pull-up is enabled upon startup. Also it's much safer to add resistors here (like 10k) so you don't kill the port if you set it to output, low.

Likewise for GPIO15: this needs the pull-down, but better add a resistor, again for protection (in case you set it to output, high), and then you can also actually use this pin. It goes great as output for a MOSFET driver, as this normally also needs a pull-down.

Like a 100nF cap? There is a bigger one just after the step-down btw.

More like 100 nF ceramic + 10µF (or greater) tantalum or electrolytic, both sides of the regulator. See data sheet.

For the ESP8266 it's recommended to have 100 nF + 100µF at the power pins. Mind that especially for the small filtering caps location matters (as close as possible to the pins) and you can't have too many of them.