-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesp8266_powerSavingMode.ino
More file actions
181 lines (157 loc) · 5.32 KB
/
esp8266_powerSavingMode.ino
File metadata and controls
181 lines (157 loc) · 5.32 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*working successfully*/
#include <DNSServer.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <elapsedMillis.h>
#include "esp8266html.h"
#include "FS.h" //include file system header
#define BAUDRATE 9600
#define CONFIGBUTTON 4 // GPIO4 is D2
SoftwareSerial swSer; //for communication to teensy
const byte DNS_PORT = 53;
const char *ssid = "Assistive Button";
const char *password = "testpassword"; //password has at least to be 8 characters, otherwise open network
const char *host = "192.168.4.23";
IPAddress local_IP(192,168,4,23);
IPAddress gateway(192,168,4,1);
IPAddress subnet (255,255,255,0);
ESP8266WebServer server(80); //create a webserver object
DNSServer dnsServer;
//WiFiClient sclient;
const byte numChars = 50;
char receivedChars[numChars]; // an array to store the received data
String modus;
String selected;
String fileList;
unsigned long int timeLimit = 120000; //this is where the wifi time limit can be set in ms
elapsedMillis wifiTime; //counting up
bool check=false;
void configWIFI(){
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAPIP();
dnsServer.setTTL(300);
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
server.begin();
dnsServer.start(DNS_PORT, "www.mybutton.com", local_IP);
// server functions
server.on("/", handleRoot);
server.on("/playlist", HTTP_GET, handlePlaylist);
server.on("/mode", HTTP_POST, handleMode);
server.on("/selected", HTTP_POST, handleSelected);
server.onNotFound(handleWebRequests); //set server all paths are not found so it can handle as per URI
}
void light_sleep(){
//turning off WiFi works!
WiFi.softAPdisconnect(true);
server.close();
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (swSer.available() > 0) {
rc = swSer.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
ndx = numChars - 1;
}
else if (rc == endMarker && ndx > 0){
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
//Serial.println(receivedChars);
fileList = receivedChars;
}
}
}
void handleRoot() {
server.send_P(200, "text/html", index_html);} //the p is for sending from program memory
void handlePlaylist(){
recvWithEndMarker();
Serial.println(fileList);
server.send(200, "text/plain", fileList);
wifiTime = 0;
}
void handleMode(){
modus = server.arg("plain");
server.send(204, ""); //http status 204: no content
swSer.println(modus);
wifiTime = 0;
}
void handleSelected(){
selected=server.arg("plain");
server.send(204, ""); //http status 204: no content
swSer.println(selected);
wifiTime = 0;
}
void handleWebRequests(){
if(loadFromSpiffs(server.uri())) return;
String message = "File Not Detected\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
Serial.println(message);
}
bool loadFromSpiffs(String path){
String dataType = "text/plain";
if(path.endsWith("/")) path += "index.htm";
if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if(path.endsWith(".html")) dataType = "text/html";
else if(path.endsWith(".htm")) dataType = "text/html";
else if(path.endsWith(".css")) dataType = "text/css";
else if(path.endsWith(".js")) dataType = "application/javascript";
else if(path.endsWith(".png")) dataType = "image/png";
else if(path.endsWith(".gif")) dataType = "image/gif";
else if(path.endsWith(".jpg")) dataType = "image/jpeg";
else if(path.endsWith(".ico")) dataType = "image/x-icon";
else if(path.endsWith(".xml")) dataType = "text/xml";
else if(path.endsWith(".pdf")) dataType = "application/pdf";
else if(path.endsWith(".zip")) dataType = "application/zip";
File dataFile = SPIFFS.open(path.c_str(), "r");
if (server.hasArg("download")) dataType = "application/octet-stream";
if (server.streamFile(dataFile, dataType) != dataFile.size()) {
}
dataFile.close();
return true;
}
void setup() {
Serial.begin(BAUDRATE); //for console output via USB
swSer.begin(BAUDRATE, SWSERIAL_8N1, 14, 12, false); //pin 14/D5 is RX, pin12/D6 is TX
SPIFFS.begin(); //initialize File System
pinMode(CONFIGBUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
configWIFI();
light_sleep();
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on
delay(5000);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
dnsServer.processNextRequest();
server.handleClient(); // to handle actual incoming of http requests: handleClient method
if(digitalRead(CONFIGBUTTON)== LOW){
WiFi.forceSleepWake();
wifi_station_connect();
configWIFI();
}
if(WiFi.softAPgetStationNum()==0) //if no one is connected to ap
wifiTime = 0;
if(wifiTime==timeLimit)
light_sleep();
}