Using a Meross Wi-Fi switch with OpenHab
The aim of this article to set up and configure a Meross Wi-Fi switch (I’m using an MSS310) with OpenHab2 to work in offline mode — with MQTT and no dependency on the Meross online services.
I gathered most, almost all, of the information required from this thread on github Offline-ONLY support.
For this article, you need to be comfortable with the command line, and have git and node.js installed; you should already have an OpenHab2 setup.
Configure a MQTT server
Install mosquitto
The Meross switch requires a secure MQTT server.
ssldir="/usr/share/licenses/mosquitto/" # ca openssl genrsa -out $ssldir/ca.key 2048 openssl req -new -x509 -days 1826 -key $ssldir/ca.key -out $ssldir/ca.crt -subj "/CN=MQTT CA" # broker openssl genrsa -out $ssldir/broker.key 2048 openssl req -new -out $ssldir/broker.csr -key $ssldir/broker.key -subj "/CN=broker" openssl x509 -req -in $ssldir/broker.csr -CA $ssldir/ca.crt -CAkey $ssldir/ca.key -CAcreateserial -out $ssldir/broker.crt -days 360
Edit mosquitto configuration
In /etc/mosquitto/mosquitto.conf:
listener 1883 allow_anonymous true listener 8883 port 1883 allow_anonymous true require_certificate false # replace with your CA Root cafile /usr/share/licenses/mosquitto/ca.crt # replace with your server certificate and key paths certfile /usr/share/licenses/mosquitto/broker.crt keyfile /usr/share/licenses/mosquitto/broker.key
Create cert_regen.sh bash script, which will create a new certificate once a week:
#!/usr/bin/env bash ssldir="/usr/share/licenses/mosquitto/" # ca openssl genrsa -out $ssldir/ca.key 2048 openssl req -new -x509 -days 1826 -key $ssldir/ca.key -out $ssldir/ca.crt -subj "/CN=MQTT CA" # broker openssl genrsa -out $ssldir/broker.key 2048 openssl req -new -out $ssldir/broker.csr -key $ssldir/broker.key -subj "/CN=broker" openssl x509 -req -in $ssldir/broker.csr -CA $ssldir/ca.crt -CAkey $ssldir/ca.key -CAcreateserial -out $ssldir/broker.crt -days 360
Create a cronjob for it
00 22 * * 7 /..../cert_regen.sh
Configure the Meross switch for your Wi-Fi and MQTT server
Get the meross tools, from your command line
git clone https://github.com/bytespider/Meross
cd Meross/bin/src
npm i
Plugin the Meross smartplug.
If it’s not your first time using the switch, press and hold the power button for 5 seconds. Connect to its Wi-Fi access point (Meross_XXX)
Configure the switch for your Wi-Fi and MQTT Server
from Meross/bin/src directory
./meross setup --gateway 10.10.10.1 --wifi-ssid <your-ssid> --wifi-pass <your-pass> --mqtt <mqtt-host-ip>:8883
At this stage, you may hear some clicks from the switch. You can also verify that it has connected using the mosquito logs:
1595295630: New client connected from 192.168.30.7
You should also see some messages from the new switch:
/appliance/1712281825467829030134298f151c4a/publish
{“header”:{“messageId”:”7c2834f83f9cc5b1c7ae1049280a9f3f”,”namespace”:”Appliance.Control.ToggleX”,”method”:”PUSH”,”payloadVersion”:1,”from”:”/appliance/1712281825467829030134298f151c4a/publish”,”timestamp”:1557534775,”timestampMs”:955,”sign”:”f07e2a7ab622997508d359cb89c74038″},”payload”:{“togglex”:{“channel”:1,”onoff”:1,”lmTime”:1557534775}}}
Note the appliance ID in bold above.
On my Mac, I also used MQTT Explorer to view the messages and to determine the appliance id.
Add the switch to Openhab
Add a new thing.
- Using the Paper UI as a new thing.
- Inbox / “+” on the top of the screen.
- Select MQTT Binding.
- Manually Add Thing
- Select Generic MQTT Thing
- Give your thing a name, and select your MQTT bridge (configured from step 1).
- Add a Channel
- On/Off Switch
- The settings are as follows:
- MQTT State Topic:
/appliance/<appliance_id>/publish - MQTT Command Topic:
/appliance/<appliance_id>/subscribe - Custom On/Open Value:
1 - Custom Off/Closed Value:
0 - Incoming Value Transformations:
JSONPATH:$.payload.togglex..onoff - Outgoing Value Transformation:
JS:meross.js
- MQTT State Topic:
Finally, download the following file, meross.js and save it to your transform directory, for me this is /etc/openhab2/transform/.
The meross.js file creates the JSON structure required to control the switch.