Sonoff - MQTT - Domoticz

Everything about esp8266 and more.

Moderator: leecollings

Post Reply
User avatar
Plaagje
Posts: 42
Joined: Thursday 30 April 2015 10:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Sonoff - MQTT - Domoticz

Post by Plaagje »

I know there are more threads about this but i think my solution is best 8-)

My Domoticz is installed on a RPI2

Sonoff Memory upgrade

you will need:
• Sonoff wifi smart switch
• Solder iron and solder
• Winbond 25Q32FV in SOP-8 package, which is a 32Mbit (4MByte) chip

1. Solder off the old chip, do this by applying a lot of solder to the 4 pins on one side and heat them all at the same time while trying to lift the chip with a screwdriver.
2. Clean up all the solder pads by stroking them with the solder tip
3. Position the new chip on the location and keep in mind that the dot on the new chip must be aligned with the marking on the board.
4. Then press the solder iron to all the pins off the chip to solder them to the board
Watch https://www.youtube.com/watch?v=8mz5sCAvDAY&t=340 for a detailed video

Flash new firmware

Sonoff Basic
Image
Pinout:
5V(GPIO14) - Not Used
VCC – VCC (3.3v)
RX – TX
TX – RX
GND – GND

you will need
• Solder a male of female header onto the board
• CP2102 module USB to TTL serial
• Male/female header

1. Download and install the Arduino IDE: https://www.arduino.cc/en/Main/Software
2. Download the ESP software for Arduino
a. File --> Preferences --> Settings
b. Paste the following link in the additional Boards Manager URLs:
c. http://arduino.esp8266.com/stable/packa ... index.json
d. And press ok
e. Tool --> Boards --> Boards Manager
f. Filter on esp
g. Download esp8266 by ESP8266 Community
3. Download the new Sonoff firmware: https://github.com/arendst/Sonoff-Tasmota
4. Place all the files in the appropriate folders on your pc
5. Open C:\Program Files (x86)\Arduino\libraries\pubsubclient-2.6\src\ PubSubClient.h and change “#define MQTT_MAX_PACKET_SIZE” to 427
6. Open the Arduino software and load the Sonoff sketch
7. Change the user_config.h to your liking
a. Domoticz
b. MQTT
c. WIFI Credentials
8. DISCONNECT THE MAIN POWER!!!!!
9. Connect the Sonoff to the serial adapter according to the pinout above, DON’T CONNECT THE 5 V!
10. Connect the USB serial adapter to your pc.
11. The Sonoff must be in programming mode before you can flash the Sonoff with new software
a. Disconnect the VCC,
b. hold down the button
c. reconnect the VCC and release the button
12. Setup the correct settings in the Arduino IDE under Tools
13. Compile the sketch to see if there are no errors.
Image
14. Make sure the Port is set on the right COM Port!
15. Upload the sketch to your Sonoff (this might take a few minutes).
16. Now you can connect to your device using a web browser and browse to the IP-address given by your router


Install Mosquitto – MQTT Broker

1. Ssh to Domoticz (I use Putty)
2. Download and install mosquitto

Code: Select all

•	sudo adduser mosquito
•	sudo apt-get update
•	sudo apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
•	sudo apt-get install mosquito
•	sudo mosquitto_passwd -c /etc/mosquitto/pwfile <USERNAME>
•	sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/
•	sudo cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
•	sudo nano /etc/mosquitto/mosquitto.conf
a. now an editor opens and you have to change some things, scroll down and uncomment/change the following rules:

Code: Select all

bind_address <yourIP>
port 1883

autosave_on_changes true

persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/

#change log_dest into one of below and add the other two below

Code: Select all

log_dest syslog
log_dest stdout
log_dest topic

Code: Select all

log_type error
log_type warning
log_type notice
log_type information

connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfile
• save the file with CTRL+O and exit CTRL+X
• add the following line to /etc/rc/local right before “exit 0”
/usr/local/sbin/mosquitto –d

Sudo nano /etc/rc.local

• save the file with CTRL+O and exit CTRL+X

next configure Domoticz

Configure MQTT in Domoticz

1. Open Domoticz web GUI
2. Setup --> Hardware
Image
3. Use your own credentials you have created at point 2 from “Install Mosquitto – MQTT Broker”
sudo mosquitto_passwd -c /etc/mosquitto/pwfile <USERNAME>
4. Test if your Domoticz can talk to MQTT
a. In your console (putty ssh session) enter the following command
mosquitto_sub -h <YOUR IPADDRESS> -v -t "#"
b. The # represents all topics
c. If all steps done correctly you should see something similar as:
Image
Exit with CTRL+C
5. Create a dummy switch Setup --> Hardware
Image
6. The name doesn’t do anything so you can enter anything
7. Create a virtual sensor Image
Image
9. Also the name does not matter.
10. After adding the switch it will be visible in Setup --> Devices. Sort on Idx. The switch is the last one in the list.
Image
11. Open your Sonoff browser --> Configuration --> Configure Domoticz
12. And fill in the Idx of the Sonoff switch you want to control
13. Save and after the Sonoff rebooted you can control it with Domoticz.


If something is not correct please let me know so i can correct it.

--Plaagje--
Last edited by Plaagje on Thursday 30 March 2017 10:45, edited 2 times in total.
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: Sonoff - MQTT - Domoticz

Post by randytsuch »

Thanks for the tutorial, will be helpful if I get a sonoff.

FYI, an easy way to remove the existing chip is the use a sharp pair of cutters, and cut its legs, one by one.
After you do the legs on one side, you can lift that side of the chip up, and then cut the legs on the other side.
Once the legs are cut off, use a soldering iron to clean the pads and remove the leg remnants that are on the pads.

Obviously the chip you remove is trashed, but using this method is easy, especially for a small 8 pin part.

Randy
jsmartin
Posts: 2
Joined: Saturday 18 April 2015 21:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Paris (FRANCE)
Contact:

Re: Sonoff - MQTT - Domoticz

Post by jsmartin »

Thank you for this post !

minor corrections :

2. mosquito is the user name, you need to install mosquitto with this command :

Code: Select all

sudo apt-get install mosquitto
I also need mosquitto client to execute some commands (such as mosquitto_sub -h <YOUR IPADDRESS> -v -t "#")

Code: Select all

sudo apt-get install mosquitto-clients
add the following line to /etc/rc.local right before “exit 0” (not rc/local)
/usr/sbin/mosquitto –d (not /usr/local/...)

11. Open your Sonoff browser --> Configuration --> Configure Domoticz
12. And fill in the Idx (e.g. Idx 1 : 296) of the Sonoff switch you want to control
13. Save and after the Sonoff rebooted you can control it with Domoticz.
14. Open your Sonoff browser --> Configuration --> Configure MQTT. Host = IP of Mqtt server (e.g. 192.168.1.45) and do not forget to fill user field (mosquito) and password field.
Domoticz on Pi2 - 8 ds18s20 - 2 PL2303 teleinformation EDF power & solar PV -RFXTrx433e - Zwave : UZB Sigma controler, Smoke and flood detector, smart plugs - Philips HUE - Flower Power
jinxedworld
Posts: 1
Joined: Friday 03 November 2017 8:25
Target OS: Linux
Domoticz version:
Contact:

Re: Sonoff - MQTT - Domoticz

Post by jinxedworld »

Nice tutorial! However, I'm not sure why i would want to do this. There's enough space on the available chip to flash Tasmoto on. ( BIN is only 472KB ). What would be the usecase for this?
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: Sonoff - MQTT - Domoticz

Post by MikeF »

jinxedworld wrote: Tuesday 21 November 2017 13:51 ...I'm not sure why i would want to do this. There's enough space on the available chip to flash Tasmoto on. ( BIN is only 472KB ).
Agreed - I've successfully flashed 6 of these with the Tasmota firmware on the existing chip, and configured them for MQTT and Domoticz.

Image
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest