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.wchusbserial1420
#SERIAL_PORT=/dev/tty.SLAB_USBtoUART
SERIAL_PORT=/dev/tty.wchusbserial1410
#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
update:
sleep 3
ampy -p $(SERIAL_PORT) put hal.py
ampy -p $(SERIAL_PORT) put main.py
ampy -p $(SERIAL_PORT) put boot.py
config:
sleep 3
ampy -p $(SERIAL_PORT) put config.json
flash:

View File

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

23
main.py
View File

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