我一直在尝试为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=149和https://github.com/esp8266/Arduino/issues/1381,但似乎没有任何代码对我有用。我在这里编写的这段代码最接近可以将功耗降低到8mA的地方。根据该文档的说明,我也无法使用此代码进入自动灯光睡眠。
wifi_fpm_close();
这行奇怪地将功耗从8mA增加到〜20mA(文档再次说明)。
问题
我在这里做的事完全愚蠢吗?
答案 0 :(得分:0)
您的睡眠代码应该在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,它对我有用
谢谢