My hardware is really simple: LoLin NodeMcu V3 Module, KY-019 5V Relay Module, microusb cable to PC and a couple of leads off CD drives. Micropython is loaded on esp, relay connected at G/3V/D4 (GPIO2) worked in MicroPython and serial connection from modem to pc worked in putty.

The terminology also confuses me with a serial connection between modem and ESP and another serial connection between ESP and PC. Maybe someone can re-describe the situation.

I can see the script needs reworking and I'm aware of all sorts of conflicts so I'd appreciate some advice on:
* Which GPIOs to use to avoid conflicts
* How to set the software up to get the serial connections working

As I understand it UART0 is needed for modem to ESP8266 communication using GPIO1 (TX) and GPIO3 (RX) [which could be remapped to GPI15(TX) and GPIO13(RX) but I can't see any advantage in doing so]. UART1 using GPIO2 (TX) should provide all required output from ESP8266 to Putty on PC.

Last edited by flywire on Tue Jan 08, 2019 8:06 pm, edited 2 times in total.

# bouncer.py (c) Mark Smith (Whirlpool). GPLv3.
# Raspberry Pi serial port sniffer for power control via a relay connected to the Pi's GPIO4
#
# If this script runs for 60s without a match message then there is some difference with your modem
# so you will have to tweak the script. Add a print(d) at the appropriate point(s) to get spammed.
#
# This program could auto tune itself (i.e. start with low timeout, then fail to get a switch message,
# then increase the timeout until a working value for the current modem is found, etc.) but considering the
# expert audience (if you can solder bridge SMT pads, add pins and get the relay circuit going) you will probably
# consider tweaking the 250ms black-out period a good challenge :) Start low and increase it until you get the
# "attempt" bit in the text, then you are good to go.
# File based GPIO control was used as it doesn't require any extra library and we don't care about inconsequential errors
# Circuit notes: The modem in the pictures required a large DC barrel - an old netbook power supply cable was suitable.
# I used a 12VDC relay and a BC549 transistor.
import serial, time, os, re, sys, traceback
# The following two commands may give harmless errors after the first run of the program
os.system("""echo "4" > /sys/class/gpio/export""")
os.system("""echo "out" > /sys/class/gpio/gpio4/direction""")
def setModemPower(state):
os.system("""echo "%i" > /sys/class/gpio/gpio4/value""" % state)
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=1)
reBankMessage = re.compile("""(?:Booting +: )([^\n\r]+)""")
reKernelMessage = re.compile("""(Starting the Linux kernel)""")
reBankSwitchedMessage = re.compile("""(?:Booting +: Bank . \(bank . failed 3 times\))""")
setModemPower(0)
time.sleep(0.5)
port.reset_input_buffer()
setModemPower(1)
d = b''
try:
while True:
d += port.read(1)
bank = reBankMessage.search(d.decode('utf-8','ignore'))
kernel = reKernelMessage.search(d.decode('utf-8','ignore'))
switched = reBankSwitchedMessage.search(d.decode('utf-8','ignore'))
if bank is not None and kernel is not None and switched is None:
print("Got a match: " + bank.group(1))
setModemPower(0)
port.reset_input_buffer()
d = b''
time.sleep(0.250)
setModemPower(1)
if switched is not None:
print("You have now booted off the alternate bank: " + bank.group(1))
break
except:
print("Unhandled exception:" + str(sys.exc_info()[0]))
traceback.print_exc()
print("At this time the serial data buffer contained: ")
print(d)

Last edited by flywire on Sun Jan 06, 2019 3:53 am, edited 3 times in total.

not yet 100% sure what you are trying to achive.
You want to switch on and off a modem depending on a certain state via a relay based on an esp?

From where do you get the status?
Where and how is the modem connected?
For me it sounds like all you want is to use the esp as a serial connection-triggerable-relay.
That is rather easy and require you to do nothing else then starting the esp with micropython and send the desired micropython commands via the serial port to the esp, where they will be evaluated and executed.

The main question I have is about the way of connection and if or not another system is involved.

* Power is supplied to a KY-019 5V Relay Module (replacing breadboard in photo) which powers the modem under the control of ESP8266 (G/3V/D2) - all works fine.
* Esp8266 (replacing Raspberry Pi in photo) is connected to PC via USB cable (ie TXD0/RXD0) running REPL in terminal.
* Modem TX/RX is connected to ESP8266 via serial.
* Modem output is optionally displayed on screen and when Booted message is received code should turn modem off/on using setModemPower, on three occassions.

Esp8266 relay to modem works, serial conection to modem works in putty.

if this is the case you need is to connect the modem via the UART port to the ESP which is as you already figured out troublesome, as the ESP has only one functional UART already used for REPL. Switching REPL on and off has all side of funny effects, as well the need to set-up the ESP wifi to connect to your network and access the ESP via webrepl from your PC.

There was/is a softuart which bascially bit-bangs (emulates) and uart interface over normal GPIOs. By defintion it is not very stable and the speed is naturally very limited. However, it might work for low baud rates. viewtopic.php?f=16&t=2204&start=20

Other then this, using an ESP8266 might be the worst candidate for your job as it comes only with 1.5 UARTs where one is used for REPL already. On a micropython board with plenty of full functional free UARTs, that would be a no-brainer.

* So essentailly, you want to replace the RPi by an ESP running micropython? Yes

* The usb uart is not required as soon as your code is working right? User feedback could be disabled. The led on the relay is probably enough feedback.

* It will be an autark system ... Automatic system I assume.

* if this is the case you need is to connect the modem via the UART port to the ESP which is as you already figured out troublesome, as the ESP has only one functional UART already used for REPL. Switching REPL on and off has all side of funny effects. Isn't this what TXD2/RXD2 is for? Still, is it doable?

* as well the need to set-up the ESP wifi to connect to your network and access the ESP via webrepl from your PC. Seems reasonable with modem on TXD2/RXD2.

There was/is a softuart which bascially bit-bangs (emulates) and uart interface over normal GPIOs. By defintion it is not very stable and the speed is naturally very limited. However, it might work for low baud rates. viewtopic.php?f=16&t=2204&start=20. Scary, application is also time related.

Other then this, using an ESP8266 might be the worst candidate for your job as it comes only with 1.5 UARTs where one is used for REPL already. On a micropython board with plenty of full functional free UARTs, that would be a no-brainer. Or ESP32 I assume.

(Finally I understand your suggestion to run the whole thing as two usb/serial terminals (esp8266 & modem) on PC controlling relay from pc via esp8266 but no idea how to do it in Windows.)

OK I'm naive, thank you for explaining the situation. I think I realise why it has taken so long for someone to reply in detail. There are a lot of esp8266 and PCs around, other hardware not so common and often with a wait. Recommendation?