summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--esplamp.ino117
1 files changed, 117 insertions, 0 deletions
diff --git a/esplamp.ino b/esplamp.ino
new file mode 100644
index 0000000..1aca1ee
--- /dev/null
+++ b/esplamp.ino
@@ -0,0 +1,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);
+}