Can't troubleshoot my code. Every part seems to work separately. NodeMCU

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.

Nr. 2

Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
PC: 0x4021636d
EXCVADDR: 0xff3fff2a

Decoding stack results
0x4020525f: loop_task(ETSEvent*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 133
0x4020b995: glue2esp_linkoutput at glue-esp/lwip-esp.c line 299
0x4020bcea: new_linkoutput at glue-lwip/lwip-git.c line 259
0x40202f00: DHT::read(bool) at /Users/Fredrick/Documents/Arduino/libraries/DHT_sensor_library/DHT.cpp line 191
0x4020bcea: new_linkoutput at glue-lwip/lwip-git.c line 259
0x4020c07f: ethernet_output at netif/ethernet.c line 305
0x4020c088: ethernet_output at netif/ethernet.c line 305
0x40211e94: etharp_output_to_arp_index at core/ipv4/etharp.c line 768
0x4020a04c: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c line 180
0x402120ec: etharp_output_LWIP2 at core/ipv4/etharp.c line 882
0x402052a4: esp_yield() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 91
0x4020150f: delay at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 51
0x40202e7e: DHT::read(bool) at /Users/Fredrick/Documents/Arduino/libraries/DHT_sensor_library/DHT.cpp line 144
0x40212ad8: ip4_output_if_opt at core/ipv4/ip4.c line 788
0x40212afe: ip4_output_if at core/ipv4/ip4.c line 761
0x402131f7: ip_chksum_pseudo at core/inet_chksum.c line 395
0x4021023a: tcp_output at core/tcp_out.c line 1123
0x4010020c: _umm_free at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/umm_malloc/umm_malloc.c line 1295
0x401006dc: free at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/umm_malloc/umm_malloc.c line 1755
0x401065ee: millis at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 183
0x402047cc: HardwareSerial::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/HardwareSerial.h line 159
0x4020499d: Print::write(char const*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.h line 60
0x401065ee: millis at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 183
0x40204a89: Print::printNumber(unsigned long, unsigned char) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.cpp line 260
0x402042c8: PubSubClient::write(unsigned char, unsigned char*, unsigned short) at /Users/Fredrick/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 463
0x402047cc: HardwareSerial::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/HardwareSerial.h line 159
0x4020499d: Print::write(char const*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.h line 60
0x40204ae7: Print::print(long, int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.cpp line 153
0x40202fd0: DHT::readTemperature(bool, bool) at /Users/Fredrick/Documents/Arduino/libraries/DHT_sensor_library/DHT.cpp line 36
0x40202c54: loop() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 118
0x40202a14: setup_wifi() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 55

Nr. 3

Exception 0: Illegal instruction
PC: 0x400fde9b
EXCVADDR: 0x00000000

Decoding stack results
0x40203350: ESP8266WiFiSTAClass::status() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp line 498
0x402034cd: ESP8266WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp line 178
0x402034a9: ESP8266WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp line 168
0x402034d3: ESP8266WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp line 178
0x402047cc: HardwareSerial::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/HardwareSerial.h line 159
0x4020499d: Print::write(char const*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.h line 60
0x402052a4: esp_yield() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 91
0x4020150f: delay at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 51
0x402029e7: setup_wifi() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 49
0x40202a5d: setup() at /Users/Fredrick/Documents/Arduino/Lydsensor_analog/Lydsensor_analog.ino line 36

Nr. 4

Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
PC: 0x401065d2: millis at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 180
EXCVADDR: 0x401065c0: millis at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 180

Decoding stack results
0x401065c0: millis at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_wiring.c line 180
0x402035db: WiFiClient::available() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/libraries/ESP8266WiFi/src/WiFiClient.cpp line 219
0x40204410: 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 180
0x402047cc: HardwareSerial::write(unsigned char const*, unsigned int) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/HardwareSerial.h line 159
0x40204494: PubSubClient::connect(char const*) at /Users/Fredrick/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.cpp line 106
0x402049b0: Print::print(char const*) at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Print.cpp line 122
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
0x40205330: loop_wrapper() at /Users/Fredrick/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/core_esp8266_main.cpp line 125