Escolar Documentos
Profissional Documentos
Cultura Documentos
com/KmanOz/Sonoff-HomeAssistant#8-dht22-sensor-installation-for-original-sonoff-
switch
Sonoff-HomeAssistant
Sonoff-HomeAssistant is alternative firmware for the brilliant & cheap ($ not quality)
range of Sonoff range of ESP-8266 based WiFi controlled switches that allow you to use
your own mqtt broker rather than the 'ITEAD CLOUD' service that's shipped with the
pre-installed firmware. They would have to be some of the cheapest IoT switches
available today. In fact, even if you knew how to build one from scratch, the
components alone would cost more so why bother.
Currently Supported Devices
Planned Support
It's designed to be installed with the Arduino IDE and has been tested on Arduino 1.6.13
but should be backwards & forwards compatible with other versions. I realize there are
many other mqtt based firmware(s) that have been written for the Sonoff switches, but I
found most of them overly complex for my liking. This firmware is basic but extremely
stable and just gets the job done. There are no frills what so ever, just the core
functionality the switch requires to turn the relay on and off (and report temperature if
using that version or power consumption if using the POW). The OTA versions of the
firmware allow OTA upgrade using the Arduino IDE (correct environment for OTA needs
to be setup). All Home Automation 'logic' is done in HomeAssistant. That is why you
installed HomeAssistant in the first place right!
The Sonoff switches do what they were designed to do, turn a load ON or OFF. I've
found that once the mqtt topic is set and the switch has connected to your mqtt broker,
you don't need to make any modifications to it ever again unless you make major
infrastructure changes (i.e walls in home have moved, light becomes a fan, additional
switches added in room etc). Even if you add additional switches, if your naming
convention is right, the switch will not need to be touched again.
I've called the project Sonoff-HomeAssistant but the switch could be used for many of
the other home automation systems that use a mqtt broker. I'm not sure why you'd
want to use anything other than Home Assistant though.
Installation
1. Clone the Repository
Clone the Sonoff-HomeAssistant repository to your local machine. Copy the required
version for your switch to your Arduino directory. If using the temperature version of the
code, you'll need the DHT library as well. It's available all over the internet.
It's currently setup to use only v3.1.1 of the mqtt standard and will only work on that
version broker unless you modify the code so make sure your broker is setup to use
v3.1.1 of the mqtt standard and not v3.1.
**** IMPORTANT **** MQTT_CLIENT needs to be unique for each Sonoff switch you
install. MQTT_USER & MQTT_PASS can be the same but consider MQTT_CLIENT to be
the equivalent of a MAC address. MQTT_CLIENT is how your broker identifies what
hardware (or software for that matter) is talking to the broker at any one time and needs
to be unique. So does MQTT_TOPIC unless you want 2 switches to respond to 1 topic.
Assuming you make no changes to the topic in the code provided, you should be able
to test the switch and be happy that you now have control using Home Assistant.
If you've installed the version that reports temperature, you can also setup sensors in
HomeAssistant to display both Temperature & Humidity. Modify your
configuration.yaml and add the following.
sensor:
- platform: mqtt
name: "Living Room Temp"
state_topic: "home/sonoff/living_room/1/temp"
qos: 1
unit_of_measurement: "°C"
value_template: "{{ value_json.Temp }}"
- platform: mqtt
name: "Living Room Humidity"
state_topic: "home/sonoff/living_room/1/temp"
qos: 1
unit_of_measurement: "%"
value_template: "{{ value_json.Humidity }}"
For the POW use the following and insert it into your configuration.yaml.
sensor:
- platform: mqtt
name: "Living Room Power"
state_topic: "home/sonoff/living_room/1/power"
qos: 1
unit_of_measurement: "W"
value_template: "{{ value_json.Power }}"
- platform: mqtt
name: "Living Room Voltage"
state_topic: "home/sonoff/living_room/1/power"
qos: 1
unit_of_measurement: "V"
value_template: "{{ value_json.Voltage }}"
As for the switch modifications, it's simply a matter of opening up the switch, installing a
4 or 5 pin header (depending on switch type) and then holding down the main switch
on the unit before you power it up with your FTDI adapter. You are then good to go to
re-flash your new firmware. The photo below is for illustration only and different
products will require the same basic connection but headers will be located in different
positions etc.
If that didn't make any sense at all, I suggest you do some reading on how to install
alternative software on a Sonoff switch before attempting anything else otherwise you
risk turning it into toast (although it's pretty hard I have to admit).
on (Turns the relay and LED on)(For 4CH precede the on command by the
number of the relay. e.g 1on, 3on etc)
off (Turns the relay and LED off)(For 4CH precede the off command by the
number of the relay. e.g 2off, 3off etc)
stat (Returns the status of the switch via mqtt message)(For 4CH the number of
the relay will precede the status. e.g. 1on, 4off)
reset (Forces a restart of the switch) (4 long flashes of the status LED)
If you've installed the version that reports temperature you have an additional option.
temp (Forces a temperature & humidity check otherwise it's reported every 1
minute) (1 short flash of the status LED)
When power is first applied the unit will immediately connect to your WiFi access point /
router and your mqtt broker. When it connects the status LED will flash fast 4 times.
That's it, your connected.
If you've installed the version that reports temperature you will see a short single flash
to indicate that the temperature & humidity has been published as well.
Press the switch on top to turn on the relay. Press it again to turn it off and watch the
status change in HomeAssistant. Toggle the switch in HomeAssistant and the relay &
LED will toggle accordingly. I could have made it more complex, but why?
To reset the switch manually, press and hold the switch for more than 4 seconds. (4CH
press and hold Relay 1 switch) The switch will respond with 4 long flashes and reboot.
OTA Operation
Assuming you have the correct environment setup for OTA and have the OTA version of
the firmware for your switch installed, you can update the firmware via the Arduino IDE
using OTA.
When the unit enters OTA, the status LED will flash twice. Once entered and firmware is
being updated the status LED will flash fast continuously as packets are received. If the
upload was successful the status LED will remain ON and in a short amount of time will
turn off. The switch will reset and the status LED will flash 4 times fast.
If unsuccessful after it enters OTA upgrade mode, it will exit with 2 fast flashes of the
Status LED and either resume normally, or reset depending on the error.
Firmware to control relay only with ON/OFF functionality and publish via mqtt. EEPROM
storage of Relay State.
Firmware to control relay with ON/OFF functionality and temperature reporting via
DHT11/22 and publish via mqtt. EEPROM storage of Relay State.
Firmware to control relay only with ON/OFF functionality and publish its status via mqtt.
EEPROM storage of Relay State. Remote Wallswitch Support.
Firmware to control relay with ON/OFF functionality and temperature reporting using
the Sonoff Sensor-AM2301 (available from iTead) and publish via mqtt. EEPROM storage
of Relay State.
Firmware to control relay with ON/OFF functionality and temperature reporting using
the Sonoff Sensor-AM2301 (available from iTead) and publish via mqtt. EEPROM storage
of Relay State. Remote Wallswitch Support.
Firmware to control relay with ON/OFF functionality and report power usage (Wattage)
and Voltage via mqtt. EEPROM storage of Relay State
ESPsonoff_4CH-v1.01 - iTead Sonoff 4CH Switch
Firmware to control 4 X relays with ON/OFF functionality and publish via mqtt. EEPROM
storage of Relay State.
How you get the wires out of the casing after it's all assembled is completely up to you. I
guess a Dremel and some handywork will be required but it's a tight squeeze.
Remember... even though the switch uses a capacitor dropper and small switchmode
supply to reduce mains voltage down to 3.3v, the whole circuit board is at mains
potential and should be considered dangerous. BE CAREFUL.
Make sure to modify the Arduino code to indicate which sensor you are using.
NOTE: I did have to enlarge the hole in the side of the top case for the plug to seat
properly in the switch. If you do not make the hole bigger, you may get read errors
because the plug doesn't make proper electrical connection! I'm not sure if that is a
general design fault or was the case in the 2 switches that I had purchased.
10. HomeAssistant Community Discussion Forum
For further information and to join the discussion for this firmware please go to the
HomeAssistant Community Discussion Forum ITEAD Sonoff Wireless Smart Switch. I
would be happy to answer any of your queries there.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12. Conclusion
That's about it. Any feature suggestions are welcome and I would be happy to answer
any further questions that you may have.
Enjoy!