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

Thank you very much kiethh666 - now I have a breakthrough

I was first challenged because I tried to cut the code from your post into configuration.yaml. As a result, the Home Assistant went down (did not answer).

After some time, I could admit it had something to do with the new code. Only after many reboots, I recognized that the code should be manually entered. There were probably some hidden codes with which the Home Assistant will not accept.

But then I got my successful breakthrough :slight_smile:
Now my switch works and I can move on to implement more switches in my configuration.

Thank you again, it has been a great help.

The total code that works

mqtt:
  broker: 192.168.1.155
  port: 1883
  client_id: home-assistant
  username: haadmin
  password: ***************
  keepalive: 60
  protocol: 3.1.1

# Sonoff Switch
switch:
  platform: mqtt
  name: "Lamp on the office"
  state_topic: "home/sonoff/living_room/1/stat"
  command_topic: "home/sonoff/living_room/1"
  qos: 1
  payload_on: "on"
  payload_off: "off"
  retain: true

As a new user of Home Assistant, it can be difficult to figure out all the things to be mastered when configuring a new system.

I therefore propose the following changes to github

Before:

4. Modify configuration.yaml in HomeAssistant and add the following to it.

switch:
  - platform: mqtt
    name: "Living Room"
    state_topic: "home/sonoff/living_room/1/stat"
    command_topic: "home/sonoff/living_room/1"
    qos: 0
    payload_on: "on"
    payload_off: "off"
    retain: true

After:

4. Modify configuration.yaml in HomeAssistant and add the following to it.

mqtt:
	broker: 192.168.1.7 (Your brokers IP address / Home Assistant IP address)
	port: 1883
	client_id: home-assistant
	username: !secret mqtt_usern
	password: !secret mqtt_pw
	keepalive: 60
	protocol: 3.1.1

switch:
      - platform: mqtt
        name: "Living Room"
        state_topic: "home/sonoff/living_room/1/stat"
        command_topic: "home/sonoff/living_room/1"
        qos: 0
        payload_on: "on"
        payload_off: "off"
        retain: true

This will make it significantly more clear for new users to get a solution in the air.

Thanks in advance

I’m not familiar with Home Assistant yet, and have trouble getting the smallest details to work, so I’ve created a new thread because my HA is constantly goes down, when i write in configuration.yaml.

Hope I figure out what I’m doing wrong.

Thank you for your great work on implementing Sonoff firmware, it’s wonderfully well done.

Glad to be of help :slight_smile:

1 Like

has anyone tried to flash a sonoff dual? I’m having a heck of a time getting it to do anything with the tasmota firmware.

@KmanOz thanks for sharing your work! I really appreciate it! I’ve managed to setup two SonOff POW following your guide in github and reading this nearly-600-commented thread. For someone not used to this electronics world is pretty amazing. I write some comments based on my little experience just to encourage other people not to give up when finding troubles:

  • I had to solder a header in the SonOff
  • I had to provide an external 3V3 to the SonOff when flashing because I read that powering it with the FTDI is not enough. I used an Arduino UNO for this. I also connected the three GNDs (FTDI, SonOff and Arduino UNO) together.
  • I spent quite a lot of time with trial-and-error approach flashing the unit. Sorry, I cannot provide a hint for the combination of: when powering the SonOff, when pushing the SonOff button and for how long, and when pressing Ctril+U in the Arduino IDE to compile and upload the code.
  • I couldn’t see any message from the SonOff sent to the serial console (is this the expected behavior?), so I had no idea if the device was properly flashed. I encourage to do the home-assistant stuff previously and have the HASS frontend so when you finish the flashing process you can switch on and off the device in the frontend and check if the red LED turns on and off.

We love pictures! Two pictures attached: the wiring connections between FTDI, SonOff and Arduino that were ok for me (sorry for the bad quality) and the result in the HASS frontend.


Thanks again!

Here are my experiences in flashing as a complete electronics noob:

  • used FTDI only to power
  • didn’t solder a header, just plugged the pins and put slight pressure on them to keep contact
  • used serial console for debugging after flashing

managed to flash two POWs, one S20 and one touch. The touch was a bit problematic since I had to change to ESP8285 for the flash to work properly and grounding the GPI0 without soldering was fiddly

I had so much fun doing it all. Maybe when my regular sonoff’s arrive I’ll actually try and solder some header pins in for a change :smiley:

1 Like

That’s exactly why I posted up here. Glad to see an electronic noob is is getting up and running quickly. Great work :smiley:

1 Like

I strictly followed Ben’s instruction on Youtube SONOFF - A $5 Alexa-Controllable, WIFI Outlet Tutorial But i facing following error :

Archiving built core (caching) in: C:\Users\WinPC\AppData\Local\Temp\arduino_cache_115109\core\core_esp8266_esp8266_generic_CpuFrequency_80,FlashFreq_40,FlashMode_dio,UploadSpeed_115200,FlashSize_1M64,ResetMethod_ck,Debug_Disabled,DebugLevel_None_____f7025fb08a3574e99f1785948801a9c1.a
Sketch uses 440072 bytes (45%) of program storage space. Maximum is 958448 bytes.
Global variables use 44176 bytes (53%) of dynamic memory, leaving 37744 bytes for local variables. Maximum is 81920 bytes.
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

Are you attached to com3?

Yes i attached to com 3


FT232RL FTDI USB 3.3V 5.5V to TTL Serial Adapter Module forArduin Mini Port

https://dl2.pushbulletusercontent.com/2K3M9CnsznKe4PYV78YlV0XnoWtiS7ob/20170601_203409.jpg
https://dl2.pushbulletusercontent.com/Q91EoNA9OS9IQrAETcE07iAFSG8sHsBt/20170601_203438.jpg

did you switch RX and TX?
did you boot the sonoff in download mode by pressing the button while connecting the ftdi?

Yes i did, but i am not sure in configuration and where can i find MQTT FINGERPRINT ? I create one user name and password during homeassistant installation port :1883 with script sudo hassbian-config install mosquitto and other one i have in CloudMQTT console and some other i create in CloudMQTT manage users.

#ifdef USE_MQTT_TLS
#define MQTT_HOST “m20.cloudmqtt.com” // [MqttHost]
#define MQTT_FINGERPRINT “A5 02 FF 13 99 9F 8B 39 8E F1 83 4F 11 23 65 0B 32 36 FC 07” // [MqttFingerprint]
#define MQTT_PORT 20123 // [MqttPort] MQTT TLS port
#define MQTT_USER “cloudmqttuser” // [MqttUser] Mandatory user
#define MQTT_PASS “cloudmqttpassword” // [MqttPassword] Mandatory password
#else
#define MQTT_HOST “RPi IP adress ?” // [MqttHost]
#define MQTT_PORT RPI IP adress ? // [MqttPort] MQTT port (10123 on CloudMQTT)
#define MQTT_USER “Managed user in CloudMQTT Console ?” // [MqttUser] Optional user
#define MQTT_PASS " Pass" // [MqttPassword] Optional password

Glad to see that Tasmota is straight forward for new users hahaha. Anyway the issue above isn’t causing this issue.

This issue is caused by not pressing and holding the button on the Sonoff BEFORE plugging the USB cable into the FTDI adaptor like blakadder said. As for the rest of your issues I think there is now a topic that covers Tasmota (or whatever the firmware you are using is) issues. You may have more luck getting answers in there. Good luck.

Hi @KmanOz, there is a bug in at least this file: ESPsonoff_TH-v1.01pOTA.ino
It prevents the wallswitch function working.

We need to add “checkWallSwitch();” to the loop:

void loop() { 
  ArduinoOTA.handle();
  if (OTAupdate == false) { 
    mqttClient.loop();
    timedTasks();
    checkStatus();
    checkWallSwitch();
  }
}

Thanks for the awesome work :slight_smile:

Nice!!

what more can be use?

Note to any others using this version, I find use in adding these 2 lines into the “void setup() {” block. This uses the nice human-readable name for the DHCP lease & the OTA line in your Arduino IDE. Makes identifying them much easier :slight_smile:

  void setup() {
  ...
  WiFi.hostname(MQTT_CLIENT);            // Set DHCP hostname
  ArduinoOTA.setHostname(MQTT_CLIENT);   // Set OTA IDE name
  ...
1 Like

Hi @KmanOz

I took a leap of faith and ordered 9 units from Itead last week (since none of the Sonoffs from Ebay came).
On friday last week the FTDI adapter I ordered during easter holiday came (!) and yesterday my units from Itead arrived. I first tried the version Ben uploaded, but quickly replaced with your version instead :slight_smile:
Thanks to you (and Ben) I now have 9 working units! Thanks alot for sharing!

Cheers

2 Likes

@KmanOz Do you know when are you going to add Sonoff dual support? Thank you for your work.

I don’t one here to test with so probably not for now sorry. Anyway I think they’re a weird unit. No earth onboard means you have to earth things externally to the unit. Stupid design as far as I am concerned.