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

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

@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.