GPIO Interrupt fails when Wifi enabled
Posted: Mon Apr 11, 2016 4:04 am
I have built a hot water system monitor with a ESP-201 board that measures electric current via a CT clamp, various temperatures via DS18B20 sensors and water usage via a hall effect flow sensor. The data is reported via a simple UDP string to my server. This was written in LUA and works well except I have noticed that any significant water flow causes ESP8266 to reset so I have tried to rebuild the software using the Arduino IDE but I have found the same issue exists. If I have Wifi enabled then the interrupts from the water sensor cause the program to fail.
I have reduced the code to a very basic level to remove any other factors that I think could cause the issue but it still exists. I have simulated the water sensor with a pulse generator running at 10hz (the maximum rate typically seen) connected to the gpio. If I run the test program without the pulse generator connected it runs reliably. If I run it with the pulse generator connected but Wifi disconnected then it is also reliable but if I have Wifi connected then it will fail after a minute or two. Even if I reduce the pulse rate it will still fail but may take longer to do so.
What am I doing wrong?
The program code:
/* Test of interrupt handler */
#include <ESP8266WiFi.h>
volatile unsigned int hwCount = 0;
volatile unsigned int lastHwCount = 0;
volatile unsigned int loopCounter = 0;
void flowCounterCb() {
hwCount++;
}
const char* ssid = "Bathern-1";
const char* password = "xxxxxxxxxxxxxxxxxxx";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(); Serial.flush();
Serial.print("Connecting to "); Serial.flush();
Serial.println(ssid); Serial.flush();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); Serial.flush();
}
Serial.println(""); Serial.flush();
Serial.print("WiFi connected - "); Serial.flush();
Serial.print("IP address: "); Serial.flush();
Serial.println(WiFi.localIP()); Serial.flush();
pinMode(5, INPUT);
attachInterrupt(5, flowCounterCb, RISING);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Loop counter: "); Serial.flush();
Serial.println(loopCounter); Serial.flush();
Serial.print("Interrupt counter: "); Serial.flush();
lastHwCount = hwCount;
Serial.println(lastHwCount); Serial.flush();
Serial.print("IP address: "); Serial.flush();
Serial.print(WiFi.localIP()); Serial.flush();
Serial.print(" Connection status: "); Serial.flush();
Serial.println(WiFi.status()); Serial.flush();
loopCounter++;
for(int y=1; y<=6;y++) {
delay(10000);
Serial.print(y*10); Serial.flush();
Serial.print("-"); Serial.flush();
}
Serial.println(""); Serial.flush();
Serial.println("------------------"); Serial.flush();
}
-----------------------------------------------------------------
Sample output:
Connecting to Bathern-1
........
WiFi connected - IP address: 192.168.200.14
Loop counter: 0
Interrupt counter: 0
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 1
Interrupt counter: 600
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 2
Interrupt counter: 1200
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 3
Interrupt counter: 1800
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 4
Interrupt counter: 2400
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 5
Interrupt counter: 3001
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 6
Interrupt counter: 3601
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 7
Interrupt counter: 4201
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 8
Interrupt counter: 4801
IP address: 192.168.200.14 Connection status: 3
10-20-30-
Exception (0):
epc1=0x402023f8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: sys
sp: 3ffffc30 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffffdd0: 40106ec4 80af1999 00000001 000000ff
3ffffde0: ffffffff 00000020 00000001 00000000
3ffffdf0: 00000000 401016d3 00000000 00000022
3ffffe00: 3fffc200 40106e8c 3fffc258 4000050c
3ffffe10: 40004376 00000030 00000019 ffffffff
3ffffe20: 60000200 00000008 ffffffff 80000000
3ffffe30: 20000000 3fff0878 80000000 2007c1c0
3ffffe40: 00000000 3fffc6fc 60000600 3fff087c
3ffffe50: 00000134 0007c1c0 60000600 00000030
3ffffe60: 00000000 00000000 00000000 40102dca
3ffffe70: 40102e2e 00080000 3fffc200 00000022
3ffffe80: 00000000 3ffea0b4 00000000 4000050c
3ffffe90: 00000000 00000000 0000001f 40105b55
3ffffea0: 4000050c 00000000 00000db2 00000000
3ffffeb0: 40000f68 00000030 0000001a ffffffff
3ffffec0: 40000f58 00000000 00000020 00000000
3ffffed0: 3ffe8b50 4020600f 3ffe8980 00000000
3ffffee0: 00000000 00000073 60000600 3fffdab0
3ffffef0: 00000000 3fffdcc0 3ffe8bb0 00000030
3fffff00: 00000000 400042db 3ffe8ca1 60000600
3fffff10: 40004b31 3fff06bc 000002f4 0007c000
3fffff20: 40105ea2 3ffee3e0 3ffecdf0 4010724c
3fffff30: 40209055 3ffecdf0 3ffee3e0 1f2ff11b
3fffff40: 3fff06bc 00001000 402094ea 00000008
3fffff50: 4021be28 40105c27 40209597 3ffecea4
3fffff60: 3ffee3e0 40222f69 3ffee3b8 3ffee3e0
3fffff70: 40222f69 60000600 00000000 00000020
3fffff80: 40222fae 3fffdab0 00000000 3fffdcb0
3fffff90: 3ffee3f0 00000000 40000f65 3fffdab0
3fffffa0: 40000f49 40000f49 3fffdab0 40000f49
<<<stack<<<
ets Jan 8 2013,rst cause:1, boot mode:(3,7)
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld
Connecting to Bathern-1
........
I have reduced the code to a very basic level to remove any other factors that I think could cause the issue but it still exists. I have simulated the water sensor with a pulse generator running at 10hz (the maximum rate typically seen) connected to the gpio. If I run the test program without the pulse generator connected it runs reliably. If I run it with the pulse generator connected but Wifi disconnected then it is also reliable but if I have Wifi connected then it will fail after a minute or two. Even if I reduce the pulse rate it will still fail but may take longer to do so.
What am I doing wrong?
The program code:
/* Test of interrupt handler */
#include <ESP8266WiFi.h>
volatile unsigned int hwCount = 0;
volatile unsigned int lastHwCount = 0;
volatile unsigned int loopCounter = 0;
void flowCounterCb() {
hwCount++;
}
const char* ssid = "Bathern-1";
const char* password = "xxxxxxxxxxxxxxxxxxx";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(); Serial.flush();
Serial.print("Connecting to "); Serial.flush();
Serial.println(ssid); Serial.flush();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); Serial.flush();
}
Serial.println(""); Serial.flush();
Serial.print("WiFi connected - "); Serial.flush();
Serial.print("IP address: "); Serial.flush();
Serial.println(WiFi.localIP()); Serial.flush();
pinMode(5, INPUT);
attachInterrupt(5, flowCounterCb, RISING);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Loop counter: "); Serial.flush();
Serial.println(loopCounter); Serial.flush();
Serial.print("Interrupt counter: "); Serial.flush();
lastHwCount = hwCount;
Serial.println(lastHwCount); Serial.flush();
Serial.print("IP address: "); Serial.flush();
Serial.print(WiFi.localIP()); Serial.flush();
Serial.print(" Connection status: "); Serial.flush();
Serial.println(WiFi.status()); Serial.flush();
loopCounter++;
for(int y=1; y<=6;y++) {
delay(10000);
Serial.print(y*10); Serial.flush();
Serial.print("-"); Serial.flush();
}
Serial.println(""); Serial.flush();
Serial.println("------------------"); Serial.flush();
}
-----------------------------------------------------------------
Sample output:
Connecting to Bathern-1
........
WiFi connected - IP address: 192.168.200.14
Loop counter: 0
Interrupt counter: 0
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 1
Interrupt counter: 600
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 2
Interrupt counter: 1200
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 3
Interrupt counter: 1800
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 4
Interrupt counter: 2400
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 5
Interrupt counter: 3001
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 6
Interrupt counter: 3601
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 7
Interrupt counter: 4201
IP address: 192.168.200.14 Connection status: 3
10-20-30-40-50-60-
------------------
Loop counter: 8
Interrupt counter: 4801
IP address: 192.168.200.14 Connection status: 3
10-20-30-
Exception (0):
epc1=0x402023f8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
ctx: sys
sp: 3ffffc30 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffffdd0: 40106ec4 80af1999 00000001 000000ff
3ffffde0: ffffffff 00000020 00000001 00000000
3ffffdf0: 00000000 401016d3 00000000 00000022
3ffffe00: 3fffc200 40106e8c 3fffc258 4000050c
3ffffe10: 40004376 00000030 00000019 ffffffff
3ffffe20: 60000200 00000008 ffffffff 80000000
3ffffe30: 20000000 3fff0878 80000000 2007c1c0
3ffffe40: 00000000 3fffc6fc 60000600 3fff087c
3ffffe50: 00000134 0007c1c0 60000600 00000030
3ffffe60: 00000000 00000000 00000000 40102dca
3ffffe70: 40102e2e 00080000 3fffc200 00000022
3ffffe80: 00000000 3ffea0b4 00000000 4000050c
3ffffe90: 00000000 00000000 0000001f 40105b55
3ffffea0: 4000050c 00000000 00000db2 00000000
3ffffeb0: 40000f68 00000030 0000001a ffffffff
3ffffec0: 40000f58 00000000 00000020 00000000
3ffffed0: 3ffe8b50 4020600f 3ffe8980 00000000
3ffffee0: 00000000 00000073 60000600 3fffdab0
3ffffef0: 00000000 3fffdcc0 3ffe8bb0 00000030
3fffff00: 00000000 400042db 3ffe8ca1 60000600
3fffff10: 40004b31 3fff06bc 000002f4 0007c000
3fffff20: 40105ea2 3ffee3e0 3ffecdf0 4010724c
3fffff30: 40209055 3ffecdf0 3ffee3e0 1f2ff11b
3fffff40: 3fff06bc 00001000 402094ea 00000008
3fffff50: 4021be28 40105c27 40209597 3ffecea4
3fffff60: 3ffee3e0 40222f69 3ffee3b8 3ffee3e0
3fffff70: 40222f69 60000600 00000000 00000020
3fffff80: 40222fae 3fffdab0 00000000 3fffdcb0
3fffff90: 3ffee3f0 00000000 40000f65 3fffdab0
3fffffa0: 40000f49 40000f49 3fffdab0 40000f49
<<<stack<<<
ets Jan 8 2013,rst cause:1, boot mode:(3,7)
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld
Connecting to Bathern-1
........