Using the Sonoff IoT/WiFi Relay with your own MQTT Server (#52)
Vložit
- čas přidán 26. 08. 2024
- Previously I connected up a Sonoff relay (about £4 to buy) to my WiFi, to switch mains loads via a phone app. What I really want, though, is to reflash the unit so that it works with a server here in the house rather than the Sonoff/eWelink server up in the cloud. Using the same method I could talk to my own cloud server, using Amazon Web Services or Google Cloud.
I'm using the Tasmota software in my Sonoff relay, an awesome piece of work from Theo Arends:
github.com/are...
There's a very nice walk-through of how to build Tasmota (using PlatformIO and the Atom editor) on the QuickPi channel:
• TUTORIAL: Install Sono...
His walk through is on a Mac, but the Atom/PlatformIO works exactly the same on Windows, and this is what I did.
PlatformIO (open source dev tools for IoT): platformio.org/
Atom IDE: atom.io/
Super video! Thanks.
Many thanks, Martyn! I'd spent hours trying to figure out how to get MQTT and sonoff/tasmota talking. Your detailed walkthrough helped immensely
I'm happy to hear it
Hi Martyn, thank you for this video. Finally I have got one small step closer to a home automation setup with your help. My ambition is to have multiple sonoff-tasmota switches on my lamps and small appliances, then control and monitor their state using Home Assistant or openHAB. I would be grateful if you could show some more of your system and how you integrate the sonoff-tasmota switches. Thank you.
Fantastic Martyn - watching your excellent tutorial finally got my Sonoff / Node Red talking via local MQTT. I had struggled to get this working and to see what's going on but using MQTT.fx and your guide help me a great deal - success at last
Great to hear
Very informative, thank you sir. I'll be ordering a few switches ASAP :)
Super, it's very helpful.
Thank you for the video!
Very nice explained :)
Thanks!
Thanks great video, helped me a ton . Thanks
great stuff. Thank you!
Sorry I don't speak english very good, but it means that I can control the sonoff device using the app in my phone without Internet connection, using my own local server?
It depends which app. Some apps allow you to set the address of your server, in which case you could control the server via WiFi. However, many apps (such as ones to control WiFi light bulbs) are contacting a server in the public cloud, and the equipment (bulb) is connected to the same server. In this case an Internet connection is needed.
@@MartynDavies thanks!
So it's better to use the browser of a cellphone and control the switch with it (without internet)? Should I find another app?
Thanks for your answer.
Hi Martyn, Thank you so much for this video. It has helped my greatly. This is the best video to explain setting up Tasmota / Sonoff and working with MQTT.fx. Are you working with Openhab2 by chance? You seem able to explain things very well...Thank you.
Thanks for the kind words. I'm not using Openhab, although I have seen a lot of questions about this - perhaps I should.
I’d second this. Your video helped clear up my confusion in 10 minutes when I had spent 2 hours scratching my head. If you do look at OpenHAB I would definitely be interested. Thanks again
Thanks great video!... do you know if you can use tasmota mqtt with thingsboard?
Hi loved the video and have joined your channel please tell me the Linux box you use thanks Bob in the UK
I can't exactly remember, but mostly I tend to use Ubuntu, and it was probably either a laptop or an Acer compact PC. Most things can be used to run MQTT server, including a VM (e.g. using virtualbox) or even a Raspberry Pi.
@@MartynDavies Thank you for your reply so anything that is Linux based will do the job, which is very helpful I have a raspberry pi 3 that I think I will use as it cost very little to run have you tried Tazmotizer? I have used it and it is very easy to program
@@MrBobWareham No I haven't seen that before. I will look.
I was following until you said "a stream called stat" where does that come from?
It's one of the topics that the Tasmota software uses to publish information on (see github.com/arendst/Sonoff-Tasmota/wiki/Commands ). Whenever the state of the relay changes you see a message published on this topic.
Thank you. There is a lot to take in. Sorry I missed that. Your videos are outstanding!!!
Hey Martin Thanks a lot for this great video, but what happen with MQTT when i can change the ON/OFF state from MQTT.fx only for tasmota??? (with togle))trough the CMND ON/OFF??? I always get the message Power OFF in my stat suscribe, even when I Publish ON cmnd on the same topic
I'm afraid I don't know the answer, but you could look on the Sonoff forum and see if someone else has reported the same issue: itead.freshdesk.com/support/home
Martyn,
Your instructions are clear and extremely helpful:
I was able to do everything just fine, but when I tried to upload I got the following error:
error: Failed to open COM5, so the firmware cannot be uploaded.
I platformio.ini line 65 specifies the COM5
Any suggestions?
Make sure that you don't have the port already open in something else, e.g. the Arduino IDE (or serial monitor), as only one app may own a com port at any one time. You could also check in the device manager that the port is correct - sometimes the plug & play chooses a different port when devices come and go.
Thanks Martyn, I identified the problem. The USB cable was faulty . Thanks for your reply
Hi Martyn... great youtube channel... just wanted to ask.. the 5v rail on the sonoff would i be able to run some ws2812b leds also known as neopixels off there ? I see the tasmota firmware supports ws2812 leds... i see the 3.3v regulator is there for running the esp8266 and the 5v is used for switching the relay already... just wondering what sort of current i can expect to get off the 5v rail.... thanks....
The 5V to drive the relay coil seems to come off the zener (d5 on Sonoff's schematic), so this is a simple zener regulator, and I doubt there's much surplus current to be had there.
You could draw some extra current out of there with a resistive load and see if the voltage starts to dip below 5V. It's only a very small transformer, so go cautiously.
yeah i see that just not really that good on the circuit theory side of it all... i am driving 2 ws2812b leds at the moment with no problem, anymore i will be putting a oad across the supply or running a seperate power supply to the led strips..
I want to tell you that in the latest version of the Raspbian Stretch the MQTT is failing. Now I have installed Raspbian Jessie Pixel and everything is working splendidly. The test that you do with the Sonoff basic and MQTT works for me too. Well, now I have to connect it to my PC, to my phone - tablet and I have tried the openHAB but even it has a binding with the MQTT it does not capture it automatically, as for example it does with TP-Link. I have 2 switches and they are installed automatically. I can emulate with Belkin Wemo the Sonoff basic because it has only one output and it's also installed automatically too. I have 6 Sonoff Dual that I must connect but the Wemo does not work for me since only one channel appears. Do you know by coincidentally how I could connect them to the openHAB ?. Do you recommend some other application that is easy to configure or maybe they are installed automatically?. I have reached so far after so many attempts and everything has been extremely fascinating. Now it's important that the Sonoffs Dual work for me and I can remotely control them inside and outside of my house. Once this is done, start installing them in my house. I NEED YOUR HELP!. It would be great if you could help me with this. Thank you very much in advance!
Send feedback
Thanks for the comment. I've just loaded a Pi 3 with Stretch, so I'll see if I get the same experience.
If i use an external server like Adafruit, how do I fulfill the MQTT Parameters? User and pass (AIO Key) from my MQTT Adafruit? Thank you!
I haven't so far used the Adafruit service (altthough it looks nice!). I'll try to find some time to experiment with it.
Hi managed to get everything sorted with Openhab i have a question for you in India the Fan is on 24/7 almost i have connected one of the switches to the FAN i am not an electrical engineer so would this switch survive the stress of being on for long period of times
I'm glad to hear that you had success.
When it comes to high-power electrical machines (i.e. that have mains powered motors or heating elements), I would definitely be cautious with this entry-level Sonoff device. There is a bigger brother that has a 16A rating, and has other safety features like a fuse, a MOV and more anti-tracking slots, which would make it safer for driving a fan.
John Ward did a teardown of one of these recently, if you would like to see the detail: czcams.com/video/1mhbxIvVft0/video.html
Thanks actually the fans conse around 100 watts of electricity so that are not that high power devices but yes will take your advice and get the 16 AMP one to be on safer side
Actually may be this will also help me test the burn in
Actually getting the device connected to openhab is pretty easy however there are tons of videos which complicate things and confuse you.
Thanks again for your kind advice at every step
Where is the database? can i use my own database server? how can i insert to my database table?
I'm not using a database
i have two Sonoff switches stopped working with 15 days on installation both have been connected to LED lights i have been told that the issue is because of noise and improper filtration in the switches.
Can you please guide me as is there anyway i can add something to protect sonoff from the noise basically i have evil LED and need something to protect my sonoff from it
I'm sorry to hear that. What's the mode of failure? Is it that the control electronics failed, or the relay? The protection circuitry doesn't look too bad in the Sonoff: it has an input inductor and a MOV across the primary, so I imagine it would be resistant to surges. You could check that all of the power supply capacitors are of suitable voltage rating and quality.
to be honest i am not sure what has failed as i am a novice, however one of the switches when connected to usb through pin shows up so i guess its something to do with the HV circuit, to be honest i dont think this is an issue with the switch as my switch connected fan is still working fine and it works almost 24/7, in India you get unbranded LED ceiling lights may be they are the one responsible i am planning to replace them with philips led downlights
If i flash the SonOff basic with Tasmota can i still be able to control it with an Ewelink App?
No, because the Ewelink app is communicating using a different MQTT server that belongs to Sonoff and is probably running in China somewhere.
Alright, Thank you for your great response.
USB interface has no galvanic isolation even with external power supply usb hub. If u accidently get fase on the low-voltage part of the circuit, you burnout usb controller chip, or get electric shock anyway. Galvanic insulation of usb is a tough task. No cheap solutions for usb 2.0 and later. Use laptop on battery, or 1:1 transformator to pc power supply.
I'm just using the USB here as it was something I could easily put in shot for the demo. I agree that these things should be used with caution.
hi i have a question sorry its off topic but i dont know whom else to ask I have LED downlights which i want to dim can i use on of those wireless wifi controllers based drivers will they work or give any issue
If you mean mains-powered LED lights (which have a small step-down power supply inside each bulb), then it needs a specific type of dimmer, not the old-fashioned triac ones, but a "trailing edge" dimmer which is more electronically complex. I haven't used these myself, but possibly a question on Big Clive's CZcams channel might throw more light on this.
actually this have a similar step down power supply like the LED strips which is external and not internal
Ok, but LED power supplies often have capacitor in the path which makes it unsuitable for the old style dimmers
Hello Martyn, thanks a lot for a great video!. Things seem so easy when you explain them. I am a beginner in this matter and I need your help. I have installed Mosquito server in my Raspberry pi and I have tried to connect Mosquitto client from Windows but it doesn't connect. I write the IP in which it is installed. In my case it's 192.168.0.37 in port 1883. I don't know what happens. Maybe a Mosquitto configuration file that is missing something. Do you have any idea what it could be?. I have configured a Sonoff Dual in the same way as you explain it, but I can not connect with the openHAB. The MQTT bind of the openHAB is installed. I can see the Sonoff in the browser and it works very well. So, I hope you can help me solve this problem. Thanks in advance.
Make sure that your Windows machine has an address on the same network (192.168.0.x). You could use ping on the RPi to make sure that it can reach the Win machine. On the RPi you could run a mosquitto client as well as the mosquitto server (something like screen or tmux makes it easy to run several things at the same time), then you could use the client to send and receive MQTT messages to prove that the server end is working well, before trying to connect the Win MQTT client to the server. Actually, this whole area of debugging networks is something I could do in a video.
Is it possible to use your own server to schedule on/off?
Yes, this is the great thing about having the MQTT messages from the Sonoff going through your very own server. You can create another task (either on the same server or a different one, using TCP/IP to connect to it) that can see whether the relay is on or off, and can send on/off instructions via MQTT. This task could be a Python script, or even a mosquitto-client instruction running from a cron job to give timing.
I'm guessing that the Sonoff eWelink app implements the timing in the iPhone app, and sends on/off instructions via MQTT when events start/finish. The downside is that to use this app, you must be connected to the Sonoff/eWelink cloud server which you have little control over, and who knows might stop working tomorrow.
I'll do some more videos on MQTT and the mosquitto server/client as this is quite a good way to automate things under control of a Linux server, or a cloud server, or even a Raspberry Pi.
I would like to use a switch on the Sonoff. GPIO 14 works perfectly
I cannot read the switch via MQTT. Which MQTT Syntax do I need to read the Status of the switch?
It will depend on the software that you flashed the Sonoff with, but if you use Tasmota (like here) then the topic will be 'stat/{name}', where {name} is whatever you called your Sonoff switch in the configuration. Note that you can always use a wildcard, so stat/# should see messages from all your Sonoffs, or # on its own should give you all MQTT messages from the server.
Thanks for the info. Yes I Flashed the Sonoff with the Tasmota Firmware. Whatever I tried, I had no chance to get the status from the switch on GPIO 14.
Did you test this?
Via GPIO14? No, but if I get a chance I'll fire it up.
Hi any vid on how to configure this with home assistant i have managed to get `my mqtt server config on the switch correct and the device detects on mosquitto mqtt server but no clue what to do after that
I will look at Home Assistant, but haven't yet used it. This Instructables article connects to the Sonoff: see step 7, which uses a YAML script to tell Home Assistant how to interact with the Sonoff via MQTT. www.instructables.com/id/Hacking-a-Sonoff-to-Work-With-Home-Assistant-and-M/
Thanks but having hard time getting to understand hassio any idea on openhab 2 have you worked with it
Thanks any clue on how to add this to Openhab 2 :)
sorry no
For anyone else looking for info on Home Assistant, I stumbled across this github site from KmanOz for HA and Sonoff: github.com/KmanOz/Sonoff-HomeAssistant
any tips on how to receive data back from sonoff to android mqtt dash i cant get any readings back ?
If you're using you're own MQTT server, you could install a client (like mosquitto) and subscribe that to receive all messages; the wildcard in MQTT is a #. This way you can make sure you know exactly what messages are coming out of the Sonoff and if they are actually reaching the MQTT server. On the unmodified Sonoff, I'm not sure as eWelink connects to Sonoff's own cloud service, and I don't know what parameters are used for that.
using cloudmqttserver i can send messages from my android device with an app called mqtt dashboard and control lights and also some ws2812 leds on the sonoff i have connected, but really want to get data back off the sonoff too for instance for testing purposes i want to for example read the sonoff rssi or sonoff vcc. in the tasmota web inteface console if i type status 11 i get back a load of data, but want to strip out the vcc and rssi and be able to show that... any ideas how to strip the data out
should my mqtt client on the mqtt parameters ( tasmota web interface) be this DVES_%06X or should it be the same as host ?
the client name shouldn't matter (I suppose each client subscribed to the same MQTT server should be unique). The most important part is subscribing to the correct host address/name and getting the topic correct. I generally use # as the topic (to see all the messages) then when you know what you're looking for you can use a more specific topic, e.g. stat/#
ok thanks i will have another look over the weekend its all proving to be quite a steep learning curve have done some arduino work before but getting back into it... so does the sonoff send data out automatically, or do i have to tell it the data i want to receive.. ?
Is there an app with which I can control the Sonoff without having an MQT server
Yes, I showed that in the earlier video: czcams.com/video/87mR_QnXwvE/video.html
Thanks but i was looking for something other then ewelink as the same requires internet and secondly the servers are in china
To work completely serverless, you probably need a relay with Bluetooth as this is what phones have built-in. Seom of the pricier Sonoff relays have 433Mhz wireless as well as WiFi, but you probably then still need some kind of gateway to Bluetooth in order to control it via app.
ok so best option would be to build a server on PI and then i can use app to control it
also would this work standalone with Alexa and Echo device
Hello Can you sell project
No, I don't sell anything
When he called Tasmota an 'operating system for the ESP8266'...