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

serial communication and variables

Post by mak_paint » Fri Jan 28, 2022 2:44 pm

hello greetings, I hope you are well, I am new to the forum, and I have been trying to find information on methods that allow me to manipulate lists and variables through serial communication, I am using an esp32 board, interacting with a touch screen through UART communication, now that is a problem in itself because I must disconnect the esp32 from the computer every time I want to test the code (do you know a method to maintain communication in both connections); but it is not the main issue, in my code I have many lists more than 500 let's say they are called as the colors:

Code: Select all

RED = (1, 0, 1, 0, 1, 1, 1, 0, 1, 2, 0)
BLUE = (0, 0, 1, 1, 0, 0, 1, 1)
GREEN = ( 0, 1, 0, 1, 2, 0, 1, 0, 1)

... and what the code does is iterate a list performs some actions on the outputs depending on the content of the list

Code: Select all

enter_list = RED [:]                        #here it should be changed for any color from the list
for estado in enter_lis[:]:            
            if if estado == 0:
  		led1.off()
  		led2.off()
	    elif estado == 1:
	    	led1.on()
	    	led2.off()
	   else:
 		led1.off()
 		led2.on()
 time.sleep_us(1000)		
            
....everything is OK until now, What I need is a method by which I can pick colors and place them in the function, through serial communication, i mean, if the color "BLUE" arrives through the serial port, it is placed in the function and does what corresponds, What I don't know how to do, (perhaps it will seem silly to some), is to pass a string that comes from the serial port as "BLUE" and make the code look for the list with that name and place it in the code

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 Jan 28, 2022 3:22 pm

I would try to use UART.
You can readline() into your enter_list. readline() is a str, but you can convert each char in an int.

Code: Select all

from machine import UART

uart = UART(1, 9600)                         # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters

enter_list = uart.readline()     # read a line like '10101110120'
	
for estado in enter_list:
    estado = int(estado)  # Convert char to int
    if estado == 0:
        led1.off()
        led2.off()
    elif estado == 1:   
        led1.on()
        led2.off()
    else:
        led1.off()
        led2.on()
time.sleep_us(1000)
Hopping this will help.

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

Re: serial communication and variables

Post by mak_paint » Fri Jan 28, 2022 9:25 pm

thanks for answering, but the examples of the lists that I put "red", "blue", are just that, examples, but in the real code I have more than 500 lists and some go from 200 elements and others exceed 700 elements...what I want is to be able to choose between them through serial communication

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

Re: serial communication and variables

Post by OlivierLenoir » Sat Jan 29, 2022 9:17 am

In that case, defined a colors dictionary with all your color. Send with uart the name of the color and get it from the dictionary.

Code: Select all

from machine import UART

uart = UART(1, 9600)                         # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters

colors = {
    'RED': (1, 0, 1, 0, 1, 1, 1, 0, 1, 2, 0),
    'BLUE': (0, 0, 1, 1, 0, 0, 1, 1),
    'GREEN': (0, 1, 0, 1, 2, 0, 1, 0, 1),
    }

enter_color = uart.readline()  # read a line like 'RED' or 'BLUE'

enter_list = colors.get(enter_color, [])  # Get color list form colors
    
for estado in enter_list:
    if estado == 0:
        led1.off()
        led2.off()
    elif estado == 1:   
        led1.on()
        led2.off()
    else:
        led1.off()
        led2.on()
time.sleep_us(1000)
If the list of color is very long, I recommend using a json file outside of your code.

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

Re: serial communication and variables

Post by mak_paint » Tue Feb 01, 2022 3:53 am

many, many thanks for the help, I'm going to try these options I imagine I can make several dictionaries... being more specific the lists are of car brands and their models, for exampel:

Code: Select all

chevrolet = {
	"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, ),
	"aveo_2008_2012" . (0, 1, 0, 1, 0, 1, ),
}
ford ={
	"fiesta_2006" : (0, 1, 0, 1, 0, 1, ),
	"fiesta_2006_2010" : (0, 1, 0, 1, 0, 1, ),
	"ranger_2011_2016" : (0, 1, 0, 1, 0, 1, ),
	"explorer_2008_2012" : (0, 1, 0, 1, 0, 1, ),
}


this would work right?

I did not know that you could go from direct string to search for the variable, in C, I could not do it

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

Re: serial communication and variables

Post by mak_paint » Tue Feb 01, 2022 4:04 am

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.
I will also look for information on json files, it may be a better option, thank you very much

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

Re: serial communication and variables

Post by OlivierLenoir » Tue Feb 01, 2022 8:30 pm

It's like a multidimensional dict. It also work with json. You can do it like that:

Code: Select all

cars = {
    'chevrolet': {
        '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, ),
        'aveo_2008_2012': (0, 1, 0, 1, 0, 1, ),
        },
    'ford': {
        'fiesta_2006': (0, 1, 0, 1, 0, 1, ),
        'fiesta_2006_2010': (0, 1, 0, 1, 0, 1, ),
        'ranger_2011_2016': (0, 1, 0, 1, 0, 1, ),
        'explorer_2008_2012': (0, 1, 0, 1, 0, 1, ),
    }
}
You can access data like that:

Code: Select all

brand = 'ford'
model = 'fiesta_2006_2010'

enter_list = cars.get(model, {}).get(model, [])
If we need to consider your UART message, I would use this one ford#fiesta_2006_2010.

Code: Select all

car = uart.readline()  # read a line like 'ford#fiesta_2006_2010'
brand, model = car.split('#')
enter_list = cars.get(model, {}).get(model, [])

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

Re: serial communication and variables

Post by mak_paint » Wed Feb 02, 2022 4:30 am

hello, friend, it looks very very good, thank you very much... a question in micropython is there a way to use the serial monitor at the same time as the program editor (Mu or Thonny), in the style of arduino

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

Re: serial communication and variables

Post by OlivierLenoir » Wed Feb 02, 2022 6:17 am

If you want to send message to the UART, you can use Adafruit 954, it's a USB-to-serial cable which uses 3.3V logic levels. Then you need a serial terminal like picocom, minicom, PuTTY...

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

Re: serial communication and variables

Post by mak_paint » 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

Post Reply