Skip to content

Commit 92f1b16

Browse files
committed
ESP8266 and ESP32 support
1 parent 9fcb3cf commit 92f1b16

12 files changed

+177
-12
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The library is a modification of the Arduino WiFi101OTA library.
1212
* ATmega AVR with at least 64 kB of flash (Arduino Mega, [MegaCore](https://github.com/MCUdude/MegaCore) MCUs, MightyCore 1284p and 644)
1313
* Arduino SAMD boards like Zero, M0 or MKR
1414
* nRF5 board supported by [nRF5 core](https://github.com/sandeepmistry/arduino-nRF5).
15+
* boards supported by ESP8266 and ESP32 Arduino boards package
1516

1617
## Supported networking libraries
1718

@@ -22,6 +23,7 @@ The library is a modification of the Arduino WiFi101OTA library.
2223
* UIPEthernet - shields and modules with ENC28j60 chip
2324
* WiFiSpi - esp8266 as SPI network adapter with WiFiSpi library updated with [this PR](https://github.com/JiriBilek/WiFiSpi/pull/12)
2425
* WiFi - Arduino WiFi Shield (not tested)
26+
* WiFi library of ESP8266 and ESP32 Arduino boards package
2527

2628
UIPEthernet, WiFiSpi and WiFi library don't support UDP multicast for MDNS, so Arduino IDE will not show the network upload port. WiFiLink doesn't support UDP multicast, but the firmware propagates the MDNS record.
2729

@@ -35,13 +37,15 @@ Arduino SAMD boards (Zero, M0, MKR) are supported 'out of the box'.
3537

3638
For nRF5 boards two lines need to be added to platform.txt file of the nRF5 Arduino package (until the PR that adds them is accepted and included in a release). Only nRF51 was tested until now. For details scroll down.
3739

40+
For ESP8266 and ESP32 boards, platform.local.txt from extras folder need to be copied into boards package installation folder and the bundled ArduinoOTA library must be deleted. For details scroll down.
41+
3842
ATmega boards require to flash a modified Optiboot bootloader for flash write operations. Details are below.
3943

4044
## ATmega support
4145

4246
The size of networking library and SD library limit the use of ArduinoOTA library to ATmega MCUs with at least 64 kB flash memory.
4347

44-
*There are other network upload options for here excluded ATmega328p ([Ariadne bootloader](https://github.com/loathingKernel/ariadne-bootloader) for Wiznet chips, [WiFiLink firmware](https://github.com/jandrassy/arduino-firmware-wifilink) for the esp8266).*
48+
*There are other network upload options for here excluded ATmega328p: ([Ariadne bootloader](https://github.com/loathingKernel/ariadne-bootloader) for Wiznet chips, [WiFiLink firmware](https://github.com/jandrassy/arduino-firmware-wifilink) for the esp8266).*
4549

4650
For upload over InternalStorage, Optiboot bootloader with [`copy_flash_pages` function](https://github.com/Optiboot/optiboot/pull/269) is required. Arduino AVR package doesn't use Optiboot for Arduino Mega. For Arduino Mega you can download [my boards definitions](https://github.com/jandrassy/my_boards) and use it [to burn](https://arduino.stackexchange.com/questions/473/how-do-i-burn-the-bootloader) the modified Optiboot and to upload sketches to your Mega over USB and over network.
4751

@@ -51,6 +55,20 @@ To use remote upload from IDE with SDStorage or InternalStorage, copy platform.l
5155

5256
The IDE upload tool is installed with Arduino AVR core package. At least version 1.2 of the arduinoOTA tool is required. For upload from command line without IDE see the command template in extras/avr/platform.local.txt.
5357

58+
## ESP8266 and ESP32 support
59+
60+
The ArduinoOTA library bundled with ESP8266 and ESP32 Arduino packages works only with native WiFi libraries. This library allows to upload a sketch to esp8266 or esp32 over Ethernet with Ethernet or UIPEthernet library. Upload over the native WiFi library works too.
61+
62+
To use this library instead of the bundled library, the bundled library must be removed from the boards package library folder. To override the configuration of OTA upload in platform.txt, copy the platform.local.txt file from extras folder of this library next to platform.txt file in boards package installation folder. Packages are located in ~/.arduino15/packages/ on Linux and %userprofile%\AppData\Local\Arduino15\packages\ on Windows (AppData is a hidden folder).
63+
64+
The esp8266 boards package has bundled Ethernet library. It is an old version of the Arduino Ethernet library which works only with W5100 chips. It is better to remove it from boards package and install the Ethernet library from Library Manager in IDE.
65+
66+
This library supports SPIFFS upload to esp8266 and esp32, but the IDE plugins have the network upload tool hardcoded to espota. It can't be changed in configuration. To upload SPIFFS, call the plugin in Tools menu and after it fails to upload over network, go to location of the created bin file and upload the file with arduinoOTA tool from command line. The location of the file is printed in the IDE console window. Upload command example (linux):
67+
```
68+
~/arduino-1.8.8/hardware/tools/avr/bin/arduinoOTA -address 192.168.1.107 -port 65280 -username arduino -password password -sketch OTEthernet.spiffs.bin -upload /data -b
69+
```
70+
(the same command can be used to upload the sketch binary, only use `-upload /sketch`)
71+
5472
## nRF5 support
5573

5674
Note: Only nRF51 was tested for now
@@ -78,6 +96,10 @@ void shutdown() {
7896
* ATmega
7997
- Arduino Mega
8098
- Badio 1284p
99+
* esp8266
100+
- Wemos D1 mini
101+
* esp32
102+
- ESP32 Dev Module
81103

82104
## Contribution
83105

extras/avr/platform.local.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
# This configuration file supports the general ArduinoOTA library https://github.com/jandrassy/ArduinoOTA
3+
24
## Create output (bin file)
35
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2hex.cmd}" -O binary {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"
46

extras/esp32/platform.local.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# This configuration file supports the general ArduinoOTA library https://github.com/jandrassy/ArduinoOTA
3+
4+
tools.esptool_py.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
5+
tools.esptool_py.upload.network_pattern="{network_cmd}" -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
6+

extras/esp8266/platform.local.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# This configuration file supports the general ArduinoOTA library https://github.com/jandrassy/ArduinoOTA
3+
4+
tools.esptool.network_cmd={runtime.tools.arduinoOTA.path}/bin/arduinoOTA
5+
tools.esptool.upload.network_pattern="{network_cmd}" -address {serial.port} -port 65280 -username arduino -password "{network.password}" -sketch "{build.path}/{build.project_name}.bin" -upload /sketch -b
6+

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=ArduinoOTA
2-
version=1.0.0
2+
version=1.0.1
33
author=Arduino,Juraj Andrassy
44
maintainer=Juraj Andrassy <[email protected]>
55
sentence=Upload sketch over network to Arduino board with WiFi or Ethernet libraries
6-
paragraph=Based on WiFi101OTA library. http://www.arduino.cc/en/Reference/WiFi101OTA
6+
paragraph=Based on WiFi101OTA library. Uploads over Ethernet, UIPEthernet, WiFi101, WiFiNina, WiFiLink, WiFi to SAMD, nRF5, esp8266, esp32 and to ATmega with more then 64 kB flash memory.
77
category=Other
88
url=https://github.com/jandrassy/ArduinoOTA
99
architectures=*

src/ArduinoOTA.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#if FLASHEND >= 0xFFFF
2525
#include "InternalStorageAVR.h"
2626
#endif
27+
#elif defined(ESP8266) || defined(ESP32)
28+
#include "InternalStorageESP.h"
2729
#else
2830
#include "InternalStorage.h"
2931
#endif
@@ -73,7 +75,11 @@ class ArduinoOTAMdnsClass : public ArduinoOTAClass<NetServer, NetClient> {
7375

7476
void begin(IPAddress localIP, const char* name, const char* password, OTAStorage& storage) {
7577
ArduinoOTAClass<NetServer, NetClient>::begin(localIP, name, password, storage);
78+
#if defined(ESP8266) && !(defined(ethernet_h_) || defined(ethernet_h) || defined(UIPETHERNET_H))
79+
mdnsSocket.beginMulticast(localIP, IPAddress(224, 0, 0, 251), 5353);
80+
#else
7681
mdnsSocket.beginMulticast(IPAddress(224, 0, 0, 251), 5353);
82+
#endif
7783
}
7884

7985
void poll() {
@@ -86,7 +92,10 @@ class ArduinoOTAMdnsClass : public ArduinoOTAClass<NetServer, NetClient> {
8692
#if defined(ethernet_h_) || defined(ethernet_h) // Ethernet library
8793
ArduinoOTAMdnsClass <EthernetServer, EthernetClient, EthernetUDP> ArduinoOTA;
8894

89-
#elif defined(WiFiNINA_h) || defined(WIFI_H) // NINA and WiFi101
95+
#elif defined(UIPETHERNET_H) // no UDP multicast implementation yet
96+
ArduinoOTAClass <EthernetServer, EthernetClient> ArduinoOTA;
97+
98+
#elif defined(WiFiNINA_h) || defined(WIFI_H) || defined(ESP8266) || defined(ESP32) // NINA, WiFi101 and Espressif WiFi
9099
#include <WiFiUdp.h>
91100
ArduinoOTAMdnsClass <WiFiServer, WiFiClient, WiFiUDP> ArduinoOTA;
92101

@@ -96,12 +105,8 @@ ArduinoOTAClass <WiFiServer, WiFiClient> ArduinoOTA;
96105
#elif defined(_WIFISPI_H_INCLUDED) // no UDP multicast implementation
97106
ArduinoOTAClass <WiFiSpiServer, WiFiSpiClient> ArduinoOTA;
98107

99-
#elif defined(UIPETHERNET_H) // no UDP multicast implementation
100-
ArduinoOTAClass <EthernetServer, EthernetClient> ArduinoOTA;
101-
102108
#else
103109
#error "Network library not included or not supported"
104110
#endif
105111

106112
#endif
107-

src/InternalStorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
by Juraj Andrassy
2222
*/
2323

24-
#ifndef __AVR__
24+
#if !defined(__AVR__) && !defined(ESP8266) && !defined(ESP32)
2525

2626
#include <Arduino.h>
2727

src/InternalStorageESP.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright (c) 2019 Juraj Andrassy. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#if defined(ESP8266) || defined(ESP32)
20+
21+
#include <Arduino.h>
22+
23+
#ifdef ESP32
24+
#include <Update.h>
25+
#endif
26+
27+
#include "InternalStorageESP.h"
28+
29+
InternalStorageESPClass::InternalStorageESPClass()
30+
{
31+
}
32+
33+
int InternalStorageESPClass::open(int length, uint8_t command)
34+
{
35+
return Update.begin(length, command == 0 ? U_FLASH : U_SPIFFS);
36+
}
37+
38+
size_t InternalStorageESPClass::write(uint8_t b)
39+
{
40+
return Update.write(&b, 1);
41+
}
42+
43+
void InternalStorageESPClass::close()
44+
{
45+
Update.end(false);
46+
}
47+
48+
void InternalStorageESPClass::clear()
49+
{
50+
}
51+
52+
void InternalStorageESPClass::apply()
53+
{
54+
ESP.restart();
55+
}
56+
57+
long InternalStorageESPClass::maxSize()
58+
{
59+
return ESP.getFlashChipSize() / 2; // Update.begin() in open() does the exact check
60+
}
61+
62+
InternalStorageESPClass InternalStorage;
63+
64+
#endif

src/InternalStorageESP.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright (c) 2019 Juraj Andrassy. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _INTERNAL_STORAGE_ESP_H_INCLUDED
20+
#define _INTERNAL_STORAGE_ESP_H_INCLUDED
21+
22+
#include "OTAStorage.h"
23+
24+
class InternalStorageESPClass : public OTAStorage {
25+
public:
26+
27+
InternalStorageESPClass();
28+
29+
virtual int open(int length) {
30+
return open(length, 0);
31+
}
32+
virtual int open(int length, uint8_t command);
33+
virtual size_t write(uint8_t);
34+
virtual void close();
35+
virtual void clear();
36+
virtual void apply();
37+
virtual long maxSize();
38+
39+
private:
40+
};
41+
42+
extern InternalStorageESPClass InternalStorage;
43+
44+
#endif

src/OTAStorage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ OTAStorage::OTAStorage() :
2828
SKETCH_START_ADDRESS(0),
2929
PAGE_SIZE(SPM_PAGESIZE),
3030
MAX_FLASH((uint32_t) FLASHEND + 1)
31+
#elif defined(ESP8266) || defined(ESP32)
32+
SKETCH_START_ADDRESS(0), // not used
33+
PAGE_SIZE(0), // not used
34+
MAX_FLASH(0) // not used
3135
#endif
3236
{
3337
bootloaderSize = 0;
@@ -46,6 +50,8 @@ void ExternalOTAStorage::apply() {
4650
#ifdef __AVR__
4751
wdt_enable(WDTO_15MS);
4852
while (true);
53+
#elif defined(ESP8266) || defined(ESP32)
54+
ESP.restart();
4955
#else
5056
NVIC_SystemReset();
5157
#endif

src/OTAStorage.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
WiFi101OTA version Feb 2017
1919
by Sandeep Mistry (Arduino)
20-
modified for ArduinoOTA Dec 2018
20+
modified for ArduinoOTA Dec 2018, Apr 2019
2121
by Juraj Andrassy
2222
*/
2323

@@ -32,6 +32,10 @@ class OTAStorage {
3232
OTAStorage();
3333

3434
virtual int open(int length) = 0;
35+
virtual int open(int length, uint8_t command) {
36+
(void) command;
37+
return open(length);
38+
}
3539
virtual size_t write(uint8_t) = 0;
3640
virtual void close() = 0;
3741
virtual void clear() = 0;

src/WiFiOTA.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
WiFi101OTA version Feb 2017
1919
by Sandeep Mistry (Arduino)
20-
modified for ArduinoOTA Dec 2018
20+
modified for ArduinoOTA Dec 2018, Apr 2019
2121
by Juraj Andrassy
2222
*/
2323

@@ -251,6 +251,12 @@ void WiFiOTAClass::pollServer(Client& client)
251251
}
252252
} while (header != "");
253253

254+
bool dataUpload = false;
255+
#if defined(ESP8266) || defined(ESP32)
256+
if (request == "POST /data HTTP/1.1") {
257+
dataUpload = true;
258+
} else
259+
#endif
254260
if (request != "POST /sketch HTTP/1.1") {
255261
flushRequestBody(client, contentLength);
256262
sendHttpResponse(client, 404, "Not Found");
@@ -268,7 +274,7 @@ void WiFiOTAClass::pollServer(Client& client)
268274
return;
269275
}
270276

271-
if (_storage == NULL || !_storage->open(contentLength)) {
277+
if (_storage == NULL || !_storage->open(contentLength, dataUpload)) {
272278
flushRequestBody(client, contentLength);
273279
sendHttpResponse(client, 500, "Internal Server Error");
274280
return;

0 commit comments

Comments
 (0)