Sonoff-HomeAssistant (Alternative firmware for Sonoff Switches for use with mqtt/HA)

I started off with a completely different routine for checking mqtt and WiFi but I found it unreliable. This firmware came about because I wrote it for my personal Sonoff’s then gave it to one guy who suggested others may want it. Anyway I found some ESP’s would come back after a while of no WiFi/mqtt and others wouldn’t and there was no reason why. The only way to get them to reconnect was to reset them so I decided to do a hard and fast (dirty) method for guaranteed connection and that was a reset and be done with it. It doesn’t suit all environments for sure but if you have a netwrok like mine then if they do reset, you have bigger issues :smiley:

Thanks for the offer. I have a couple of people who have sent me 1 each. At the moment I am OK but again thanks for the offer.

Also this is my version of a Sonoff Dev LOL. I prefer playing with the ESP direct :smiley:

Great insight into why you do it that way, I’m sure that’s going to come back around to prevent some hair pulling on my end!

I’m awaiting my ESP boards in the mail. Next project is to turn my compressor “smart”. Besides the stove and microwave (not playing with that one…) I’ve almost got everything being reported and controlled with ha.

1 Like

I had to. I would leave it on and I have a slight leak which meant it would scare the shit out of me at 3:00am when it decided to switch on :smiley: Also if you happen to write a routine that’s bulletproof, I’d be happy to incorporate it into the code. The way it is it works 100% albeit it clunky.

1 Like

Like suggested, set retry to a high number see if it works. I have the same problem but i fixed it using a different method.

Why don’t you post the method here for others to use?

Of course, basically it loops indefinitely during startup. It should have the same behavior of increasing the retry count

Can’t wait to try your OTA version

I have also tried that. I came across the same issue I’ve had with ESP’s not Sonoff’s in general but ultimately the Sonoff is built using an ESP chip. Some ESP’s would find the WiFi and connect. Others just wouldn’t. I had a few to play with (I have 28 LOL). They vary and I have come to the conclusion that it’s in the Silicon. Even stock firmware from ITEAD they would sometimes not find a AP or need a reboot after power up. That’s why I did a retry count, because if it at the end of the retries it didn’t connect, it just rebooted. Looping indefinitely could potentially mean it never connects again on some ESP’s.

I needed to spend time to troubleshoot and make it bulletproof. I have 28 ESP devices all running it and stable so I’m fairly happy. I’m sure it will break for some people though just because LOL. I’ll try and get it on Github in the next couple of days.

@KmanOz - thnaks for this wicked library. I’ve just picked up a sonoff to test with, hopefully integrate a few of them after a sucessful bed-in period.

I’m a bit of a noob, and have trashed and rebuilt my pi2 HASS more times than i’d like to admit, so please forgive any really stupid/ignorant questions.

Part two of your installtion guide talks about lmroy installation. I currently have mosquitto installed via the HASS AiO installer. Will the lmroy library run alongside mosquitto? does it require any special installation? reading again it looks like lmroy runs on the machine which flashes the sonoff, so won’t have anything to do with the machine on which the mqtt broker is installed - is this correct?

you wrote:
so make sure your broker is setup to use v3.1.1 of the mqtt standard and not v3.1.
how do i do this?

If you could provide more detailed instruction for the installation of imroy in a mosquitto-equipped environment, i would be very grateful.

Lol this was exactly what my first sonoff went too. I moved mine to my crawlspace so it didnt’ take up so much room in the shop.

1 Like

how do you use them?

I mean inside a wall socket, outside in serial with a power cord, next to the power plug …?

I just successfully went through the installation of the KmanOz firmware, so I can answer to your questions:

Will the lmroy library run alongside mosquitto? does it require any special installation?

No, you just need to install the lmroy library into the Arduino IDE before you flash the firmware, you do that (assuming you are under windows) by clicking the Download button in the top right corner of the github page of the lmory library, then you uncompress the archive and you place the folder into your Arduino /libraries/ folder.

it looks like lmroy runs on the machine which flashes the sonoff, so won’t have anything to do with the machine on which the mqtt broker is installed - is this correct?

That’s correct.

so make sure your broker is setup to use v3.1.1 of the mqtt standard and not v3.1.
how do i do this?

You keep sure that you have the variable “protocol” set to 3.1.1 into you MQTT configuration in HA. Example:

mqtt:
  broker: 192.168.1.100
  port: 1883
  client_id: home-assistant-1
  username: USERNAME
  password: PASSWORD
  protocol: 3.1.1

Perfect. Thanks.

OTA firmware is up on Github.

Thanks

Nice will try tonight!!

80 Gallon 7.5hp here. It’s in the basement…directly below my room…hah

I’m not sure how yours is setup but mine is wired into my garage, basement and a couple other locations. At each location is two lighted switches: Green/On/On and Red/Stop/Running correlating to Switch Color/Push Action/Light action. It’s wired up via a latching relay (on/off) along with a contactor, pressure sensor, etc etc.

What I actually needed was two momentary contacts (on/off) and two inputs (ON/RUN - technically just ON but I got pins so why not?)

For the callback I just write low/delay/high to the appropriate relay. For the button function I check if the inputs are different then a stored variable

if (digitalRead(COMP_RUN) != compRun) {
compRun = digitalRead(COMP_RUN);

and then parse appropriate action to publish mqttClient

I’ve been programming for many years but never worked with Arduino before. Your code was able to help me write that up in under an hour yesterday. I can post the full code and/or pictures if you want.

Two questions for you.

I was reading through your new OTA firmware. I was going to update mine. I stumbled across this: OTA Update · ESP8266 Arduino Core There appears to be a couple different methods for OTA - was there any reason you chose the Arduino method over say the Web Browser? I’ll be updating my Sonoff with your OTA firmware. When my compressor is finished I was going to add OTA, just wondering which method was best.

mqtt. I’m reading up on it because prior to the sonoff I had never used it. In my compressor build I’m using the same logic you are

mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", “on”).set_retain().set_qos(1));

Which is triggered when the compressor goes ON (a set of relays closes GND to a GPIO) which then sends the MQTT message.

I have yet another set of contacts which will close when the actual motor is spinning. Is it really as simple as

mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/run", “on”).set_retain().set_qos(1));

Assuming my MQTT_TOPIC is “home/compressor/1” could I then use a sensor in HA with

binary_sensor:
  platform: mqtt
  name: "Compressor"
  state_topic: "/home/compressor/1/run"
  sensor_class: power

?

Thanks!

1 Like

I have purchased the 4ch unit https://www.itead.cc/wiki/Sonoff_4CH . It works ok with the EWeLink software but I would like to get it off the cloud and onto my Home Assistant. Is there any plans of making a firmware flash for that?

Yes I just spoke to ITEAD yesterday. They are out of stock but will get one to me they’re back sometime in March.

Mine is right next to the main bedroom too. Very loud at 3:00 and startling :smiley: I have a simple one controlled by a Sonoff. Not as complex as yours.

Mainly because of the demographic of the people using my firmware, beginners. It’s all within the IDE. It’s the same procedure to upload to the Sonoff via serial, instead they select the Sonoff via it’s mdns name and the steps are no different. Open IDE and do what they’ve always done.

Yep pretty much. That’s why mqtt is an amazing protocol for connected stuff. The /run topic is a sub topic off the main but you could have used a whole new main topic if you wish. You can even use 1 topic for everything and then use a JSON string representing everything you need in that topic and parse it back out in HA. There is no real way to do it, just what suits you. The mqtt broker is smart in that it doesn’t require topics to be setup, knows when a topic is no longer used and just kills it, and generally looks after itself with little intervention. As for the HA end you could use a switch or a Binary Sensor to trigger automations etc. Again it’s up to you. Good luck.

1 Like

I use ESP easy as firmware, and in it you can switch the rx and tx lines as new gpio, say sda/scl and instead of dht22 I use a bmp280, works fine.
Big advantage of espeasy is there are so many predefined sensor and switch modules. If you need a little more working space you chuck out the ones you don’t need in the arduino ide, and add the ones you need.
Further multiple broker protocols.
And runs smooth on sonoff. Connect to a time server in the sonoff and you can place rules in the sonoff too!

I’m happy that you’ve found a solution that works for you but it’s not for everyone. Yes many people have spoken about ESPeasy but for a lot of beginners it’s far from EASY to setup. Remember that some people who are using this firmware in the Sonoff are using an IDE for the first time ever. They’re not coders and have no interest in coding, just Home Automation. They have no idea what sda/sci is, what rx & tx lines do, what i2c is what anything is. The learning curve for ESPeasy or Theo Arends software is steep, hence why I designed this. In 1 hour and with very little knowledge you are running.

Why would you want the Sonoff to handle rules of it’s own when you’re using HomeAssistant. I don’t understand the concept of distributing automation logic in your network when that’s exactly why you install and run HomeAssistant, to bring it all together. What a headache trying to work out automation rules and logic across your whole network. When all the logic is in HomeAssistant, you know where to look. If you have a couple of Sonoff’s and that’s your whole network, distributing logic would be good and cheap, but imagine 20 Sonoff’s each with their own logic in them coming back to HomeAssistant which has logic in it as well. What a nightmare to troubleshoot.

2 Likes