【问题标题】:Light Sleep with GPIO Wakeup in ESP8266ESP8266 中带有 GPIO 唤醒的轻度睡眠
【发布时间】:2019-03-16 23:02:12
【问题描述】:

我一直在尝试为 ESP8266 创建轻度睡眠,我想让系统睡眠并在 GPIO 输入上触发唤醒(就像按下按钮一样)。

它的代码非常简单

#include <ESP8266WiFi.h> 
#include <Wire.h> 
#define PIN 5

extern "C"
{
  #include "gpio.h"
}

extern "C"
{
  #include "user_interface.h"
}


int wifi_connection_attempts;
const char * password = "PASSWORD";
const char * ssid = "SSID";
bool isAsleep = false;
void setup()
{
  Serial.begin(115200);
  delay(100);
  Serial.println("Setup!");
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // set sleep type, the above         posters wifi_set_sleep_type() didnt seem to work for me although it did let me     compile and upload with no errors 
  WiFi.mode(WIFI_STA);
  connect_to_wifi();
  gpio_pin_wakeup_enable(digitalPinToInterrupt(PIN), GPIO_PIN_INTR_ANYEDGE);
  gpio_intr_handler_register(wakeup, NULL);
  delay(5000);
  Serial.println("Going to Sleep!");
  sleep();
  delay(5000);
}

void connect_to_wifi()
{
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi..");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("Connected to the WiFi network");
  }
  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
}

void sleep()
{
  Serial.println("Going to sleep");
  delay(1000);
  wifi_station_disconnect();
  Serial.println("WifiDisconnected");
  bool stopped;
  do {
    stopped = wifi_station_get_connect_status() == DHCP_STOPPED;
    if (!stopped)
    {
      Serial.println("dhcp not stopped?");
      delay(100);
    }
  } while (!stopped);
  Serial.println("---off...");
  wifi_set_opmode(NULL_MODE);
  wifi_set_sleep_type(MODEM_SLEEP_T);
  wifi_fpm_open();
  wifi_fpm_do_sleep(0xFFFFFFF);
}

void wakeup(uint32 interruptMask, void * arg)
{
  os_printf("Interrupted: %x\n", interruptMask);
  Serial.print("lastIntr:");
  Serial.println(system_get_time());
  gpio_intr_ack(interruptMask);
  Serial.println("Waking Up");
  wifi_fpm_do_wakeup();
  wifi_fpm_close();
  wifi_set_opmode(STATION_MODE);
  wifi_station_connect();
  Serial.println("Woke up!");
  delay(10000);
}

void loop()
{
  /* do whatever you want */
  Serial.println("Loop!");
  delay(5000);
}

相信我,当我这么说的时候,我已经尝试阅读所有关于轻度睡眠如何工作的内容,我的参考资料包括 https://blog.creations.de/?p=149https://github.com/esp8266/Arduino/issues/1381 似乎没有一个代码对我有用。我在这里编写的这段代码是我可以将功耗降至 8mA 的最接近的地方。我也未能成功使用此代码进入 Auto Light sleep,根据文档,它应该这样做。

wifi_fpm_close(); 

这条线奇怪地把用电量从 8mA 增加到了 ~20mA(这也是文档中提到的)。

问题

  1. 如何实现难以捉摸的 1mA 强制 Light sleep
  2. 无论我对 PIN 5 做什么,我的系统都拒绝唤醒。我将如何为唤醒编写回调处理程序?或者至少让循环在 GPIO 5 中断上运行。 (清醒的中断效果很好,但在“睡眠”时就不行了。)

我在这里做了什么完全愚蠢的事情吗?

【问题讨论】:

  • 你只能从pin 16唤醒,但这也只能用于深度睡眠,否则你不能“从gpio唤醒”

标签: esp8266 nodemcu arduino-esp8266


【解决方案1】:

您的睡眠代码应该在 loop() 函数中。你可以这样做

#define WAKE_UP_PIN 4

void light_sleep(){
    wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
    wifi_fpm_open();
    gpio_pin_wakeup_enable(GPIO_ID_PIN(LIGHT_WAKE_PIN), GPIO_PIN_INTR_HILEVEL);
    wifi_fpm_do_sleep(0xFFFFFFF);
}

 void loop(){
   delay(200);
   light_sleep();
   delay(200);
   Serial.println("Wake Up Sid");
 }

我是这样做的,我提到了你提到的 github issue,它对我有用

谢谢你

【讨论】:

  • 你解决了这个问题吗,如果是,请回复,如果这个答案有任何帮助,那么你可以将其标记为已接受的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 2018-09-29
  • 2020-11-19
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多