I'm planning to use an ESP8266 (Wemos D1 Mini) to use as a software SPI monitor to send over WiFi.
As a test, I've done a cut-down sketch that just counts the interrupts, a simple frequency counter. I've made it as simple as possible but it won't stay connected to WiFi and crashes after about 10 seconds.
The input signal to CLKPin is 6667 Hz.
Here's the sketch:
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
long nextPrint = 1000; // when to print the next message to serial
const int CLKPin = 5; // Pin connected to CLK
volatile long ClkCount = 0; // number of times clock interrupt has fired
void CLK_ISR() {
ClkCount++;
}
void setup() {
Serial.begin(115200);
long startTime = millis();
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to wifi");
attachInterrupt(CLKPin, CLK_ISR, RISING);
//Set the CLK-pin to input
pinMode(CLKPin, INPUT);
}
void loop() {
if (millis() > nextPrint) {
Serial.print("Freq is: ");
Serial.println(ClkCount);
ClkCount = 0;
nextPrint += 1000;
}
yield();
}
It always gets in a reboot/crash loop (no idea why the first few counts are wrong - I guess the ESP isn't ready yet):
Connecting to SSID ...
.Connected to wifi
Freq is: 1
Freq is: 1
Freq is: 0
Freq is: 0
Freq is: 1
Freq is: 0
Freq is: 2313
Freq is: 6667
Freq is: 6666
Freq is: 6667
Freq is: 6667
Freq is: 6667
Freq is: 6667
Freq is: 6667
Freq is: 6667
Freq is: 6667
Exception (0):
epc1=0x40202034 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: sys
sp: 3ffffc40 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffffde0: 401067da 00000026 00000000 00000001
3ffffdf0: c0017025 3ffe8d04 3ffee188 00000001
3ffffe00: 00000003 00000008 3ffee99c 00000022
3ffffe10: 3fffc200 401067a0 3fffc258 4000050c
3ffffe20: 40004384 00000030 00000012 ffffffff
3ffffe30: 60000200 00000006 30043003 80000000
3ffffe40: 20000000 3fff0c38 80000000 203fc120
3ffffe50: 00000000 3fffc6fc 00000001 3fff0c3c
3ffffe60: 00000154 003fc120 60000600 00000030
3ffffe70: 40106845 00000080 00000022 3fffc200
3ffffe80: c0017025 3fffc258 4000050c 00000022
3ffffe90: 3fffc200 401067a0 3fffc258 00000022
3ffffea0: 3fffc200 401067a0 3fffc258 4000050c
3ffffeb0: 40202b20 00000030 00000012 ffffffff
3ffffec0: 40000f49 3ffee9e8 00000000 3fffd9d0
3ffffed0: 00000000 00000000 00000000 fffffffe
3ffffee0: ffffffff 3fffc6fc 00000001 3fffdab0
3ffffef0: 00000000 3fffdad0 3ffee9e8 00000030
3fffff00: 3ffee99c 0000000a 3ffe8bc8 402094cc
3fffff10: 00000000 400042db 3ffeff84 40104580
3fffff20: 40004b31 3fff0b84 000001f4 003fc080
3fffff30: 40105aac 000001f4 3ffed190 401004f4
3fffff40: 40106e4d 3fff0b84 00000000 00f8676f
3fffff50: 3ffe8bc8 3ffe8d04 3ffed190 40224d02
3fffff60: 40223d49 40223d52 3ffed004 3ffee1b0
3fffff70: 40228445 3ffed004 3ffee1b0 00f857fe
3fffff80: 4022848a 3fffdab0 00000000 3fffdcb0
3fffff90: 3ffee1c8 3fffdad0 3ffee9e8 40202b43
3fffffa0: 40000f49 40000f49 3fffdab0 40000f49
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
What am I doing wrong, or is it just not possible to stay connected to WiFi while using interrupts on an ESP8266?
nextPrint += 1000;is nonsense. UsenextPrint = millis() + 1000;if you need to capture the ClkCount for an interval of a second. I'm pretty sure that's the reason for the low values at the beginning. In addition to what @jwh20 said: I'm not convinced that the Watchdog is the cause, I think another Error (perhaps caused by the WIFI lib) crashed the ESP and then the Watchdog did it's job and performed a reset. But it's a guess, jwh20 might be right, so try what he/she wrote.yield()would work.