Reputation: 343
I am trying to implement the FOTA on esp8266. I am building the 2 images: user1.bin and user2.bin, writing the user2.bin to the esp8266 with 1.7 bootloader and when I am trying to make the OTA update I am getting the error.
the code:
#include "user_interface.h"
#include "mem.h"
#include "osapi.h"
#include "espconn.h"
#include "upgrade.h"
#include "user_update.h"
void ICACHE_FLASH_ATTR user_esp_platform_upgrade_rsp(void *arg) {
struct upgrade_server_info *server = arg;
if (server->upgrade_flag == true) {
INFO("user_esp_platform_upgarde_successfully\n");
}
else {
INFO("user_esp_platform_upgrade_failed\n");
}
os_free(server->url);
server->url = NULL;
os_free(server);
server = NULL;
}
void ICACHE_FLASH_ATTR user_esp_platform_upgrade_begin(struct espconn *pespconn, struct upgrade_server_info *server)
{
uint8 user_bin[9] = {0};
INFO("user_bin is set\r\n");
// os_memcpy(devkey, esp_param.devkey, 40);
os_memcpy(server->ip, pespconn->proto.tcp->remote_ip, 4);
INFO("pespconn->proto.tcp->remote_ip\r\n");
if (server->port == NULL) {
INFO("server port is null\r\n");
#ifdef UPGRADE_SSL_ENABLE
server->port = 443;
#else
server->port = 80;
#endif
}
else
{
INFO("server port is NOT null\r\n");
}
server->check_cb = user_esp_platform_upgrade_rsp;
INFO("callback is set\r\n");
server->check_times = 120000;
INFO("checktimes is set\r\n");
if (server->url == NULL) {
server->url = (uint8 *)os_zalloc(512);
}
if (system_upgrade_userbin_check() == UPGRADE_FW_BIN1)
{
os_memcpy(user_bin, "user2.bin", 10);
}
else if (system_upgrade_userbin_check() == UPGRADE_FW_BIN2)
{
os_memcpy(user_bin, "user1.bin", 10);
}
os_sprintf(server->url,"GET /%s"
"HTTP/1.0\r\nHost: %s:%d\r\n",
user_bin, IP2STR(server->ip),
server->port);
INFO("%s\n", server->url);
#ifdef UPGRADE_SSL_ENABLE
if (system_upgrade_start_ssl(server) == false)
{
#else
if (system_upgrade_start(server) == false)
{
#endif
INFO("upgrade is already started\n");
}
}
}
void ICACHE_FLASH_ATTR ota_finished_callback(void *arg)
{
struct upgrade_server_info *update = arg;
if (update->upgrade_flag == true)
{
INFO("[OTA]success; rebooting!\n");
system_upgrade_reboot();
}
else
{
INFO("[OTA]failed!\n");
}
os_free(update->pespconn);
os_free(update->url);
os_free(update);
}
void ICACHE_FLASH_ATTR handleUpgrade(uint8_t serverVersion, const char *server_ip, uint16_t port, const char *path)
{
const char* file;
uint8_t userBin = system_upgrade_userbin_check();
switch (userBin)
{
case UPGRADE_FW_BIN1: file = "user2.bin"; break;
case UPGRADE_FW_BIN2: file = "user1.bin"; break;
default:
INFO("[OTA]Invalid userbin number!\n");
return;
}
uint16_t version=1;
if (serverVersion <= version)
{
INFO("[OTA]No update. Server version:%d, local version %d\n", serverVersion, version);
return;
}
INFO("[OTA]Upgrade available version: %d\n", serverVersion);
struct upgrade_server_info* update = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
update->pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));
os_memcpy(update->ip, server_ip, 4);
update->port = port;
INFO("[OTA]Server "IPSTR":%d. Path: %s%s\n", IP2STR(update->ip), update->port, path, file);
update->check_cb = ota_finished_callback;
update->check_times = 10000;
update->url = (uint8 *)os_zalloc(512);
os_sprintf((char*)update->url,
"GET %s%s HTTP/1.1\r\n"
"Host: "IPSTR":%d\r\n"
"Connection: close\r\n"
"\r\n",
path, file, IP2STR(update->ip), update->port);
if (system_upgrade_start(update) == false)
{
INFO("[OTA]Could not start upgrade\n");
os_free(update->pespconn);
os_free(update->url);
os_free(update);
}
else
{
INFO("[OTA]Upgrading...\n");
}
}
The debug data:
[INFO] BOOTUP...
STATION_IDLE
WIFI connected
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt
connected with Home, channel 7
dhcp client start...
ip:192.168.31.30,mask:255.255.255.0,gw:192.168.31.1
WIFI connected
WIFI got ip
firmware version 2
[OTA]Upgrade available version: 2
[OTA]Server 192.168.31.73:3000. Path: /user2.bin
system_upgrade_start
upgrade_connect 41560
[OTA]Upgrading...
upgrade_connect_cb
pusrdata = HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/octet-stream
Content-Length: 312356
Content-Disposition: attachment; filename=user2.bin
x-MD5: 99771edeb6f0f3e6b1eacccd5ee521fe
Date: Mon, 06 Nov 2017 20:14:39 GMT
Connection: close
sumlength = 312356
sec_block 77
.............................................................................upgrade_get_sum_disconcb 39232
erase sector=129 ok
....
erase sector=205 ok
ALL=77 sectors erase ok!
upgrade_connect_cb
sumlength = 312356
upgrade file download start.
please check the bin file
totallen = 2664
totallen = 4124
upgrade_check
[OTA]failed!
pm open,type:2 0
Can i make configure the chip to boot from the second sector to check the user2.bin? Can be the problem in the update server? Maybe the problem in the code or the makefile?
Upvotes: 2
Views: 626
Reputation: 2079
I made my server in node.js
version 12.x
on Debian and had the same problem. When upgrading it was failing with Please check the bin file
message.
Then I downgraded the node.js
to version 6.x
and it started to work!
I tried also node.js
version 8.x
and it again gave the error.
UPDATE 22.04.2023
Using Nginx server with tcp_nopush off;
also works.
Here is the snippet of the nginx config:
location /firmware {
try_files $uri $uri/ =404;
sendfile on;
tcp_nopush off;
}
Upvotes: 0
Reputation: 11
Short supposition: your Makefile doesn't name properly user1.bin and user2.bin OR the output of gen_appbin.py isn't handled properly.
I had exactly the same problem. It happened that I mistakenly load the same userN.bin file as the current running version (e.g. the current running firmware is user1.bin and the updater tried to download the same user1.bin and to flash it).
Here is the output of the broken updater:
connected with WIFINETWORK, channel 3
dhcp client start...
event 0
ip:192.168.0.103,mask:255.255.255.0,gw:192.168.0.1
event 3
we have IP!
handleUpgrade()
current fw is user1.bin
[OTA]Upgrade available version: 2
[OTA]Server 8.8.8.8:80. Path: /user1.bin
[OTA][URL] GET /user1.bin HTTP/1.1
Host: your.site.com:80
Connection: keep-alive
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
/**/
system_upgrade_start
upgrade_connect 40840
[OTA]Upgrading...
upgrade_connect_cb
pusrdata = HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 19 Jan 2018 18:22:59 GMT
Content-Type: application/octet-stream
Content-Length: 327252
Last-Modified: Fri, 19 Jan 2018 18:22:04 GMT
Connection: close
ETag: "5a62374c-4fe54"
Accept-Ranges: bytes
sumlength = 327252
sec_block 80
................................................................................ upgrade_get_sum_disconcb 38568
erase sector=257 ok
...
erase sector=336 ok
ALL=80 sectors erase ok!
upgrade_connect_cb
sumlength = 327252
upgrade file download start.
please check the bin file
totallen = 2653
totallen = 4113
upgrade_check
[OTA]failed!
pm open,type:2 0
After I changed the code so as to use the correct bin file, the updater succeeded in flashing the firmware over the air.
Here is the output of the working updater:
connected with WIFINETWORK, channel 3
dhcp client start...
event 0
ip:192.168.0.103,mask:255.255.255.0,gw:192.168.0.1
event 3
we have IP!
handleUpgrade()
current fw is user1.bin
[OTA]Upgrade available version: 2
[OTA]Server 8.8.8.8:80. Path: /user2.bin
[OTA][URL] GET /user2.bin HTTP/1.1
Host: your.site.com:80
Connection: keep-alive
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8
/**/
system_upgrade_start
upgrade_connect 40872
[OTA]Upgrading...
upgrade_connect_cb
pusrdata = HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Sat, 20 Jan 2018 08:28:42 GMT
Content-Type: application/octet-stream
Content-Length: 327188
Last-Modified: Sat, 20 Jan 2018 08:27:57 GMT
Connection: close
ETag: "5a62fd8d-4fe14"
Accept-Ranges: bytes
sumlength = 327188
sec_block 80
................................................................................upgrade_get_sum_disconcb 38736
erase sector=257 ok
...
erase sector=336 ok
ALL=80 sectors erase ok!
upgrade_connect_cb
sumlength = 327188
upgrade file download start.
totallen = 2653
totallen = 4113
totallen = 5573
...
totallen = 325313
totallen = 326773
totallen = 327188
upgrade file download finished.
flash_crc = 1177058584
img_crc = 1177058584
upgrade_check
[OTA]success; rebooting!
reboot to use2
state: 5 -> 0 (0)
rm 0
del if0
usl
event 1
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x40100000, len 2408, room 16
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0
tail 8
chksum 0x84
load 0x3ffe8310, len 632, room 0
tail 8
chksum 0xd8
csum 0xd8
2nd boot version : 1.6
SPI Speed : 40MHz
SPI Mode : QIO
SPI Flash Size & Map: 32Mbit(1024KB+1024KB)
jump to run user2 @ 101000
Here is my code:
// credits to Martin Harizanov, IoT_Demo
#include "fota.h"
#include "ets_sys.h"
#include "upgrade.h" //for struct update
#include "ip_addr.h" //for ip_addr_t and etc.
#include "espconn.h" //for struct espconn
#include "mem.h" //os_free()
#include "osapi.h" //os_memcpy
#define pheadbuffer "Connection: keep-alive\r\n\
Cache-Control: no-cache\r\n\
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 \r\n\
Accept: */*\r\n\
Accept-Encoding: gzip,deflate\r\n\
Accept-Language: en-US,en;q=0.8\r\n\r\n"
/* dummy comment to debug npp code highlighting */
static void ICACHE_FLASH_ATTR ota_finished_callback(void *arg) {
struct upgrade_server_info *update = arg;
if (update->upgrade_flag == true) {
os_printf("[OTA]success; rebooting!\n");
system_upgrade_reboot();
}
else { os_printf("[OTA]failed!\n"); }
os_free(update->pespconn);
os_free(update->url);
os_free(update);
}
static void ICACHE_FLASH_ATTR handleUpgrade(uint8_t serverVersion, const char *server_ip, uint16_t port, const char *path) {
const char* file;
os_printf("handleUpgrade()\r\n");
uint8_t userBin = system_upgrade_userbin_check();
switch( userBin ) {
case UPGRADE_FW_BIN1: file = "user2.bin"; break;
case UPGRADE_FW_BIN2: file = "user1.bin"; break;
default: os_printf("[OTA]Invalid userbin number!\n"); return;
}
os_printf( "[OTA]current fw is %s\r\n" , file );
uint16_t version=1;
if (serverVersion <= version) {
os_printf("[OTA]No update. Server version:%d, local version %d\n", serverVersion, version);
return;
}
os_printf("[OTA]Upgrade available version: %d\n", serverVersion);
struct upgrade_server_info* update = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
update->pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));
os_memcpy(update->ip, server_ip, 4);
update->port = port;
os_printf("[OTA]Server "IPSTR":%d. Path: %s%s\n", IP2STR(update->ip), update->port, path, file);
update->check_cb = ota_finished_callback;
update->check_times = 120000;
update->url = (uint8 *)os_zalloc(512);
os_sprintf( update->url , "GET /%s HTTP/1.1\r\nHost:
your.site.com:80\r\n"pheadbuffer, file );
os_printf( "[OTA][URL] %s" , update->url );
if (system_upgrade_start(update) == false) {
os_printf("[OTA]Could not start upgrade\n");
os_free(update->pespconn);
os_free(update->url);
os_free(update);
}
else { os_printf("[OTA]Upgrading...\n"); }
}
void ICACHE_FLASH_ATTR doFotaUpdate() {
const char remote_ip[4] = {8, 8, 8, 8};
handleUpgrade( 2, remote_ip , 80 , "/" );
return;
}
Upvotes: 1