Wemos D1 sleep and wake by LDR

Hello, I’m new here but am already on my second project.
I’m trying to create a simple mailbox notifier.-
Problem is, I need to save on current as power source is solar panel

This is my code. could you please check it?
What it normally did is notify V4 as high whenever LDR light was over 100

Now the code should turn off wifi after 10s LDR is below 100, and while over 100, turn on wifi and send V4 to high
Unfortunately, WIFI goes and STAYS off after 10s in any condition

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
int sensor = A0;

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

}

void loop()
{
  int ldrval = analogRead(sensor);
  if (ldrval < 100)

  {
    delay(10000);
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
    WiFi.forceSleepBegin();
    delay(1);
  }
  else {
    WiFi.forceSleepWake();
    delay(1);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);
    delay(2000);
    Blynk.virtualWrite(4, HIGH);
  }
  Blynk.run();

}


Well, we are not code mechanics sitting around waiting for requests here :stuck_out_tongue:

I suggest you Google ESP sleep options and search this forum for all the other deep and lite sleep options using Blynk to see how others have done it… or tried… I have not mastered it yet (still working on my own sleep patterns :sleeping: ) but I am sure it is mostly done outside of the void loop() and with less delay() :wink:

Using deepsleep on an ESP etc isn’t really a great solution for this type of application as the whole idea if deepsleep is that you get the device to wake up every so often, take a reading and go back to sleep. If you only polled the microswitch attachyed to the door of your mailbox every 5 minutes than you’d obviously miss the majority of occasions when your mailbox is opened.

Personally, I wouldn’t use an ESP8366/NodeMCU type device as a mailbox notifier.
I’d use a 433MHz transmitter like a garage door remote that’s powered from a small 12v battery and which is engineered to draw no power when the button isn’t pressed. You could even use a small 433MHz PIR detector mounted inside the mailbox instead, if its one of those mailboxes that’s opened by the mail person to put your stuff in.
I’d then have the ESP/NodeMCU device indoors, powered by mains power, linked to a cheap 433MHz receiver which listens for the specific code that your mailbox will send.

I use one of these buttons for a different purpose:


It’s really handy because when the button is pressed it transmits the same code 24 times in a burst that lasts almost 2 seconds.
This means that you can have a Blynk timer running as infrequently as once every second to check for incoming 433MHz signals and you won’t miss a button press.
For your mailbox monitor you’d obviously need to disassemble the unit and replace the push button with a microswitch that’s activated when the mailbox is opened. I guess the only situation where this wouldn’t work would be if a large parcel was placed in the mailbox and the door wouldn’t close. In this scenario the batterywould be drained as you’d constantly be transmitting a signal. If you have this type of mailbox then maybe the PIR approach would be better?

Pete.

1 Like

thanks for the advice!