Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By RIN67630
#87042 Now I am baffled.
I want to save power on on my ESP8266 Lolin dev board.

The initial programm started WiFi, loaded NTP data and began to run just retrieving every second
Code: Select alllocaltime(&now);
leaving WiFi active.
The power draw oscillated between mainly 22mA having some spikes to 60mA.

Now I added
Code: Select allWiFi.mode(WIFI_OFF);
one second after NTP being loaded, letting the code run without WiFi.

Now the power draw is constantly 74mA!
Has someone got an explanation?
That is the opposite of what I would have logically expected!

Code: Select all#define OFFLINE // Remove to keep online

#if defined(ESP8266)
#include <ESP8266WiFi.h>  // default from Espressif
//#include <WiFiUdp.h>      // default from Espressif
#include <TZ.h>           // default from Espressif
#elif defined(ESP32)
#include <WiFi.h>         // defaults from Espressif
#include <time.h>
#endif

#define WIFI_SSID "your SSID"
#define WIFI_PASS "Your Password"
#define HOST_NAME "ESP-IoT"
#define NTP_SERVER "de.pool.ntp.org"

// maintain for ESP32
#define TZ            1               // (utc+) TZ in hours
#define DST_MN        60              // use 60mn for summer time in some countries
#define GMT_OFFSET_SEC 3600 * TZ      // Do not change here...
#define DAYLIGHT_OFFSET_SEC 60 * DST_MN // Do not change here...
// maintain for ESP8266
#define MYTZ TZ_Europe_Paris

// Variables for Time
static int lastSecond;
tm*        timeinfo;                 //localtime returns a pointer to a tm structstatic int Second;
time_t     Epoch;
time_t     now;

int Second;
int Minute;
int Hour;
int Day;
int Month;
int Year;
int Weekday;
char DayName[12];
char MonthName[12];
char Time[10];
char Date[12];

static IPAddress ip;

//*** Buffers ***
char charbuff[80];    //Char buffer for many functions


// Constructors
   //not needed, yet

void getWiFi()
{
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while ( WiFi.status() != WL_CONNECTED )
  {
    delay ( 500 );
    Serial.print ( "." );
  }
  ip = WiFi.localIP();
  sprintf(charbuff, "Connected to IP: %03d.%03d.%03d.%03d", ip[0], ip[1], ip[2], ip[3]);
  Serial.println(charbuff);
}

void getNTP()
{
#if defined(ESP8266)
  configTime(MYTZ, NTP_SERVER);
#elif defined(ESP32)
  configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER);
#endif
  now = time(nullptr);
  Epoch = now;
}

void getEpoch()
{
  now = time(nullptr);
  Epoch = now;
}
void getTimeData()
{
  timeinfo  = localtime(&now);  // cf: https://www.cplusplus.com/reference/ctime/localtime/
  Second    = timeinfo->tm_sec;
  Minute    = timeinfo->tm_min;
  Hour      = timeinfo->tm_hour;
  Weekday   = timeinfo->tm_wday + 1 ;
  Day       = timeinfo->tm_mday;
  Month     = timeinfo->tm_mon + 1;
  Year      = timeinfo->tm_year + 1900; //returns years since 1900
  strftime (DayName , 12, "%A", timeinfo); //cf: https://www.cplusplus.com/reference/ctime/strftime/
  strftime (MonthName, 12, "%B", timeinfo);
  strftime (Time,10, "%T", timeinfo);
  strftime (Date,12, "%d/%m/%Y", timeinfo);
}

void disConnect()
{
  //  WiFi.disconnect(); //temporarily disconnect WiFi as it's no longer needed
  WiFi.mode(WIFI_OFF);
}


void setup()
{
  Serial.begin(115200);
 
  getWiFi();
  getNTP();
  delay(5000);
  getEpoch();            // writes the Epoch (Numbers of seconds till 1.1.1970...
  getTimeData();         // breaks down the Epoch into discrete values.
  Serial.print(F("! Date is "));   Serial.print( Date );
  Serial.print(F(" and Time is "));   Serial.println( Time );
   
#if defined (OFFLINE)
  disConnect();
  Serial.println (F("Sketch is now running offline with own time"));
#endif

}
void loop()
{
  getEpoch();            // writes the Epoch (Numbers of seconds till 1.1.1970...
  getTimeData();         // breaks down the Epoch into discrete values.

  // Examples with Strings
  yield; Serial.print(F("Thanks God, it is: "));   
  yield; Serial.print( DayName );
  yield; Serial.print(F("! Date is "));   Serial.print( Date );
  yield; Serial.print(F(" and Time is "));   Serial.println( Time );
  yield;

  // Examples with a character buffer (can be used for displays as well)
  sprintf(charbuff, "Now is %02d Hour, %02d Minutes and %02d Seconds. The Epoch is: %10lu" , Hour , Minute, Second, Epoch);
  yield;  Serial.println(charbuff);
  sprintf(charbuff, "Date is %s, %02d %s %04d ", DayName , Day , MonthName, Year);
  yield;  Serial.print(charbuff);
  yield;  Serial.println();

  // Examples with Posix expressions
  strftime (charbuff, 80, "Americans say it is %I:%M%p, but Europeans prefer using %T", timeinfo);
  yield;  Serial.println(charbuff);
  yield;  Serial.println();
  yield;  Serial.println();
  delay(1000);
}