I got it working, although there is something I don't understand. From the documentation and some posts in this forum (viewtopic.php?t=1740), REPL is "only run when nothing else is running", meaning only after main.py exits. I don't know if I am misunderstanding that statement or something has changed since I read that sentence (or my code is just wrong somewhere), because I can only the communications as expected when I disable REPL.
On the master:
Code: Select all
# Master device (host) main program
import serial
import time
slave = serial.Serial('/dev/ttyUSB0', 115200, timeout=3)
try:
timer = time.time()
while True:
if slave.in_waiting > 0:
msgFromSlave = slave.readline()
print('{:.3f}: [uart read] {}'.format(time.time(), msgFromSlave.decode('utf-8').strip('\r\n')))
timeCheck = time.time()
if timeCheck - timer > 3:
timer = timeCheck
print('{:.3f}: [print] Asking slave 2+2?'.format(time.time()))
slave.write(b'2 + 2?\r\n')
# Uncomment following line to reduce host CPU usage
# time.sleep(.1)
except KeyboardInterrupt:
print(' Ctrl+C was pressed')
finally:
if slave.is_open:
slave.close()
print(' Serial connection was closed')
Code: Select all
# Slave device (ESP8266 board) main program
import machine
import time
led = machine.Pin(2, machine.Pin.OUT) # set to 2 for builtin LED in Wemos D1 mini pro
def main():
uart = machine.UART(0) # init with given baudrate
uart.init(115200, timeout=3000) # init with given parameters
ticker = time.ticks_ms()
while True:
if uart.any():
hostMsg = uart.readline()
if hostMsg is not None:
strMsg = str(hostMsg.decode('utf-8'))
uart.write(strMsg.strip('\r\n') + ' 2 plus 2 equals 4' + '\r\n')
timeCheck = time.ticks_ms()
if time.ticks_diff(timeCheck, ticker) > 1000:
ticker = timeCheck
uart.write('Slave time: ' + str(timeCheck) + '\r\n')
led.value(not led.value()) # Toggle LED
# Run main loop
main()
Code: Select all
$ python host/testSerial.py
1553711688.217: [uart read] Slave time: 3272
1553711689.217: [uart read] Slave time: 4273
1553711690.219: [uart read] Slave time: 5274
1553711691.187: [print] Asking slave 2+2?
1553711691.220: [uart read] Slave time: 6275
1553711692.221: [uart read] Slave time: 7276
1553711693.221: [uart read] Slave time: 8277
1553711694.187: [print] Asking slave 2+2?
1553711694.223: [uart read] Slave time: 9278
1553711695.224: [uart read] Slave time: 10279
1553711696.225: [uart read] Slave time: 11280
1553711697.187: [print] Asking slave 2+2?
1553711697.226: [uart read] Slave time: 12281
^C Ctrl+C was pressed
Serial connection was closed
However, if I disable REPL in UART(0) in boot.py, bidirectional communication is established as expected:
boot.py:
Code: Select all
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import uos, machine
uos.dupterm(None, 1) # disable REPL on UART(0)
import gc
#import webrepl
#webrepl.start()
gc.collect()
Code: Select all
$ python host/testSerial.py
1553711500.240: [uart read] Slave time: 3277
1553711501.241: [uart read] Slave time: 4278
1553711502.242: [uart read] Slave time: 5279
1553711502.840: [print] Asking slave 2+2?
1553711502.853: [uart read] 2 + 2? 2 plus 2 equals 4
1553711503.243: [uart read] Slave time: 6280
1553711504.244: [uart read] Slave time: 7281
1553711505.245: [uart read] Slave time: 8282
1553711505.840: [print] Asking slave 2+2?
1553711505.852: [uart read] 2 + 2? 2 plus 2 equals 4
1553711506.245: [uart read] Slave time: 9283
1553711507.247: [uart read] Slave time: 10284
1553711508.248: [uart read] Slave time: 11285
1553711508.840: [print] Asking slave 2+2?
1553711508.853: [uart read] 2 + 2? 2 plus 2 equals 4
1553711509.249: [uart read] Slave time: 12286
^C Ctrl+C was pressed
Serial connection was closed
On the other hand, when disabling REPL on UART(0) as above, code loading with ampy does not work anymore. Only way I could find to continue testing is to enable webREPL and upload the files through the websocket. I guess ampy uses the (raw) REPL connection.
Can you please enlighten me with an explanation of how REPL is implemented?