Unstable WiFi connection
I'm using a LoPy4 (same problem using a WiPy) that reads sensor data (using modbus), and publishes the sensor data to a private mqtt server. I'm also using threads using the _thread library. It works as expected, but after a short while (1-5 mins), the telnet repl connection drops and I get the following message:
> Failed to connect ( Connection was reset). Click here to try again.
I'm not able to connect via the telnet repl until 10-30s has passed (
> Connection error: Error: login timed out), during this downtime no mqtt messages are recieved by the mqtt server either. After the 10-30s I'm able to connect via telnet repl, and messages are recieved by the mqtt server.
I have also tried connecting via USB, sensor data is read every 10th second and printed in the repl, however after a few minutes, the mqtt server stops recieveing messages (sensor data still printed in the repl). Then after a while the mqtt server starts recieveing messages again. Thus maybe indicating problems with the wifi connection.
Have anyone else had similar problems, or could the usage of threads cause similar problems?
this never happens if I use USB.
You have to try harder! Or you have simply more luck when using USB.
I noticed that this happens not very often, but when it happens a few times in short distance. So it may not be related to the kind of connection.
@timh Thanks, I will look into it.
However (slightly off-topic), when I ctrl-c my program to upload changes/new files the upload sometimes fails and I have to manually reboot the device, this never happens if I use USB. Safe boot on upload is enabeled for both cases..
You need to check the state of your mqtt connection, if it times out you probably are hung up on the socket, trying to write. Which means you loop is effectively stalled.
@crumble Thanks, and sorry for the late reply.
I am not using deepsleep, and I believe not any other stuff which reinit the WiFi connection either. Basically I'm using this code from pycom wlan docs: Multiple Networks using a Static IP Address in my boot file to connect to a WiFi network.
And the stripped version of the main file contains:
from umqtt.simple import MQTTClient client = MQTTClient("joe", "server ip", port=1883, user= auth['username'], password= auth['password']) client.connect() client.publish("/RFM/Test2", "Connected to: %s on %s" % (wl.ssid(),wl.ifconfig())) while True: #sensor data, dummy values now result = [0, 123] try: client.publish("/RFM/Test2", str(result)) except: print("Publish failed")
Sounds like you use deepsleep or other stuff which reinit the WiFi connection. Sometimes the connection buildup needs some seconds at my home. Can't tell if it is the *Py or the router.
I never had problems with telnet when the connection is established. But ftp hangs after a while and stops transfering data. But I was able to start another get/put immediatly.
Update: Removed the thread, only publishing a dummy value via mqtt. Still having the same problems.