How to Control 3 Speed Fan With Esp8266

Disperse the love

  • 1
  • 6
  • 7

    Shares

When you create a new IoT project probably you need to tie in microcontroller to a battary power source, but if you don't use a power saving options your battery bequeath run outer in nobelium time.

As very much of IoT microcontroller, WeMos D1 mini have some power saving mode.

To create a simple rechargeable power bank read "Emergency power bank homemade".

Hera the WeMos D1 mini AliExpress WeMos D1 miniskirt - AliExpress NodeMCU V2 V2.1 V3 - AliExpress esp01

The sleep modes types are modem nap,light sleep, anddeep sleep. The table below shows the differences between each mode.

Item Modem-sleep Light-sleep Deep-log Z's
Badger State-Fi OFF Away OFF
System clock ON Disconnected OFF
RTC Happening Connected ON
CPU Happening Pending OFF
Substrate current 15 mama 0.4 mA ~20 uA
Norm current (DTIM = 1) 16.2 mom 1.8 Massachusetts
Average current (DTIM = 3) 15.4 mA 0.9 mA
Average current (DTIM = 10) 15.2 mA 0.55 Master of Arts

Modem sleep

This is the nonremittal res publica of esp8266, merely normally you interrupt this state when connect twist via WIFI. But if you assume't use WIFI for a while, the better thing is to put together the device in Modem-sleep.

You can put your gimmick in modem-sleep with this command:

WiFi.disconnect(); WiFi.forceSleepBegin(); delay(1); //For some reason the modem South Korean won't bed unless you do a delay          

you can restore WIFI with:

WiFi.forceSleepWake(); delay(1); //Insert code to connect to WLAN, pop your servers or clients or whatsoever          

Commend that if you want usage a delay in normal style you must add this:

            wifi_set_sleep_type(NONE_SLEEP_T);          

Or you must disabled the auto sleep style (better explained in the next paragraph) we are going to call this function.

            wifi_fpm_auto_sleep_set_in_null_mode(NULL_MODE);          

Here a complete sketch, pay attention I use Serial1 to debug, refer to Part 3 WeMos D1 miniskirt (esp8266), debug on secondary UART

#include "Arduino.h" #include <ESP8266WiFi.h> // Required for LIGHT_SLEEP_T delay mode extern "C" { #include "user_interface.h" }  const sear* ssid = "<your-ssid>"; const char* password = "<your-passwd>";  //The frame-up function is titled once at startup of the sketch void setup() {   Serial1.commence(115200);   while(!Serial1) { }    Serial1.println();   Serial1.println("Start device in formula mode!");    WiFi.mode(WIFI_STA);    WiFi.begin(ssid, word);   Serial1.println("");    // Wait for connector   while (WiFi.position() != WL_CONNECTED) {     delay(500);     Serial1.print(".");   }   Serial1.println("");   Serial1.print("Connected to ");   Serial1.println(ssid);   Serial1.print("IP address: ");   Serial1.println(WiF.localIP()); } null recall() {   Serial1.println("Callback");   Sequential.gush(); } void loop() { 	  Serial1.println("Enter modem sleep musical mode");         uint32_t sleep_time_in_ms = 10000; //      WiFi.disconnect();       Wireless local area network.forceSleepBegin();       delay(sleep_time_in_ms + 1);        WiFi.forceSleepWake();       delay(1);       Serial1.println("Exit modem kip mode");        WiFi.modal value(WIFI_STA);       WiFi.begin(ssid, password);       Serial1.println("");        // Look for connection       while (WiFi.position() != WL_CONNECTED) {         delay(500);         Serial1.print(".");       }       Serial1.println("");       Serial1.print("Connected to ");       Serial1.println(ssid);       Serial1.print("IP address: ");       Serial1.println(WLAN.localIP());        wifi_set_sleep_type(NONE_SLEEP_T);       delay(10000);  //  Put the esp to eternal rest for 15s }          

This type of rest mode, American Samoa you can see in the telecasting permit to get 20mA of power consumption from the gimmick.

Lighter-than-air sleep

This type of sleep is quite usefully if you indigence to mantain the gimmick active, and the big difference from the previous type is that you fire wake up the device via disturb happening GPIO.

To put connected light rest mode you must exercise this code, there isn't a simple way to activate It like modem sleep.

You must add this configuration to the sketch:

            // Here entirely the code to put con light sleep 	  // the problem is that there is a bug on this 	  // process 	  //wifi_station_disconnect(); //not needed 	  uint32_t sleep_time_in_ms = 10000; 	  wifi_set_opmode(NULL_MODE); 	  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); 	  wifi_fpm_open(); 	  wifi_fpm_set_wakeup_cb(callback); 	  wifi_fpm_do_sleep(sleep_time_in_ms *1000 ); 	  delay(sleep_time_in_ms + 1);          

At the same manner we can use this encode to move in in MODEM_SLEEP mode:

            // Here all the code to put con light sleep 	  // the problem is that in that location is a bug on this 	  // action 	  //wifi_station_disconnect(); //not needed 	  uint32_t sleep_time_in_ms = 10000; 	  wifi_set_opmode(NULL_MODE); 	  wifi_fpm_set_sleep_type(MODEM_SLEEP_T); 	  wifi_fpm_open(); 	  wifi_fpm_set_wakeup_cb(callback); 	  wifi_fpm_do_sleep(sleep_time_in_ms *1000 ); 	  delay(sleep_time_in_ms + 1);          

The twist move in light mode with less use of battery and fewer temperature.

#let in "Arduino.h" #admit <ESP8266WiFi.h> // Required for LIGHT_SLEEP_T delay mode extern "C" { #let in "user_interface.h" }  const char* ssid = "<your-ssid>"; const char* password = "<your-passwd>";  //The setup function is called erstwhile at startup of the resume void setup() {   Serial1.begin(115200);   while(!Serial1) { }    Serial1.println();   Serial1.println("Get down device in normal mode!");    WiFi.mode(WIFI_STA);    WiFi.start out(ssid, parole);   Serial1.println("");    // Wait for link   while (WiFi.position() != WL_CONNECTED) {     delay(500);     Serial1.print(".");   }   Serial1.println("");   Serial1.print("Connected to ");   Serial1.println(ssid);   Serial1.print("IP handle: ");   Serial1.println(Wireless local area network.localIP()); } void recall() {   Serial1.println("Callback");   Serial.flush(); } void eyelet() { 	  Serial1.println("Go into light sleep mode");        // Here all the code to set con light sleep       // the problem is that there is a bug on this       // appendage       //wifi_station_disconnect(); //non needed       uint32_t sleep_time_in_ms = 10000;       wifi_set_opmode(NULL_MODE);       wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);       wifi_fpm_open();       wifi_fpm_set_wakeup_cb(recall);       wifi_fpm_do_sleep(sleep_time_in_ms *1000 );       delay(sleep_time_in_ms + 1);        Serial1.println("Release light sleep mode");        WiFi.begin(ssid, password);       Serial1.println("");            // Wait for connection       spell (WiFi.status() != WL_CONNECTED) {         check(500);         Serial1.print(".");       }       Serial1.println("");       Serial1.print("Connected to ");       Serial1.println(ssid);       Serial1.print("IP address: ");       Serial1.println(WiFi.localIP());              wifi_set_sleep_type(NONE_SLEEP_T);       delay(10000);  //  Put the extrasensory perception to sleep for 15s }          

This typewrite of rest mode, Eastern Samoa you can meet in the video let to get 20mA of baron to the device, to obtain better power saving you mustiness set the wake up via GPIO.

Light sleep GPIO come alive

WeMos D1 mini on light sleep and debug on Serial1 check ampere
WeMos D1 mini connected lit sopor and debug happening Serial1 crack ampere

Loose sleep can be come alive by GPIO interrupt, the command to set is

gpio_pin_wakeup_enable(GPIO_ID_PIN(LIGHT_WAKE_PIN), GPIO_PIN_INTR_LOLEVEL);          

Than you must set max quietus time and than put a delay to activate all.

            wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME); 	  time lag(1000);          

The gross code can be like this

#include "Arduino.h" #include <ESP8266WiFi.h>  #define	FPM_SLEEP_MAX_TIME			 0xFFFFFFF  // Required for LIGHT_SLEEP_T delay mode extern "C" { #include "user_interface.h" }  const charwoman* ssid = "<your-ssid>"; const char* countersign = "<your-passwd>";  //The setup function is known as erst at startup of the sketch void setup() {   Serial1.begin(115200);   while(!Serial1) { }    Serial1.println();   Serial1.println("Start gimmick in pattern modality!");    WiFi.mode(WIFI_STA);    WiFi.begin(ssid, password);   Serial1.println("");    // Waitress for connection   piece (WiFi.status() != WL_CONNECTED) {     detain(500);     Serial1.photographic print(".");   }   Serial1.println("");   Serial1.print("Connected to ");   Serial1.println(ssid);   Serial1.print("Information science address: ");   Serial1.println(WiFi.localIP()); } void callback() {   Serial1.println("Callback");   Serial.flush(); }  #define LIGHT_WAKE_PIN D5  void loop() { 	  Serial1.println("Embark light sleep mode");  	  //wifi_station_disconnect(); //not needed 	  gpio_pin_wakeup_enable(GPIO_ID_PIN(LIGHT_WAKE_PIN), GPIO_PIN_INTR_LOLEVEL); 	  wifi_set_opmode(NULL_MODE); 	  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); 	  wifi_fpm_open(); 	  wifi_fpm_set_wakeup_cb(callback); 	  wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME); 	  delay(1000);    	  Serial1.println("Exit light sleep mode");        WiFi.Begin(ssid, password);       Serial1.println("");        // Wait for connection       while (WiFi.status() != WL_CONNECTED) {         delay(500);         Serial1.print(".");       }       Serial1.println("");       Serial1.print("Connected to ");       Serial1.println(ssid);       Serial1.print("IP address: ");       Serial1.println(Wireless local area network.localIP());        wifi_set_sleep_type(NONE_SLEEP_T);   	  delay(10000);  //  Put the esp to sleep for 15s }          

The result is

As you derriere see in the video the power saving is better (6mA), and you can use interrupt to restore twist.

Deep sleep

The most common and most utilized modality is deep-sleep, a pratical application is to send data to a server all predefined metre period.

  • You must put your device in deep-sleep
  • set a timer to wake
  • fire up
  • send data
  • put gimmick in quietus

All esp8266 (except esp01) have a pin with wake label, and through this pin connected to the RESET IT will be possible to wake up the microcontroller.

WeMos D1 mini esp8266 pinout mischianti low resolution
WeMos D1 mini esp8266 pinout mischianti low resolution

You can se various sleep in alternative, check your better choiche.

system_deep_sleep_set_option(0) The 108th Byte of init parameter decides whether RF calibration will live performed subsequently the chip wakes up from Deep-sleep.
system_deep_sleep_set_option(1) The chip leave piddle RF calibration later on waking upbound from Deep-sleep. Power consumption is mellow.
system_deep_sleep_set_option(2) The chip South Korean won't get to RF calibration after waking up from Deep-sleep. Office consumption is low-altitude.
system_deep_sleep_set_option(3) The chip South Korean won't turn on RF after awake up from Mystifying-sleep. Force consumption is the lowest, same equally in Modem-eternal rest.

On WeMos D1 mini, equally you can see, the pin for wake il D0.

ESP.deepsleep(0) // suspends the module until it is woken up by a spike on the RST pin ESP.deepsleep(5 * 1000000) // wake in the lead the faculty all 5 seconds Extrasensory perception.deepsleep(5000000, RF_DISABLED) // wakes up the module every 5 seconds without re-activating the WiFi modem          

To wake a microcontroller you mustiness put LOW reset PIN.

You can use a Wake pin (D0) connected to Reset to wake after some clip, or you can use external button (or other) pulled up and than go Humiliated on key press.

After putt the esp8266 into deep sleep mode, thither are 2 shipway to wake IT up:

  • Past background a timer
  • with a button that put away low Reset pin.
WeMos D1 mini deep sleep and debug on Serial1 check Ampere
WeMos D1 mini deep sleep and debug on Serial1 assay A
#include "Arduino.h"  //The apparatus social function is named once at startup of the survey vacuum frame-up() {   Serial1.Menachem Begin(115200);   while(!Serial1) { }   Serial1.println();   Serial1.println("Start device in normal mode!");    holdup(5000);   // Wait for in series to initialize.   spell(!Serial1) { }    // Deep sleep mode for 10 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU dining table) is connected to the RESET pin   Serial1.println("I'm awake, but I'm going into heavy sleep mode for 10 seconds");   ESP.deepSleep(10e6); }  void loop() { }          

The power that use device is similar to light-sleep with wake up via GPIO (6mA).

Automatic musical mode

Past default the esp device have auto sleep musical mode enabled. If you add the code for light sleep or modem sleep at the frame-up and not disable the auto mode WeMos run in sleep late automaticalli aft 10 seconds of holdup like so:

#let in "Arduino.h" #include <ESP8266WiFi.h> // Required for LIGHT_SLEEP_T delay mode extern "C" { #include "user_interface.h" }  const char* ssid = "<your-ssid>"; const char* parole = "<your-passwd>";  //The setup affair is called once at startup of the sketch void frame-up() {   Serial1.begin(115200);   piece(!Serial1) { }    Serial1.println();   Serial1.println("Get device in normal modal value!");    WiF.mode(WIFI_STA);    wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);    WiFi.set out(ssid, password);   Serial1.println("");    // Wait for connection   while (WiFi.status() != WL_CONNECTED) {     delay(500);     Serial1.print(".");   }   Serial1.println("");   Serial1.print("Connected to ");   Serial1.println(ssid);   Serial1.print("IP cover: ");   Serial1.println(WiFi.localIP()); } unsigned long interval = 30000; unsigned long previousMillis = millis() + interval;  void loop() { 	  unsigned long currentMillis = millis();  	  if (currentMillis - previousMillis >= interval) { 		  Serial1.println("Enter delay");  		  detain(20000);  		  Serial1.println("Die off delay");  		  WiFi.begin(ssid, parole); 		  Serial1.println("");  		  // Wait for joining 		  while (WiFi.status() != WL_CONNECTED) { 			delay(500); 			Serial1.print("."); 		  } 		  Serial1.println(""); 		  Serial1.print("Coupled to "); 		  Serial1.println(ssid); 		  Serial1.print("IP address: "); 		  Serial1.println(Wireless local area network.localIP()); 		  previousMillis = currentMillis;  	  } }          

the result is this:

When execute hold up command for the first 10 seconds nothing append, than after that catch some Z's way is automatic activated.

  1. WeMos D1 mini (esp8266), specs and IDE configuration
  2. WeMos D1 mini (esp8266), united SPIFFS Filesystem
  3. WeMos D1 mini (esp8266), debug along secondary UART
  4. WeMos D1 mini (esp8266), the three type of sleep mode to care energy savings
  5. WeMos D1 mini (esp8266), integrated LittleFS Filesystem
  6. esp12 esp07 (esp8266): flash, pinout, specs and IDE shape
  7. […]

Gap the love

  • 1
  • 6
  • 7

    Shares

How to Control 3 Speed Fan With Esp8266

Source: https://www.mischianti.org/2019/11/21/wemos-d1-mini-esp8266-the-three-type-of-sleep-mode-to-manage-energy-savings-part-4/

0 Komentar

Post a Comment




banner