mFI mPower Pro: how to set relays ON or OFF

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

mFI mPower Pro: how to set relays ON or OFF

Post by Number8 »

For those willing to control relays of mFI mPowerPro (6 outlets), I thought it might be interesting to have in one place everything that needs to be done in order to have Domoticz able to trigger the relays. Everything has been done using a Windows PC
Step 1: set a static IP address. There is no provision in the Web GUI to do that.
• Get the unit directly connected to your PC with an Ethernet cable.
• Set the PC Ethernet static address to 192.168.1.10
• WinSCP to mPowerPro using 192.168.1.20. User = ubnt, password = ubnt
• Edit /tmp/sytem.cfg, and modify it according the following instructions

Code: Select all

dhcpc.2.status=disabled
netconf.2.ip=staticIPAddress
netconf.2.netmask=netmask
route.1.status=enabled

Add the following lines
route.1.devname=eth1
route.1.gateway=(Put your gateway address here)
route.1.ip=0.0.0.0
route.1.netmask=0
• Quit
• Log to the unit using SSH
• Hit Save, then Reboot. The unit shall have now a definitive static address. Connect mPowerPro and the PC to the regular network. Set the PC network interface to what it was before this step.

Step 2: update mPowerPro to the latest firmware as per these instructions: https://help.ubnt.com/hc/en-us/articles ... s-manually. I did it using SSH.

Step 3 (optional): you may wish to get the relays OFF when the unit boots (default behavior is ON).
• Log to the unit using WinSCP
• Edit /etc/persistent/vpower_cfg
• Add the following lines (if you wish to have all relays off at boot time)

Code: Select all

vpower.1.relay=off
vpower.2.relay=off
vpower.3.relay=off
vpower.4.relay=off
vpower.5.relay=off
vpower.6.relay=off
• Save it, then enter cfgmtd -w -p /etc
Please bear in mind that at next boot time those 6 lines will be deleted, but the unit will behave as expected until the configuration of the unit is modified with the Web GUI.

Step 4: automate SSH login from Raspberry to mPowerPro.
Domoticz runs the script under root, so it is critical to get the public/private key created under root.
• Log to Raspberry using your preferred software (I’m using putty).
• Go to SU (you may need to activate this account the first time. Instructions are easily found on the Web)

Code: Select all

ssh-keygen –t rsa <return>
Hit 3 times return
• Private and public keys are in /root/.ssh. Copy id_rsa.pub to mPowerPro /var/etc/persistent/.ssh/authorized_keys
This is it, Domoticz will be able to send a script to mPowerPro without being asked a password.

Step 5: it is time to write the script. Mine stored in /home/pi/domoticz/scripts

Code: Select all

#! /bin/bash
ssh ubnt@mPowerProIPAddress "\$(echo $2 > /proc/power/relay$1)"
First parameter sets the relay to be controlled (1 to 6), the second parameter (0 or 1) sets the relay OFF or ON.

Step 6: Create a virtual switch, then edit the ON and OFF action as per following for relay 1:
• ON - script:///home/pi/domoticz/scripts/switchOnOff.sh 1 1
• OFF - script:///home/pi/domoticz/scripts/switchOnOff.sh 1 0
Debian buster on NUC and three RPi with buster.
Josh123
Posts: 1
Joined: Saturday 23 April 2016 23:11
Target OS: Linux
Domoticz version:
Contact:

Re: mFI mPower Pro: how to set relays ON or OFF

Post by Josh123 »

Thanks for this. FYI to anyone else trying this, I still use the mFi controller so I needed to use admin@ for the script.

Did you look into reading values from mFi gear?
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: mFI mPower Pro: how to set relays ON or OFF

Post by Number8 »

I have this bash script to read values

Code: Select all

#!/bin/bash                                                                     
#PLUGNAME=MPBureau #Not needed it looks, works fine without
PLUGIP6OUTLETS=192.168.21.235
PLUGIP3OUTLETS=192.168.21.236
PLUGUSER=ubnt			#default "ubnt"
PLUGPASSWORD=ubnt		#default "ubnt"
SESSIONID=01234567890123456789012345678901

DOMOTICZSERVER=192.168.21.240
DOMOTICZPORT=8080

#Create virtual sensors in Domoticz and enter their IDX's here. For the sensor types, see Domoticz JSON wiki
IDX_POWER_1=164
IDX_VOLT_1=158
IDX_CURRENT_1=170
IDX_PF_1=
IDX_STATE_1=152

IDX_POWER_2=165
IDX_VOLT_2=159
IDX_CURRENT_2=171
IDX_PF_2=
IDX_STATE_2=153

IDX_POWER_3=166
IDX_VOLT_3=160
IDX_CURRENT_3=172
IDX_PF_3=
IDX_STATE_3=154

IDX_POWER_4=167
IDX_VOLT_4=161
IDX_CURRENT_4=173
IDX_PF_4=
IDX_STATE_4=155

IDX_POWER_5=168
IDX_VOLT_5=162
IDX_CURRENT_5=174
IDX_PF_1=
IDX_STATE_5=156

IDX_POWER_6=169
IDX_VOLT_6=163
IDX_CURRENT_6=175
IDX_PF_6=
IDX_STATE_6=157

IDX_POWER_11=182
IDX_VOLT_11=179
IDX_CURRENT_11=185
IDX_PF_11=
IDX_STATE_11=176

IDX_POWER_12=183
IDX_VOLT_12=180
IDX_CURRENT_12=186
IDX_PF_12=
IDX_STATE_12=177

IDX_POWER_13=184
IDX_VOLT_13=181
IDX_CURRENT_13=187
IDX_PF_13=	
IDX_STATE_13=178

if ping -c 1 $PLUGIP6OUTLETS > /dev/null ; then  # if host is online then:
	
	#Login to webinterface of plug
	curl --silent -X POST -d "username=$PLUGUSER&password=$PLUGPASSWORD" -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP6OUTLETS/login.cgi

	#Turn outlet no1 on
	#curl --silent -X PUT -d output=1 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null
	
	#Retrieve all data from JSON-output
	SENSOR=$(curl --silent -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP6OUTLETS/sensors) 

	for i in 1 2 3 4 5 6
		do
			#compute offset between lines
			line=$(((i-1)*9))
			Current=$((line+5))
			Volt=$((line+6))
			Power=$((line+3))
			PowerFactor=$((line+7))
			State=$((line+2))
			VoltPosition="f"$Volt
			CurrentPosition="f"$Current
			PowerPosition="f"$Power
			StatePosition="f"$State
			PowerFactorPosition="f"$PowerFactor
			MPVOLTAGE=$(echo $SENSOR | cut -d "," -$VoltPosition | cut -d ":" -f2  | awk '{printf("%.3f\n", $1)}')
			#MPCURRENT=$(echo $SENSOR | cut -d "," -$CurrentPosition | cut -d ":" -f2 | awk '{printf("%.2f\n", $1)}')
			MPPOWER=$(echo $SENSOR | cut -d "," -$PowerPosition | cut -d ":" -f2  | awk '{printf("%.1f\n", $1)}')
# this value is processed in a specific script
#			MPSTATE=$(echo $SENSOR | cut -d "," -$StatePosition | cut -d ":" -f2  | awk '{printf("%.0f\n", $1)}')
#			if [ "$MPSTATE" = "1" ] ; then
#				MPSTATE=On
#			else
#				MPSTATE=Off
#			fi
			#echo $MPVOLTAGE 'Volt-new'
			#echo $MPCURRENT 'Current-new'
			#echo $MPPOWER 'Power-new'
			#echo $MPSTATE 'Relay state'
			#report Voltage
			eval IDX='$'IDX_VOLT_$i
			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPVOLTAGE > /dev/null
			#report Power
			eval IDX='$'IDX_POWER_$i
			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPPOWER > /dev/null 
			#report Current
			#eval IDX='$'IDX_CURRENT_$i
			#curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPCURRENT > /dev/null
#			#report Report relay state
#			eval IDX='$'IDX_STATE_$i
#			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd='$MPSTATE > /dev/null
		done


	#Grab powerfactor (cos phi) from retrieved values and leave 2 decimals, stripoff rest (change printf %.2f if you want more/less decimals)
	MPPOFA=$(echo $SENSOR | cut -d "," -f7 | cut -d ":" -f2 | awk '{printf("%.2f\n", $1)}')
	#Send data to Domoticz sensor without displaying any curl output on commandline
	#curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_PF'&svalue='$MPPOFA > /dev/null 
	#Display mains voltage (Volt) on commandline
	#echo $MPPOFA 'cos phi'

	#Display info message
	#echo "Sensors in Domoticz should be updated with new values, have a look at them"
	
	#Turn outlet no1 off
	#curl --silent -X PUT -d output=0 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null

	#Logout from plug
	curl -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP6OUTLETS/logout.cgi


else #Plug not responding to ping, display error message
	echo "Plug not responding to ping, is it connected to your WLAN?"
fi

if ping -c 1 $PLUGIP3OUTLETS > /dev/null ; then  # if host is online then:
	
	#Login to webinterface of plug
	curl --silent -X POST -d "username=$PLUGUSER&password=$PLUGPASSWORD" -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP3OUTLETS/login.cgi

	#Turn outlet no1 on
	#curl --silent -X PUT -d output=1 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null
	
	#Retrieve all data from JSON-output
	SENSOR=$(curl --silent -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP3OUTLETS/sensors) 

	for i in 11 12 13
		do
			#compute offset between lines
			line=$(((i-11)*11))
			Current=$((line+5))
			Volt=$((line+6))
			Power=$((line+3))
			PowerFactor=$((line+7))
			State=$((line+2))
			VoltPosition="f"$Volt
			CurrentPosition="f"$Current
			PowerPosition="f"$Power
			StatePosition="f"$State
			PowerFactorPosition="f"$PowerFactor
			MPVOLTAGE=$(echo $SENSOR | cut -d "," -$VoltPosition | cut -d ":" -f2  | awk '{printf("%.3f\n", $1)}')
			#MPCURRENT=$(echo $SENSOR | cut -d "," -$CurrentPosition | cut -d ":" -f2 | awk '{printf("%.2f\n", $1)}')
			MPPOWER=$(echo $SENSOR | cut -d "," -$PowerPosition | cut -d ":" -f2  | awk '{printf("%.1f\n", $1)}')
#			MPSTATE=$(echo $SENSOR | cut -d "," -$StatePosition | cut -d ":" -f2  | awk '{printf("%.0f\n", $1)}')
#			if [ "$MPSTATE" = "1" ] ; then
#				MPSTATE=On
#			else
#				MPSTATE=Off
#			fi
			#echo $MPVOLTAGE 'Volt-new'
			#echo $MPCURRENT 'Current-new'
			#echo $MPPOWER 'Power-new'
			#echo $MPSTATE 'Relay state'
			#report Voltage
			eval IDX='$'IDX_VOLT_$i
			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPVOLTAGE > /dev/null
			#report Power
			eval IDX='$'IDX_POWER_$i
			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPPOWER > /dev/null 
			#report Current
			#eval IDX='$'IDX_CURRENT_$i
			#curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX'&svalue='$MPCURRENT > /dev/null 
#			#report state of the switch
#			eval IDX='$'IDX_STATE_$i
#			curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=switchlight&idx='$IDX'&switchcmd='$MPSTATE > /dev/null
		done


	#Grab powerfactor (cos phi) from retrieved values and leave 2 decimals, stripoff rest (change printf %.2f if you want more/less decimals)
	MPPOFA=$(echo $SENSOR | cut -d "," -f7 | cut -d ":" -f2 | awk '{printf("%.2f\n", $1)}')
	#Send data to Domoticz sensor without displaying any curl output on commandline
	#curl --silent $DOMOTICZSERVER:$DOMOTICZPORT'/json.htm?type=command&param=udevice&idx='$IDX_PF'&svalue='$MPPOFA > /dev/null 
	#Display mains voltage (Volt) on commandline
	#echo $MPPOFA 'cos phi'

	#Display info message
	#echo "Sensors in Domoticz should be updated with new values, have a look at them"
	
	#Turn outlet no1 off
	#curl --silent -X PUT -d output=0 -b "AIROS_SESSIONID="$SESSIONID 192.168.4.22/sensors/1 > /dev/null

	#Logout from plug
	curl -b "AIROS_SESSIONID=$SESSIONID" $PLUGIP3OUTLETS/logout.cgi


else #Plug not responding to ping, display error message
	echo "Plug not responding to ping, is it connected to your WLAN?"
fi
To get it running every 5', this line is added in the crontab

Code: Select all

*/5 * * * * ( sleep 30 ; /home/pi/domoticz/scripts/mFIGetValues.sh)
Sleep 30 is because I don't want this script to be fired at the same time as other scripts
Debian buster on NUC and three RPi with buster.
jschatorje
Posts: 1
Joined: Thursday 19 May 2016 22:24
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Schiedam
Contact:

Re: mFI mPower Pro: how to set relays ON or OFF

Post by jschatorje »

Hello!

I am reading this post for over 2 days and i have a few questions,
Situation: Domoticz on synology nas but can't reach the script location so installed another domiticz on my raspberry pi.
I have a MFI Mpower (password and username NOT default).

- in step 1 u disable the auto dhcp is that necessary when i have a fixed IP set in my router ?
- in step 4 u create a ssh key, i did create it and tried to upload it to the mfi but i can't find the location so i'm unable to upload them.
- in step 5 u place the script i did the same but as you will aspect no result.

can u please help me with this one ?
or is somebody working on a package the contains the solution already.

kind gerards!
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: mFI mPower Pro: how to set relays ON or OFF

Post by Number8 »

Hi jschatorje,
Sorry, I took a few days off I could'nt answer to you right away
in step 1 u disable the auto dhcp is that necessary when i have a fixed IP set in my router ?
You may use your solution it will lead to the same result, ie contacting the mFI with a static IP address.
in step 4 u create a ssh key, i did create it and tried to upload it to the mfi but i can't find the location so i'm unable to upload them
you may use this path with is the same. You may have to create the .ssh directory I don't recall whether it was there or not

Code: Select all

/etc/persistent/.ssh
Something I may have overlooked. The first time you have to initiate a manual SSH connection to the mFI gear in order to get the connection authentified. To do so, log the Raspberry with your ssh client and issue the following commands:

Code: Select all

su
ssh ubnt@mFIIPAddress
A windows will popup, say yes.
Then you may check the execution of the script manually. If it succeeds the final step is to get the script registered in the crontab.
Debian buster on NUC and three RPi with buster.
ayasystems
Posts: 66
Joined: Tuesday 19 April 2016 23:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mFI mPower Pro: how to set relays ON or OFF

Post by ayasystems »

If you have a problems to connect try this:

(need to install sshpass)

Code: Select all

sshpass -p 'ubnt' ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o KexAlgorithms=+diffie-hellman-group1-sha1 [email protected]
so the script must be adapted:

Code: Select all

#! /bin/bash
sshpass -p 'ubnt' ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o KexAlgorithms=+diffie-hellman-group1-sha1 [email protected] "\$(echo $2 > /proc/power/relay$1)"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest