Switching relay via uart works from REPL but not from main.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
gregor_g
Posts: 5
Joined: Mon Dec 24, 2018 5:06 pm

Switching relay via uart works from REPL but not from main.py

Post by gregor_g » Mon Dec 24, 2018 5:16 pm

Hello everybody,

I´m running micropython 1.9.4 on an esp8266-01 with a relay board that is switched via serial commands. I have the following code:

Code: Select all

import sys,socket,machine,ubinascii
port = 2222
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('0.0.0.0',port))
uart=machine.UART(0,9600) 
uart.init(9600,bits=8,parity=None,stop=1)
while True:
    data,addr = s.recvfrom(1024)
    print(data)
    command = data.decode("utf-8")
    if command == "lampon\n":
        uart.write(ubinascii.unhexlify('A00101A2'))
    if command == "lampoff\n":
        uart.write(ubinascii.unhexlify('A00100A1'))
If I use that code from webrepl, everything works fine. However, if I put the code in a main.py file, it doesn´t execute. I narrowed down the problem to "uart=machine.UART(0,9600)" which throws an error if I put the same code into a separate file and import that file from webrepl.
If I remove that part from the code, the "print(data)" actually prints the data i send, but if I try to define uart, nothing is printed.
I read that UART is somehow used by repl, but then why would it work if I invoke everything from inside webrepl?

I tried searching the forum but found no hints for a difference between the two ways of invoking that code. If i missed to give some needed information for you to help me please tell me what I forgot.

Best regards,
Gregor
Last edited by gregor_g on Wed Dec 26, 2018 4:29 pm, edited 1 time in total.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by kevinkk525 » Tue Dec 25, 2018 5:58 am

Why do people not use the search?
Just the last few days I answered a post like that and the solution is here: viewtopic.php?f=16&t=5359

If you use the webrepl, nothing is printed to uart as I understand it. Therefore your code works in webrepl although it does basically "hack into" the uart0 from the repl. Use the solution in the link I posted an you'll be fine (be sure to read the whole thread).
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Switching relay via uart works from REPL but not from main.py

Post by Roberthh » Tue Dec 25, 2018 7:50 am

If you just have to send, then you may also use UART1, which is not used for REPL.

P.S.: Even reading may make things not obvious when starting with the matter here. Knowing what to search for is part of the knowledge to gain.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by kevinkk525 » Tue Dec 25, 2018 7:56 am

Guess you are right. Although searching esp8266 uart(0) gives you the results you are looking for.

But true, if you only need to send, use UART(1) which has only a TX pin. I did use that in one of my projects.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

gregor_g
Posts: 5
Joined: Mon Dec 24, 2018 5:06 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by gregor_g » Tue Dec 25, 2018 2:09 pm

I read about UART1, but as I understand it that is another physical pin of the chip, right? My esp8266 is on a pcb and there is only one physical connection to the relay. So i guess to use UART1 i would have to resolder something?!
@kevinkk525 I already read the post you mention, but didn´t understand the difference in using webrepl or a main.py (and still don´t, maybe someone is willing to try to enlighten me).
Anyway, i will try the daily build and use the uos.dupterm(None, 1) command.

Best regards
Gregor

deonis
Posts: 11
Joined: Wed Aug 08, 2018 3:02 am

Re: Switching relay via uart works from REPL but not from main.py

Post by deonis » Wed Dec 26, 2018 3:44 am

Hi, Gregor, you might be interested in the project below. You can easily modify it for your needs. This is esp-12 based timed delay relay. It uses a socket server to serve webpage but you can send an array of values to the socket server if you do not want GUI control.

https://github.com/deonis1/wifi-switch

cheers,

Denis

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by kevinkk525 » Wed Dec 26, 2018 7:03 am

gregor_g wrote:
Tue Dec 25, 2018 2:09 pm
I read about UART1, but as I understand it that is another physical pin of the chip, right? My esp8266 is on a pcb and there is only one physical connection to the relay. So i guess to use UART1 i would have to resolder something?!
@kevinkk525 I already read the post you mention, but didn´t understand the difference in using webrepl or a main.py (and still don´t, maybe someone is willing to try to enlighten me).
Anyway, i will try the daily build and use the uos.dupterm(None, 1) command.

Best regards
Gregor
Yes you would have to resolder it for UART1.
Maybe you should read this: https://docs.micropython.org/en/latest/ ... /repl.html
Webrepl and main.py have nothing to with each other. Main.py starts your program, webrepl is a means of accessing the repl without using UART0.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

gregor_g
Posts: 5
Joined: Mon Dec 24, 2018 5:06 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by gregor_g » Wed Dec 26, 2018 3:42 pm

Hello Kevin,

sorry for being unclear in my question. Of course i understand what repl, webrepl and main.py are. Of course I had already read that part of the documentation you linked. I just don´t understand what the technical differences are ("behind the curtain"), that cause my code to run OK if i input it via webrepl or serial connection, but doesn´t run if in put the very same code into a main.py.

I will google a little further and post back if I have concrete questions.

In the meantime I want to thank all developers of micropython. Really a great thing that you are building here!

gregor_g
Posts: 5
Joined: Mon Dec 24, 2018 5:06 pm

Re: Switching relay via uart works from REPL but not from main.py

Post by gregor_g » Wed Dec 26, 2018 4:06 pm

Hello Denis,

thanks for the link to your code. I know there are relays out there that can be controlled via gpio (like in your code). I have one like that running, too. But the relay board I am talking about here can only be accessed and controlled via the serial connection.

Best regards,
Gregor

gregor_g
Posts: 5
Joined: Mon Dec 24, 2018 5:06 pm

SOLVED: Re: Switching relay via uart works from REPL but not from main.py

Post by gregor_g » Wed Dec 26, 2018 5:18 pm

I found the problem, although I still don´t understand what the real reason is.

If I define my access to the UART on the webrepl command line like this:

Code: Select all

uart = machine.UART(0,9600)
everything is OK. However the very same code gives a syntax error if it is in main.py or is in another file and invoked from main.py as a function.

Code: Select all

uart = machine.UART(0)
works from main.py

Is this a bug and should i file an issue or am I doing/understanding something wrong?

Post Reply