Go Down

Topic: NodeMCU crashes, probably in loop (Read 104 times) previous topic - next topic

darthvader072

Sep 16, 2020, 02:25 pm Last Edit: Sep 16, 2020, 02:25 pm by darthvader072
Hi all,

I was working on my mobile outdoor WiFi lamp, and recently added a LDR (light sensor) to determine if it's bright or dark outside (auto ON/OFF).

However, I made in my loop some statements;
First of all - Light measurement, is it brighter then X then dark = false, else dark = true.

Also in my loop: if dark is true, execute the program dark (which will start a millis to check if it's dark for 10 minutes) and the other way, if dark is false, execute the program light (which will start a millis to check f it's bright for 10 minutes).

With the millis I can be certain that it really is dark or not.
However, as soon as I boot my NodeMCU, it crashes at the start of the loop.

So I'm curious, should I do it in a different way? I did some things, but below is my current code ( at the start of the loop you can find the mentioned statements). SORRY: I HAD TO DELETE QUITE A LOT TO FIT THE 9000char. FOR FULL CODE, PLEASE ASK

First I had a interval to check the light-- every 5 minutes or something, but it was not saving any power (which I mostly was worried about), and less precise as a constant light check. That's why it is disabled for now, and, if you ask me, forever..

code:
Code: [Select]


#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>
#include <EEPROM.h>

#include "secret.h"

#define LED_PIN     6
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    3
CRGB leds[NUM_LEDS];


/* BOOLEANS */
bool candleAllowance = false;
bool connectionLive = false;
bool dark = false;
bool light = false;
bool stateIdle = false;
bool stateAvailable = false;
bool stateOrdered = false;
bool stateOff = true;
/* END */

/* LIGHT SENSOR */
int lightSensor;
long makingSureItIsDark = 0;
long makingSureItIsLight = 0;

int sensorState = LOW;
unsigned long last_time = 0L ;
unsigned long intervalSensor = 10000;
/* END */

void setup() {

  pinMode(ledPin, OUTPUT);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.clear(true);
  // FastLED.setBrightness( BRIGHTNESS );

  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  delay(100);
  Serial.println();
  delay(500);
  Serial.println(" ***** NEW SESSION STARTED ***** ");
  delay (500);
  Serial.println("THE BULB. terraslamp -- V01 2020");
  Serial.println("Setting up network now, hold your drinks high..!");
  Serial.println();
  delay(100);
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress());
  delay(100);
  WiFi.mode(WIFI_STA);
  Serial.print("Connecting");
  WiFi.begin(ssid, pass);
  //WiFi.config(staticIP, gateway, subnet);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    //status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:  **updated later to 1s -- so far no issues to report.
    delay(1000);
  }

  Serial.println("Yes! We are connected to WiFi! Cheers all!");
  Serial.println("Connection info:");
  connectionLive = true;
  printWifiStatus();
  Serial.printf("UDP server on port %d\n", localPort);
  Serial.println();
  Serial.println("Almost there, finalizing boot process.. Setting Table ID...");

  //Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);

  stateOff = false;
  stateAvailable = true;

  EEPROM.begin(512);

  delay(250);
  Serial.println("Device is ready to use!");
  Serial.println();
}

void loop() {


  connectionStatus();   //CHECK CONNECTION EVERY 10S
  startTime = millis();
  if ((startTime - comparisonTime) > 10000) //every 10s
  {
    comparisonTime = startTime;
    TestWiFiConnection(); //test connection, and reconnect if necessary
  }
  // END

  if (connectionLive == true) {

     if (lightSensor < 550 ){
        dark = true;
      }
     else (lightSensor > 550){
        dark = false;
     }
     

    if (dark == false) {
      checkSensorDark();
    }

    if (dark == true) {
      checkSensorLight();
    }

    // Read packet
    int packetSize = Udp.parsePacket();
    if (packetSize) {
      if (debug == true) {
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remoteIp = Udp.remoteIP();
        Serial.print(remoteIp);
        Serial.print(", port ");
        Serial.println(destinationPort);
      }
      // Place it in buffer
      int len = Udp.read(packetBuffer, 255);
      if (len > 0) {
        packetBuffer[len] = 0;
      }
      if (debug == true) {
        Serial.print("Command: ");
        Serial.print(packetBuffer);
        Serial.println();
        Serial.println("--END OF SPECIFICATIONS OF RECEIVED PACKET--");
      }
    }
  }
}

/*  ==== LIGHT MODES =====  */

//HERE ARE LIGHT MODES. THEY CAN ONLY BE ACTIVATED IF DARK == TRUE


/* _______ADDITIONAL SYSTEM SETTINGS_______ */

/* ==== LIGHT MEASUREMENT ==== */
/*
void sensorInterval () {
  unsigned long currentMillisSensor = millis();


  if (currentMillisSensor - last_time >= intervalSensor)  // wrap-around safe test
  {
    last_time += intervalSensor ; // setup for next time
    //lightSensor = analogRead(A0);
  }
}
*/
void checkSensorDark() {

  unsigned long currentMillisDark = millis();

  lightSensor = analogRead(A0);
  //Serial.println(lightSensor);   //DISABLE THIS FUNCTION -- DEBUG: use this for adjustment of light settings

  if (lightSensor < 550) {
    if (currentMillisDark - makingSureItIsDark > 60000)//default: 600000 )
      // check if it really is dark = set to 10 minutes.
      // for debug -- change this to X-seconds
    {
      dark = true;
      light = false;
      changeLampStatus();
      makingSureItIsDark = currentMillisDark;
    }
  }
}

void checkSensorLight() {
  unsigned long currentMillisLight = millis();

  lightSensor = analogRead(A0);
  if (lightSensor > 550) {
    if (currentMillisLight - makingSureItIsLight > 60000)//default: 600000 )
      // check if it really is light = set to 10 minutes.
      // for debug -- change this to X-seconds
    {
      dark = false;
      light = true;
      FastLED.clear(true);
      makingSureItIsLight = currentMillisLight;
    }
  }
}

void changeLampStatus() {
  if (dark == true && stateIdle == true) {
    candleEffect();
    FastLED.show();
  }
  if (dark == true && stateAvailable == true) {
    candleAllowance = false;
    leds[0].setRGB(0, 255, 0);
    leds[1].setRGB(0, 255, 0);
    FastLED.show();
  }
  if (dark == true && stateOrdered == true) {
    candleAllowance = false;
    leds[1].setRGB(0, 0, 255);
    FastLED.show();
  }
  if (dark == true && stateOff == true) {
    candleAllowance = false;
    FastLED.clear(true);
  }
}

darthvader072

This is the result in the monitor:

14:30:09.101 ->  ***** NEW SESSION STARTED *****
14:30:09.620 -> THE BULB. terraslamp -- V01 2020
14:30:09.620 -> Setting up network now, hold your drinks high..!
14:30:09.620 ->
14:30:09.744 -> MAC: C8:2B:96:30:20:69
14:30:09.819 -> Connecting....Yes! We are connected to WiFi! Cheers all!
14:30:13.860 -> Connection info:
14:30:13.860 -> SSID: HetTerras
14:30:13.860 -> IP Address: 10.168.1.106
14:30:13.860 -> signal strength (RSSI):-60 dBm
14:30:13.896 -> UDP server on port 1821
14:30:13.896 ->
14:30:13.896 -> Almost there, finalizing boot process.. Setting Table ID...
14:30:14.135 -> Table number 215 is ONLINE
14:30:14.380 -> Device is ready to use!
14:30:14.380 ->
14:30:28.444 -> Lost connection.. trying to reconnect
14:30:28.514 -> Lost connection.. trying to reconnect
14:30:28.550 -> Lost connection.. trying to reconnect
14:30:28.618 -> Lost connection.. trying to reconnect
14:30:28.618 -> Lost connection.. trying to reconnect
14:30:28.618 -> Lost connection.. trying to reconnect
14:30:28.618 -> Lost connection.. trying to reconnect



Basically after 'Device is ready to use' the loop starts.

countrypaul

You've missed the section of code that outputs the "connection lost" message which makes it almost impossible to work out why it might be issued.

When you compile what are the memory statistics as you appear to have a lot of text for messages.

darthvader072

Hi countrypaul,

Yes, unfortunately the code was to long to share, please attached.

countrypaul

A quick look shows that TestWiFiConnection calls WiFiConnect which calls TestWiFiConnection which calls...
It looks like you can simply end up going down a call tree until you run out of stack space.

I think you need to put some more debugging serial.println statements in those 2 functions and work out what is actually happening - and more importantly why.

darthvader072

Hi countrypaul,

Thanks for the response, I've worked on it almost all day and found a new problem. I'll request to close this topic, so I can make a new one to explain myself better. Thanks!

UKHeliBob

Please don't start a new topic about the same subject as it wastes time

Why not add to this one ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up