I have my heatpump mysensor working, and hooked up to Openhab.
Arduino code here:
#include <MySensor.h>
#include <SPI.h>
#include <Arduino.h>
#include <PanasonicCKPHeatpumpIR.h>
#include <PanasonicHeatpumpIR.h>
#define POWER_ID 0
#define MODE_ID 1
#define FAN_ID 2
#define TEMP_ID 3
#define VDIR_ID 4
#define HDIR_ID 5
MySensor gw;
MyMessage powerMsg(POWER_ID, V_STATUS);
MyMessage modeMsg(MODE_ID, V_HVAC_FLOW_STATE);
MyMessage fanMsg(FAN_ID, V_PERCENTAGE);
MyMessage tempMsg(TEMP_ID, V_TEMP);
MyMessage vdirMsg(VDIR_ID, V_VAR1);
MyMessage hdirMsg(HDIR_ID, V_VAR2);
IRSenderPWM irSender(3); // IR led on Arduino digital pin 3, using Arduino PWM
HeatpumpIR *heatpumpIR = new PanasonicNKEHeatpumpIR();
//Some global variables to hold the states
int POWER_STATE;
int TEMP_STATE;
int FAN_STATE;
int MODE_STATE;
void setup()
{
gw.begin(incomingMessage, AUTO, false);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Heatpump", "1.0");
// Register all sensors to gw (they will be created as child devices)
gw.present(POWER_ID, S_BINARY);
gw.present(MODE_ID, S_HVAC);
gw.present(FAN_ID, S_HVAC);
gw.present(TEMP_ID, S_HVAC);
gw.present(VDIR_ID, S_CUSTOM);
gw.present(HDIR_ID, S_CUSTOM);
// Load our values on start
POWER_STATE = gw.loadState(POWER_ID);
TEMP_STATE = gw.loadState(TEMP_ID);
FAN_STATE = gw.loadState(FAN_ID);
MODE_STATE = gw.loadState(MODE_ID);
sendHeatpumpCommand();
}
void loop() {
gw.process();
}
void handlePowerMessage(bool newState) {
if (newState) {
POWER_STATE = POWER_ON;
}
else {
POWER_STATE = POWER_OFF;
}
gw.saveState(POWER_ID, newState);
}
void handleModeMessge(int newMode) {
switch(newMode) {
case 0:
MODE_STATE = MODE_HEAT; break;
case 1:
MODE_STATE = MODE_COOL; break;
case 2:
MODE_STATE = MODE_AUTO; break;
case 3:
MODE_STATE = MODE_FAN; break;
case 4:
MODE_STATE = MODE_DRY; break;
}
MODE_STATE = newMode;
gw.saveState(MODE_ID, newMode);
}
void handleFanMessage(int newFan) {
if (newFan > 5) newFan=5;
switch(newFan) {
case 0:
FAN_STATE = FAN_AUTO; break;
case 1:
FAN_STATE = FAN_1; break;
case 2:
FAN_STATE = FAN_2; break;
case 3:
FAN_STATE = FAN_3; break;
case 4:
FAN_STATE = FAN_4; break;
case 5:
FAN_STATE = FAN_5; break;
default:
FAN_STATE = FAN_AUTO; break;
}
FAN_STATE = newFan;
gw.saveState(FAN_ID, newFan);
}
void handleTempMessage(int newTemp) {
TEMP_STATE = newTemp;
gw.saveState(TEMP_ID, newTemp);
}
void sendHeatpumpCommand() {
Serial.println("Power = " + (String)POWER_STATE);
Serial.println("Mode = " + (String)MODE_STATE);
Serial.println("Fan = " + (String)FAN_STATE);
Serial.println("Temp = " + (String)TEMP_STATE);
heatpumpIR->send(irSender, POWER_STATE, MODE_STATE, FAN_STATE, TEMP_STATE, VDIR_AUTO, HDIR_AUTO);
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println("This is an ack from gateway");
}
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
switch(message.sensor) {
case POWER_ID: {
bool newState = message.getBool();
handlePowerMessage(newState);
break;
}
case MODE_ID: {
int newMode = message.getInt();
handleModeMessge(newMode);
break;
}
case FAN_ID: {
int newFan = message.getInt();
handleFanMessage(newFan);
break;
}
case TEMP_ID: {
int newTemp = message.getInt();
handleTempMessage(newTemp);
break;
}
}
sendHeatpumpCommand();
}
Openhab items
Switch Power_HeatPump "Heatpump on/off" <aircon>
Number Mode_HeatPump "Heatpump mode"
Number Fan_HeatPump "Heatpump fan" <airing>
Number Temp_HeatPump "Heatpump temperature [%d C]" <temperature>
String VDir_HeatPump "vertical"
String HDir_HeatPump "horizontal"
Openhab sitemap
sitemap heatpump label="Heatpump" {
Frame {
Switch item=Power_HeatPump
Selection item=Mode_HeatPump mappings=[0="heat", 1="cool", 2="auto", 3="fan", 4="dry"]
Selection item=Fan_HeatPump mappings=[0="auto", 1="Level 1", 2="Level 2", 3="Level 3", 4="Level 4", 5="Level 5"]
Setpoint item=Temp_HeatPump minValue=10 maxValue=30 step=1
Text item=VDir_HeatPump
Text item=HDir_HeatPump
}
}
Openhab rules
val org.eclipse.xtext.xbase.lib.Functions$Function4 numberOperation = [
org.openhab.core.library.items.NumberItem numberItem,
org.openhab.core.library.items.StringItem arduinoItem,
String arduinoDevMap,
Integer subType|
logInfo("number", "Sending number message to mysensors node")
var Integer state = numberItem.state
logInfo("mysensors", "Function: numberOperation >> "+arduinoDevMap + "1;1;" + subType + ";" + state )
arduinoItem.sendCommand(arduinoDevMap + "1;0;" + subType + ";" + state + "\n")
]
//switch function
val org.eclipse.xtext.xbase.lib.Functions$Function4 switchOperation = [
org.openhab.core.library.items.SwitchItem relayItem,
org.openhab.core.library.items.StringItem arduinoItem,
String arduinoDevMap,
Integer subType|
logInfo("switch", "Sending switch message to mysensors node")
var Integer state = 0
if (relayItem.state == OFF) {
state = 0
}
else {
state = 1
}
logInfo("mysensors", "Function: switchOperation >> "+arduinoDevMap + "1;1;" + subType + ";" + state )
arduinoItem.sendCommand(arduinoDevMap + "1;0;" + subType + ";" + state + "\n")
]
rule "Heatpump on"
when
Item Power_HeatPump received command
then
switchOperation.apply(Power_HeatPump, MySensors_Gateway, sensorToItemsMap.get("Power_HeatPump"), V_STATUS)
end
rule "Heatpump mode"
when
Item Mode_HeatPump received update
then
numberOperation.apply(Mode_HeatPump, MySensors_Gateway, sensorToItemsMap.get("Mode_HeatPump"), V_VAR1)
end
rule "Heatpump temp"
when
Item Temp_HeatPump received command
then
numberOperation.apply(Temp_HeatPump, MySensors_Gateway, sensorToItemsMap.get("Temp_HeatPump"), V_TEMP)
end
rule "heatpump fan"
when
Item Fan_HeatPump received command
then
numberOperation.apply(Fan_HeatPump, MySensors_Gateway, sensorToItemsMap.get("Fan_HeatPump"), V_PERCENTAGE)
end