Hi guys,
I've got a NodeMCU that I'm trying to write a piece of code for in the Arduino IDE. I've got it wired with a DHT22-sensor and a sound sensor (analog). My problem is that the NodeMCU crashes, some times with exceptions. The exceptions and stacks changes between each time. Some times it restarts, other times it seems to stay dead.
Thing is I've tried to troubleshoot this for quite some time now. Braking down the code into smaller codes or using the DHT-examples seems to work perfectly. Bits that I've tested individually:
- DHT reporting
- WiFi and MQTT-connection
- Sound-sensor polling and taking average
The code might be "dirty". I'm a noob and its mostly copy-paste from various examples. I've tried to clean it up and commented it for you guys/myself.
//Library and defining DHT
#include "DHT.h"
#define DHTTYPE DHT22
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
//Library for NodeMCU
#include <ESP8266WiFi.h>
//WiFi setup
const char* ssid = "MYSSID";
const char* password = "MYPASSWORD";
WiFiClient espClient;
//Libraries, setup and topics for MQTT
#include "PubSubClient.h"
const char* mqtt_server = "192.168.1.7";
const int mqtt_port = 1883;
#define temperature_topic "ESP/Storms/temp/temp"
#define lyd10_topic "ESP/Storms/lyd/lyd10"
#define lyd60_topic "ESP/Storms/lyd/lyd60"
PubSubClient client(espClient);
//Variables for temperature-reporting.
long lastMsg = 0; //Storing last sent temp
float temp = 0.0;
float diff = 1.0; //change to a suited value based on max difference in temp that should be accepted
int pinSignal = A0; // pin connected to sound sensor //Sound-sensor connected to A0
void setup ()
{
pinMode (pinSignal, INPUT); // Set the signal pin as input for sound sensor
Serial.begin (115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
dht.begin();
}
void setup_wifi() {
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
//Wait 5 seconds before retrying
delay(5000);
}
}
}
//Check if new temp value can be accepted
bool checkBound(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); //&& newValue != prevValue
}
void loop ()
{
if (!client.connected()) {
reconnect();
}
client.loop();
int level60 = 0;
//Averages the last minute and publishes to MQTT
for (int x = 0; x<6; x++){
int level10 = 0;
//Averages the reads from last 10 seconds and publishes to MQTT
for (int y = 0; y<10; y++){
int level1 = 0;
//Takes 200 polls from sound sensor and stores the sum. 5ms between polls
for (int z = 0; z<20; z++){
level1 = level1 + analogRead(pinSignal);
delay(50);
}
level1 = level1 / 20;
level10 = level10 + level1;
}
level10 = level10 / 10;
level60 = level60 + level10;
client.publish(lyd10_topic, String(level10).c_str(), true);
Serial.print("Lyd siste 10: ");
Serial.println(level10);
//Reporting new temp each minute if it passes checkBound
float newTemp = dht.readTemperature();
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("Ny temperatur rapportert: ");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
}
else {
Serial.print("Skippet rapportering av temp, newTemp: ");
Serial.print(newTemp);
Serial.print(" temp: ");
Serial.println(temp);
}
}
level60 = level60 / 6;
client.publish(lyd60_topic, String(level60).c_str(), true);
Serial.print("Lyd siste 60: ");
Serial.println(level60);
}
Here are some of the decoded exceptions.
Nr. 1
Exception 0: Illegal instruction
PC: 0x4010039a: _umm_malloc at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/umm_malloc/umm_malloc.c line 1354
EXCVADDR: 0x00000000
Decoding stack results
0x40205398: optimistic_yield(uint32_t) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 111
0x4020151a: delay at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 54
0x4020150f: delay at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 51
0x40203d8f: WiFiClient::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/include/DataSource.h line 23
0x40205274: loop_task(ETSEvent*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 134
0x40203d8f: WiFiClient::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/include/DataSource.h line 23
0x40204320: PubSubClient::connect(char const*, char const*, char const*, char const*, unsigned char, unsigned char, char const*) at /Users/Fredrick/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 120
0x402044e0: PubSubClient::publish(char const*, unsigned char const*, unsigned int, unsigned char) at /Users/Fredrick/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 367
0x402048c0: _GLOBAL__sub_I__ZN14HardwareSerialC2Ei() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/HardwareSerial.cpp line 139
0x40204588: PubSubClient::loop() at /Users/Fredrick/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 285
0x40204aa4: Print::print(long, int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.cpp line 140
0x40202ab8: reconnect() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 62
0x40202bc0: loop() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 85
0x40202a14: setup_wifi() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 55
0x40205424: memcpy_P(void*, void const*, size_t) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/pgmspace.cpp line 68
Posting next couple of exceptions in second post.