serial communication and variables

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
mak_paint
Posts: 9
Joined: Fri Jan 28, 2022 1:39 pm

Re: serial communication and variables

Post by mak_paint » Fri Feb 04, 2022 1:13 am

The code doesn't work, I suspect because of the UART communication so I tried this little code and it doesn't work

Code: Select all


serial = UART(0, 115200)
serial.init(115200, bits=8, parity=None, stop=1)
ch = ""


while True:
    if serial.any() > 0:
        ch = serial.readline()
        if ch == "on":
            serial.write("led on")
            led.on()
        elif ch == "off":
            serial.write("led off")
            led.off()
        else:
            serial.write(ch)

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: serial communication and variables

Post by Shards » Fri Feb 04, 2022 10:48 am

Your basic problem is that UART 0 is used by the REPL see: https://docs.micropython.org/en/latest/ ... serial-bus in the standard documentation for the ESP8266 port.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: serial communication and variables

Post by OlivierLenoir » Fri Feb 04, 2022 4:07 pm

mak_paint wrote:
Thu Feb 03, 2022 10:01 pm
OlivierLenoir wrote:
Sat Jan 29, 2022 9:17 am


If the list of color is very long, I recommend using a json file outside of your code.
hello, I'm a bit confused about what you mean when you tell me to use json outside the code
Create a json file name it cars.json and write the following json structure in it. Documentation is here

Code: Select all

{"ford": {"ranger_2011_2016": [0, 1, 0, 1, 0, 1], "explorer_2008_2012": [0, 1, 0, 1, 0, 1], "fiesta_2006": [0, 1, 0, 1, 0, 1], "fiesta_2006_2010": [0, 1, 0, 1, 0, 1]}, "chevrolet": {"aveo_2008_2012": [0, 1, 0, 1, 0, 1], "corsa_2005": [0, 1, 0, 1, 0, 1], "corsa_2006_2010": [0, 1, 0, 1, 0, 1], "corsa_2011_2016": [0, 1, 0, 1, 0, 1]}}
You can access json data like that.

Code: Select all

from json import load

DATA_JSON = 'cars.json'

with open(DATA_JSON) as _json:
    cars = load(_json)

mak_paint
Posts: 9
Joined: Fri Jan 28, 2022 1:39 pm

Re: serial communication and variables

Post by mak_paint » Fri Feb 04, 2022 6:07 pm

yes but, this car.json document is inside the microcontoler?.. because if that is the case, I don't see the advantage regarding the use of a dictionary, now if the car.json file comes via serial it would be difficult because remember that what is going to communicate with the esp8266 via serial is going to be a a touch screen, which will be the user interface and I simply want it to send the make and model of the car to the microcontroller to search for and place in the function

mak_paint
Posts: 9
Joined: Fri Jan 28, 2022 1:39 pm

Re: serial communication and variables

Post by mak_paint » Fri Feb 04, 2022 6:34 pm

Shards wrote:
Fri Feb 04, 2022 10:48 am
Your basic problem is that UART 0 is used by the REPL see: https://docs.micropython.org/en/latest/ ... serial-bus in the standard documentation for the ESP8266 port.
ok so far what I have done is enter the boot.py file of the micro and uncomment this line (uos.dupterm(None, 1) ), then I restart the micro and open the serial communication of the arduino IDE and there I try (it is annoying because then I have to flash the microcontroller to be able to continue in the code)... however, it does communicate because if you look at the last line of code I ask that everything that is received by serial be printed again, and it does, but I can't get the led to turn on because it never enters that conditional.
then i tried this and it works but notice how i put the ch variant

Code: Select all

from machine import Pin, UART


led = Pin(5, Pin.OUT)

serial = UART(0, 115200)
serial.init(115200, bits=8, parity=None, stop=1)
ch = b""



while True:
    if serial.any() > 0:
        ch = serial.readline()
        if ch == b"on":
            serial.write("led on")
            led.on()
        elif ch == b"off":
            serial.write("led off")
            led.off()
        else:
            serial.write(ch)

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: serial communication and variables

Post by OlivierLenoir » Fri Feb 04, 2022 8:51 pm

mak_paint wrote:
Fri Feb 04, 2022 6:07 pm
yes but, this car.json document is inside the microcontoler?.. because if that is the case, I don't see the advantage regarding the use of a dictionary, now if the car.json file comes via serial it would be difficult because remember that what is going to communicate with the esp8266 via serial is going to be a a touch screen, which will be the user interface and I simply want it to send the make and model of the car to the microcontroller to search for and place in the function
Yes the json file is in the micro-controller and it's a good practice to separate data from code.

Post Reply