Skip to content

Commit 0f513e4

Browse files
committed
add first code for HTTP Update
1 parent 93f215c commit 0f513e4

3 files changed

Lines changed: 225 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=ESP8266httpUpdate
2+
version=1.0
3+
author=Markus Sattler
4+
maintainer=Markus Sattler
5+
sentence=Http Update for ESP8266
6+
paragraph=
7+
url=https://github.com/Links2004/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266httpUpdate
8+
architectures=ESP8266
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/**
2+
*
3+
* @file ESP8266httpUpdate.cpp
4+
* @date 21.06.2015
5+
* @author Markus Sattler
6+
*
7+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
8+
* This file is part of the ESP8266 Http Updater.
9+
*
10+
* This library is free software; you can redistribute it and/or
11+
* modify it under the terms of the GNU Lesser General Public
12+
* License as published by the Free Software Foundation; either
13+
* version 2.1 of the License, or (at your option) any later version.
14+
*
15+
* This library is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License along with this library; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
*
24+
*/
25+
26+
#include "ESP8266httpUpdate.h"
27+
28+
ESP8266httpUpdate::ESP8266httpUpdate(void) {
29+
30+
}
31+
32+
ESP8266httpUpdate::~ESP8266httpUpdate(void) {
33+
34+
}
35+
36+
t_httpUpdate_return ESP8266httpUpdate::update(const char *host, uint16_t port, const char * url, const char *current_version) {
37+
38+
t_httpUpdate_return ret = HTTP_UPDATE_FAILD;
39+
WiFiClient tcp;
40+
DEBUG_HTTP_UPDATE("[httpUpdate] connected to %s:%u %s .... ", host, port, url);
41+
42+
if(!tcp.connect(host, port)) {
43+
DEBUG_HTTP_UPDATE("faild.\n");
44+
return ret;
45+
}
46+
DEBUG_HTTP_UPDATE("ok.\n");
47+
48+
// set Timeout for readBytesUntil and readStringUntil
49+
tcp.setTimeout(2000);
50+
tcp.setNoDelay(true);
51+
52+
String req = "GET ";
53+
54+
req += url;
55+
req += " HTTP/1.1\r\n"
56+
"Host: ";
57+
req += host;
58+
req += "\r\n"
59+
"Connection: close\r\n"
60+
"User-Agent: ESP8266-http-Update\r\n"
61+
"x-ESP8266-STA-MAC: ";
62+
req += WiFi.macAddress();
63+
req += "\r\n"
64+
"x-ESP8266-AP-MAC: ";
65+
req += WiFi.softAPmacAddress();
66+
req += "\r\n"
67+
"x-ESP8266-free-space: ";
68+
req += ESP.getFreeSketchSpace();
69+
req += "\r\n"
70+
"x-ESP8266-sketch-size: ";
71+
req += ESP.getSketchSize();
72+
req += "\r\n"
73+
"x-ESP8266-chip-size: ";
74+
req += ESP.getFlashChipRealSize();
75+
76+
if(current_version[0] != 0x00) {
77+
req += "\r\n"
78+
"x-ESP8266-version: ";
79+
req += current_version;
80+
}
81+
82+
req += "\r\n"
83+
"\r\n";
84+
85+
tcp.write(req.c_str(), req.length());
86+
87+
uint32_t code = 0;
88+
size_t len = 0;
89+
90+
while(true) {
91+
String headerLine = tcp.readStringUntil('\n');
92+
headerLine.trim(); // remove \r
93+
94+
if(headerLine.length() > 0) {
95+
DEBUG_HTTP_UPDATE("[httpUpdate][Header] RX: %s\n", headerLine.c_str());
96+
if(headerLine.startsWith("HTTP/1.")) {
97+
// 9 = lenght of "HTTP/1.x "
98+
code = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
99+
} else if(headerLine.startsWith("Content-Length: ")) {
100+
// 16 = lenght of "Content-Length: "
101+
len = headerLine.substring(16).toInt();
102+
}
103+
} else {
104+
break;
105+
}
106+
}
107+
108+
DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n");
109+
DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n");
110+
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
111+
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
112+
113+
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
114+
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
115+
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
116+
117+
if(current_version[0] != 0x00) {
118+
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version);
119+
}
120+
121+
switch(code) {
122+
case 200: ///< OK (Start Update)
123+
if(len > 0) {
124+
if(len > ESP.getFreeSketchSpace()) {
125+
ret = HTTP_UPDATE_FAILD;
126+
DEBUG_HTTP_UPDATE("[httpUpdate] FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
127+
} else {
128+
if(ESP.updateSketch(tcp, len)) {
129+
// may never reached!
130+
ret = HTTP_UPDATE_OK;
131+
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
132+
} else {
133+
ret = HTTP_UPDATE_FAILD;
134+
DEBUG_HTTP_UPDATE("[httpUpdate] Update failed\n");
135+
}
136+
}
137+
} else {
138+
ret = HTTP_UPDATE_FAILD;
139+
DEBUG_HTTP_UPDATE("[httpUpdate]Content-Length is 0?!\n");
140+
}
141+
break;
142+
case 304:
143+
///< Not Modified (No updates)
144+
ret = HTTP_UPDATE_NO_UPDATES;
145+
break;
146+
case 403:
147+
///< Forbidden
148+
// todo handle login
149+
default:
150+
ret = HTTP_UPDATE_FAILD;
151+
DEBUG_HTTP_UPDATE("[httpUpdate] Code is (%d)\n", code);
152+
break;
153+
}
154+
155+
return ret;
156+
}
157+
158+
t_httpUpdate_return ESP8266httpUpdate::update(String host, uint16_t port, String url, String current_version) {
159+
return update(host.c_str(), port, url.c_str(), current_version.c_str());
160+
}
161+
162+
ESP8266httpUpdate ESPhttpUpdate;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
*
3+
* @file ESP8266httpUpdate.h
4+
* @date 21.06.2015
5+
* @author Markus Sattler
6+
*
7+
* Copyright (c) 2015 Markus Sattler. All rights reserved.
8+
* This file is part of the ESP8266 Http Updater.
9+
*
10+
* This library is free software; you can redistribute it and/or
11+
* modify it under the terms of the GNU Lesser General Public
12+
* License as published by the Free Software Foundation; either
13+
* version 2.1 of the License, or (at your option) any later version.
14+
*
15+
* This library is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License along with this library; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
*
24+
*/
25+
26+
#ifndef ESP8266HTTPUPDATE_H_
27+
#define ESP8266HTTPUPDATE_H_
28+
29+
#include <Arduino.h>
30+
#include <ESP8266WiFi.h>
31+
32+
#define DEBUG_HTTP_UPDATE(...) Serial1.printf( __VA_ARGS__ )
33+
34+
#ifndef DEBUG_HTTP_UPDATE
35+
#define DEBUG_HTTP_UPDATE(...)
36+
#endif
37+
38+
typedef enum {
39+
HTTP_UPDATE_FAILD,
40+
HTTP_UPDATE_NO_UPDATES,
41+
HTTP_UPDATE_OK
42+
} t_httpUpdate_return;
43+
44+
class ESP8266httpUpdate {
45+
public:
46+
ESP8266httpUpdate(void);
47+
~ESP8266httpUpdate(void);
48+
49+
t_httpUpdate_return update(const char *host, uint16_t port, const char * url = "/", const char *current_version = "");
50+
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "");
51+
};
52+
53+
extern ESP8266httpUpdate ESPhttpUpdate;
54+
55+
#endif /* ESP8266HTTPUPDATE_H_ */

0 commit comments

Comments
 (0)