An ESP8266 Can't Monitor Serial Connection

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
flywire
Posts: 22
Joined: Wed Jan 02, 2019 7:08 am

An ESP8266 Can't Monitor Serial Connection

Post by flywire » Wed Jan 02, 2019 10:54 pm

I'd like to run https://github.com/mswhirl/bouncer/blob ... bouncer.py (apparently linux on RPi) which checks the output from a modem and restarts it (https://github.com/mswhirl/bouncer/blob ... icture.jpg). I don't understand where to make the physical connection and required code changes.

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.

flywire
Posts: 22
Joined: Wed Jan 02, 2019 7:08 am

Re: ESP8266 Serial Sniffer

Post by flywire » Thu Jan 03, 2019 6:48 am

I don't really understand how to handle the serial comms and I realise it will soon use the ESP8266 resources.

I'll update the code as I go here:

Code: Select all

import time, os, ure, sys, machine

from machine import UART
import uos

pin = machine.Pin(4, machine.Pin.OUT)

def setModemPower(state):
    pin.value(state)

print("Starting...")

# port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=1)
uos.dupterm(None, 1)
uart = UART(0, 115200, tx=machine.Pin(13), rx=machine.Pin(15))

setModemPower(0)
time.sleep(0.5)
# port.reset_input_buffer()
setModemPower(1)
# d = b''

uart.write(b"The quick brown fox jumps over the lazy dog\r\n")
pattern = "s"
ch = b""
while True:
    if uart.any():
        ch += uart.read()
        if pattern in ch:
            break
        ch = ch[-len(pattern):]

setModemPower(0)

uos.dupterm(UART(0, 115200, tx=machine.Pin(1), rx=machine.Pin(3)), 1)
print("Booted")
Original code:

Code: Select all

# 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.

flywire
Posts: 22
Joined: Wed Jan 02, 2019 7:08 am

Re: ESP8266 Serial Sniffer

Post by flywire » Thu Jan 03, 2019 10:15 am

@kevinkk525 - thanks for the PM confirming hardware connections for relay to G/3V/D2 (GPIO4) and modem to D10/D9 (GPIO1-TX and GPIO3-RX).

Image

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Can Anyone Direct Me to I/O Serial Monitoring Example

Post by torwag » Mon Jan 07, 2019 8:10 am

Hi,

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.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Can Anyone Direct Me to I/O Serial Monitoring Example

Post by torwag » Mon Jan 07, 2019 8:30 am

So,
reading again, I guess you have the following situation

modem--serial-->PC---usb(serial)-->esp-->ESP-->relay

in that case what you want to do is to rewrite the
bouncer.py (which will be started on the PC!) to use the ESP to switch the relay thats all.

In detail all you need to do is to redefine

def setModemPower(state):

to send python commands to the ESP via the serial USB uart.

flywire
Posts: 22
Joined: Wed Jan 02, 2019 7:08 am

Re: Can Anyone Direct Me to I/O Serial Monitoring Example

Post by flywire » Mon Jan 07, 2019 8:51 am

@torwag I assume photo at the end of the first sentence in this topic is not clear so to elaborate on first sentence in second paragraph:

Code: Select all

power --> relay <-> esp
modem <-- power     esp
modem               esp <-> Terminal (REPL)
modem <-----------> esp <-> Terminal
                    
* 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.

uPyLoader Will Not Connect with USB and Serial Connection. I now understand, contrary to third post, REPL and modem serial need to share a single UART (ie TXD0/RXD0 and TXD2/RXD2). I have not seen any communication from the modem in MicroPython terminal so hard to start debugging.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Can Anyone Direct Me to I/O Serial Monitoring Example

Post by torwag » Mon Jan 07, 2019 9:50 am

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

modem--serial-->ESP--GPIO-->relay

The usb uart is not required as soon as your code is working right? It will be an autark system based on the following scheme.

Modem:
sends serial logs
ESP:
analyse serial logs
depending on results sends command to reset via GPIO and relay

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.

flywire
Posts: 22
Joined: Wed Jan 02, 2019 7:08 am

Re: Can Anyone Direct Me to I/O Serial Monitoring Example

Post by flywire » Mon Jan 07, 2019 10:35 am

* 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?

Post Reply