Skip to content

Commit 9767f7d

Browse files
committed
Use light-sleep mode code from the web
esp8266/Arduino#1381 torntrousers proposed this, which seems to work (RST not connected to anything).
1 parent 93c60e1 commit 9767f7d

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed

src/Main.cpp

+71-26
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
#include <Main.h>
33

44
#define CLASS "Main"
5-
//#define TICKS_PERIOD_TIMER1 300000
6-
#define SLEEP_DELAY_US 1000 * 1000 * 10
5+
#define TICKS_PERIOD_TIMER1 300000
6+
#define SLEEP_DELAY_US 1000 * 1000 * 5
7+
#define FPM_SLEEP_MAX_TIME 0xFFFFFFF
8+
#ifndef WIFI_SSID
9+
#error "Must provide WIFI_SSID"
10+
#endif
11+
#ifndef WIFI_PASSWORD
12+
#error "Must provide WIFI_PASSWORD"
13+
#endif
14+
15+
16+
extern "C" {
17+
#include "user_interface.h"
18+
}
719

8-
//volatile char nroInterruptsQueued = 0; // counter to keep track of amount of timing
20+
volatile char nroInterruptsQueued = 0; // counter to keep track of amount of timing
921
// interrupts queued
1022

1123
enum ButtonPressed {
@@ -20,9 +32,10 @@ Module m;
2032
/** INTERRUPTS ***/
2133
/*****************/
2234

23-
//ICACHE_RAM_ATTR void timingInterrupt(void) {
24-
//nroInterruptsQueued++;
25-
//}
35+
ICACHE_RAM_ATTR void timingInterrupt(void) {
36+
nroInterruptsQueued++;
37+
}
38+
2639

2740
/******************/
2841
/*** CALLBACKS ***/
@@ -47,17 +60,17 @@ void setupPins() {
4760
pinMode(LED0_PIN, OUTPUT);
4861
pinMode(LED1_PIN, OUTPUT);
4962
pinMode(LCD_BACKLIGHT_PIN, OUTPUT);
50-
pinMode(BUZZER0_PIN, OUTPUT);
63+
//pinMode(BUZZER0_PIN, OUTPUT); // will break serial communication
5164
log(CLASS, Info, "PINS READY");
5265
}
5366

5467
//void setupInterrupts() {
55-
//timer1_disable();
56-
//timer1_isr_init();
57-
//timer1_attachInterrupt(timingInterrupt);
58-
//timer1_enable(TIM_DIV265, TIM_EDGE, TIM_LOOP);
59-
//timer1_write(TICKS_PERIOD_TIMER1);
60-
//log(CLASS, Info, "INT READY");
68+
// timer1_disable();
69+
// timer1_isr_init();
70+
// timer1_attachInterrupt(timingInterrupt);
71+
// timer1_enable(TIM_DIV265, TIM_EDGE, TIM_LOOP);
72+
// timer1_write(TICKS_PERIOD_TIMER1);
73+
// log(CLASS, Info, "INT READY");
6174
//}
6275

6376
void setup() {
@@ -88,25 +101,57 @@ ButtonPressed readButtons() {
88101
}
89102
}
90103

104+
void doDelays();
105+
void initWifi();
106+
107+
const char* ssid = "ssid";
108+
const char* password = "pass";
109+
91110
void loop() {
92-
//bool wdtInterrupt = nroInterruptsQueued > 0;
93111

94-
//if (wdtInterrupt) {
95-
ButtonPressed button = readButtons();
96-
//nroInterruptsQueued--;
97-
log(CLASS, Info, "INT");
98-
m.loop(button == ButtonModeWasPressed, button == ButtonSetWasPressed, 1/*wdtInterrupt*/);
99-
//m.getClock()->setNroInterruptsQueued(nroInterruptsQueued);
100-
//}
112+
initWifi();
101113

102-
//if (nroInterruptsQueued <= 0) { // no interrupts queued
103-
//nroInterruptsQueued = 0;
104-
//}
114+
Serial.println("Light sleep & delays:");
115+
wifi_set_sleep_type(LIGHT_SLEEP_T);
116+
doDelays();
105117

106-
//ESP.deepSleep(SLEEP_DELAY_US, WAKE_RF_DEFAULT);
118+
Serial.println("Run module:");
119+
ButtonPressed button = readButtons();
120+
log(CLASS, Info, "INT");
121+
m.loop(button == ButtonModeWasPressed, button == ButtonSetWasPressed, true);
107122

108-
log(CLASS, Info, "WOKE UP");
123+
Serial.println("None sleep & delays:");
124+
wifi_set_sleep_type(NONE_SLEEP_T);
125+
doDelays();
109126

127+
WiFi.disconnect();
128+
Serial.print("WiFi disconnected, IP address: "); Serial.println(WiFi.localIP());
129+
Serial.println("Light sleep & delays:");
130+
wifi_set_sleep_type(LIGHT_SLEEP_T);
131+
doDelays();
132+
133+
}
134+
135+
void doDelays() {
136+
Serial.println("Yield for 1 sec");
137+
long endMs = millis() + 1000;
138+
while (millis() < endMs) {
139+
yield();
140+
}
141+
142+
Serial.println("Delay for 1 sec");
143+
delay(1000);
144+
}
145+
146+
void initWifi() {
147+
WiFi.mode(WIFI_STA);
148+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
149+
while ((WiFi.status() != WL_CONNECTED)) {
150+
delay(500);
151+
Serial.print(".");
152+
}
153+
Serial.println("");
154+
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
110155
}
111156

112157
#endif // UNIT_TEST

src/actors/Messenger.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const char *Messenger::getName() {
4343
return name;
4444
}
4545

46+
/*
4647
void Messenger::connectToWifi() {
4748
#ifndef UNIT_TEST
4849
@@ -68,13 +69,14 @@ void Messenger::connectToWifi() {
6869
}
6970
#endif // UNIT_TEST
7071
}
72+
*/
7173

7274
void Messenger::cycle(bool cronMatches) {
7375
if (bot == NULL) {
7476
return;
7577
}
7678
if (cronMatches) {
77-
connectToWifi();
79+
//connectToWifi();
7880
updateClockProperties();
7981
updateBotProperties();
8082
}

src/actors/Messenger.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Messenger : public Actor {
4444
FreqConf *getFrequencyConfiguration();
4545

4646
void setBot(WebBot* b);
47-
void connectToWifi();
47+
//void connectToWifi();
4848
void updateBotProperties();
4949
void updateClockProperties();
5050
void setUpDweetClient(HTTPClient* client, Buffer<MAX_URL_EFF_LENGTH> *url);

0 commit comments

Comments
 (0)