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:
#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);
}
}