MQTT C# program stops responding after a while


I have a c# program to control my sonoff basic /ESP8266 with MQTT
When i start, every thing is ok (connects and get/send messages) but for some reason, it stops
after a while. When I do a restart, it’s reconnecting, and it’s working again.

Could this be the problem:

I have changed it to (which is working better):
while (Program.IsRunning && MqttIsConnected)

Can somebody explain what the problem could be and of the solution is the solution (don’t know if it has a performance impact on the server)?

Hope my question is clear,
Kind regards,

On my 525 installation I have a similar issue. I have a sensor reporting data every couple minutes (still working) but the MQTT app stopped working a few weeks ago. Restarting the app fixed it, but I’m sure it will stop again. I can post my whole code but it’s based on the stock MQTT app Gene wrote.

I found the stock code didnt actually match the implementation!

Could it be that you have an error in your code and you are crashing the app?

Have you tried wrapping everything in a try catch?

I had mqtt working reliably but am still trying to rebuild the box into a stable condition in an automated way.

Nearly there with an ansible script to configure a pi from scratch too :slight_smile:

The problem with the above is that you probably have an infinite loop too, take a look between the two options and see what cpu load is like without the sleep.

can you upload the program please?
Would like to control a Sonoff relay via HG (MQTT).
I have no experience.

Many Thanks


@David_Wallis I did do the try … and catch but looks like the “event” is not triggert by mqtt. I shaw also that my 2.4ghz wifi was down today, perhaps that’s the problem. When i have time, i do some more digging.

@pinie_pinie (no rights can be derived from it :wink: )

  1. Flash sonoff with
    (I’m gonna change to,… if i have time)

  2. make program with code below (c#)

  3. add event (program number is here 1001) if you want to switch it on time
    js script:
    var Heater = $$.modules.withAddress(“1001”).get();
    Heater.command(‘Control.On’).execute(); //turn it on
    Heater.command(‘Control.Off’).execute(); // turn it off

c# code (startup & program)
code.txt (5.0 KB)

the Sonoff relay is ready so far. How do I get it under HG (MGTT) to run.
I created your program in HG. Is not I missing a widget now?
Would it be possible to upload all parts (widget + program). For a layman it is difficult to realize the project. What is normal for you is not solvable for me.
Thank you for your support!


It’s better if you want help, to make a new thread.
But for this time, here is some extra info :slight_smile:

I use mosquitto as broker. (installed on the same pc as HG)
Also for testing, i use my phone with MQTT Dash app, so i can check if the node is correct.
This is a modified switch-widget (the toggle give an error, i have not programmed it) (2.4 KB)

Hello David,

I found one problem (made by me), the unique ID for this client was used twice. One for a relays and one for esp8266 with bme280 for temperature/humidity/pressure. It’s now still working with Program.GoBackground(); so hopefully that was the problem.

Hope to hear from you when you have finish your project.
Kind regards,