Skip to content

Commit db3e7c8

Browse files
committed
Merge pull request #920 from pgollor/change-OTA-example
use ArduinoOTA class in OTA-mDNS-SPIFFS example
2 parents a16c41d + c773140 commit db3e7c8

File tree

3 files changed

+51
-108
lines changed

3 files changed

+51
-108
lines changed

libraries/ArduinoOTA/ArduinoOTA.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ArduinoOTA::handle() {
6767

6868
WiFiUDP::stopAll();
6969

70-
if(!Update.begin(size)){
70+
if(!Update.begin(size, cmd)){
7171
if (_serial_debug)
7272
Serial.println("Update Begin Error");
7373
if (_error_callback) _error_callback();

libraries/ESP8266mDNS/examples/OTA-mDNS-SPIFFS/OTA-mDNS-SPIFFS.ino

+49-106
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22
* @file OTA-mDNS-SPIFFS.ino
33
*
44
* @author Pascal Gollor (http://www.pgollor.de/cms/)
5-
* @data 2015-09-18
5+
* @date 2015-09-18
6+
*
7+
* changelog:
8+
* 2015-10-22:
9+
* - Use new ArduinoOTA library.
10+
* - loadConfig function can handle different line endings
11+
* - remove mDNS studd. ArduinoOTA handle it.
612
*
713
*/
814

9-
15+
// includes
1016
#include <ESP8266WiFi.h>
1117
#include <ESP8266mDNS.h>
1218
#include <WiFiUdp.h>
1319
#include <FS.h>
20+
#include <ArduinoOTA.h>
1421

1522

1623
/**
1724
* @brief mDNS and OTA Constants
1825
* @{
1926
*/
2027
#define HOSTNAME "ESP8266-OTA-" ///< Hostename. The setup function adds the Chip ID at the end.
21-
#define APORT 8266 ///< Port for OTA update
2228
/// @}
2329

2430
/**
@@ -29,8 +35,11 @@ const char* ap_default_ssid = "esp8266"; ///< Default SSID.
2935
const char* ap_default_psk = "esp8266esp8266"; ///< Default PSK.
3036
/// @}
3137

32-
/// OTA Update UDP server handle.
33-
WiFiUDP OTA;
38+
/// Uncomment the next line for verbose output over UART.
39+
//#define SERIAL_VERBOSE
40+
41+
/// OTA server handle.
42+
ArduinoOTA ota_server;
3443

3544

3645
/**
@@ -41,7 +50,7 @@ WiFiUDP OTA;
4150
*
4251
* The config file have to containt the WiFi SSID in the first line
4352
* and the WiFi PSK in the second line.
44-
* Line seperator have to be \r\n (CR LF).
53+
* Line seperator can be \r\n (CR LF) \r or \n.
4554
*/
4655
bool loadConfig(String *ssid, String *pass)
4756
{
@@ -61,8 +70,21 @@ bool loadConfig(String *ssid, String *pass)
6170
content.trim();
6271

6372
// Check if ther is a second line available.
64-
uint8_t pos = content.indexOf("\r\n");
65-
if (pos == 0)
73+
int8_t pos = content.indexOf("\r\n");
74+
uint8_t le = 2;
75+
// check for linux and mac line ending.
76+
if (pos == -1)
77+
{
78+
le = 1;
79+
pos = content.indexOf("\n");
80+
if (pos == -1)
81+
{
82+
pos = content.indexOf("\r");
83+
}
84+
}
85+
86+
// If there is no second line: Some information is missing.
87+
if (pos == -1)
6688
{
6789
Serial.println("Infvalid content.");
6890
Serial.println(content);
@@ -72,7 +94,18 @@ bool loadConfig(String *ssid, String *pass)
7294

7395
// Store SSID and PSK into string vars.
7496
*ssid = content.substring(0, pos);
75-
*pass = content.substring(pos + 2);
97+
*pass = content.substring(pos + le);
98+
99+
ssid->trim();
100+
pass->trim();
101+
102+
#ifdef SERIAL_VERBOSE
103+
Serial.println("----- file content -----");
104+
Serial.println(content);
105+
Serial.println("----- file content -----");
106+
Serial.println("ssid: " + *ssid);
107+
Serial.println("psk: " + *pass);
108+
#endif
76109

77110
return true;
78111
} // loadConfig
@@ -105,91 +138,6 @@ bool saveConfig(String *ssid, String *pass)
105138
} // saveConfig
106139

107140

108-
/**
109-
* @brief Handle OTA update stuff.
110-
*
111-
* This function comes from ESP8266 Arduino example:
112-
* https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266mDNS/examples/DNS_SD_Arduino_OTA/DNS_SD_Arduino_OTA.ino
113-
*
114-
* Modification for uploading SPIFFS images from Pascal Gollor.
115-
*
116-
*/
117-
static inline void ota_handle(void)
118-
{
119-
bool spiffs = false;
120-
121-
if (! OTA.parsePacket())
122-
{
123-
return;
124-
}
125-
126-
// Get remote IP
127-
IPAddress remote = OTA.remoteIP();
128-
129-
// Get command
130-
int cmd = OTA.parseInt();
131-
Serial.print("command: ");
132-
Serial.println(cmd);
133-
if (cmd == U_SPIFFS)
134-
{
135-
spiffs = true;
136-
Serial.println("Get SPIFFS image.");
137-
}
138-
139-
// Get remote port
140-
int port = OTA.parseInt();
141-
142-
// Get sketch size.
143-
int sketch_size = OTA.parseInt();
144-
145-
// Output stuff
146-
Serial.print("Update Start: ip:");
147-
Serial.print(remote);
148-
Serial.printf(", port:%d, size:%d\r\n", port, sketch_size);
149-
150-
// Stop all UDP connections.
151-
WiFiUDP::stopAll();
152-
153-
// OTA start Time
154-
uint32_t startTime = millis();
155-
156-
// Start Updateing.
157-
if(!Update.begin(sketch_size, cmd))
158-
{
159-
Serial.println("Update Begin Error");
160-
return;
161-
}
162-
163-
WiFiClient client;
164-
if (client.connect(remote, port))
165-
{
166-
uint32_t written;
167-
while(!Update.isFinished())
168-
{
169-
written = Update.write(client);
170-
if(written > 0) client.print(written, DEC);
171-
}
172-
Serial.setDebugOutput(false);
173-
174-
if(Update.end())
175-
{
176-
client.println("OK");
177-
Serial.printf("Update Success: %u\nRebooting...\n", (unsigned int)(millis() - startTime));
178-
ESP.restart();
179-
}
180-
else
181-
{
182-
Update.printError(client);
183-
Update.printError(Serial);
184-
}
185-
}
186-
else
187-
{
188-
Serial.printf("Connect Failed: %u\n", (unsigned int)(millis() - startTime));
189-
}
190-
} // ota_handle
191-
192-
193141
/**
194142
* @brief Arduino setup function.
195143
*/
@@ -212,8 +160,8 @@ void setup()
212160
WiFi.hostname(hostname);
213161

214162
// Print hostname.
215-
Serial.print("hostname: ");
216-
Serial.println(WiFi.hostname());
163+
Serial.println("Hostname: " + hostname);
164+
//Serial.println(WiFi.hostname());
217165

218166

219167
// Initialize file system.
@@ -295,14 +243,8 @@ void setup()
295243
Serial.println(WiFi.softAPIP());
296244
}
297245

298-
// Initialize mDNS service.
299-
MDNS.begin(hostname.c_str());
300-
301-
// ... Add OTA service.
302-
MDNS.addService("arduino", "tcp", APORT);
303-
304-
// Open OTA Server.
305-
OTA.begin(APORT);
246+
// Start OTA server.
247+
ota_server.setup();
306248
}
307249

308250

@@ -311,7 +253,8 @@ void setup()
311253
*/
312254
void loop()
313255
{
314-
// Handle OTA update.
315-
ota_handle();
256+
// Handle OTA server.
257+
ota_server.handle();
258+
yield();
316259
}
317260

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
YOUR_SSID
1+
YOUR_SSID
22
YOUR_PSK

0 commit comments

Comments
 (0)