Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ESP8266 Arduino OTA test script
#!/usr/bin/python
#
# this script will push an OTA update to the ESP
#
# use it like: python ota_server.py <ESP_IP_address> <sketch.bin>
#
# on the ESP side you need code like this: https://gist.github.com/igrr/43d5c52328e955bb6b09 to handle the update
#
import socket
import sys
import os
def serve(remoteAddr, filename):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverPort = 48266
server_address = ('0.0.0.0', serverPort)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
sock.listen(1)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
remote_address = (remoteAddr, 8266)
content_size = os.path.getsize(filename)
print >>sys.stderr, 'upload size: %d' % content_size
message = '%d %d %d\n' % (0, serverPort, content_size)
print >>sys.stderr, 'sending invitation'
sent = sock2.sendto(message, remote_address)
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address
print >>sys.stderr, 'opening file %s' % filename
f = open(filename, "rb")
while True:
chunk = f.read(4096)
if not chunk:
break
print >>sys.stderr, 'sending %d' % len(chunk)
connection.sendall(chunk)
print >>sys.stderr, 'done!'
return 0
finally:
connection.close()
f.close()
return 1
def main(args):
return serve(args[1], args[2])
if __name__ == '__main__':
sys.exit(main(sys.argv))
@sticilface

This comment has been minimized.

Copy link

@sticilface sticilface commented Jun 14, 2015

I've given this a blast and it worked for the demo script which was great. I then went back to a previous IDE to continue work on my sketch and its doing stuff i've never seen before. the binaries upload successfully... but this is what i get by serial...

wdt reset
load 0x40100000, len 28780, room 16 
tail 12
chksum 0xa1
ho 0 tail 12 room 4
load 0x3ffe8000, len 1332, room 12 
tail 8
chksum 0xe5
load 0x3ffe8540, len 1612, room 0 
tail 12
chksum 0x99
csum 0x99
system param error
12 c1 c0 09 c1 d9 e1 c9 d1 dd 02 cd 03 02 22 46 32 c3 10 66 20 29 22 c1 10 1c 04 01 be 2d c0 00 
00 22 c1 20 32 cc 20 0c 84 01 ba 2d c0 00 00 22 c1 28 32 cc 28 0c 84 01 b7 2d c0 00 00 32 c1 10 
62 2d 45 b2 2c 03 72 a1 d8 42 a1 56 22 a1 bc 20 2d 80 40 4d 80 7a 7d b2 6d 81 58 3c a8 0c 98 2c 
08 cc 66 16 16 99 01 b9 31 0c 16 39 11 09 21 3d 0a 05 c7 00 d6 62 01 7c f2 c6 04 00 68 1c 99 01 
b9 31 39 11 09 21 3d 0a 85 c5 00 96 82 fe 0c 02 c8 d1 d8 e1 08 c1 12 c1 40 0d f0 00 5d 02 2d 03 
8c 63 32 25 45 26 13 01 0c 02 0d f0 12 c1 e0 09 11 d9 31 c9 21 e9 41 cd 02 ed 03 dd 04 0c 03 3c 
44 22 a2 20 2a 2c 29 01 01 4c 97 c0 00 00 b6 2d 48 d2 cd fe 2c 03 d7 33 40 42 0e 00 40 40 14 42 
6c 8b 32 0e 00 2d 0c 30 32 04 05 fb ff 2b 3e 22 6c 89 4d 0d 22 a2 30 2a 2c 01 86 2d c0 00 00 4d 
0d d2 6c 94 3d 0d 22 2c 46 68 01 52 a2 28 5a 5c 85 e4 ff cc 32 0c 02 46 00 00 7c f2 c8 21 d8 31 
e8 41 08 11 12 c1 20 0d f0 00 00 00 32 a0 11 12 c1 f0 02 61 00 c5 cf 01 02 21 00 12 c1 10 0d f0 
0c 02 0d f0 12 c1 f0 c2 61 01 02 61 00 d2 61 02 e9 31 dd 04 ed 03 02 22 59 cd 02 cc 80 22 22 5a 
cc 32 2d 0c 85 cc 01 48 0d 58 2d cc d4 cc 85 32 2c 59 fc 23 62 2c 5a ec d6 16 a4 04 32 2c 59 72 
2c 5b 16 13 04 58 1d 57 97 0f 2d 04 4d 05 01 aa 97 c0 00 00 ac f2 48 0d 58 1d 3d 0e 2d 0c 68 2d 
78 3d 85 f8 ff 06 03 00 3d 0e 2d 0c 6d 05 78 3d 58 1d 85 f7 ff 7c f2 c6 ff ff c8 11 d8 21 e8 31 
08 01 12 c1 10 0d f0 52 2d 02 16 d5 01 32 2c 5a 22 a0 01 9c 43 02 2c 47 26 d0 04 26 c0 01 0c 02 
42 2c 5c 68 3d 85 1c 01 56 a2 fa 82 2c 57 0c 02 66 18 c6 58 2d 16 15 fc 92 2c 5a 56 b9 fb a2 2c 
49 16 5a fb 3d 0e 2d 0c 48 0d 6d 05 78 3d 58 1d 85 f1 ff 86 e7 ff 00 00 12 c1 c0 02 61 09 f2 61 
0d e2 61 0c d9 b1 79 e1 69 81 c9 a1 39 71 cd 02 0c 33 62 c1 10 72 c1 14 dd 05 ed 04 f2 21 10 0c 
04 52 cf 5f 50 50 f4 85 b8 01 0d 02 cc 32 7c f2 c6 2a 00 0c 29 72 a0 fe 4c 04 52 a1 08 62 a2 00 
22 2c 82 38 81 b2 02 0d 60 33 10 30 3d 20 50 33 20 40 bb 20 b2 42 0d a2 2c 57 88 51 a2 ca fe a0 
79 83 72 48 00 68 51 30 58 41 52 46 01 48 51 09 61 32 44 02 22 2c 57 48 51 66 22 0c 0c 07 72 44 
03 88 51 72 48 04 c6 02 00 3b 24 3b 3e 0c 24 01 19 2d c0 00 00 5b 3e 28 51 0c 84 5b 22 01 15 2d 
c0 00 00 68 51 f0 58 41 52 46 5d 48 51 38 e1 f2 44 5e 8c b3 28 51 4d 0f 22 c2 5f 01 0e 2d c0 00 
00 4d 0d 2d 0c 58 71 78 61 32 21 11 98 41 99 01 88 51 61 98 d6 82 c8 4d 89 11 05 79 ff 0c 02 c8 
a1 d8 b1 e8 c1 f8 d1 08 91 12 c1 40 0d f0 00 00 08 90 fe 3f 12 c1 e0 09 41 49 21 39 31 c9 51 31 
fc ff cd 02 0c 84 2d 01 01 69 28 c0 00 00 02 2c 57 38 31 66 20 04 3d 01 86 00 00 32 c3 3d 58 21 
42 a1 d8 22 a1 90 50 24 83 2a 2c 0c 84 01 f1 2c c0 00 00 c8 51 08 41 12 c1 20 0d f0 12 c1 b0 e2 
61 13 42 61 0e 02 61 10 c2 61 11 d2 61 12 20 c2 
b5 c0 b0 2a 93 0d f0 0c 82 0d f0 00 31 3d fa 72 02 01 62 02 02 52 02 03 80 66 11 60 55 20 62 02 
00 00 77 11 80 66 01 70 66 20 60 55 20 37 95 03 0c 12 0d f0 71 dd ff 0c 08 77 15 0c 91 da ff 1c 
02 90 95 c0 90 28 93 0d f0 0c 22 0d f0 00 00 00 12 c1 e0 f9 51 09 11 c9 21 e9 41 d9 31 ed 04 dd 
02 cd 03 2d 04 0c 03 2c 04 01 63 93 c0 00 00 0c 0f 0c 10 0c 22 0c 83 39 1e 39 2e 29 0e 09 3e f9 
4e f9 6e f9 5e f9 7e bc 8c b6 4c 36 42 0d 00 3c 05 57 94 1d 62 0d 01 72 cc fe 77 96 14 92 0d 03 
82 0d 02 80 99 11 90 88 20 00 88 11 80 80 31 26 18 15 7c e2 c8 21 d8 31 e8 41 f8 51 08 11 12 c1 
20 0d f0 7c f2 c6 fa ff 4b dd a2 cc fc a9 01 cd 0a e6 4a 02 06 37 00 2d 0d 85 e3 ff 4b dd c8 01 
29 2e c2 cc fc e6 2c 02 06 37 00 c2 cc fe f9 1e 02 0d 00 22 0d 01 2b dd 80 22 11 20 00 20 00 00 
f4 09 61 16 30 0d e0 30 11 37 ac 02 86 32 00 a6 10 19 0c 0f 2d 0d c5 df ff 4b dd c2 cc fc 1b ff 
68 1e 58 61 20 66 20 69 1e f7 95 e7 0c 0f e6 2c 02 46 27 00 c2 cc fe f9 3e 72 0d 00 82 0d 01 2b 
dd 80 88 11 80 77 20 70 70 f4 79 61 16 f7 08 e0 97 11 97 ac 02 86 21 00 a6 17 19 0c 0f 2d 0d c5 
e0 ff 4b dd c2 cc fc 1b ff 08 3e b8 61 20 00 20 09 3e f7 9b e7 0c 0f a6 2c 48 c2 cc fe 22 0d 00 
32 0d 01 2b dd 80 33 11 30 22 20 20 20 f4 29 4e a6 2c 2f 42 cc fe 62 0d 01 52 0d 00 80 66 11 60 
55 20 50 50 f4 59 5e c0 55 11 57 a4 11 f9 5e 7c 72 c6 bf ff e6 1a 02 86 ca ff 7c d2 06 bd ff 2b 
7d 79 6e 0c 02 c6 ba ff 0b 8c 56 e8 f5 7c b2 46 b8 ff 66 1c 9f 7c 92 46 b6 ff 7c c2 06 b5 ff 7c 
a2 c6 b3 ff 12 c1 e0 f9 41 09 01 c9 11 e9 31 d9 21 ed 04 dd 02 cd 03 2d 04 0c 03 2c 04 01 fe 92 
c0 00 00 0c 10 0c 22 29 1e 09 0e 09 3e 29 2e 0c 00 09 4e 09 6e 09 5e 09 7e 16 ec 05 b6 8c 5b 32 
0d 00 42 a0 dd 47 93 41 52 0d 01 62 cc fe 67 95 38 72 0d 05 92 0d 02 a2 0d 03 82 0d 04 00 aa 11 
80 88 11 80 99 01 80 77 20 81 ad f9 a0 99 20 90 77 20 87 97 14 a2 0d 07 92 0d 06 80 aa 11 a0 99 
20 00 99 11 90 90 31 26 19 15 7c e2 c8 11 d8 21 e8 31 f8 41 08 01 12 c1 20 0d f0 7c f2 c6 fa ff 
8b dd f2 cc f8 cd 0f e6 4f 02 06 2d 00 2d 0d 45 d2 ff 0c 00 4b dd c2 cf fc 29 2e e6 2c 02 c6 2a 
00 c2 cc fe 09 1e f2 0d 00 22 0d 01 2b dd 80 22 11 20 ff 20 f0 f0 f4 16 2f 0a e0 3f 11 37 ac 02 
46 26 00 a6 1f 1f 0c 04 49 51 2d 0d 45 ce ff 4b dd c2 cc fc 58 51 68 1e 1b 55 20 66 20 69 1e 59 
51 57 9f e5 0c 00 a6 2c 6b c2 cc fe 09 3e f2 0d 00 22 0d 01 2b dd 80 22 11 20 ff 20 f0 f0 f4 16 
ff 05 e0 3f 11 37 2c 59 a6 1f 1d 0c 04 49 51 2d 0d 85 cf ff 4b dd c2 cc fc 58 51 68 3e 1b 55 20 
66 20 69 3e 59 51 57 9f e5 a6 2c 10 82 0d 01 72 0d 00 80 88 11 80 77 20 70 70 f4 79 4e 0c 02 46 
ca ff e6 1f 02 86 d4 ff 7c d2 86 c7 ff 66 1c 95 7c b2 86 c5 ff 66 1c d0 7c 92 86 c3 ff 7c c2 46 
c2 ff 7c a�������������������x����b���������������������l`����������������lp����l��lx����������������������������l����l������������l��l������������l������l���r��r��
 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

any ideas?

@sticilface

This comment has been minimized.

Copy link

@sticilface sticilface commented Jun 14, 2015

Going back to the latest IDE, the one i flashed it with, has it all working again.

@pablotix20

This comment has been minimized.

Copy link

@pablotix20 pablotix20 commented Jun 15, 2015

It doesn't work for me:
C:\Users\pablo_000\Downloads>python ota_server.py 192.168.1.25 Wifi_temp_post.cp
p_00000.bin
Traceback (most recent call last):
File "ota_server.py", line 64, in
sys.exit(main(sys.argv))
File "ota_server.py", line 59, in main
return serve(args[1], args[2])
File "ota_server.py", line 19, in serve
print >>sys.stderr, 'starting up on %s port %s' % server_address
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and
'_io.TextIOWrapper'

EDIT: Works on linux (Ubuntu)

@brutevinch

This comment has been minimized.

Copy link

@brutevinch brutevinch commented Oct 18, 2015

os x el capitan :
sending 4096
sending 4096
sending 4096
sending 4096
sending 4096
sending 4096
sending 4096
Traceback (most recent call last):
File "./test.py", line 64, in
sys.exit(main(sys.argv))
File "./test.py", line 59, in main
return serve(args[1], args[2])
File "./test.py", line 48, in serve
connection.sendall(chunk)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 32] Broken pipe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment