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

Hello KmanOz, thank you very for your last suggestion. After I manage to get Sonoff to work, It is very stable. But when I tried to cut off the power supply then I waited for a while. I turned the electricity on. It powered up but stops communicate with the Web UI in front end. I tried to press the button on Sonoff several times to activate the communication. Sometimes it takes ten minutes. Sometimes more than ten minutes. Sometimes It never go back online. Last night I had to leave it as it was. But in the morning It starts working again and it becomes very stable. As long as I leave its power on, It is very stable. But Once I shut down the unit It is very hard to restart it to work again. May be I don’t know the proper way to start Sonoff. Any comments would be very useful to me. Thank you very much in advanced.

When you turn the switch on, do you notice the LED. Does it flash fast 4 times?

Yes, it flash fast 4 times. and it takes a rest for sometimes (may be about 5 or 10 minutes) and it flash 4 times again. Thanks, KmanOz.

Well you have a problem. It’s either losing it’s connection to your mqtt broker, which could be network related, or it is resetting by itself which I have seen on some Sonoff switches (or should I say esp8266’s). You are going to have to troubleshoot and start understanding mqtt. I would look at logs of your broker to start with. You can also leave the FTDI cable plugged into the Sonoff Switch and power it that way. Open a serial console and have a look at what the switch is doing and why it’s rebooting.

Your suggestion is very useful to me. Thank you very much. I will look into MQTT log file and connect FTDI cable to see the result on serial monitor as you guided. It is very kind of you.

Also check to see this isn’t happening to you.

1 Like

Hello, KmanOz. I have just received 8 new Sonoffs to replace all of my existing wired cable. I found that when I turned on all of my Sonoffs at the same time, half of them start working within a minute but the remaining others take more than ten minutes to start working. But after the unpredictable time all Sonoffs works perfectly and become very stable. I looked into Mosquitto log file. I could not see anything wrong. Then I extracted the log file and posted below in case I look over something that caused my trouble. I also use FTDI cable to plug into Sonoff Switch and called Arduino IDE Serial Monitor. When I type AT command, It does not respond to what I typed in. But I could still upload both versions of your Firmware successfully. If you could possibly take a look into the log below. I would highly appreciate. May I also ask you one more question, Is it possible if I could change Esp8266 from Access Point mode to Station mode. It is because I have a lot of Access Points listed on public WiFi, I am afraid all of my Sonoff access points would disturb my very nice neighbors. Thank you very much for your precious time. Have a nice day.

1472061174: New client connected from 192.1472077023: mosquitto version 1.3.4 (build date 2014-08-22 06:10:51+0000) starting
1472077023: Config loaded from /etc/mosquitto/mosquitto.conf.
1472077023: Opening ipv4 listen socket on port 1883.
1472077023: Opening ipv6 listen socket on port 1883.
1472077080: New connection from 192.168.1.41 on port 1883.
1472077080: New client connected from 192.168.1.41 as Sonoff_Living_Room_2 (c1, k90, uparnupong).
1472077080: New connection from 192.168.1.45 on port 1883.
1472077080: New client connected from 192.168.1.45 as Sonoff_Living_Room_10 (c1, k90, uparnupong).
1472077080: New connection from 192.168.1.49 on port 1883.
1472077080: New client connected from 192.168.1.49 as Sonoff_Living_Room_8 (c1, k90, uparnupong).
1472077081: New connection from 192.168.1.43 on port 1883.
1472077081: New client connected from 192.168.1.43 as Sonoff_Living_Room_v1.0t (c1, k90, uparnupong).
1472077081: New connection from 192.168.1.36 on port 1883.
1472077081: New client connected from 192.168.1.36 as Sonoff_Living_Room_3 (c1, k90, uparnupong).
1472079979: New connection from 192.168.1.48 on port 1883.
1472079979: New connection from 192.168.1.44 on port 1883.
1472079979: New client connected from 192.168.1.48 as Sonoff_Living_Room_9 (c1, k90, uparnupong).
1472079979: New client connected from 192.168.1.44 as Sonoff_Living_Room_5 (c1, k90, uparnupong).
1472080550: New connection from 192.168.1.51 on port 1883.
1472080550: New client connected from 192.168.1.51 as Sonoff_Living_Room_7 (c1, k90, uparnupong).
1472081523: New connection from 192.168.1.40 on port 1883.
1472081523: New client connected from 192.168.1.40 as home-assistant-1 (c1, k60, uparnupong).
1472081568: New connection from 192.168.1.45 on port 1883.
1472081568: Client Sonoff_Living_Room_10 already connected, closing old connection.
1472081568: New client connected from 192.168.1.45 as Sonoff_Living_Room_10 (c1, k90, uparnupong).
1472081568: New connection from 192.168.1.36 on port 1883.
1472081568: New connection from 192.168.1.49 on port 1883.
1472081568: Client Sonoff_Living_Room_8 already connected, closing old connection.
1472081568: New client connected from 192.168.1.49 as Sonoff_Living_Room_8 (c1, k90, uparnupong).
1472081568: Client Sonoff_Living_Room_3 already connected, closing old connection.
1472081568: New client connected from 192.168.1.36 as Sonoff_Living_Room_3 (c1, k90, uparnupong).
1472081568: New connection from 192.168.1.48 on port 1883.
1472081568: Client Sonoff_Living_Room_9 already connected, closing old connection.
1472081568: New client connected from 192.168.1.48 as Sonoff_Living_Room_9 (c1, k90, uparnupong).
1472081569: New connection from 192.168.1.47 on port 1883.
1472081569: New client connected from 192.168.1.47 as Sonoff_Living_Room_11 (c1, k90, uparnupong).
1472081626: Socket error on client Sonoff_Living_Room_2, disconnecting.
1472081626: Socket error on client Sonoff_Living_Room_5, disconnecting.
1472081626: Socket error on client Sonoff_Living_Room_7, disconnecting.
1472081632: New connection from 192.168.1.51 on port 1883.
1472081632: New client connected from 192.168.1.51 as Sonoff_Living_Room_7 (c1, k90, uparnupong).
1472081659: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1472081758: New connection from 192.168.1.41 on port 1883.
1472081758: New client connected from 192.168.1.41 as Sonoff_Living_Room_2 (c1, k90, uparnupong).
1472081758: New connection from 192.168.1.44 on port 1883.
1472081758: New client connected from 192.168.1.44 as Sonoff_Living_Room_5 (c1, k90, uparnupong).
1472083460: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1472084102: New connection from 192.168.1.42 on port 1883.
1472084102: New client connected from 192.168.1.42 as Sonoff_Living_Room_4 (c1, k90, uparnupong).
1472084129: Client Sonoff_Living_Room_10 has exceeded timeout, disconnecting.
1472085261: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1472086486: Client Sonoff_Living_Room_2 has exceeded timeout, disconnecting.
1472086488: Client Sonoff_Living_Room_3 has exceeded timeout, disconnecting.

You need to make sure each Sonoff unit has a unique MQTT_CLIENT name as per the notes in the firmware. Every switch you have is knocking off every other switch from the broker because the same name is perpetually logging in.

#define MQTT_CLIENT “Sonoff_Living_Room_v1.0p” // mqtt client_id (Must be unique for each Sonoff)

Have you done this?

Sonoff_Living_Room_1
Sonoff_Living_Room_2

Or any name you like. It’s only used for logging into mosquito. Also they no longer respond to AT commands. I will update the firmware to take them out of Access Point Mode in the next couple of days. That is set in EEPROM by ITEAD and my firmware doesn’t even use the mode so understand that it could be annoying. Actually I have found that mode is set on some switches from the factory and not on others. I have 20 and it’s not set in mine.

Kman

Thanks a lot for answering my question. I will rename them as you suggested. It 's good news to hear that you will update your firmware to the new version to get rid to Access Point Mode. I am very glad to hear that I am sure it would be very useful to the others too. You said to have 20, a plenty of Sonoffs. what is the maximum number of Sonoffs that your firmware supports. Is there any circumstance that the Sonoff wifi collide each other. I am planning to buy more too for using as the temperature and humidity sensor and if possible I will use for my Kitchen Gas Leak Sensor. Thank you for devoting your time helping people.

Is there any circumstance that the Sonoff wifi collide each other.

Yes you created that situation by using the same MQTT_CLIENT id for all your switches. Every time a client connects to mosquito, mosquitto needs a unique client id for that particular hardware. You can use the same username / password but the client id (almost like a MAC address) needs to be different so the broker can identify the Sonoff switch from another switch or whatever is connecting.

what is the maximum number of Sonoffs that your firmware supports

It’s not limited by the firmware but mosquito itself and your h/w etc. Lets just say your house won’t get close to the limit so you don’t have to worry :smiley:

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