Server OTA support for ESP8266

Dear all,

we just made a new release of the server - 0.26.3. This server version allows you to perform OTA for your ESP devices. For now, this is supported only for private servers while we are in testing mode.

How does it work:

  • You need to use regular sketch for exported apps;
  • After you launched your hardware you are ready for OTA;
  • You can trigger firmware update for specific hardware via it’s token with next curl command:

curl -v -F file=@Template_ESP8266.ino.nodemcu.bin --insecure -u admin@blynk.cc:admin https://localhost:9443/admin/ota/start?token=123

Template_ESP8266.ino.nodemcu.bin - is relative (or full) path to your firmware;
--insecure - flag for servers with self-generated certificates. You don’t need this flag if you used Let’s Encrypt or other trusted certificates;
admin@blynk.cc:admin - admin credentials to your server. This is default ones. Format is username:password;
token - is token of your hardware you want apply the firmware update to. The firmware update will be initiated only in case device is online.

To trigger OTA for all devices when they are connected to the cloud you need to remove the token part.

curl -v -F file=@Template_ESP8266.ino.nodemcu.bin --insecure -u admin@blynk.cc:admin https://localhost:9443/admin/ota/start

In that case, OTA will be triggered right after device connected to the server.

This is basic OTA sketch:

/**************************************************************
 * This is a DEMO. You can use it only for development and testing.
 *
 * If you would like to add these features to your product,
 * please contact Blynk for Business:
 *
 *                  http://www.blynk.io/
 *
 **************************************************************
 *
 * How to trigger an OTA update?
 *   1. In Arduino IDE menu: Sketch -> Export compiled Binary
 *   2. Open console, navigate to the sketch directory
 *   3.a Trigger update using HTTP API on local server for specific hardware:
 *       curl -v -F file=@Template_ESP8266.ino.nodemcu.bin --insecure -u admin@blynk.cc:admin https://localhost:9443/admin/ota/start?token=123
 *   3.b Trigger update using HTTP API on local server for all hardware:
 *       curl -v -F file=@Template_ESP8266.ino.nodemcu.bin --insecure -u admin@blynk.cc:admin https://localhost:9443/admin/ota/start
 *
 * More about ESP8266 OTA updates:
 *  https://github.com/esp8266/Arduino/blob/master/doc/ota_updates/readme.md
 */

#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

String overTheAirURL;

BLYNK_WRITE(InternalPinOTA) {
  overTheAirURL = param.asString();

  // Disconnect, not to interfere with OTA process
  Blynk.disconnect();

  // Start OTA
  BlynkState::set(MODE_OTA_UPGRADE);
  delay(500);
}

void enterOTA() {
  BlynkState::set(MODE_OTA_UPGRADE);

  DEBUG_PRINT(String("Firmware update URL: ") + overTheAirURL);
  switch (ESPhttpUpdate.update(overTheAirURL, BOARD_FIRMWARE_VERSION)) {
  case HTTP_UPDATE_FAILED:
    DEBUG_PRINT(String("Firmware update failed (error ") + ESPhttpUpdate.getLastError() + "): " + ESPhttpUpdate.getLastErrorString());
    BlynkState::set(MODE_ERROR);
    break;
  case HTTP_UPDATE_NO_UPDATES:
    DEBUG_PRINT("No firmware updates available.");
    BlynkState::set(MODE_CONNECTING_CLOUD);
    break;
  case HTTP_UPDATE_OK:
    DEBUG_PRINT("Firmware update: OK.");
    delay(10);
    restartMCU();
    break;
  }
}

In order to make firmware in Arduino IDE - go to menu: Sketch -> Export compiled Binary.
Also ESP8266 right now takes firmware via HTTP. And not HTTPS.

P. S. Added docs - http://docs.blynk.cc/#ota

7 Likes

@Dmitriy looks a nice addition when we get our head around it. Been following some of your progress on GitHub.

1 Like

This is just first step. Later it would be much more usable and “nicer” when we will finish web ui.

2 Likes

is private server here means local server or the paid server for business ?

Both.

1 Like

some suggestions here which it maybe already in your plane

  • in Admin page give option to categories the Devices - Hardware has Blynk Library and Connected to the Local server - so if i have a different HW version of a specific models which all should run the same FW version to be able later to Trigger Update to Selected group of HW together.

  • Add the Option in Admin Page to brows and select the New FW directly without the need to use curl CLI commands.

  • See the Status of the update on Admin page , so we can now which Device updates and which is update pending.

  • Add option on App side to notify the user to initiate a restart request for if the Update is pending and the cases of the Hardware is implemented on Always On Application which user can’t pull off the power or reset the MCU - the developer should take that situations scenarios in considered .

Just giving this a go…

This is the output I got… no sign of “success” message. Has it performed the OTA update to the firmware?
What happens at the hardware side? Do I need to cycle power or does it do a software reboot?

root@blynk-v:/data# curl -v -F file=Project-Unicorn-Provisioning.ino.adafruit.bin --insecure -u admin@blynk.cc:admin https://192.168.1.2:9443/admin/
ota/start                                                                                                                                           
* Hostname was NOT found in DNS cache                                                                                                               
*   Trying 192.168.1.2...                                                                                                                           
* Connected to 192.168.1.2 (192.168.1.2) port 9443 (#0)                                                                                             
* successfully set certificate verify locations:                                                                                                    
*   CAfile: none                                                                                                                                    
  CApath: /etc/ssl/certs                                                                                                                            
* SSLv3, TLS handshake, Client hello (1):                                                                                                           
* SSLv3, TLS handshake, Server hello (2):                                                                                                           
* SSLv3, TLS handshake, CERT (11):                                                                                                                  
* SSLv3, TLS handshake, Server key exchange (12):                                                                                                   
* SSLv3, TLS handshake, Server finished (14):                                                                                                       
* SSLv3, TLS handshake, Client key exchange (16):                                                                                                   
* SSLv3, TLS change cipher, Client hello (1):                                                                                                       
* SSLv3, TLS handshake, Finished (20):                                                                                                              
* SSLv3, TLS change cipher, Client hello (1):                                                                                                       
* SSLv3, TLS handshake, Finished (20):                                                                                                              
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256                                                                                        
* Server certificate:                                                                                                                               
*        subject: CN=example.com                                                                                                                    
*        start date: 2016-09-04 20:03:36 GMT                                                                                                        
*        expire date: 2016-09-04 20:03:36 GMT                                                                                                       
*        issuer: CN=example.com                                                                                                                     
*        SSL certificate verify result: self signed certificate (18), continuing anyway.                                                            
*Server auth using Basic with user 'admin@blynk.cc'                                                                                                 
> POST /admin/ota/start HTTP/1.1                                                                                                                    
> Authorization: Basic YWRtaW5AYmx5bmsuY2M6YWRtaW4=                                                                                                 
> User-Agent: curl/7.38.0                                                                                                                           
> Host: 192.168.1.2:9443                                                                                                                            
> Accept: */*                                                                                                                                       
> Content-Length: 184                                                                                                                               
> Expect: 100-continue                                                                                                                              

@Jamin do you use Dynamic Provisioning sketch (App Export template)?
On the fw side, you should see that upgrade uri is received by the hw and it should enter OTA mode. Then hardware resets automatically and reboots with (hopefully) new firmware.

1 Like

Yes using the template sketch with dynamic provisioning… works a charm!

I will give it another go with terminal debugging on.

1 Like

Not getting as far as I was earlier…


root@blynk-v:/data/data# curl -v -F file=Project-Unicorn-Provisioning.ino.adafruit.bin --insecure -u admin@blynk.cc:admin https://192.168.1.2:9443/admin/ota/start                
* Hostname was NOT found in DNS cache                                                                                                                                             
*   Trying 192.168.1.2...                                                                                                                                                         
* Connected to 192.168.1.2 (192.168.1.2) port 9443 (#0)                                                                                                                           
* successfully set certificate verify locations:                                                                                                                                  
*   CAfile: none                                                                                                                                                                  
  CApath: /etc/ssl/certs                                                                                                                                                          
* SSLv3, TLS handshake, Client hello (1):                                                                                                                                         
* SSLv3, TLS handshake, Server hello (2):                                                                                                                                         
* SSLv3, TLS handshake, CERT (11):                                                                                                                                                
* SSLv3, TLS handshake, Server key exchange (12):                                                                                                                                 
* SSLv3, TLS handshake, Server finished (14):                                                                                                                                     
* SSLv3, TLS handshake, Client key exchange (16):                                                                                                                                 
* SSLv3, TLS change cipher, Client hello (1):                                                                                                                                     
* SSLv3, TLS handshake, Finished (20):                                                                                                                                            
* SSLv3, TLS change cipher, Client hello (1):                                                                                                                                     
*SSLv3, TLS handshake, Finished (20):                                                                                                                                             
 * *SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256                                                                                                                    
 Server certificate:                                                                                                                                                              
*        subject: CN=example.com                                                                                                                                                  
 *       start date: 2016-09-04 20:03:36 GMT                                                                                                                                      
*        expire date: 2016-09-04 20:03:36 GMT                                                                                                                                     
*        issuer: CN=example.com                                                                                                                                                   
*        SSL certificate verify result: self signed certificate (18), continuing anyway.                                                                                          
* Server auth using Basic with user 'admin@blynk.cc'                                                                                                                              
> POST /admin/ota/start HTTP/1.1                                                                                                                                                  
> Authorization: Basic YWRtaW5AYmx5bmsuY2M6YWRtaW4=                                                                                                                               
> User-Agent: curl/7.38.0                                                                                                                                                         
> Host: 192.168.1.2:9443                                                                                                                                                          
> Accept: */*                                                                                                                                                                     
> Content-Length: 184                                                                                                                                                             
> Expect: 100-continue                                                                                                                                                            
> Content-Type: multipart/form-data; boundary=------------------------a81fa6f4694fb06c                                                                                            
>                                                                                                                                                                                 
< HTTP/1.1 100 Continue                                                                                                                                                           
< HTTP/1.1 500 Internal Server Error                                                                                                                                              
< connection: keep-alive                                                                                                                                                          
< content-length: 0                                                                                                                                                               
* HTTP error before end of send, stop sending                                                                                                                                     
<                                                                                                                                                                                 
* Closing connection 0                                                                                                                                                            
* SSLv3, TLS alert, Client hello (1):

Is there any way to find out why im getting HTTP/1.1 500 Internal Server Error ?

Yes, in blynk.log. Please post here what you have found. Also what server version do you have?

Latest 0.27.1

The entire file:

01:20:55.301 ERROR- Error invoking handler. Reason : Can't find field..
01:20:58.992 ERROR- Error invoking handler. Reason : Can't find field..
03:43:53.415 ERROR- Look like you are trying to connect with IPv6 : 0:0:0:0:0:0:0:1%0.  While in 'allowed.administrator.ips' you are using IPv4.
10:17:06.435 ERROR- Error invoking handler. Reason : Can't find field..

Should be

file=@Project-Unicorn-Provisioning.ino.adafruit.bin

1 Like

A bloody typo this whole time … :disappointed_relieved:

1 Like

Here are the logs from the firmware.

Where does it get that IP from? 172.17.0.2 isnt on my network.

EDIT: looks like this IP is the Docker image’s internal IP (running blynk)

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP-12

[6453] Connecting to ds.cu***bs.co.nz:8442
[6465] Ready (ping: 1ms).
[6467] CONNECTING_CLOUD => RUNNING
[6521] Disconnected
[6522] RUNNING => OTA_UPGRADE
[7028] Firmware update URL: http://172.17.0.2:8080/static/ota/FUp_2441873656843727242_upload.bin?auth=5d3500f0990a48c5a399e18dfde63ef2
[13141] Firmware update failed (error -1): HTTP error: connection refused
[13142] OTA_UPGRADE => ERROR
[23426] Restarting after error.

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v3de0c112
~ld

Could this be it?

EDIT: is there any chance you could use the external server address instead of pulling the host IP?
Could I push updates over the internet and not just within the local network.

You need to set server.host in server.properties

1 Like

Okay so done some digging.

I’ve found server.host in server.properties and updated it to my hostname.

I’ve then performed another OTA update, firmware receives it fine, but its now adding the arg ?auth= which is causing the following error. The URL works if you remove the auth arg

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP-12

[5001] Connecting to ds.customtabs.co.nz:8442
[5019] Ready (ping: 3ms).
[5022] CONNECTING_CLOUD => RUNNING
[5075] Disconnected
[5075] RUNNING => OTA_UPGRADE
[5582] Firmware update URL: http://ds.customtabs.co.nz:8080/static/ota/FUp_4522110642755461446_upload.bin?auth=5d3500f0990a48c5a399e18dfde63ef2
[5598] Firmware update failed (error -102): File not Found (404)
[5599] OTA_UPGRADE => ERROR
[15611] Restarting after error.

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v3de0c112
~ld