LED light on battery to run at certain time of day

Hello Blynkers,

Before I start, I am still a total newbie and I have not physically tested what I have in code so far, I am just stuck with some parts of it.

The scenario:
I have a D1 mini on a solar-charged battery 3.7V stepping up to a 12V LED. If the D1 stays on the whole night, (nevermind the LED) the battery dies and it fails to start again the next morning as I assume it drains the battery before it can even charge.
I aim to have the LDR check light intensity during the evening and when it’s too low, it should turn on the LED. The idea was to deep sleep the whole day and only turn on when the timer starts, but I read that it can’t stay off for such a long time so I want it to take a reading every now and then, go back to sleep if the light intensity is high enough.

This is the code so far:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetRTC rtc;

//You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxxxxxx";

BlynkTimer timer;

//Need to add a timer to keep awake during certain times

BLYNK_CONNECTED() { 
  rtc.begin();    
    Blynk.syncAll(); // Request Blynk server to re-send latest values for all pins
}

//*********************LED BUTTON 1**************************//
BLYNK_WRITE(V1)//LED on or off
{
  int pinValue1 = param.asInt();                        // assigning incoming value from pin V1 to a variable
  if (pinValue1 == 1) {                                // If value is 1 run this command
    digitalWrite(2, LOW);                             //ON........2........D4 output from Wemos D1 mini
    Blynk.setProperty(V1, "onLabel", "LED ON");
  }
  else {                                            // If value is 0 run this command
    digitalWrite(2, HIGH);                         //OFF
    Blynk.setProperty(V1, "offLabel", "LED OFF");
  }
  Serial.print("V1 Button value is: ");
  Serial.println(pinValue1);
}

//********************LDR SENSOR*************************//
int sensorValue;
BLYNK_READ(V2) //Light Sensor
{
  sensorValue = analogRead(A0);                   //reading the sensor on A0
  Blynk.virtualWrite(V2, sensorValue);           //sending to Blynk
  Blynk.setProperty(V2, "label", "LIGHT LX");
}

void analogCheck(){
if(analogRead(A0) < 15){
 digitalWrite(2, LOW);                          //Turn ON LED
  Blynk.setProperty(V1, "onLabel", "LED ON");
}
else{
digitalWrite(2, HIGH);                         //Turn OFF LED
    Blynk.setProperty(V1, "offLabel", "LED OFF");
delay(1000);
ESP.deepSleep(1800000L); //sleep 30 min
}
else if{  //keep light on for only 3-4 hours or while timer is active if timer is not active, go back to deep sleep
// timer widgit code runs here
}
}


void sendSensor()
{
String notifyString;
//LDR
sensorValue = analogRead(A0);
}


void setup()
{
// Debug console
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, LOW);
Blynk.begin(auth, ssid, pass);
pinMode(0, OUTPUT); //
digitalWrite(0, LOW); // Set GPIO 0 LOW
 timer.setInterval(50000L, sendSensor); // send reading to blynk every 5 sec (Once while awake) 
 timer.setInterval(60000L, analogCheck); //Check Analog Pin Every 6 sec switch off if light detected
}

void loop()
{
Blynk.run();
timer.run();
}

  1. Now, I would like to use the timer widget to keep the light on when the correct circumstances are met, say from 17:00 to 22:00 but prevent the light to come on (even if dark) if the timer is not active.
    How would I do that?
  2. I would also like to switch off the LED with a widget button and have it go to deep sleep ignoring the other variables like if it’s dark and the timer is on. Point 2 isn’t really necessary but would be cool tho. I can just adjust the timer.

Thanks for any advice or for pointing me in the right direction.

A few issues here:

  1. The Wemos D1 Mini can’t switch 12v without the use of a relay or transistor. Energising a relay will consume more current, so a transistor is a better option.
  2. The relay/transistor won’t stay latched on unless the Wemos is awake.

There are options about putting the Wemos into WiFi sleep mode while it’s on, to save some power, but it won’t respond to widgets in the app when it’s in either WiFi sleep or deep sleep modes.
If you wanted to use WiFi sleep mode during the LED On hours, and still have some form of Blynk control then you’d need to switch WiFi on every so often and connect to Blynk, then turn WiFi off again. But, if you did this say every 5 minutes then it would take an average of two and a half minutes for it to respond to a widget change.

I guess you could explore some sort of latching circuit to hold your LED on, and use the Wemos in deep sleep mode to check for widget updates and do time/light level comparisons.

To be honest, the easiest solution would be a larger solar panel and battery.

Pete.

Thank you @PeteKnight,

So there is no way to have deep sleep run on the timer?

I have the LED working with a widget button and I do plan to get a bigger solar and battery for my home project, but it’s not in the books right now.

I will explore the latching circuit you mention, it might be the way to go for now.

Otherwise, just use a relay to switch between solar charge during the day and usb charge during nighttime when the battery is low… use timer to switch on the LED at certain times.

I would have liked to be able to use the deep-sleep function.

I don’t ready understand which ‘timer’ you’re referring to in the question.

And you can, but within the restrictions that I mentioned above.

Pete.

The timer widget on blynk, to run deepsleep ouside certain hours (Awake between 17:00 and 22:00)

As I already explained, the NodeMCU will be sleeping most of the time, and won’t ‘see’ the output from the timer widget during this time. You can read the widget value when the NodeMCU wakes up, and use that value to adjust the way that your NodeMCU responds, but there will be a delay in the response time that will be a function of how long your NodeMCU sleeps between wake-ups.
The same applies to any other widgets such as a manual override switch.
If your NodeMCU sleeps for five minutes then it may see the change in widget status immediately, if it happens to be awake at that precise moment, or it may be four minutes fifty nine seconds before it sees the change if the change occurs immediately after the NodeMCU sleeps. On average this will be around the two minutes thirty seconds mark, but will be random between zero and five minutes.

You could potentially do some clever stuff with your sleep times, increasing them during daylight and decreasing them during darkness, but as those hours change considerably with the seasons and daylight saving time changes it wouldn’t necessarily give you a great saving in battery usage.

Pete.