Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By SwiCago
#60510 Below is code to connect to a Mitsubishi heat pump and set it to the corresponding settings wanted.
NOTE: ESP requires pullups to 5V on TX and RX It can be powered by the 5V rail of the heat pump, just add a regulator for 3.3V

I plan to further the code and add MQTT to it and remote control each heat pump in my house. Hope this helps anyone else, that is also wanting to talk to these heat pumps. When I am done I will probably setup a git for it, but for now I'll post updates here.


I have to give a big shout out to Hadley in New Zealand, who cracked the code of port cn105 and posted his python code. I just basically made a port for Arduino.
https://nicegear.co.nz/blog/hacking-a-m ... nditioner/

Working code for ESP, with captive portal test page, MQTT samples
Update: Added library, see post below for sample
Last edited by SwiCago on Wed Feb 22, 2017 11:33 am, edited 5 times in total.
User avatar
By SwiCago
#60617 Working library for ESP
https://github.com/SwiCago/HeatPump.git

Update: 20170109
- Added various getters and setters to library and also added check on values sent, where invalid values result to default.
Update: 20170110
-Added readme to git and a circuit for esp01
Update: 20170114
-Added celcuis to fahrenheit and fahrenheit to celcuis conversion. Heatpump uses celcuis. In heatmode values are rounded up to next nearest integer, all other modes rounded down.
Update: 20170116
-Can get heat pumps current state. Settings and room temperature
-Did some class cleanup
Update: 20170211
-Fork merge with Kayno, some nice additions including MQTT sample
Update: 20170222
-Major upgrades for debugging and fine tune controlling.
-Can now get set temp at 0.5C increments if HP supports it.
-Wide vane is working.

Sample usage:
Code: Select all#include <HeatPump.h>
 HeatPump hp;
void setup() {
  // put your setup code here, to run once:

  hp.connect(&Serial);    //For ESP8266
//hp.connect(&Serial1);   //Use UART1 or Arduino Micro Pro
  heatpumpSettings mySettings = {
    "ON",  /* ON/OFF */
    "FAN", /* HEAT/COOL/FAN/DRY/AUTO */
    26,    /* Between 16 and 31 */
    "4",   /* Fan speed: 1-4, AUTO, or QUIET */
    "3",   /* Air direction (vertical): 1-5, SWING, or AUTO */
    "|"    /* Air direction (horizontal): <<, <, |, >, >>, <>, or SWING */
  };
  hp.setSettings(mySettings);
  hp.update();
}

void loop() {
  // put your main code here, to run repeatedly:
  hp.sync();
}


More complex sample
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <HeatPump.h>

const char* ssid = "esp8266";

const char* html = "<html>\n<head>\n<meta name='viewport' content='width=device-width, initial-scale=2'/>\n"
                   "<style></style>\n"
                   "<body><h3>Heat Pump Demo</h3>TEMP: _ROOMTEMP_\n<form>\n<table>\n"
                   "<tr>\n<td>Power:</td>\n<td>\n_POWER_</td>\n</tr>\n"
                   "<tr>\n<td>Mode:</td>\n<td>\n_MODE_</td>\n</tr>\n"
                   "<tr>\n<td>Temp:</td>\n<td>\n_TEMP_</td>\n</tr>"
                   "<tr>\n<td>Fan:</td>\n<td>\n_FAN_</td>\n</tr>\n"
                   "<tr>\n<td>Vane:</td><td>\n_VANE_</td>\n</tr>\n"
                   "<tr>\n<td>WideVane:</td>\n<td>\n_WVANE_</td>\n</tr>\n"
                   "</table>\n<br/><input type='submit' value='Change Settings'/>\n</form><br/><br/>"
                   "<form><input type='submit' name='CONNECT' value='Re-Connect'/>\n</form>\n"
                   "</body>\n</html>\n";

const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
IPAddress netMsk(255, 255, 255, 0);
DNSServer dnsServer;
ESP8266WebServer server(80);

HeatPump hp;

void setup() {
  hp.connect(&Serial);
  hp.setSettings({ //set some default settings
    "ON",  /* ON/OFF */
    "FAN", /* HEAT/COOL/FAN/DRY/AUTO */
    26,    /* Between 16 and 31 */
    "4",   /* Fan speed: 1-4, AUTO, or QUIET */
    "3",   /* Air direction (vertical): 1-5, SWING, or AUTO */
    "|"    /* Air direction (horizontal): <<, <, |, >, >>, <>, or SWING */
  });
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(ssid);//, password);
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "*", apIP);
  server.on("/", handle_root);
  server.on("/generate_204", handle_root);
  server.onNotFound(handleNotFound);
  server.begin();
}

void loop() {
  dnsServer.processNextRequest();
  server.handleClient();
  hp.sync();
}

String encodeString(String toEncode) {
  toEncode.replace("<", "&lt;");
  toEncode.replace(">", "&gt;");
  toEncode.replace("|", "&vert;");
  return toEncode;
}

String createOptionSelector(String name, const String values[], int len, String value) {
  String str = "<select name='" + name + "'>\n";
  for (int i = 0; i < len; i++) {
    String encoded = encodeString(values[i]);
    str += "<option value='";
    str += values[i];
    str += "'";
    str += values[i] == value ? " selected" : "";
    str += ">";
    str += encoded;
    str += "</option>\n";
  }
  str += "</select>\n";
  return str;
}

void handleNotFound() {
  server.send ( 200, "text/plain", "URI Not Found" );
}

void handle_root() {
  change_states();
  String toSend = html;
  String power[2] = {"OFF", "ON"};
  toSend.replace("_POWER_", createOptionSelector("POWER", power, 2, hp.getPowerSetting()));
  String mode[5] = {"HEAT", "DRY", "COOL", "FAN", "AUTO"};
  toSend.replace("_MODE_", createOptionSelector("MODE", mode, 5, hp.getModeSetting()));
  String temp[16] = {"31", "30", "29", "28", "27", "26", "25", "24", "23", "22", "21", "20", "19", "18", "17", "16"};
  toSend.replace("_TEMP_", createOptionSelector("TEMP", temp, 16, String(hp.getTemperature())));
  String fan[6] = {"AUTO", "QUIET", "1", "2", "3", "4"};
  toSend.replace("_FAN_", createOptionSelector("FAN", fan, 6, hp.getFanSpeed()));
  String vane[7] = {"AUTO", "1", "2", "3", "4", "5", "SWING"};
  toSend.replace("_VANE_", createOptionSelector("VANE", vane, 7, hp.getVaneSetting()));
  String widevane[7] = {"<<", "<", "|", ">", ">>", "<>", "SWING"};
  toSend.replace("_WVANE_", createOptionSelector("WIDEVANE", widevane, 7, hp.getWideVaneSetting()));
  toSend.replace("_ROOMTEMP_", String(hp.getRoomTemperature()) + "°C");
  server.send(200, "text/html", toSend);
  delay(100);
}

void change_states() {
  if (server.hasArg("CONNECT")) {
    hp.connect(&Serial);
  }
  else {
    if (server.hasArg("POWER")) {
      hp.setPowerSetting(server.arg("POWER"));
    }
    if (server.hasArg("MODE")) {
      hp.setModeSetting(server.arg("MODE"));
    }
    if (server.hasArg("TEMP")) {
      hp.setTemperature(server.arg("TEMP").toInt());
    }
    if (server.hasArg("FAN")) {
      hp.setFanSpeed(server.arg("FAN"));
    }
    if (server.hasArg("VANE")) {
      hp.setVaneSetting(server.arg("VANE"));
    }
    if (server.hasArg("DIR")) {
      hp.setWideVaneSetting(server.arg("WIDEVANE"));
    }
    hp.update();
  }
}


Image
Last edited by SwiCago on Wed Feb 22, 2017 11:36 am, edited 6 times in total.
User avatar
By Albertooo
#61986 Hi swicago,

Can you help me?

My european unit pead rp100 ja dont work with this tructure header bytes.

i have wifi adaptor mac-55-ife7 and works well

How could I figure out the byte structure of my heat pump?

I made your schema with wittycloud esp8266 and my heat pump does nothing.

Thanks in advance