NodeMCU won't sleep when externally powered, works fine on USB laptop power
Posted: Sat Aug 17, 2024 8:47 pm
I have a simple NODEMCU I want to setup at a remote location and be powered by batteries. I have it working 100% and deep sleeping just fine when it is connected to my laptop USB port, I am using an inline USB current monitor (FNB48S) and in deep sleep it's about 500uA, when I power it from a USB block it idles at 8mA and never goes below. I have tried various USB power sources and batteries powering Vin, it only sleeps when connected to my laptop. I'm stumped. I am not using any serial print functions in the code, although I have a suspicion it might be related to the CP2102, but don't know why the differences in power source would make a difference.
Anyone else run into this and have a solution?
Thank you
Paul
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
float distance;
float duration;
float input_volt;
float temp;
float r1=42400; //r1 value
float r2=18000; //r2 value
SoftwareSerial HC12(D1, D2); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
//Serial.begin(9600); // Serial port to computer
HC12.begin(1200); // Serial port to HC12 ****THIS HAS TO BE THE SAME AS "AT+BXXXX"
pinMode(D5, OUTPUT); // Set pin HC12
pinMode(D6, OUTPUT); // Power ENABLE 2N3906 base pin JSN-SRO4
pinMode(D7, OUTPUT); // Trigger pin JSN-SRO4
pinMode(D8, INPUT); // Echo pin JSN-SRO4
digitalWrite(D6, LOW); // Keep JSN-SR04 off until needed
}
void hc_12_sleep() {
delay(150);
digitalWrite(D5, LOW);
delay(150);
HC12.println("AT+SLEEP");
delay(150);
digitalWrite(D5, HIGH);
}
void hc_12_wakeup() {
digitalWrite(D5, LOW);
delay(150);
digitalWrite(D5 , HIGH);
delay(150);
}
void hc12_loop() {
hc_12_wakeup();
temp = (analogRead(A0) * 3.3) / 1024.0; // FORMULA USED TO CONVERT THE VOLTAGE
input_volt = temp / (r2/(r1+r2));
if (distance < 100) HC12.print('0');
if (distance < 10) HC12.print('0');
//Serial.print("Tank Level ");
// Serial.println(distance);
// Serial.print("Voltage ");
// Serial.println(input_volt);
HC12.print(distance);
HC12.print(",");
HC12.print(input_volt);
HC12.println("");
}
void getSensordata(){
digitalWrite(D6, HIGH);
delay(100);
digitalWrite(D7, LOW);
delayMicroseconds(10);
digitalWrite(D7, HIGH);
delayMicroseconds(1100);
digitalWrite(D7, LOW);
duration = pulseIn(D8, HIGH);
distance = duration * .034 / 2;
digitalWrite(D6, LOW);
}
void loop() {
getSensordata();
hc12_loop();
hc_12_sleep();
ESP.deepSleep(58.7e6, WAKE_RF_DISABLED);
delay(100);
}
Anyone else run into this and have a solution?
Thank you
Paul
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
float distance;
float duration;
float input_volt;
float temp;
float r1=42400; //r1 value
float r2=18000; //r2 value
SoftwareSerial HC12(D1, D2); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
//Serial.begin(9600); // Serial port to computer
HC12.begin(1200); // Serial port to HC12 ****THIS HAS TO BE THE SAME AS "AT+BXXXX"
pinMode(D5, OUTPUT); // Set pin HC12
pinMode(D6, OUTPUT); // Power ENABLE 2N3906 base pin JSN-SRO4
pinMode(D7, OUTPUT); // Trigger pin JSN-SRO4
pinMode(D8, INPUT); // Echo pin JSN-SRO4
digitalWrite(D6, LOW); // Keep JSN-SR04 off until needed
}
void hc_12_sleep() {
delay(150);
digitalWrite(D5, LOW);
delay(150);
HC12.println("AT+SLEEP");
delay(150);
digitalWrite(D5, HIGH);
}
void hc_12_wakeup() {
digitalWrite(D5, LOW);
delay(150);
digitalWrite(D5 , HIGH);
delay(150);
}
void hc12_loop() {
hc_12_wakeup();
temp = (analogRead(A0) * 3.3) / 1024.0; // FORMULA USED TO CONVERT THE VOLTAGE
input_volt = temp / (r2/(r1+r2));
if (distance < 100) HC12.print('0');
if (distance < 10) HC12.print('0');
//Serial.print("Tank Level ");
// Serial.println(distance);
// Serial.print("Voltage ");
// Serial.println(input_volt);
HC12.print(distance);
HC12.print(",");
HC12.print(input_volt);
HC12.println("");
}
void getSensordata(){
digitalWrite(D6, HIGH);
delay(100);
digitalWrite(D7, LOW);
delayMicroseconds(10);
digitalWrite(D7, HIGH);
delayMicroseconds(1100);
digitalWrite(D7, LOW);
duration = pulseIn(D8, HIGH);
distance = duration * .034 / 2;
digitalWrite(D6, LOW);
}
void loop() {
getSensordata();
hc12_loop();
hc_12_sleep();
ESP.deepSleep(58.7e6, WAKE_RF_DISABLED);
delay(100);
}