I'm utilizing Light Sleep mode in an effort to minimize idle current draw. After a period of no motion, it goes to sleep just as expected and a subsequent motion sense brings it back to life. Everything works perfectly until coming out of sleep. Once it wakes up, a subsequent motion sense does not result in the same behavior as prior to the sleep event. Instead of flashing the LED and playing the MP3, it appears to do nothing. However, watching the serial monitor it appears to pause and then start again. The countdown timer on the serial monitor will display (for example) "...10, 9, 8, {pause at the moment I wave my hand} 3, 2, 1). It is almost as if the micro is off doing something else. Strangely, it only does this if I include the code to restart SoftwareSerial after coming out of sleep. If I comment out the SoftwareSerial restart, it behaves normally coming out of sleep- except of course that the MP3 module does not play.
Can anyone offer an ideas I can explore? I would be very grateful!!
PS: The light sleep code comes from this great tutorial:
https://www.mischianti.org/2019/11/21/w ... gs-part-4/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
extern "C" {
#include "gpio.h"
}
extern "C" {
#include "user_interface.h"
}
#define WIFI_SSID "****"
#define WIFI_PASS "****"
// PINS
const byte ledPin = 15; //D8
const byte pirPin = 5; //D1
const byte standbyPin = 4; //D2
const byte rxPin = 12; //D6
const byte txPin = 14; //D5
//TIMING
const byte sleepDelay = 20; //length of idle time (s) to wait before going to sleep
const byte standbyDelay = 30; // initial delay (s) from power-up w/ standby button pressed
boolean justAwoke = false;
long triggerBaseline = 0;
const byte WiFiTimeout = 30; // WiFi connect timeout (s)
const byte mp3Volume = 10; // 0 to 30
const int numTracks = 4;
int TriggerCount = 0;
SoftwareSerial mySoftwareSerial(rxPin, txPin); //RX, TX
DFRobotDFPlayerMini myDFPlayer;
void setup() {
gpio_init();
pinMode(pirPin, INPUT);
pinMode(standbyPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
startSerial(); // start Serial and MySoftwareSerial
startMP3(); // start DFPlayer Mini MP3
triggerBaseline = millis();
if (digitalRead(standbyPin) == LOW) {
standbyMode();
}
triggerBaseline = millis();
}
void loop() {
Serial.print("Time to sleep: " );
Serial.print(sleepDelay - ((millis() - triggerBaseline) / 1000));
Serial.println("s");
Serial.print("PIR state: " );
Serial.println(digitalRead(pirPin));
Serial.print("justAwoke state:" );
Serial.println(justAwoke);
Serial.println();
while (digitalRead(pirPin) == HIGH || justAwoke == true) {
Serial.println("Checkpoint A");
justAwoke = false;
TriggerCount++;
Serial.println("");
Serial.println("Motion triggered");
int Track = random(1, numTracks);
Serial.print("Track: ");
Serial.println(Track);
myDFPlayer.play(Track);
for (int i = 0; i <= 5; i++) {
long OnTime = random(100, 1500);
long OffTime = random(100, 1500);
digitalWrite(ledPin, HIGH);
delay(OnTime);
digitalWrite(ledPin, LOW);
delay(OffTime);
}
digitalWrite(ledPin, HIGH);
triggerBaseline = millis();
}
// **** SLEEP TIMER ***
if ((millis() - triggerBaseline) / 1000 >= sleepDelay && digitalRead(pirPin) == LOW) {
for (int i = 0; i <= 5; i++) { // rapdid pulse led
digitalWrite(ledPin, LOW);
delay(300);
digitalWrite(ledPin, HIGH);
delay(300);
}
sendReport();
stopMP3();
//stopSerial();
digitalWrite(ledPin, LOW);
Serial.println("Entering Light Sleep.");
delay(2000);
lightSleep(); // put esp8266 in Light Sleep Mode
delay(2000);
wakeUp();
}
delay(1000);
}
void stopSerial () {
Serial.println("Stopping serial and entering Light Sleep...");
delay(500);
Serial.flush();
delay(500);
Serial.end();
}
void startSerial() {
Serial.begin(115200);
while (!Serial) {
}
Serial.println("");
Serial.println("Serial started.");
}
void stopMP3 () {
Serial.println("Stopping MP3...");
//mySoftwareSerial.flush();
delay(1000);
mySoftwareSerial.end();
//digitalWrite(miniFETpin, LOW); // cut MP3 power
Serial.println("MP3 stopped.");
}
void startMP3() {
Serial.println("Starting MP3...");
mySoftwareSerial.begin(9600); //Initialize DLPlayer on software serial connection
Serial.println("Checkpoint 1");
if (!myDFPlayer.begin(mySoftwareSerial, false)) {
Serial.println("Unable to begin:");
Serial.println("1.Please recheck the connection!");
Serial.println("2.Please insert the SD card!");
while (true) {
Serial.println("Starting MP3 Module...");
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println("MP3 started.");
myDFPlayer.volume(mp3Volume);
Serial.println("MP3 volume set.");
}
void lightSleep() {
wifi_station_disconnect();
wifi_set_opmode_current(NULL_MODE);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
wifi_set_sleep_type()
wifi_fpm_open();
gpio_pin_wakeup_enable(GPIO_ID_PIN(5), GPIO_PIN_INTR_HILEVEL);
GPIO_PIN_INTR_HILEVEL
delay(200);
wifi_fpm_do_sleep(0xFFFFFFF);
delay(200);
}
void wakeUp() {
//startSerial();
digitalWrite(ledPin, HIGH);
Serial.println("");
Serial.println("Just woke up!");
startMP3();
justAwoke = true; // Trigger alarm upon wake up
triggerBaseline = millis(); //reset awake timer
}
void sendReport() {
Serial.println("");
Serial.println("Sending report...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.println("");
Serial.print("Connecting to WiFi");
long WiFiBaseline = millis();
while (WiFi.status() != WL_CONNECTED && (millis() - WiFiBaseline) / 1000 < WiFiTimeout) {
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected");
// *** Reserved for Connect and send to Adafruit IO
}
else {
Serial.println("");
Serial.print("WiFi connection failed");
}
Serial.println("");
Serial.println("Report sent.");
}
void standbyMode() {
Serial.println("Checkpoint 3");
long standbyBaseline = millis();
while ((millis() - standbyBaseline) / 1000 < standbyDelay) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(1000);
}
}