Skip to content

Commit

Permalink
Merge pull request #272 from dsstewa/correct-fahrenheit-celsius-to-ma…
Browse files Browse the repository at this point in the history
…tch-remote

Correct Fahrenheit/Celsius conversions to match MA Remote Controller
  • Loading branch information
gysmo38 authored Aug 25, 2024
2 parents 678a1c1 + e3c00ce commit 00c49b8
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/mitsubishi2mqtt/mitsubishi2mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ ESP8266WebServer server(80); // ESP8266 web
#include <PubSubClient.h> // MQTT: PubSubClient 2.8.0
#include <DNSServer.h> // DNS for captive portal
#include <math.h> // for rounding to Fahrenheit values
#include <map>
#include <cmath> // For roundf function

#include <ArduinoOTA.h> // for OTA
#include <HeatPump.h> // SwiCago library: https://github.com/SwiCago/HeatPump
Expand Down Expand Up @@ -747,7 +749,7 @@ void handleUnit() {
if (!checkLogin()) return;

if (server.method() == HTTP_POST) {
saveUnit(server.arg("tu"), server.arg("md"), server.arg("lpw"), (String)convertLocalUnitToCelsius(server.arg("min_temp").toInt(), useFahrenheit), (String)convertLocalUnitToCelsius(server.arg("max_temp").toInt(), useFahrenheit), server.arg("temp_step"));
saveUnit(server.arg("tu"), server.arg("md"), server.arg("lpw"), (String)convertLocalUnitToCelsius(server.arg("min_temp").toFloat(), useFahrenheit), (String)convertLocalUnitToCelsius(server.arg("max_temp").toFloat(), useFahrenheit), server.arg("temp_step"));
rebootAndSendPage();
}
else {
Expand Down Expand Up @@ -1264,7 +1266,7 @@ heatpumpSettings change_states(heatpumpSettings settings) {
update = true;
}
if (server.hasArg("TEMP")) {
settings.temperature = convertLocalUnitToCelsius(server.arg("TEMP").toInt(), useFahrenheit);
settings.temperature = convertLocalUnitToCelsius(server.arg("TEMP").toFloat(), useFahrenheit);
update = true;
}
if (server.hasArg("FAN")) {
Expand Down Expand Up @@ -1772,15 +1774,51 @@ bool connectWifi() {
return true;
}

// temperature helper functions
float toFahrenheit(float fromCelcius) {
return round(1.8 * fromCelcius + 32.0);
// temperature helper these are direct mappings based on the remote
float toFahrenheit(float fromCelsius) {
// Lookup table for specific mappings
const std::map<float, int> lookupTable = {
{16.0, 61}, {16.5, 62}, {17.0, 63}, {17.5, 64}, {18.0, 65},
{18.5, 66}, {19.0, 67}, {20.0, 68}, {21.0, 69}, {21.5, 70},
{22.0, 71}, {22.5, 72}, {23.0, 73}, {23.5, 74}, {24.0, 75},
{24.5, 76}, {25.0, 77}, {25.5, 78}, {26.0, 79}, {26.5, 80},
{27.0, 81}, {27.5, 82}, {28.0, 83}, {28.5, 84}, {29.0, 85},
{29.5, 86}, {30.0, 87}, {30.5, 88}
};

// Check if the input is in the lookup table
auto it = lookupTable.find(fromCelsius);
if (it != lookupTable.end()) {
return it->second;
}

// Default conversion and rounding to nearest integer
return roundf(fromCelsius * 1.8 + 32.0);
}

// temperature helper these are direct mappings based on the remote
float toCelsius(float fromFahrenheit) {
return (fromFahrenheit - 32.0) / 1.8;
// Lookup table for specific mappings
const std::map<int, float> lookupTable = {
{61, 16.0}, {62, 16.5}, {63, 17.0}, {64, 17.5}, {65, 18.0},
{66, 18.5}, {67, 19.0}, {68, 20.0}, {69, 21.0}, {70, 21.5},
{71, 22.0}, {72, 22.5}, {73, 23.0}, {74, 23.5}, {75, 24.0},
{76, 24.5}, {77, 25.0}, {78, 25.5}, {79, 26.0}, {80, 26.5},
{81, 27.0}, {82, 27.5}, {83, 28.0}, {84, 28.5}, {85, 29.0},
{86, 29.5}, {87, 30.0}, {88, 30.5}
};

// Check if the input is in the lookup table
auto it = lookupTable.find(static_cast<int>(fromFahrenheit));
if (it != lookupTable.end()) {
return it->second;
}

// Default conversion and rounding to nearest 0.5
return roundf((fromFahrenheit - 32.0) / 1.8 * 2) / 2.0;
}


float convertCelsiusToLocalUnit(float temperature, bool isFahrenheit) {
if (isFahrenheit) {
return toFahrenheit(temperature);
Expand Down

0 comments on commit 00c49b8

Please sign in to comment.