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
|
// To upload through terminal you can use: curl -F "image=@firmware.bin" esp8266-webupdate.local/update
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char *ssid = "";
const char *password = "";
int32_t channel = 0; // optional
// uint8_t bssid[6] = NULL; // optional
// #define BSSID [0, 0, 0, 0, 0, 0] // optional
#define CMD_STATIC_IP WiFi.config(IPAddress(192, 168, 1, 28), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0)) // optional
// R G B
const int leds[] = {2, 0, 3};
unsigned int state[] = {0, 0, 0};
#define LRED 0
#define LGREEN 1
#define LBLUE 2
#define OFF 0
#define ON 1
ESP8266WebServer server(80);
#ifndef BSSID
#define BSSID NULL
#endif
void updateLeds() {
for(int i=0; i<3; i++)
analogWrite(leds[i], state[i]);
}
void handleRoot() {
char temp[400];
if(server.hasArg("red"))
state[0] = server.arg("red").toInt();
if(server.hasArg("green"))
state[1] = server.arg("green").toInt();
if(server.hasArg("blue"))
state[2] = server.arg("blue").toInt();
updateLeds();
snprintf(temp, 400,
" {'red': %d,\
'green': %d,\
'blue': %d}\
",
state[LRED],
state[LGREEN],
state[LBLUE]
);
server.send(200, "text/html", temp);
}
void setup (void) {
WiFi.begin(ssid, password, channel, BSSID);
CMD_STATIC_IP;
while(WiFi.status() != WL_CONNECTED) {
updateLeds();
delay(500);
for(int i=0; i<3; i++) {
if(state[i] < 512) {
state[i] = 1023;
} else {
state[i] = 0;
}
}
}
MDNS.begin("esplamp", WiFi.localIP());
server.on("/", handleRoot);
server.onNotFound(handleRoot);
server.onFileUpload([]() {
if(server.uri() != "/update") return;
HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
WiFiUDP::stopAll();
state[LRED] = state[LGREEN] = OFF;
state[LBLUE] = ON;
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if(!Update.begin(maxSketchSpace)){ //start with max available size
state[LRED] = ON;
}
} else if(upload.status == UPLOAD_FILE_WRITE){
state[LGREEN] = ON;
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
state[LRED] = ON;
}
} else if(upload.status == UPLOAD_FILE_END){
state[LBLUE] = OFF;
if(!Update.end(true)){ //true to set the size to the current progress
state[LRED] = ON;
}
}
updateLeds();
yield();
});
server.on("/update", HTTP_POST, [](){
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
ESP.restart();
});
server.begin();
MDNS.addService("http", "tcp", 80);
}
void loop(void) {
server.handleClient();
delay(1);
}
|