-
-
Save igrr/d35ab8446922179dc58c to your computer and use it in GitHub Desktop.
| #!/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)) | |
Going back to the latest IDE, the one i flashed it with, has it all working again.
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)
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
Dont upload SPIFF! [CRITICAL]: Not enough arguments.
but all arguments set! -s spiff.bin , ip and etc
upload only firmware!
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...
any ideas?