Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IRMQTTServer: SHT-3x Temperature Sensor Support #1951

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/IRMQTTServer/IRMQTTServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ const uint32_t kMqttReconnectTime = 5000; // Delay(ms) between reconnect tries.
#define MQTT_CLIMATE "ac" // Sub-topic for the climate topics.
#define MQTT_CLIMATE_CMND "cmnd" // Sub-topic for the climate command topics.
#define MQTT_CLIMATE_STAT "stat" // Sub-topic for the climate stat topics.
// Sub-topic for the temperature/humidity sensor stat topics.
#define MQTT_SENSOR_STAT "sensor"
// Enable sending/receiving climate via JSON. `true` cost ~5k of program space.
#define MQTT_CLIMATE_JSON false

Expand Down Expand Up @@ -221,6 +223,25 @@ const uint16_t kMinUnknownSize = 2 * 10;
// actual a/c unit.
#define REPLAY_DECODED_AC_MESSAGE false

// ------------------------ SHT-3x Support -------------------------------------
// Set the following to true enable SHT-3x sensor support (such as the Lolin
// SHT30 Shield), connected to GPIOs 4 and 5 (D2 and D1).
// *** IMPORTANT ***
// You must also uncomment the line in the platformio.ini file to enable the
// SHT-3x library.
// #define SHT3X_SUPPORT true
crankyoldgit marked this conversation as resolved.
Show resolved Hide resolved
// Default address for SHT-3x sensor.
#define SHT3X_I2C_ADDRESS 0x44
// Requires MQTT_DISCOVERY_ENABLE to be true as well.
// If set, will send HA MQTT Discovery messages for the SHT-3x sensor.
#define SHT3X_MQTT_DISCOVERY_ENABLE true
// I2C SDA pin for SHT-3x sensor (D2).
#define SHT3X_I2C_SDA 4
// I2C SCL pin for SHT-3x sensor (D1).
#define SHT3X_I2C_SCL 5
// Check frequency for SHT-3x sensor (in seconds).
#define SHT3X_CHECK_FREQ 60

// ------------------------ Advanced Usage Only --------------------------------

// Reports the input voltage to the ESP chip. **NOT** the input voltage
Expand All @@ -238,6 +259,7 @@ const uint16_t kMinUnknownSize = 2 * 10;
#define KEY_POWER "power"
#define KEY_MODE "mode"
#define KEY_TEMP "temp"
#define KEY_HUMIDITY "humidity"
#define KEY_FANSPEED "fanspeed"
#define KEY_SWINGV "swingv"
#define KEY_SWINGH "swingh"
Expand Down Expand Up @@ -465,6 +487,9 @@ bool parseStringAndSendPronto(IRsend *irsend, const String str,
#if SEND_RAW
bool parseStringAndSendRaw(IRsend *irsend, const String str);
#endif // SEND_RAW
#if SHT3X_ENABLE
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, where/how is SHT3X_ENABLE defined? Is it from WEMOS_SHT3X.h?
e.g. the following is in the .ino file, but not here in the .h file.

#include <WEMOS_SHT3X.h>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted - that's a typo! :(
Pushing a fix shortly

void sendMQTTDiscoverySensor(const char *topic, String type);
#endif // SHT3X_ENABLE
void handleIr(void);
void handleNotFound(void);
void setup_wifi(void);
Expand Down
108 changes: 107 additions & 1 deletion examples/IRMQTTServer/IRMQTTServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ using irutils::msToString;
ADC_MODE(ADC_VCC);
#endif // REPORT_VCC

#ifdef SHT3X_SUPPORT
#include <WEMOS_SHT3X.h>
#endif

// Globals
uint8_t _sanity = 0;
#if defined(ESP8266)
Expand Down Expand Up @@ -495,9 +499,15 @@ String MqttClimateCmnd; // Sub-topic for the climate command topics.
#if MQTT_DISCOVERY_ENABLE
String MqttDiscovery;
String MqttUniqueId;
#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
String MqttDiscoverySensor;
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
#endif // MQTT_DISCOVERY_ENABLE
String MqttHAName;
String MqttClientId;
#if SHT3X_SUPPORT
String MqttSensorStat;
#endif // SHT3X_SUPPORT

// Primative lock file for gating MQTT state broadcasts.
bool lockMqttBroadcast = true;
Expand Down Expand Up @@ -537,6 +547,11 @@ bool isSerialGpioUsedByIr(void) {
return false; // Not in use as far as we can tell.
}

#if SHT3X_SUPPORT
SHT3X TemperatureSensor(SHT3X_I2C_ADDRESS);
TimerMs statSensorReadTime = TimerMs();
#endif // SHT3X_SUPPORT

// Debug messages get sent to the serial port.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
Expand Down Expand Up @@ -1290,7 +1305,13 @@ void handleAdmin(void) {
#if MQTT_DISCOVERY_ENABLE
html += htmlButton(
kUrlSendDiscovery, F("Send MQTT Discovery"),
F("Send a Climate MQTT discovery message to Home Assistant.<br><br>"));
#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
F("Send a Climate and Sensor MQTT"
#else
F("Send a Climate MQTT"
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
" discovery message to Home Assistant.<br><br>"));

#endif // MQTT_DISCOVERY_ENABLE
#if MQTT_CLEAR_ENABLE
html += htmlButton(
Expand Down Expand Up @@ -1492,6 +1513,10 @@ bool clearMqttSavedStates(const String topic_base) {
#if MQTT_DISCOVERY_ENABLE
// Clear the HA climate discovery message.
success &= mqtt_client.publish(MqttDiscovery.c_str(), "", true);
#if SHT3X_SUPPORT && MQTT_DISCOVERY_ENABLE
// Clear the HA sensor discovery message.
success &= mqtt_client.publish(MqttDiscoverySensor.c_str(), "", true);
#endif // SHT3X_SUPPORT && MQTT_DISCOVERY_ENABLE
#endif // MQTT_DISCOVERY_ENABLE
for (size_t channel = 0;
channel <= kNrOfIrTxGpios;
Expand Down Expand Up @@ -2207,12 +2232,19 @@ void init_vars(void) {
// Sub-topic for the climate stat topics.
#if MQTT_DISCOVERY_ENABLE
MqttDiscovery = "homeassistant/climate/" + String(Hostname);
#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
MqttDiscoverySensor = "homeassistant/sensor/" + String(Hostname);
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
MqttUniqueId = WiFi.macAddress();
MqttUniqueId.replace(":", "");
#endif // MQTT_DISCOVERY_ENABLE
MqttHAName = String(Hostname) + "_aircon";
// Create a unique MQTT client id.
MqttClientId = String(Hostname) + String(kChipId, HEX);
#if SHT3X_SUPPORT
// Sub-topic for the climate stat topics.
MqttSensorStat = String(MqttPrefix) + '/' + MQTT_SENSOR_STAT + '/';
#endif // SHT3X_SUPPORT
#endif // MQTT_ENABLE
}

Expand Down Expand Up @@ -2518,6 +2550,9 @@ void handleSendMqttDiscovery(void) {
htmlMenu() +
F("<p>The Home Assistant MQTT Discovery message is being sent to topic: ")
+ MqttDiscovery +
#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
F(" and ") + MqttDiscoverySensor +
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
F(". It will show up in Home Assistant in a few seconds."
"</p>"
"<h3>Warning!</h3>"
Expand All @@ -2530,6 +2565,10 @@ void handleSendMqttDiscovery(void) {
if (i > 0) channel_id = "_" + String(i);
sendMQTTDiscovery(MqttDiscovery.c_str(), channel_id);
}
#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
sendMQTTDiscoverySensor(MqttDiscoverySensor.c_str(), KEY_TEMP);
sendMQTTDiscoverySensor(MqttDiscoverySensor.c_str(), KEY_HUMIDITY);
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
}
#endif // MQTT_DISCOVERY_ENABLE

Expand Down Expand Up @@ -2734,6 +2773,9 @@ void sendMQTTDiscovery(const char *topic, String channel_id) {
"\"swing_modes\":[\"" D_STR_OFF "\",\"" D_STR_AUTO "\",\"" D_STR_HIGHEST
"\",\"" D_STR_HIGH "\",\"" D_STR_MIDDLE "\",\""
D_STR_LOW "\",\"" D_STR_LOWEST "\"],"
#if SHT3X_SUPPORT
"\"curr_temp_t\":\"") + MqttSensorStat + F(KEY_TEMP "\","
#endif // SHT3X_SUPPORT
"\"uniq_id\":\"") + MqttUniqueId + channel_id + F("\","
"\"device\":{"
"\"identifiers\":[\"") + MqttUniqueId + channel_id + F("\"],"
Expand All @@ -2752,6 +2794,47 @@ void sendMQTTDiscovery(const char *topic, String channel_id) {
mqttLog(PSTR("MQTT climate discovery FAILED to send."));
}
}

#if SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
// Send the MQTT Discovery data for the SHT3X sensor.
// type must be a String of either KEY_TEMP or KEY_HUMIDITY.
void sendMQTTDiscoverySensor(const char *topic, String type) {
String pub_topic = String(topic) + F("_") + type + F("/config");
String uom = "%";
String ha_class = type;
// XXX Update units of measure for temperature.
if (type == KEY_TEMP) {
uom = "°C";
ha_class = "temperature";
}
if (mqtt_client.publish(
pub_topic.c_str(), String(
F("{"
"\"name\":\"") + MqttHAName + "_" + type + F("\","

"\"stat_t\":\"") + MqttSensorStat + type + F("\","
"\"dev_cla\":\"") + ha_class + F("\","
"\"unit_of_meas\":\"") + uom + F("\","

"\"uniq_id\":\"") + MqttUniqueId + type + F("\","
"\"device\":{"
"\"identifiers\":[\"") + MqttUniqueId + type + F("\"],"
"\"connections\":[[\"mac\",\"") + WiFi.macAddress() + F("\"]],"
"\"manufacturer\":\"IRremoteESP8266\","
"\"model\":\"IRMQTTServer\","
"\"name\":\"") + Hostname + F("\","
"\"sw_version\":\"" _MY_VERSION_ "\""
"}"
"}")).c_str(), true)) {
mqttLog(PSTR("MQTT sensor discovery successful sent."));
hasDiscoveryBeenSent = true;
lastDiscovery.reset();
mqttSentCounter++;
} else {
mqttLog(PSTR("MQTT sensor discovery FAILED to send."));
}
}
#endif // SHT3X_SUPPORT && SHT3X_MQTT_DISCOVERY_ENABLE
#endif // MQTT_DISCOVERY_ENABLE
#endif // MQTT_ENABLE

Expand Down Expand Up @@ -2820,6 +2903,29 @@ void loop(void) {
}
// Periodically send all of the climate state via MQTT.
doBroadcast(&lastBroadcast, kBroadcastPeriodMs, climate, false, false);
#if SHT3X_SUPPORT
// Check if it's time to read the SHT3x sensor.
if (statSensorReadTime.elapsed() > SHT3X_CHECK_FREQ * 1000) {
byte result = TemperatureSensor.get();
if (result == 0) {
// Success
float temp = TemperatureSensor.cTemp;
// XXX Convert units
float humidity = TemperatureSensor.humidity;
// Publish the temp and humidity to MQTT.
String mqttTempTopic = MqttSensorStat + KEY_TEMP;
String mqttHumidityTopic = MqttSensorStat + KEY_HUMIDITY;
mqtt_client.publish(mqttTempTopic.c_str(), String(temp).c_str());
mqtt_client.publish(mqttHumidityTopic.c_str(),
String(humidity).c_str());
} else {
// Error
mqttLog((String(F("SHT3x sensor read error: ")) +
String(result)).c_str());
}
statSensorReadTime.reset();
}
#endif // SHT3X_SUPPORT
}
#endif // MQTT_ENABLE
#if IR_RX
Expand Down
2 changes: 2 additions & 0 deletions examples/IRMQTTServer/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ lib_deps_builtin =
lib_deps_external =
PubSubClient@>=2.8.0
ArduinoJson@>=6.0
# Uncomment the following to enable SHT-3x support.
# https://github.com/wemos/WEMOS_SHT3x_Arduino_Library.git

[common_esp8266]
lib_deps_external =
Expand Down