Uncaught Error: This socket is closed. MQTT connectivity issues #4666
Replies: 1 comment
-
Posted at 2018-08-02 by @gfwilliams You might be hitting a classic Espruino USB issue here. What happens if you power it from a USB Wall supply or Power Bank? Basically Espruino is using USB flow control and is trying not to lose any text that you output with console.log - it doesn't know if the PC isn't reading data because the IDE is busy, or because nothing is running, so when its output buffer gets full it just waits for it to start emptying before it continues. It's a bit buried now, but it's worth checking out "I typed save() but Espruino won't work (or stops working quickly) when powered from a computer (it only works from a USB power supply, battery, or the computer when the Web IDE is running)" in http://www.espruino.com/Troubleshooting for a more detailed explanation. As far as the MQTT message though, I'm not sure. Is it possible you were saving after running Posted at 2018-08-02 by d0773d
That makes total sense. Is it possible for the espruino to check if it is connected to a computer, if not, don't output text or just periodically clear the buffer?
That sounds like what is going on. Does the current mqtt connection get terminated if the Pico looses power? Then once power is supplied again the mqtt connection should start back up as new? IF there is a fresh mqtt connection(without uploading/saving code again) I shouldn't be receiving the ping Uncaught Error as described earlier, but I still am. Posted at 2018-08-02 by @gfwilliams
That's what it does - hence why it works on a power bank. However it can't easily tell if an application is running on the PC or not, which is the frustrating bit :(
Yes - however what might have happened is when you save, it basically just stops everything right there, then kills all the socket connections. It means that the connection is killed, but by that point it's not running and JavaScript, so the JS never gets told the connection has been destroyed and so when it loads again it tries to use that connection and gets the error. I guess the MQTT library could trap that error and then decide that the socket had closed and tidy up. Posted at 2018-08-02 by d0773d I re-flashed the firmware onto the PICO and changed the Save on Send setting to Direct to Flash (execute code at boot). I then uploaded the code again, waited a few minutes, and I haven't received the Uncaught Error. I also commented out all of my console.log code used for debugging purposes and the PICO stopped hanging waiting to dump out the buffer. Posted at 2018-08-02 by d0773d
Is this something that will need to be implemented into the Espruino firmware or is this something that I would need to code into the IDE? Either way I wouldn't know where to begin to "trap the error" Posted at 2018-08-02 by d0773d
I apologize for overlooking your first question. I used an external power source and using my original code that I posted to this thread worked as expected. To double check this again I hooked the power source back to my computers usb port and after a few msgs sent back and forth the espruino stopped responding until I connected back to the IDE so the console.log can dump to the screen. However, even though the ping error hasn't come back, it still bugs me :-/ Posted at 2018-08-03 by @gfwilliams
It'd be done in the MQTT.js source code. I've just made some tweaks - please can you try using However the issue would seem to be that you were uploading your code, running Posted at 2018-08-03 by d0773d I'm not infront of my computer at the moment to check the require code.
When I was getting the issue all I was doing was hitting the upload button then type save(). Now that I changed the Save on Send setting to "Direct to Flash (execute code at boot)" I haven't seen that Error again.
Honestly, I wouldn't spend too much time on that. I'm the only one that seemed to run across that issue. And hooking the pico into an external supply doesn't hang while waiting for text to be dumped, so it runs as expected. Posted at 2018-08-03 by d0773d
@gfwilliams. Before I tested your recommendations I unplugged the ethernet cable to my raspberrypi. waited a few moments and I received MQTT disconnected... reconnecting. which stayed there for a few more moments. Then I received "Uncaught Error: This socket is closed.". I then reconnected everything and uploaded the code with the new require. Everything connected. Removed the lan cable again..... received the MQTT disconnected message. Waite a few more moments and no Error :-) Posted at 2018-08-04 by d0773d @gfwilliams I noticed after about 3 MQTT messages are received by the Espruino I get:
It's nothing major, everything seems to run. Thank you for everything that you have been doing for Espruino community :-) Posted at 2018-08-06 by @gfwilliams That's interesting - what happens if you switch back to the original MQTT? Are you still doing the thing where you save when there's an active connection? It could be that the connection that was active when you saved tries to ping, and can't because it finds out it is closed. It'll then clean up and emit the However in the mean time you already created a second MQTT connection in You could try replacing:
with
and see if that helps? Posted at 2018-08-06 by d0773d
I executed
I changed the code with your suggestion, executed reset(true) then uploaded the code. code:
Is it good practice to execute reset(true) before I upload any new code in the future? Posted at 2018-08-07 by @gfwilliams
No, you should be fine without it - realistically it's only if you were switching the 'save on send' setting and you were sure you didn't want anything left behind (sometimes you might - eg if you're making your own bootloader ).
Ok - so this is unrelated to the recent changes then? What MQTT server are you using? Could you see if you can read the logs for it? It's entirely likely it doesn't like something that Espruino's MQTT client is doing, and so it drops it. Posted at 2018-08-07 by d0773d
If I understood your question correctly... after about 3 messages I receive: MQTT disconnected... reconnecting for both For the sake of debugging this, should I just use the normal
I followed your guide: http://www.espruino.com/Home+Automation , I am using mosquitto.
exceeded timeout and Socket error on client is what stood out the most. You are probably right, the mqtt broker doesn't like what the Espruino's MQTT client is doing, and so it drops the connection. I'm really not sure why or what the Socket error means and why I've exceeded the timeout, disconnecting. I executed mqtt in the console which spits out "keep_alive": 60 so, I'm not sure why it keeps timing out. So I can understand the keep_alive setting, does the Espruino mqtt client ping every 60 seconds to keep the session alive? Log segment: 1533682953: New connection from 192.168.0.18 on port 1883. Posted at 2018-08-08 by @gfwilliams Yes, you might as well use Thanks for checking into this... Looking at the code (here, if you're interested) it seems that the keep-alive and ping are very slightly different - but the end result is Espruino tells the server it has a keep-alive of 60s and then sends pings every 40s by default, which should be ok. Having said that, it feels like there's something odd in the mosquitto log. What's the IP of the Espruino that's connecting? And do you know what Posted at 2018-08-08 by d0773d
192.168.0.18
I created an Electron App using the mqttjs module. My Electron app just sends a mqtt message every 30 seconds with updates about the status of my computer. Electron App code:
Espruino IDE console:
Here is a new log: 1533770037: New connection from 192.168.0.18 on port 1883. 1533770037: Posted at 2018-08-09 by @gfwilliams Thanks - that definitely looks fine. Can you try just publishing a random message every 10 seconds on the Pico and see if that helps? And also if you keep getting the messages on the server. I'm not sure why the built-in Ping wouldn't be doing it for you, but it'd be interesting to see if that fixed it - it might be that something has happened and the Pico isn't sending anything. Posted at 2018-08-09 by d0773d @gfwilliams You are definitely onto something. I turned on verbose logging in mosquito. I didn't see a PINGREQ or a PINGRESP for the PICO. I uploaded a screen shot. I wish the log file wasn't so cluttered and easier to read/understand. PICO IP is now 192.168.0.89. I setup static IP for the PICO because I thought there might be an ip conflict, possibly causing that error, but that's not the case. I am sending "Hello World!" mqtt message from the pico every 10 seconds. The Pico seems to work as expected WITHOUT those errors as I described earlier :-) I then commented out the setInterval and uploaded the code again. Now the PICO is disconnecting with the same error message :-( It couldn't hurt for me to send an 'OK' message every few seconds from the PICO to nodered to keep everything up and running properly. Attachments: Posted at 2018-08-10 by @gfwilliams Great - thanks! So it looks like the Ping isn't being sent at all? I'll take a look at the code and see if I can figure out why. Posted at 2018-08-10 by @gfwilliams Please can you add the following code? I just tried this on desktop Espruino and it appears to be at least calling the 'ping' function:
Posted at 2018-08-10 by d0773d I'm getting an error Uncaught Error: Cannot read property '_ping' of undefined Posted at 2018-08-10 by d0773d Nevermind about that error. I moved the code into the onInit() function and only received one "PING" inside of the console. After the third mqtt message I get: MQTT disconnected... reconnecting. I typed MQTT disconnected... reconnecting. Posted at 2018-08-10 by d0773d Update to my last post I closed my Electron APP so there are no more mqtt messages being sent. I let the Espruino IDE just idle for a few minutes and I received multiple "PING" in the console. My mosquito log now shows PINGREQ and PINGRESP from the PICO. PING seems to halt when messages are being received. To test this I opened my app again, after a few messages I get the same disconnected... reconnecting message. I then closed my app and PING seems to resume again and so does the PINGREQ and PINGRESP in my mosquito log file. I also downloaded a chrome extension called mqttbox. I'm manually sending the same MQTT message using mqttbox every 30 seconds and again I'm getting the same disconnected... reconnecting. message in the console. I then just sent one message from mqttbox and waited... PING continued to resume without getting and MQTT disconnected... reconnecting. message. I seem to not get the MQTT disconnected... message for any message received that was sent after 60 seconds. For example, I can send one message every 65 seconds and still receive a PINGREQ properly. For my project the PICO doesn't need to receive EVERY single message over my mqtt network. Just only messages pertaining to the PICO; however, I will still run into the disconnected... issue if messages are received inside the 60 seconds keepalive window. Attachments: Posted at 2018-08-13 by @gfwilliams Perfect - thanks for all your debugging! That sounds like the issue then - I'd seen some code in the module that looked a bit odd... It reschedules the Ping whenever it receives data... So that means if it's always getting data but not sending anything, it won't get the ping and it'll time out. I've just made a change - can you try Posted at 2018-08-13 by d0773d @gfwilliams I'm on my lunch break and wanted to check out the source code: But, I'm getting 404: Not Found Posted at 2018-08-13 by @gfwilliams Odd - you sure there isn't some punctuation in it? It's the same one as last time - and I just clicked it and it worked fine here. Posted at 2018-08-13 by d0773d @gfwilliams Ok, I can view the file. Not sure what happened before. Posted at 2018-08-13 by Robin Mon 2018.08.13 12:31 CST @d0773d Sanity check: link in #26 above rendered the .js file in the browser for me Just reading along FYI Posted at 2018-08-14 by d0773d Looks like this is working as expected :-) What line of code did you change in the MQTT.js file? Thanks so much for your help! :-) Attachments: Posted at 2018-08-14 by @gfwilliams Great - I'll make sure that goes live for the normal module then! It's this change here: espruino/EspruinoDocs@4880f0d Basically every time it received a message it rescheduled the ping - and there was no need. It should have just kept pinging every X seconds regardless. It's been like it for a while but I guess it was never an issue because whenever most people used it they were transmitting data every so often or not receiving data more often than every minute :) Posted at 2018-08-16 by d0773d Thanks Gordon! I am almost finished with this project. I need to add a few more commands and features. Once finished I will post my updates to my project post. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-08-01 by d0773d
Hardware setup: Pico, wiznet 5500io, Dual relay module
I executed save() in the IDE to save the code.
Functionality:
Pico connects to my mqtt broker and sends commands to latch/unlatch my relay.
Odd behavior:
Code will execute even though there is an Error thrown; however, when I disconnect the Pico from the IDE the code doesn't execute. To debug this issue I disconnected the Pico from my computer and plugged it back in to reboot, but the code still doesn't execute.
I then connected the Pico to the IDE again. Once Console.log and the Error dumps out to the screen the code executes correctly again.
Error:
Code:
Beta Was this translation helpful? Give feedback.
All reactions