-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModemSleep.ino
99 lines (79 loc) · 2.16 KB
/
ModemSleep.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <WiFi.h>
#include <BluetoothSerial.h>
#include "driver/adc.h"
#include <esp_bt.h>
#define STA_SSID "<YOUR-SSID>"
#define STA_PASS "<YOUR-PASSWD>"
BluetoothSerial SerialBT;
void setModemSleep();
void wakeModemSleep();
void setup() {
Serial2.begin(115200);
while(!Serial2){delay(500);}
SerialBT.begin("ESP32test"); //Bluetooth device name
SerialBT.println("START BT");
Serial2.println("START WIFI");
WiFi.begin(STA_SSID, STA_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial2.print(".");
}
Serial2.println("");
Serial2.println("WiFi connected");
Serial2.println("IP address: ");
Serial2.println(WiFi.localIP());
setModemSleep();
Serial2.println("MODEM SLEEP ENABLED FOR 5secs");
}
//void loop() {}
unsigned long startLoop = millis();
bool started = false;
void loop() {
if (!started && startLoop+5000<millis()){
// Not use delay It has the own policy
wakeModemSleep();
Serial2.println("MODEM SLEEP DISABLED");
started = true;
}
}
void disableWiFi(){
adc_power_off();
WiFi.disconnect(true); // Disconnect from the network
WiFi.mode(WIFI_OFF); // Switch WiFi off
Serial2.println("");
Serial2.println("WiFi disconnected!");
}
void disableBluetooth(){
// Quite unusefully, no relevable power consumption
btStop();
Serial2.println("");
Serial2.println("Bluetooth stop!");
}
void setModemSleep() {
disableWiFi();
disableBluetooth();
setCpuFrequencyMhz(40);
// Use this if 40Mhz is not supported
// setCpuFrequencyMhz(80);
}
void enableWiFi(){
adc_power_on();
delay(200);
WiFi.disconnect(false); // Reconnect the network
WiFi.mode(WIFI_STA); // Switch WiFi off
delay(200);
Serial2.println("START WIFI");
WiFi.begin(STA_SSID, STA_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial2.print(".");
}
Serial2.println("");
Serial2.println("WiFi connected");
Serial2.println("IP address: ");
Serial2.println(WiFi.localIP());
}
void wakeModemSleep() {
setCpuFrequencyMhz(240);
enableWiFi();
}