1) Requirements: apt-get install npm sudo screen jq
2) Install miIO Device Library: npm install --save miio
3) Install miio command line utility: npm install -g miio
4) Run this command to discover and sync the key from your Humidifier: miio --discover --sync
humidifier.js
Code: Select all
#!/usr/bin/node
/* eslint-disable */
// Location of miio node lib
const miio = require('miio');
// No need to change any lines in this section
var deviceip = process.argv[2];
var secondarg = process.argv[3];
var thirdarg = process.argv[4];
function exit() {
process.exit(-1);
}
// Power On (on / off specified as on or off)
if ( secondarg === "power" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('set_power', [thirdarg])
.then(console.log)
.catch(console.error);
})}
// Status
if ( secondarg === "status" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('get_prop', ["humidity","temp_dec","power","mode","led_b","buzzer","child_lock","limit_hum","trans_level"])
.then(console.log)
.catch(console.error);
})}
// Status temperature and humidity only
if ( secondarg === "status2" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('get_prop', ["temp_dec","humidity"])
.then(console.log)
.catch(console.error);
})}
// Set mode (silent, medium or high)
if ( secondarg === "mode" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('set_mode', [thirdarg])
.then(console.log)
.catch(console.error);
})}
// Set buzzer (on or off)
if ( secondarg === "buzzer" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('set_buzzer', [thirdarg])
.then(console.log)
.catch(console.error);
})}
// led
if ( secondarg === "led" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('set_led_b', [JSON.parse(thirdarg)])
.then(console.log)
.catch(console.error);
})}
// Humidity limit percent (specified as 40, 50, 60, 70 or 80)
if ( secondarg === "humiditylimit" ) {
setTimeout(exit, 7000);
miio.device({
address: deviceip
}).then(device => {
return device.call('set_limit_hum', [JSON.parse(thirdarg)])
.then(console.log)
.catch(console.error);
})}
node humidifier.js 192.168.0.95 power off
node humidifier.js 192.168.0.95 status
node humidifier.js 192.168.0.95 status2
node humidifier.js 192.168.0.95 mode medium
node humidifier.js 192.168.0.95 buzzer off
node humidifier.js 192.168.0.95 led 2
node humidifier.js 192.168.0.95 humiditylimit 50
domoticzupdate.sh
Code: Select all
#!/bin/bash
cd /root/xiaomi/humidifier
while true; do
# Main Bedroom Humidifier
# Get the data
data=$(node humidifier.js 192.168.0.95 status2)
# Sort it
temperature=$(echo "$data" | jq '.[0]' | sed 's/.\{2\}/&./g')
humidity=$(echo "$data" | jq '.[1]')
# Load it into Domoticz
curl -s "http://USERNAME:[email protected]/json.htm?type=command¶m=udevice&idx=617&nvalue=0&svalue=${temperature};${humidity};0"
sleep 60
done
Code: Select all
#!/bin/bash
cd /root/xiaomi/humidifier
if [ “$2” == “on” ]; then
node humidifier.js $1 buzzer off
node humidifier.js $1 mode $3
node humidifier.js $1 power on
node humidifier.js $1 led 2
fi
if [ “$2” == “off” ]; then
node humidifier.js $1 power off
fi
Code: Select all
# Start humidifiermonitor
cd /root/xiaomi/humidifier
/usr/bin/screen -S humidifiermonitor -d -m /root/xiaomi/humidifier/domoticzupdate.sh
Code: Select all
commnadArray = {}
-- Main B Humidifier
if devicechanged['Main B Humidifier'] == 'Silent' then
os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on silent')
end
if devicechanged['Main B Humidifier'] == 'Medium' then
os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on medium')
end
if devicechanged['Main B Humidifier'] == 'High' then
os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 on high')
end
if devicechanged['Main B Humidifier'] == 'Off' then
os.execute ('sudo screen -S humidifiertemp -d -m bash /root/xiaomi/humidifier/humidifiercontrol.sh 192.168.0.95 off')
end
return commandArray