Skip to content

Commit d8f57cb

Browse files
committed
Merge pull request #837 from mangelajo/esp8266-HTTPUpdateServer
ESP8266HTTPUpdateServer extracted from WebUpdate example.
2 parents ca9f570 + 91b1bd4 commit d8f57cb

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
To upload through terminal you can use: curl -F "[email protected]" esp8266-webupdate.local/update
3+
*/
4+
5+
#include <ESP8266WiFi.h>
6+
#include <WiFiClient.h>
7+
#include <ESP8266WebServer.h>
8+
#include <ESP8266mDNS.h>
9+
#include <ESP8266HTTPUpdateServer.h>
10+
11+
const char* host = "esp8266-webupdate";
12+
const char* ssid = "........";
13+
const char* password = "........";
14+
15+
ESP8266WebServer httpServer(80);
16+
ESP8266HTTPUpdateServer httpUpdater;
17+
18+
void setup(void){
19+
20+
Serial.begin(115200);
21+
Serial.println();
22+
Serial.println("Booting Sketch...");
23+
WiFi.mode(WIFI_AP_STA);
24+
WiFi.begin(ssid, password);
25+
26+
while(WiFi.waitForConnectResult() != WL_CONNECTED){
27+
WiFi.begin(ssid, password);
28+
Serial.println("WiFi failed, retrying.");
29+
}
30+
31+
MDNS.begin(host);
32+
33+
httpUpdater.setup(&httpServer);
34+
httpServer.begin();
35+
36+
MDNS.addService("http", "tcp", 80);
37+
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
38+
}
39+
40+
void loop(void){
41+
httpServer.handleClient();
42+
delay(1);
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#######################################
2+
# Syntax Coloring Map For HTTPUpdateServer
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
ESP8266HTTPUpdateServer KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
setup KEYWORD2
17+
18+
#######################################
19+
# Constants (LITERAL1)
20+
#######################################
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ESP8266HTTPUpdateServer
2+
version=1.0
3+
author=Ivan Grokhotkov, Miguel Ángel Ajo
4+
maintainer=Ivan Grokhtkov <[email protected]>
5+
sentence=Simple HTTP Update server based on the ESP8266WebServer
6+
paragraph=The library accepts HTTP post requests to the /update url, and updates the ESP8266 firmware.
7+
category=Communication
8+
url=
9+
architectures=esp8266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <Arduino.h>
2+
#include <WiFiClient.h>
3+
#include <WiFiServer.h>
4+
#include <ESP8266WebServer.h>
5+
#include <WiFiUdp.h>
6+
#include "ESP8266HTTPUpdateServer.h"
7+
8+
9+
const char* ESP8266HTTPUpdateServer::_serverIndex =
10+
R"(<html><body><form method='POST' action='/update' enctype='multipart/form-data'>
11+
<input type='file' name='update'>
12+
<input type='submit' value='Update'>
13+
</form>
14+
</body></html>)";
15+
16+
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
17+
{
18+
_serial_output = serial_debug;
19+
_server = NULL;
20+
}
21+
22+
void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server)
23+
{
24+
_server = server;
25+
26+
// handler for the /update form page
27+
_server->on("/update", HTTP_GET, [&](){
28+
_server->sendHeader("Connection", "close");
29+
_server->sendHeader("Access-Control-Allow-Origin", "*");
30+
_server->send(200, "text/html", _serverIndex);
31+
});
32+
33+
// handler for the /update form POST (once file upload finishes)
34+
_server->on("/update", HTTP_POST, [&](){
35+
_server->sendHeader("Connection", "close");
36+
_server->sendHeader("Access-Control-Allow-Origin", "*");
37+
_server->send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
38+
ESP.restart();
39+
});
40+
41+
// handler for the file upload, get's the sketch bytes, and writes
42+
// them through the Update object.
43+
_server->onFileUpload([&](){
44+
if(_server->uri() != "/update") return;
45+
HTTPUpload& upload = _server->upload();
46+
if(upload.status == UPLOAD_FILE_START){
47+
if (_serial_output)
48+
Serial.setDebugOutput(true);
49+
WiFiUDP::stopAll();
50+
if (_serial_output)
51+
Serial.printf("Update: %s\n", upload.filename.c_str());
52+
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
53+
if(!Update.begin(maxSketchSpace)){//start with max available size
54+
if (_serial_output) Update.printError(Serial);
55+
}
56+
} else if(upload.status == UPLOAD_FILE_WRITE){
57+
if (_serial_output) Serial.printf(".");
58+
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
59+
if (_serial_output) Update.printError(Serial);
60+
61+
}
62+
} else if(upload.status == UPLOAD_FILE_END){
63+
if(Update.end(true)){ //true to set the size to the current progress
64+
if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
65+
} else {
66+
if (_serial_output) Update.printError(Serial);
67+
}
68+
if (_serial_output) Serial.setDebugOutput(false);
69+
} else if(upload.status == UPLOAD_FILE_ABORTED){
70+
Update.end();
71+
if (_serial_output) Serial.println("Update was aborted");
72+
}
73+
yield();
74+
});
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef __HTTP_UPDATE_SERVER_H
2+
#define __HTTP_UPDATE_SERVER_H
3+
4+
class ESP8266WebServer;
5+
6+
class ESP8266HTTPUpdateServer
7+
{
8+
private:
9+
bool _serial_output;
10+
ESP8266WebServer *_server;
11+
static const char *_serverIndex;
12+
public:
13+
ESP8266HTTPUpdateServer(bool serial_debug=false);
14+
void setup(ESP8266WebServer *server=NULL);
15+
};
16+
17+
18+
#endif

0 commit comments

Comments
 (0)