wifi interupt

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
msloat
Posts: 2
Joined: Sun Dec 23, 2018 6:59 am

wifi interupt

Post by msloat » Mon Dec 24, 2018 2:49 pm

I was wondering if someone can give me an example of how I could do a wifi interrupt on micropython. I have a python server sending messages back and forth to each of my esp32s. I have one that during the off time will be just doing neopixel routines. I need to be able to stop that with a interrupt when a message is sent to it. Can someone give me an example of how I could do that. The server is not threaded. I have not learned that yet. But I assume I need to have the esp32 do a thread to listen all the time?

msloat
Posts: 2
Joined: Sun Dec 23, 2018 6:59 am

Re: wifi interupt

Post by msloat » Wed Dec 26, 2018 6:13 pm

Sorry, I know I didn't explain myself very well. let me try this.

I have python server. It is basically a Raspberry pi. The server isn't threaded. I have not learned to do that yet.

But basically the server looks for incoming connections from 4 to 5 esp32s. Which I have working no problem.

Each esp32 will connect and then send its id to the server. Like esp32-1 would be called light esp32-2 might be called wing, etc. Each esp32 is connected to a prop. Once the server is read to ask the esp32 to do its thing, it will send a message to that esp32 and ask it to run. Most of the run with just a command of "go". Once the esp32 is done with its thing, it sends a message to the server, "done". The server then goes to the next esp32 and asks it to do its thing. sometimes random etc. But you get what it is doing.

With what I have now, everything works. But would I would like to do is have one of the esp32s do a neopixel routine and then when the server is ready, it sends a command and the esp32 stops what it is doing and then does what the server asks it too. So I am looking into doing a interrupt when the esp32 sees a command like "DTH" then it stops and shows the days to Halloween. I am not sure how I can get it to listen all the time, and then stop doing the light show, and then starts up this Day to Halloween routine.

Anyone?

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

Re: wifi interupt

Post by kevinkk525 » Wed Dec 26, 2018 6:47 pm

just let your neopixel loop check a variable "do_stop" which you set if you get the command "DTH"?
If the variable gets true, the neopixel loop exits.

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

Re: wifi interupt

Post by Roberthh » Wed Dec 26, 2018 8:46 pm

If you have a socket opened in which you are waiting for a message, you can declare a handler which is called once a message arrives (or the socket is close). The message handler is registered with:

my_socket.setsockopt(socket.SOL_SOCKET, 20, message_handler)

message_handler is a functions which with a single argument, which is the socket for which the handler is called. Unlike an interrupt service routine (ISR) the handler code is not restricted in what it can do, but should return fast, like a ISR. This is a non-standard mechanism in MicroPython, created for Webrepl.

Examples of usage: Weblrepl.py or https://github.com/robert-hh/FTP-Server ... -and-ESP32

User avatar
pythoncoder
Posts: 3157
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: wifi interupt

Post by pythoncoder » Fri Dec 28, 2018 8:01 am

The standard way to deal with real time events in Python is to use asyncio, Python's cooperative scheduler. The MicroPython version is uasyncio. There is a tutorial on its use here.
Peter Hinch

Post Reply