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

Thank you for your clear explanation.

I like it!

I am putting an order for them now. is OTA possible with these? It would be good to be able to name them and update them without pulling them out of the (cieling/wall/whatever) and undoing the screws.

cheers!

No OTA function at the moment. When I put this together I never thought of a situation where I would have to do what you are suggesting. The unit in my bedroom will always control my bedroom light, unless it stops functioning and needs replacing. Can you give me an example of why you would want to change a units name after it’s been installed? I guess I can write OTA code for it if it’s a real necessity.

i guess if i want to add extra functionality, or change their address. once they are flashed to this firmware, firmware upgrades for what ever reason are really easy.

There’s only 1 relay tied to a LED in there and only 1 of the GPIO pins is easy to get to and useful. There’s not a lot that can be added to them because of the size of the case and the way they’ve been designed. They’re a purpose built product. Anyway there’s other f/w around there that will support OTA.

Hello KmanOz, now I have almost twenty of Sonoffs. I have changed all of my Sonoffs to use your firmware version 1.0t. This version is also very stable. All of my Sonoffs work perfectly with your version 1.0t. I also equipped some of them with DHT-11, But I have a small problem that the temperature and humidity do not show up the precise value or decimal number in the front end. It report only, for instance, 30.0, 31.0, 32.0 and so on. It doesn’t show the decimal number between .0 to .9 . Is this symptom normal for DHT-11. If so I would change to DHT-22. Thank you in advanced for your suggestion.

Hi thanks for that. Glad they’re all working well for you. If you want accurate temperature reading use a DHT-22. The 11’s are limited to whole numbers only which is why you’re getting those results.

Hello KmanOz, I have ordered a new 2016 version of Sonoff (https://www.itead.cc/smart-home/sonoff-th.html) I hope It would work with your existing versions of your firmware. I tell you later when I receive and test it. Anyway I could suppress AP mode. Now no esp8266 devices listed on my network and nearby. I just only put WiFi.mode(WIFI_STA); before WiFi.begin. It works. Thank you very much for creating easy-to-upload and stable firmware and thank for your help.

Mine have just arrived, and i will have a play shortly :smiley:

kiratikarnpat

Yes that’s all I would have done to the firmware as well to set it to STATION mode. I figured most people would work it out. I have ordered some new Sonof switches to play with as well. Let me know how it goes, but I plan to add support for them if it doesn’t work as is.

Kman

@KmanOz please Help. I flash sonoff with your ESPsonoff-v1.0p.ino via Arduino IDE and standard USB to TTL converter (PL-2303HX). Arduino IDE shows 100% data uploaded. But if I try to plug sonoff into power it always shows ITEAD-xxxxxxx wifi access point (checked with notebook) which means the firmware is not flashed. Is there some way how to check if the firmware is really uploaded in the device?

In Arduino IDE setting I use flash size: 1M (64K SPIFFS). Could you please post your settings of Arduino?

Thank you very much.

In the Arduino IDE you need to pick Tools > Board > Generic ESP8266 Module and set the following options:

  • Flash Mode: DIO
  • Flash Frequency: 40MHz
  • CPU Frequency: 80MHz
  • Flash Size: 1M (64K SPIFFS)
  • Debug Port: Disabled
  • Debug Level: None
  • Reset Method: ck
  • Upload Speed: 115200
  • Port: COM port connected to Sonoff

The IDE will tell you if it uploaded successfully. Add this line just before WiFi.begin(WIFI_SSID, WIFI_PASS) so it should look like this.

WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS) ;

Recompile and upload and it will no longer appear as an access point. That won’t stop it working though and if the IDE said it installed then it probably did. Leave the serial cable plugged in, power the Sonoff from your FTDI adaptor and open the Serial Monitor window. You will see details there as it boots, connects etc.

@KmanOz Thank you for fast answer!

Yet another thing. I use your Arduino saved file but it gives me error:
ESPsonoff-v1.0p:35: error: no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, const char [11], int)'

PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);

Maybe this is because I installed pubsubclient-master.zip but I also update this library? If I use also "#include <MQTT.h the problem is gone.

Jiran

The Arduino Library manager is crappy to say the least. What has happened is that the Library Manager has overwritten the lmroy/pubsubclient with olleary/pubsubclient. It cannot distinguish between the 2. Delete your pubsubclient directory and re download the right one.

Oh and don’t update Libraries with library manager unless you know exactly what is about to happen :smiley:

Kman

1 Like

Also the problem may be gone but the switch will not work. The calls to the library are completely different and there is no way it will work with the olleary library instead of the lmroy library.

@KmanOz thank you for valuable answers. Do you think there could be some problem with wpa2 or other wifi security? Is SSID and PASSWORD the only setting for wifi connection? Seems like my sonoff can´t connect to WPA2 Personal (PSK) + AES. I use thos wifi with all other devices like notebooks etc.

Btw. sonoff flashes fast 4 times…but after a few minutes it makes 4 long flashes…again 4 fast etc.

Seems like it is connected now:
pi@raspberrypi:~ $ mosquitto
1475778954: mosquitto version 1.4.9 (build date 2016-09-26 21:18:47+0200) starting
1475778954: Using default config.
1475778954: Opening ipv4 listen socket on port 1883.
1475778954: Opening ipv6 listen socket on port 1883.
1475778990: New connection from 10.0.0.109 on port 1883.
1475778990: New client connected from 10.0.0.109 as spinac1 (c1, k90, u’user’).

But still not working from home assistant. Is there some way how to test it?

Is it still flashing 4 times after a few minutes? If so it means it’s dropping it’s connection to your broker and resetting so it has nothing to do with WiFi passwords etc. There are so many possibilities. Do you have only 1 switch, is the MQTT_CLIENT id unique across your whole mosquito network, is your network connection solid. I could go on and on but without knowing your exact environment it’s going to be difficult. Download mqtt-spy or send commands to mosquito of “on” or “off” and see if the switch responds. You don’t need HA to operate the switch. If that work then at lest you know it’s not the switch. It wont be the switch though. It will be your setup. Post what you modified in the code here and lets have a look.

I have been playing around with the homie-esp8266 for sometime.

I was able to do the following within 30 mins with a Sonoff:

  1. solder a 5 pin connector ( still learning to solder)
  2. connect the sonoff up to a PC
  3. upload homie up to the sonoff note: there is an sonoff example in the homie sub directories … I changed a few parameters within the code , but should run as it)
  4. reset sonoff , and it jumped in configuration mode ( read homie doco)
  5. connected nexus 7 tablet to sonoff (wireless) and enter configuration mode ( read doco)
  6. strip power cord done and connected up to the sonoff ( in and out power)
  7. power up
  8. used phoa client to test MQTT messages
  9. configure HASS MQTT Switch
  10. has now been in production for about 2 weeks turning my bedside lamps on and off

With homie , start with the basic LED light on and off example , turn the LED on and off on the esp8266-12
and READ THE DOCO (+8 pages)

refer https://github.com/marvinroger/homie-esp8266 and download the sable version not from GITHUB

also refer https://community.openhab.org/t/framework-homie-for-esp8266/8674
for others issues and problems with homie

should have a basic understanding of MQTT too

Just want to give my 2 cents here.

I have over 10 sonoff switches and they all flashed to arendst firmware stright out of the box, it support OTA, included a webserver, etc… so basically arendst firmware is more than we need to integrate it to HASS.

Firmware and instruction on how to flash and change mqtt settings are on the link below. You can even update the mqtt settings via the buildin webserver after you flash the firmware.

After that just put the following in your hass config file. update the the topic with your topic name. (my example below use topic “sonoff4”. restart hass and that’s it. Hope this help.

light:
platform: mqtt
name: “Office light 4”
command_topic: “cmnd/sonoff4/light”
state_topic: “stat/sonoff4/LIGHT”
qos: 2
retain: true
payload_on: “On”
payload_off: “Off”