YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 15908|回复: 5
打印 上一主题 下一主题

ESP8266 core for Arduino应用:ESP8266睡眠模式(Sleep mode)测试与使用

[复制链接]

签到天数: 866 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2017-5-22 10:06:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
睡眠模式: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
  • modem-sleep 模式只能在ESP8266处于 station 模式连接到接入点(例如:路由器)时使用。 这种模式的应用是当ESP8266仍然需要执行工作,但最大限度地减少无线传输的数量。ESP8266通过Wi-Fi的DTIM Beacon机制与路由器保持连接。
  • light-sleep 模式与modem-sleep 模式相似,但在这种情况下还会关闭时钟并暂停内部CPU,比Modem-sleep功耗更低。
  • deep-sleep 模式下,设备真的睡着了。芯会断开所有Wi-Fi连接与数据连接,进睡眠模式,只有RTC模块仍然作,它可以以指定的定期间隔唤醒。

要进入深度睡眠模式,我们可以调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 : [url=http://www.yfrobot.com/test/testNetwork.html]http://www.yfrobot.com/test/testNetwork.html[/url]
    [url=http://www.yfrobot.com]http://www.yfrobot.com[/url]
    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)。


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对

签到天数: 566 天

[LV.9]以坛为家II

板凳
发表于 2018-12-30 15:07:22 | 只看该作者

18ma对电池供电,还是不够的,要到达数十ua以下
回复 支持 反对

使用道具 举报

签到天数: 22 天

[LV.4]偶尔看看III

地板
发表于 2019-5-7 16:28:30 | 只看该作者
不错,很值得学习一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-4-26 04:43 , Processed in 0.046977 second(s), 25 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表