I have been working with nodeMCU for a while, but due to not knowing C (I am a self taught hobbyist and usually stick to bash or python) never worked with these boards directly, instead just flashed `tasmota` and went from there.
After stumbling upon micropython I decided I'd write my personal little programs for individual tasks that I need around the house. The code below is a test project to get used to the libraries etc. It will connect to my wifi, then to my mqtt broker, then act accordingly to incoming messages - it also works with a push button, which I later want to use to send mqtt payloads with.
This is a simplified version of my code. I am sure there are way better ways to solve this, but I am currently learning, so please bare with me.
```
from umqttsimple import MQTTClient
import machine
import time
import network
# soft vars
mynet = "somethingsomethingsomethingwifi"
mynetpw = "actual_password"
mqid = "nodemcu-testy"
mqhost = "10.10.10.10"
mquser = "mqttuser"
mqpw = "mqttpassword"
mqport = 1883
# hard vars
diode_blue = machine.Pin(14, machine.Pin.OUT)
diode_board = machine.Pin(16, machine.Pin.OUT)
mybutton = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
buzzer = machine.Pin(0, machine.Pin.OUT)
def buzzer():
# buzzer on, sleep, buzzer off
def flash(color):
diode_color.on()
time.sleep(.12)
diode_color.off()
time.sleep(.12)
# MQTT
def sub_cb(topic, msg):
print(msg)
# test 1
if msg == b'buzz':
buzzer()
for i in range(5):
flash(blue)
elif msg == b'door':
buzzer()
flash(red)
flash(yellow)
# wifi
wlan = network.WLAN(network.STA_IF)
wlan.connect(mynet, mypw)
# waiting to connect
while not wlan.isconnected():
flash(red)
print("trying to connect")
# connection successfully
for i in range (4)
flash(green)
print("connect successfully")
# for loop below
presses = 0
while True:
client.subscribe(topic="test/nodemcu-testy")
new_message = client.check_msg()
if mybutton():
presses += 1
buzzer()
flash(yellow)
print("%i button presses." % presses)
time.sleep(1)
```
This is a simplified version of my script; I typed it from scratch because all my variables etc. are in German, so if there are typos here, this is due to me manually typing this; the original script runs as expected, **but eventually** will just stop.
When I run it, I get
* multiple "trying to connect" lines and red diode flashing (until connected to wifi)
* "connected successfully" and green diode flashing (once connected to wifi)
button press => "1 button presses", "2 button presses", etc.
mqtt payload b"buzz" => buzzer does its thing, blue diode flashing
mqtt payload b"door" => buzzer does its thing, red and yellow diode flashing
However, after multiple button presses and/or mqtt payloads, nothing will happen any longer. I didn't notice this at first, because I'd usually run the script and test it, exit, change it, test again. But now I had to go away from my machine and when I returned got
```
1 button presses
2 button presses
Traceback (most recent call last):
File "<stdin", line 151, in <module>
File "umqttsimple.py", line 157, in subscribe
File "umqttsimple.py", line 171, in wait_msg
OSError: [Errno 103] ECONNABORTED
>
```
Before traceback and before that last `>` there is what looks like an envelope ascii symbol rotated 90 degrees (shot sides top/bottom, long sides left/right).
As I said before, I am sure my code isn't all that great. That's fine. But it does connect to the wifi and mqtt server, it does what I tell it to... and eventually it just crashes..? Could you please tell me why this is?
And unrelated bonus question: I usually use `paho.mqtt` for my "regular python" mqtt projects. It allows me to do something like `newmsg = msg.decode('utf-8')`, so that instead of `b'my payload'` I just get `my payload` as output. This does not seem to work on umqttsimple, however, there must be a way to get rid off that `b` and quotes around `b'<payload>`, right?
Sorry if this is a total beginner question. I have tried fixing this on my own but could not figure it out. Thanks in advance for your help
