Openhabian Service Stopped and Started Again
Smart Switches are pretty much a must-accept on whatever home automation project only surprisingly, the market isn't flooded with these type of devices yet. In addition to this, virtually of the options aren't very affordable.
Recently, I got the opportunity to test some devices from SmartHomeProducts, some smart switches amidst them. They work smashing but unfortunately, they aren't compatible out of the box with openHAB.
This is non the cease of the story though…
On this tutorial, I have compiled and explained a couple of ways you can get openHAB to work with smart switches from SmartHomeProducts or whatever devices that use the Tuya firmware.
If you lot are looking to get some Smart Switches or want to know how to connect openHAB and Tuya stick effectually.
Description of the Products
First of all, I would like to thank the guys at SmartHomeProducts for providing the samples for testing. They were very confident in their product and set up to answer every question that I would throw at them. They are a United states based company that distributes smart devices that use the Tuya Firmware.
I accept tested several products but my favorite ones are the smart dimmer (very proficient looking) and the mechanical switch. They accept other switches besides, but the mechanical one is more responsive than the others and feels more natural.
3rd Party Integrations
Out of the box, these devices are compatible with the SmartLife App, Alexa, Google Home and IFTTT.
Unfortunately, they are not compatible with open-source Home automation systems, at to the lowest degree Equally-IS.
If y'all take been post-obit the blog for some fourth dimension you volition probably know that I am a big advocate for Open Source Habitation Automation Systems, like openHAB or Dwelling Assistant. Unfortunately, there is no connectivity out of the box for these kinds of systems.
However, this is never the stop of the story for united states of america, makers, we love a good challenge. Even if there is no support out of the box, at that place are definitely alternatives and I will tell you lot near them.
On this post, I want to get through 2 dissimilar options, the first one is quick and more or less clean. The second one is a bit hacky merely not too complicated to implement. Each of them has its pros and cons so let's get started…
Tuya Smart Home Devices
As I said before this method should cover pretty much every Tuya device out there that uses the ESP8266 chipset. Here is a list of the dissimilar products I take tried myself in case yous want to cheque them out.
Description | |
Smart Outlet | Cheque Price |
Smart Light Dimmer | Bank check Price |
Smart Wall Switch | Check Price |
Smart Plug | Check Price |
Smart Bulb | Cheque Price |
Tabular array of Contents
In case you desire to jump directly to one particular method, go ahead and click on the link.
- Connecting openHAB and Tuya using IFTTT
- openHAB and Tuya: Local Control using a MiM Attack.
- Flash Tuya Devices with Tasmota Using Tuya-Convert
Connecting openHAB and Tuya using IFTTT
Although the fix of devices from SmartHomeProducts aren't supported by openHAB directly, they do support IFTTT out of the box and we can use it to bridge the communication.
Advantages of this Strategy
- The strategy is fairly clean to implement.
- It does not require besides much technical knowledge.
- One time implemented, information technology is very stable.
Disadvantages of this Strategy
- We innovate an boosted point of failure.
- Bridging the connexion through IFTTT may innovate a 1 or 2 seconds filibuster sometimes.
- There is no feedback from the devices. This ways that openHAB doesn't know if the switch is ON or OFF.
Let´southward go started with the process.
Creating an IFTTT Account
In order for this process to piece of work for yous, yous need an IFTTT account. It is completely free and information technology takes about 2 minutes to set it up.
I already wrote a tutorial virtually openHAB and IFTTT and if y'all have questions setting up the account y'all can definitely check information technology out. However, in the part that relates to connecting IFTTT to openHAB I will exist using a dissimilar arroyo so make sure you come back to this post.
Connecting the Devices with the SmartLife App
Earlier you start connecting the devices to IFTTT, it is required to register them in the SmartLife App. That will connect your new smart switches to the Tuya deject and will brand them bachelor to IFTTT.
The user guide that comes with the products is very minimalistic but more than than sufficient to get them upward and running in about 5 to ten minutes.
Here you lot have a small summary of the steps:
Before yous start the process you may want to have you wifi countersign in handy, you lot will demand it.
- Download the SmartLife app on your smartphone.
- Click on add devices and select the device type.
- Set the device in pairing mode (normally property theONpush for more than than v seconds)
- Become ahead and pair the devices.
Connecting Tuya Devices to IFTTT
Let's get to the good stuff, get ahead and login into your IFTTT account. It is fourth dimension to offset adding devices.
Earlier nosotros do that, I want to requite you lot some context.
openHAB really has a bounden to connect straight to IFTTT, however, later on testing the binding for quite some time I noticed that there were significant delays triggering some of the events. I attribute it to the fact that at that place were too many clouds involved in the process, Tuya Cloud + IFTTT Cloud + openHAB deject…that is pretty much the whole tempest.
For this tutorial, I will be using a pretty cool functionality from IFTTT which is called Webhooks. It basically allows you to trigger any IFTTT applet using a spider web service call.
This method has endless potential becuase you can connect any device supported past IFTTT to any habitation automation system that supports spider web service calls (pretty much all the open source ones).
This method has given me much better results than using the openHAB binding and it is also more versatile. If you have any device for which at that place isn't an openHAB bounden but information technology is supported by IFTTT, here is how you can use it.
Let'south become started…
The first step is to login into your IFTTT account and create a new applet. Every applet involves a trigger and an activeness, and so allow'due south get ahead and select the trigger.
IFTTT volition show you a very long list of services, you can pick webhooks and then select the but available pick,Receive a web request.
The only configuration required for the webhook is the event name. Cull something meaningful since you will have to employ it in your web service call.
We are done with the trigger and then let'due south get alee an add together an activeness. Among the assortment of options provided by IFTTT you will find SmartLife. Select it and connect to information technology.
After connecting, you will be prompted with a window to put on your SmartLife credentials, the same ones you used in the smartphone app.
IFTTT volition now display all the unlike actions available. You lot volition have to create one different applet for every different action that your device provides.
For example, if I am installing a smart switch, I have to echo this procedure twice, i for the ON role and i for the OFF function.
At this point, you should have already created one applet per action that your device allows. I am adding a switch so I but demand ii applets linked to two webhooks events.
KITCHEN_ON
KITCHEN_OFF
In order for y'all to trigger the applet, you will need a token from IFTTT which you can go from the webhook settings page. Your token will be the funky string of characters that comes after /use/ in the URL.
The web service endpoint will look similar this:
https : //maker.ifttt.com/trigger/{EVENT_NAME}/with/fundamental/{IFTTT TOKEN} |
Subsequently replacing the details:
https : //maker.ifttt.com/trigger/KITCHEN_ON/with/key/l5GL_ZO-bh9w40InAsgksdjfhg576lksdi4m2_6xUlWi |
https : //maker.ifttt.com/trigger/KITCHEN_OFF/with/fundamental/l5GL_ZO-bh9w40InAsgksdjfhg576uCGi4m2_6xUlWi |
Everything is ready to test the event. Requite it a shot with a simple curl command to make y'all followed the steps correctly.
curl -X Post "https://maker.ifttt.com/trigger/KITCHEN_ON/with/cardinal/l5GL_ZO-bh9w0IDdsx8R2uCGi4m2_6xUlWi" |
If everything went OK, this command should turn on your lite.
Trigger IFTTT Webhook using openHAB
To trigger the web service request from openHAB, you will need the HTTP bounden, no worries it is very elementary to employ.
I like to install my bindings using the configuration files but yous can utilise PaperUI if you are more comfortable with it.
Edit the file /etc/openhab2/services/addons.cfg.
It will have different parameters to configure, you simply care most binding though.
# A comma-separated list of bindings to install (e.g. "binding = sonos,knx,zwave") binding = milight , network , mqtt , elapse , amazonechocontrol , expire1 , orvibo , http1 |
Add http1 at the cease of the list and look for a couple of minutes until openHAB installs the bounden. One time the bounden is installed you can create an item in openHAB to control the Smart Switches.
Edit your preferred items file, they all should exist under /etc/openhab2/items/and create an detail as follows:
Switch I_KT_Light_Switch ( gLights ) { http=">[ON:Mail:https://maker.ifttt.com/trigger/KITCHEN_ON/with/key/l5GL_ZO-bh9w40InADEfDuCGi4m2_6xUlWi] >[OFF:POST:https://maker.ifttt.com/trigger/KITCHEN_OFF/with/key/l5GL_ZO-bh9w4ADOOEfDdsx8Rm2_6xUlWi]" } |
That'due south information technology, openHAB and Tuya are at present integrated. Now y'all can use that detail as you would with any other. Don't forget to supersede the event proper name and the token from IFTTT with your own.
openHAB and Tuya: Local Command
Using IFTTT to connect the Tuya devices to openHAB has obvious disadvantages, mainly the delay triggering the event. If you are not happy with the other solution, go along reading, this one has it all simply it needs some legwork.
With this method you will exist able to control the devices locally (no deject involved) with minimal to no lag.
Before I go any further, I would like to thank AgentK for putting together the MQTT wrapper and codetheweb for the TuyaAPI. They have done all the work that made this connection possible, I accept but put the information together.
Likewise thank you to Arilux in the openHAB forums for compiling all the info. His method to retrieve the local key didn't work for me just it is worth checking if y'all have issues/reservations with the procedure on this post.
Preconditions:
Before you lot get ahead with the installation at that place are three things to keep in mind:
- You should take an MQTT broker connected to openHAB.
- All the devices that you want to control with openHAB should exist added to the SmartLife App first.
- Make certain yous practice all the steps in the process using the openHABian user.
Installing Required Node Modules
- SSH into the Raspberry pi and navigate to /etc/openhab2/scripts
- Install all the required NodeJS Modules
npm i @ tuyapi/cli -thousand npm i anyproxy -g npm i forever -grand npm i forever-service -g npm i mqtt |
If you are using openHABian you lot will most probable run into permission issues, yous tin can install the parcel with sudo although I would fix the permissions instead of doing that. Yous will avoid future issues.
MITM Attack
Hither is the funky part of the project…All the Tuya devices are identified by 3 parameters; ID, IP and Local Key. The first ii of them are very easy to go, the last one…not so much.
The Tuya devices use the local primal to secure the communications between the cloud and the device. Nosotros would need to decrypt the messages between the SmartLife App and the Tuya Cloud, and in a way, that is what we are doing.
We volition practice something called Man-In-The-Center Attack. This basically means sitting between the SmartLife App and the internet reading the communications betwixt the two.
It sounds very complicated and in a style it is, however, the contributors of the TuyaApi have made the process very elementary.
The start step is to create a proxy in your Raspberry PI that will bridge the connections.
- Use Anyproxy to generate a document.
Anyproxy will generate a certificate that y'all volition have to download from your smartphone and trust. If you don't trust the document, your telephone volition observe there is something going on and volition shut downwards the communication.
2. Execute the list role in the tuya-cli app in lodge to start the MITM assault.
If everything went OK, a gigantic QR code should be generated in the final.
Using a QR Scanner App in your smartphone, get ahead and scan the code. The code is storing a URL to download the document. Once y'all download it, your phone volition inquire y'all if you want to trust information technology, which is required for you lot to go along. You should untrust information technology every bit soon as you are done with the tutorial.
3. Now that you have installed the certificate in your telephone, it is time to configure your connection in guild to use the Proxy that y'all have just created. Things might be slightly different here, depending if you have an Android or an iPhone. The details of the proxy should be correct below the QR lawmaking, IP and port.
Don't forget to make sure that you are on the same wifi every bit the smart switches.
Now your phone should be connected through the proxy and all the connections will be bridged through the Raspberry Pi.
4. After the proxy is configured in your phone, open the SmartLife App and refresh the devices.
Similar magic, the list of Tuya devices should show up on your terminal, including the elusive local fundamental. Copy those details and keep them in handy, perhaps on a spreadsheet and so you tin have all the information organized.
Configure the Tuya-MQTT Wrapper
This script will act as an interface between openHAB and the Tuya devices using MQTT.
Before you continue, assemble all the details on a spreadsheet so you are ready to configure everything. Y'all volition need primarily 3 data points for each Tuya Device that you desire to command.
- Id
- Key
- IP
You already got the id and key from the prior pace then you only need the IP. At that place a few means you can go this.
- First, get the MAC accost of the device from the device properties in the Tuya App.
- Go to the admin page of the router and bank check the IP assigned to the device with that MAC accost.
- If you don't accept admission to the router or just can't do information technology this mode, you tin can use the following command in Linux.
ip neighbor | grep "bc:dd:c2:fa:97:ba" | cut -d " " -f1 |
Make sure you input the MAC address in lower instance.
Proper name | ID | KEY | IP |
Bathroom Switch | 217261bf250809 | bc55c9230a | 192.168.1.101 |
Kitchen Switch | 024285bcdd97ba | 920d0cf7bc | 192.168.1.107 |
Once y'all have all this data, you lot tin can move forward with the side by side pace.
- Navigate to /etc/openhab2/scripts
- Clone the Tuya-MQTT repository from GitHub
git clone https : //github.com/TheAgentK/tuya-mqtt.git |
3. Navigate to /etc/openhab2/scripts/tuya-mqtt and create a configuration file.
cp config . json . sample config . json |
iv. Edit the Configuration file with the details of your MQTT Broker.
{ "host" : "localhost" , "port" : 1883 , "topic" : "tuya/" , "mqtt_user" : "openhabian" , "mqtt_pass" : "YDi@4Yx78314i%$" , "qos" : two } |
Before you go ahead with these steps, make sure your MQTT Broker is up and running and it is continued to openHAB.
You will need at least, athingand an item.
openHAB Things
Permit'southward define a new MQTT generic thing to communicate with the Tuya-MQTT Script.
Span mqtt : banker : mosquitto [ host="localhost" , secure=false , username="{USERNAME}" , password="{PASSWORD}" ] { Thing mqtt : topic : tuya { Channels : Type switch : T_KT_Light_Switch "Kitchen" [ stateTopic="tuya/lightbulb/02428575bcddc2fa97ba/920d0ca3b609f7bc/192.168.1.102/country" , commandTopic="tuya/lightbulb/02428575bcddc2fa97ba/920d0ca3b609f7bc/192.168.1.102/command" ] Type switch : T_BT_Light_Switch "Bathroom" [ stateTopic="tuya/lightbulb/217268523c71bf250809/bc55c0fa26e9230a/192.168.i.103/state" , commandTopic="tuya/lightbulb/217268523c71bf250809/bc55c0fa26e9230a/192.168.ane.103/command" ] } } |
There are a few details you need to pay attention to here:
Fill out correctly the details of the MQTT broker:
Bridge mqtt : broker : mosquitto [ host="localhost" , secure=false , username="{USERNAME}" , countersign="{Countersign}" ] |
Configure the tuya topics correctly:
State Topic: The topic where the devices publish the land of the device.
tuya/lightbulb/{ID}/{Fundamental}/{IP}/state
Instance:
tuya/lightbulb/02428575b2fa97ba/920d0ca09f7bc/192.168.1.102/state
Command Topic: The topic where the device receives ON and OFF commands.
tuya/lightbulb/{ID}/{KEY}/{IP}/command
Case:
tuya/lightbulb/02428575bcda97ba/920d0ca9f7bc/192.168.1.102/control
I am using the object lightbulb in the topic, simply it is valid for smart switches also.
openHAB Items
The items are much more direct forwards…
Switch I_KT_Light_Switch "Kitchen" ( gLights ) { channel="mqtt:topic:Mosquitto:Tuya:T_KT_Light_Switch" } Switch I_BT_Light_Switch "Bathroom" ( gLights ) { channel="mqtt:topic:Mosquitto:Tuya:T_BT_Light_Switch" } |
Now you lot can utilise the items in openHAB to control the Smart Switches. This arroyo takes a flake more fourth dimension but once it is upwardly and running, you lot can fix and forget near it.
Run the openHAB Tuya Script every bit a service
This is optional but I highly recommend you to do it, otherwise, you will have to bring up the script every time you reboot your openHAB server.
You tin can test your script first to iron out potential issues:
forever start tuya-mqtt . js |
Navigate to /etc/openhab2/scripts/tuya-mqtt and run the following command:
sudo forever-service install tuya-mqtt --script tuya-mqtt . js |
You lot are good to go! Restart your raspberry pi and test your new openHAB and Tuya Integration.
Troubleshooting
Forever path is not constitute
If you run into issues becuase the forever control is not constitute check this out.
Forever-service not found for Sudo
Similar to the effect above but when adding the script as a service. Cheque this out.
That's all the is, if you have feedback on the topic make sure you leave it on the comments.
Flash Tasmota on Tuya Devices using Tuya-Convert
The next approach and, the 1 I currently use for all my Tuya devices, is based on the Tuya-Convert project. The Tuya-Convert project allows you to costless up from the cloud virtually of the Tuya devices that are based on the ESP8266 chipset.
You may have noticed that I have saidvirtually of and the reason is that Tuya has been trying to forbid their devices from being flashed, which they have achieved at least during some time.
The relentless community behind the project recently released a new version that makes this hack possible again.
The Tuya-Convert hack basically allows you to install an ESP8266 uniform firmware into your devices, like Tasmota, and lets yous control them locally using HTTP or MQTT. I flashed all the devices and I take been using them for 2 months. I couldn't be happier, Tasmota is rock solid and has endless possibilities.
Permit's get started.
Requirements
There are some hardware requirements that you need to fulfill to perform this hack. I will tell yous what I used but I am sure information technology is non the only option.
- Raspberry Pi ii with Raspbian Installed.
- Wifi Dongle Linux Compatible. I tried a few unlike ones and I struggled with them until I got the Panda PAU05. In case yous need to buy one, you lot should consider that one.
Download and install the Tuya-Convert Project
one. Clone the Tuya-Convert project.
git clone https : //github.com/ct-Open-Source/tuya-convert.git |
2.Navigate into the Folder
3.Install Prerequisites
This is a very long process, it tin can take 20 to thirty minutes so go become yourself some coffee while y'all wait.
Flashing Process
one. Open up a second window to monitor the logs.
cd tuya-catechumen/scripts/ tail -f smart* |
the logs files are created the first time you lot execute the script then you lot won't see annihilation until you get-go running it.
ii. Have your phone in handy to connect to the wifi that the script will create.
3. Start the Flashing Procedure
The first screen will show you the usual disclaimer. Delight, continue in mind that you lot are doing this at your own gamble and you could potentially brick your device. I have done it with several devices, notwithstanding, and I haven't had whatever outcome so far.
Typeyes, hit enter and become ready to start the party.
four. Before yous proceed with the side by side step make sure of the following 2 messages prove up. Information technology will be required for yous to be able to wink the device.
5. Connect your telephone to the AP created by the script.
If everything went OK, you should see a wifi called vtrust-wink which you can connect using the passwordflashmeifyoucan.If you can connect to the wifi simply at that place is no internet, review the logs and bank check if there was an event creating the AP.
6. Put the Tuya device Device in Pairing Manner
Put the tuya device in pairing mode. Every device is a little unlike so bank check your user manual to make sure you understand how to practice it in your device. After the device is in pairing mode, hit enter and wait for the magic to happen.
Later on a few seconds (minutes sometimes) you should come across a screen like this 1:
That ways you are doing peachy. The script is backing upwards the current version of the firmware on the device and getting it ready to upload a new one. The process should not take more than a couple of minutes and afterwards that, you should run into something like this.
Before getting on with the next step, make sure the agile user space is user2.If it is not, run the post-obit command!!
curl http : //ten.42.42.42/flash2 |
Upload Tasmota Firmware
The following command volition offset uploading the firmware to the tuya device. The tuya-convert project should already come up packaged with the latest version of Tasmota but don't worry if it isn't the last one since the starting time thing that nosotros volition do is to upgrade the firmware.
gyre http : //10.42.42.42/flash3 |
Almost instantly you will get the post-obit message on the screen which is extrmely disconcerting since yous don't know what to do next. Don't freak out, I got you covered.
Now you accept to go on an middle on the wifi'south in range until you lot see something similar the following showing up.
** In the latest version of the firmare is tasmota-****
Connect to the wifi and apply your browser to go to 192.168.4.i.
Configure Tasmota
The start screen you volition meet when you open 192.192.4.ane is a page to configure your home wifi.
Wifi Connectivity
- Striking the link below to start searching for wifi'south within range.
2. Select your home wifi in the list that pops upwards.
3. Provide the wifi countersign.
Brand sure you re-create and paste information technology from somewhere to avoid a fatty finger situation and and so save.
That'due south it, your wifi is configured. If everything went OK, the device should now connect to your domicile network.
If the password was incorrect, information technology should default to the initial configuration and make the AP available once again.
Y'all will need to find out the IP that has been assigned to the device, y'all tin can normally exercise this through the page of the router.
Upgrade the firmware
After we provide the wifi connexion details, the tuya device will exist assigned an IP from your router that y'all will need to access to it.
Apply the configuration page of your router to notice out the IP of the new device. It should accept a name similar to the proper noun of the AP. Paste the IP on the browser to access the Tasmota configuration page.
Earlier we configure annihilation on the device, we should make sure you are upwards to date with the version of the firmware. Let's get ahead a do a firmware upgrade.
Configure the Tuya Device
Tasmota is a multipurpose firmware merely in order to use a specific device, you need to provide the configuration with the different inputs and outputs. This used to be a big deal but at present with the repository that blakadder put together, it is a piece of cake.
- Go to the Blakadder repository to locate your device and click on it.
two. Copy the template and paste it somewhere in handy.
3. Go to the Tasmota configuration page over again and paste the template.
** Make sure you striking activate before saving the template, otherwise it won't work.
Configure MQTT Connectivity
The device is already flashed and functional just if y'all want to use information technology with openHAB or any other open-source home automation system through MQTT, it is a good idea to configure the MQTT Connectivity.
Make sure you provide a Topic name so yous can place the MQTT messages. The commands for every device type are slightly different so you may want to read advisedly the commands page
fostergavempurneth.blogspot.com
Source: https://smarthomeblog.net/openhab-tuya/
0 Response to "Openhabian Service Stopped and Started Again"
Post a Comment