ESP-NOW and two M5Stick C
-
I am experimenting with ESP-NOW and two M5Stick Cs.
The application is designed for two people living in the same house. The two people are often in their own rooms studying. And they scare each other to death when they enter the room to discuss something.
The application is designed so that when one of the two people goes to visit the other, clicks on button A, a 5-second timer starts. If the other person does not want the visit because they are in a moment of concentration, they press the A button on their device and a message appears on the device of the person who sent the request that it is not a good time with a red circle. If he/she does nothing, a green circle shows that the invitation to the meeting is agreed by both.
I have been able to get it, except for the part where the other person clicks the A button, it does nothing. It continues the counter and does not show the red circle.
I don't know if it's the nature of the ESP-NOW communication or if it's because something is wrong with my programming.
from m5ui import * from uiflow import * import espnow import wifiCfg import time setScreenColor(0x111111) status_str = None my_mac_addr = None a = None d = None f = None status = None mynameis = None i = None wifiCfg.wlan_ap.active(True) wifiCfg.wlan_sta.active(True) espnow.init() circle0 = M5Circle(40, 115, 32, 0xe60909, 0xFFFFFF) status_label = M5TextBox(74, 6, "status_label", lcd.FONT_Default, 0xFFFFFF, rotate=90) count_label = M5TextBox(17, 83, "5", lcd.FONT_DejaVu72, 0xFFFFFF, rotate=0) console_log = M5TextBox(12, 11, "console_log", lcd.FONT_DefaultSmall, 0xa9a4e6, rotate=90) mynameis_label = M5TextBox(52, 11, "My name is:", lcd.FONT_DefaultSmall, 0xFFFFFF, rotate=90) def longblink(): global status_str, my_mac_addr, f, a, d, status, mynameis, i M5Led.on() wait_ms(500) M5Led.off() def hello_from_the_other(): global status_str, my_mac_addr, f, a, d, status, mynameis, i count_label.hide() shortblink() circle0.show() for i in range(6): circle0.setBgColor(0xffffff) circle0.setSize((i * 6 + 8)) wait(1) if status != 'hello': circle0.setBgColor(0xff0000) longblink() break if i == 5: circle0.show() circle0.setBgColor(0x33cc00) shortblink() restart() def inicialize(): global status_str, my_mac_addr, f, a, d, status, mynameis, i mynameis_label.setText(str(mynameis)) count_label.hide() circle0.hide() status = 'waiting' status_label.setText(str(status)) def hello(): global status_str, my_mac_addr, f, a, d, status, mynameis, i count_label.show() shortblink() for i in range(5, -1, -1): count_label.setText(str(i)) wait(1) if status != 'hello': circle0.show() circle0.setBgColor(0xff0000) longblink() break if i == 0: circle0.show() circle0.setBgColor(0x33cc00) restart() def shortblink(): global status_str, my_mac_addr, f, a, d, status, mynameis, i M5Led.on() wait_ms(50) M5Led.off() def restart(): global status_str, my_mac_addr, f, a, d, status, mynameis, i wait(6) inicialize() def change_status(status_str): global my_mac_addr, f, a, d, status, mynameis, i status = status_str status_label.setText(str(status)) espnow.send(id=1, data=str(status)) my_mac_addr = str((espnow.get_mac_addr())) if my_mac_addr==: mynameis = 'Alice' espnow.add_peer('24:a1:60:45:6a:3d', id=1) elif my_mac_addr==my_mac_addr: mynameis = 'Bob' espnow.add_peer('d8:a0:1d:56:b9:59', id=1) else: mynameis = "I don't know" inicialize() def buttonA_wasPressed(): global my_mac_addr, f, a, d, status, mynameis, status_str, i if status=='waiting': change_status('hello') hello() elif status=='hello': change_status('right_now_no_thanks') else: pass pass btnA.wasPressed(buttonA_wasPressed) def send_cb(flag): global my_mac_addr,f,a,d,status,mynameis,status_str,i f = flag if f: shortblink() else: status = 'not connected' status_label.setText(str(status)) longblink() shortblink() longblink() shortblink() pass espnow.send_cb(send_cb) def recv_cb(_): global my_mac_addr,f,a,d,status,mynameis,status_str,i a, _, d = espnow.recv_data(encoder='str') shortblink() console_log.setText(str(d)) if d=='hello': status = 'hello' status_label.setText(str(status)) hello_from_the_other() elif d=='right_now_no_thanks': status = 'right_now_no_thanks' status_label.setText(str(status)) else: pass pass espnow.recv_cb(recv_cb)
-
@mario
I tried your flow and I see what you are saying. I think your issue is that your program has loops containing waits, which is causing blocking in the code. So when the button A is pressed to respond to the request, it is not received by the button callback. My suggestion is to use some timers to minimize the blocking. -
Well, I hadn't thought of that! It reminded me of my MSX programming days. :D
I'll try your proposal and I'll let you know.
Thank you very much @world101 for your reply.