YFROBOT创客社区

标题: ESP8266 core for Arduino应用:ESP8266睡眠模式(Sleep mode)测试与使用 [打印本页]

作者: AllBlue    时间: 2017-5-22 10:06
标题: ESP8266 core for Arduino应用:ESP8266睡眠模式(Sleep mode)测试与使用
睡眠模式:http://www.esp8266.com/wiki/doku.php?id=esp8266_power_usage

当ESP8266设备一直上电,那么它将不断消耗电流。如果电源是无限制的,那么这不一定是一个问题;但是当使用电池或其他有限的电源运行时,我们可能需要最大限度地减少消耗。实现此目的的一种方法是在不使用时暂停设备的操作。 当设备被暂停时,这个概念是消耗将被减少。
ESP8266有三种定义的睡眠模式;他们被称为modem-sleep(调制解调器睡眠),light-sleep(轻睡眠),deep-sleep(深睡眠)。
通过查看下表,我们可以了解这三种模式的能力:
Function Modem Light Deep
WiFi off off off
System Clock on off off
Real Time Clock on on on
CPU on pending off
Current consumption 15mA 0.5mA 20μA

要进入深度睡眠模式,我们可以调ESP8266-ArduinoCroe ->ESP.h/.CPP提供的函数:void deepSleep(uint32_t time_us, RFMode mode = RF_DEFAULT);其中time_us:睡眠时间 RFMode:唤醒后的工作模式(可选参数),默认为RF_DEFAULT;
可选模式:
WAKE_RF_DEFAULT  : do or not do the radio calibration depending on the init byte 108. 做或不做无线电校准依赖于init字节108。
WAKE_RFCAL       : do the radio calibration every time.每次都要进行无线电校准。
WAKE_NO_RFCAL    : do NOT the radio calibration on wake up.不要在醒来时使用无线电校准。
WAKE_RF_DISABLED : on wake up DISABLE the modem. So for example I can't connect the esp to wifi.在醒来时禁用调制解调器。例如,我无法将esp连接到wifi。


睡眠模式介绍完了,使用下面的程序进行简单的功耗测试吧。
使用万用表电流档串联入nodemcu电源部分,然后观察电流变化:
程序:
[C++] 纯文本查看 复制代码
/*
    ESP8266 Sleep Mode test
    Use ammeter to monitor current values:
    1.when use the ESP8266 module connect the network,and get data from the html web;
    2.when use the ESP8266 module into the deep-sleep mode;
    test module : NodeMCU v1
    test web adress : http://www.yfrobot.com/test/testNetwork.html
    http://www.yfrobot.com
    5-20-2017
*/

// Include the ESP8266 WiFi library. (Works a lot like the Arduino WiFi library.)
#include <ESP8266WiFi.h>

//////////////////////// WiFi Definitions ////////////////////////
const char WiFiSSID[] = "YFROBOT";
const char WiFiPSK[] = "yfrobot2016";

/////////////////////// Pin Definitions ///////////////////////
const int LED_PIN = 16;      // Thing's onboard, green LED

////////////////// require address //////////////////
const char YFHost[] = "www.yfrobot.com";
//

// Time to sleep (in seconds):
const int sleepTimeS = 15;  

void setup()
{
  initHardware();
  connectWiFi();
  digitalWrite(LED_PIN, HIGH);
  while (postToHTML() != 1)
  {
    delay(100);
  }
  digitalWrite(LED_PIN, LOW);

  //
  // WAKE_RF_DEFAULT  : do or not do the radio calibration depending on the init byte 108. 做或不做无线电校准依赖于init字节108。
  // WAKE_RFCAL       : do the radio calibration every time.每次都要进行无线电校准。
  // WAKE_NO_RFCAL    : do NOT the radio calibration on wake up.不要在醒来时使用无线电校准。
  // WAKE_RF_DISABLED : on wake up DISABLE the modem. So for example I can't connect the esp to wifi.在醒来时禁用调制解调器。例如,我无法将esp连接到wifi。
  // deepSleep time is defined in microseconds. Multiply seconds by 1e6
  ESP.deepSleep(sleepTimeS * 1000000); // 默认模式 WAKE_RFCAL
}

void loop()
{
}

void connectWiFi()
{
  byte ledStatus = LOW;

  // Set WiFi mode to station (as opposed to AP or AP_STA)
  WiFi.mode(WIFI_STA);
  // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
  // to the stated [ssid], using the [passkey] as a WPA, WPA2,
  // or WEP passphrase.
  WiFi.begin(WiFiSSID, WiFiPSK);

  // Use the WiFi.status() function to check if the ESP8266 is connected to a WiFi network.
  while (WiFi.status() != WL_CONNECTED)
  {
    // Blink the LED
    digitalWrite(LED_PIN, ledStatus); // Write LED high/low
    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;

    // Delays allow the ESP8266 to perform critical tasks
    // defined outside of the sketch. These tasks include
    // setting up, and maintaining, a WiFi connection.
    delay(100);
    // Potentially infinite loops are generally dangerous.
    // Add delays -- allowing the processor to perform other
    // tasks -- wherever possible.
  }
  Serial.println("WiFi connected");
  Serial.println("");
}

void initHardware()
{
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  // Don't need to set ANALOG_PIN as input, that's all it can be.
}

int postToHTML()
{
  // LED turns on when we enter, it'll go off when we
  // successfully post.
  digitalWrite(LED_PIN, HIGH);

  // Now connect to data.sparkfun.com, and post our data:
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(YFHost, httpPort))
  {
    // If we fail to connect, return 0.
    return 0;
  }
  String reurl = "/test/testNetwork.html";    // We now create a URI for the request
  // If we successfully connected, print our Phant post:
  // This will send the request to the server
  client.print(String("GET ") + reurl + " HTTP/1.1\r\n" +
               "Host: " + YFHost + "\r\n" +
               "Connection: close\r\n\r\n");

  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 2000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return 0;
    }
  }
  String getWeb = "";
  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    char e = client.read();
    getWeb += e;
  }
  Serial.print(getWeb);

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);

  return 1; // Return success
}



程序理解:ESP8266上电连接到当前的wifi(路由),然后获取固定网页数据,然后进入睡眠 15s;15s后唤醒,并点亮LED提示睡眠结束;
主要观察,进入睡眠模式的电流情况;
我观察的数据(测试模块NodeMCU V1):
上电连接wifi电流约90ma;进入睡眠模式后电流约18ma;唤醒后电流约为33ma(点亮一个LED)。



作者: lfp136    时间: 2018-4-7 14:02
很受用
作者: devcang    时间: 2018-12-30 15:07

18ma对电池供电,还是不够的,要到达数十ua以下
作者: tiantianyouyou    时间: 2019-5-7 16:28
不错,很值得学习一下




欢迎光临 YFROBOT创客社区 (http://yfrobot.com.cn/) Powered by Discuz! X3.1