2
2
* @file OTA-mDNS-SPIFFS.ino
3
3
*
4
4
* @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.
6
12
*
7
13
*/
8
14
9
-
15
+ // includes
10
16
#include < ESP8266WiFi.h>
11
17
#include < ESP8266mDNS.h>
12
18
#include < WiFiUdp.h>
13
19
#include < FS.h>
20
+ #include < ArduinoOTA.h>
14
21
15
22
16
23
/* *
17
24
* @brief mDNS and OTA Constants
18
25
* @{
19
26
*/
20
27
#define HOSTNAME " ESP8266-OTA-" // /< Hostename. The setup function adds the Chip ID at the end.
21
- #define APORT 8266 // /< Port for OTA update
22
28
// / @}
23
29
24
30
/* *
@@ -29,8 +35,11 @@ const char* ap_default_ssid = "esp8266"; ///< Default SSID.
29
35
const char * ap_default_psk = " esp8266esp8266" ; // /< Default PSK.
30
36
// / @}
31
37
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;
34
43
35
44
36
45
/* *
@@ -41,7 +50,7 @@ WiFiUDP OTA;
41
50
*
42
51
* The config file have to containt the WiFi SSID in the first line
43
52
* 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 .
45
54
*/
46
55
bool loadConfig (String *ssid, String *pass)
47
56
{
@@ -61,8 +70,21 @@ bool loadConfig(String *ssid, String *pass)
61
70
content.trim ();
62
71
63
72
// 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 )
66
88
{
67
89
Serial.println (" Infvalid content." );
68
90
Serial.println (content);
@@ -72,7 +94,18 @@ bool loadConfig(String *ssid, String *pass)
72
94
73
95
// Store SSID and PSK into string vars.
74
96
*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
76
109
77
110
return true ;
78
111
} // loadConfig
@@ -105,91 +138,6 @@ bool saveConfig(String *ssid, String *pass)
105
138
} // saveConfig
106
139
107
140
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\n Rebooting...\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
-
193
141
/* *
194
142
* @brief Arduino setup function.
195
143
*/
@@ -212,8 +160,8 @@ void setup()
212
160
WiFi.hostname (hostname);
213
161
214
162
// Print hostname.
215
- Serial.print ( " hostname : " );
216
- Serial.println (WiFi.hostname ());
163
+ Serial.println ( " Hostname : " + hostname );
164
+ // Serial.println(WiFi.hostname());
217
165
218
166
219
167
// Initialize file system.
@@ -295,14 +243,8 @@ void setup()
295
243
Serial.println (WiFi.softAPIP ());
296
244
}
297
245
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 ();
306
248
}
307
249
308
250
@@ -311,7 +253,8 @@ void setup()
311
253
*/
312
254
void loop ()
313
255
{
314
- // Handle OTA update.
315
- ota_handle ();
256
+ // Handle OTA server.
257
+ ota_server.handle ();
258
+ yield ();
316
259
}
317
260
0 commit comments