9
0
Fork 0

refactor, remove debug output

This commit is contained in:
Silver Kuusik 2018-05-22 21:42:11 +03:00
parent 8d4ed5f9b8
commit dec8eb7708
3 changed files with 22 additions and 19 deletions

View File

@ -1,18 +1,22 @@
SERIAL_PORT=/dev/tty.SLAB_USBtoUART #SERIAL_PORT=/dev/tty.SLAB_USBtoUART
#SERIAL_PORT=/dev/tty.wchusbserial1420 SERIAL_PORT=/dev/tty.wchusbserial1410
#SERIAL_PORT=/dev/ttyUSB0 #SERIAL_PORT=/dev/ttyUSB0
all: flash update config all: flash lib config update reboot
reboot:
esptool.py -p $(SERIAL_PORT) --after hard_reset read_mac
lib:
sleep 1
ampy -p $(SERIAL_PORT) put uwebsockets.py ampy -p $(SERIAL_PORT) put uwebsockets.py
update: update:
sleep 3
ampy -p $(SERIAL_PORT) put hal.py ampy -p $(SERIAL_PORT) put hal.py
ampy -p $(SERIAL_PORT) put main.py ampy -p $(SERIAL_PORT) put main.py
ampy -p $(SERIAL_PORT) put boot.py ampy -p $(SERIAL_PORT) put boot.py
config: config:
sleep 3
ampy -p $(SERIAL_PORT) put config.json ampy -p $(SERIAL_PORT) put config.json
flash: flash:

View File

@ -3,7 +3,7 @@ import network
from hal import * from hal import *
from time import sleep from time import sleep
print("Press Ctrl-C to stop boot script...") #print("Press Ctrl-C to stop boot script...")
sleep(0.2) sleep(0.2)
# open and parse the config file # open and parse the config file
@ -25,7 +25,7 @@ if not wlan.isconnected():
ssid = network[0].decode("utf-8") ssid = network[0].decode("utf-8")
# check if the SSID is in the config file # check if the SSID is in the config file
if ssid in config["wifis"].keys(): if ssid in config["wifis"].keys():
print("connecting to: " + ssid) #print("connecting to: " + ssid)
# start to connect to the pre-configured network # start to connect to the pre-configured network
wlan.connect(ssid, config["wifis"][ssid]) wlan.connect(ssid, config["wifis"][ssid])
break break

23
main.py
View File

@ -2,12 +2,12 @@ import _thread
import ubinascii import ubinascii
import uwebsockets import uwebsockets
# to remember WiFi disconnects
has_wifi_connection = False
# WebSocket connection # WebSocket connection
conn = None conn = None
# blockly highlihgt WebSocket connection # blockly highlihgt WebSocket connection
conn_highlight = None conn_highlight = None
# to remember WiFi disconnects
has_wifi_connection = False
# extract a unique name for the robot from the device MAC address # extract a unique name for the robot from the device MAC address
name = "sumo-%s" % ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii") name = "sumo-%s" % ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii")
@ -73,12 +73,10 @@ def ws_handler():
# continue to wait for a WiFi connection # continue to wait for a WiFi connection
continue continue
try: try: # try to read from the WebSocket
fin, opcode, data = conn.read_frame() fin, opcode, data = conn.read_frame()
except: # urror except: # socket timeout, no data received
# socket timeout, no data received # continue to reconnect to WiFi
print("socket timeout")
# continue to read again from socket
continue continue
if data == b"forward": if data == b"forward":
@ -114,24 +112,25 @@ def ws_handler():
sumorobot.calibrate_line() sumorobot.calibrate_line()
#print("Got calibrate") #print("Got calibrate")
elif b"Gone" in data: elif b"Gone" in data:
print("Server said 410 Gone, attempting to reconnect...") #print("Server said 410 Gone, attempting to reconnect...")
conn = uwebsockets.connect(url) conn = uwebsockets.connect(url)
else: else:
print("unknown command:", data) pass
#print("unknown command:", data)
# wait for WiFi to get connected # wait for WiFi to get connected
while not wlan.isconnected(): while not wlan.isconnected():
sleep_ms(100) sleep_ms(100)
# connect to the websocket # connect to the websocket
print("Connecting to:", url) #print("Connecting to:", url)
conn = uwebsockets.connect(url) conn = uwebsockets.connect(url)
# set X seconds timeout for socket reads # set X seconds timeout for socket reads
conn.settimeout(1) conn.settimeout(1)
# send a ping to the robot # send a ping to the robot
print("Sending ping") #print("Sending ping")
conn.send("{'ping': true}") conn.send("{'ping': true}")
conn.send("{'ip': '" + wlan.ifconfig()[0] + "'}") conn.send("{'ip': '" + wlan.ifconfig()[0] + "'}")
@ -144,7 +143,7 @@ sumorobot = Sumorobot(conn_highlight.send)
# indicate that the WebSocket is connected # indicate that the WebSocket is connected
sumorobot.set_led(STATUS, True) sumorobot.set_led(STATUS, True)
print("Starting WebSocket and code loop") #print("Starting WebSocket and code loop")
# start the code processing thread # start the code processing thread
_thread.start_new_thread(step, ()) _thread.start_new_thread(step, ())
# start the Websocket processing thread # start the Websocket processing thread