TTGO-T-HIGrow
TTGO-T-HIGrow copied to clipboard
not an issue, mqtt autodiscovery idea for ha
Auto discovery is complicated, it is not working for me, I don't understand what it tries to do, so I played with code.
save-configuration.h
new function
void mqttSetup(String identyfikator, String chipId, String uom = "x" )
{
Serial.println("*****************************************" );
const String topicStr_c = "homeassistant/sensor/" + device_name + "-" + chipId + "/" + identyfikator +"/config";
const char* topic_c = topicStr_c.c_str();
Serial.println(topic_c);
StaticJsonDocument<1536> doc_c;
JsonObject root = doc_c.to<JsonObject>();
root["name"] = device_name +" "+ identyfikator;
root["state_topic"] = "homeassistant/sensor/" + device_name + "-" + chipId + "/status";
root["value_template"] = "{{ value_json['" + identyfikator +"'] }}";
if ( uom != "x" ) {
root["unit_of_measurement"] = uom;
}
//copy of existing sending code
// // Send to mqtt
char buffer_c[1536];
serializeJson(doc_c, buffer_c);
Serial.print("Sending message to topic: ");
if (logging) {
writeFile(SPIFFS, "/error.log", "Sending message to topic: \n");
}
Serial.println(buffer_c);
// Connect to mqtt broker
Serial.print("Attempting to connect to the MQTT broker: ");
if (logging) {
writeFile(SPIFFS, "/error.log", "Attempting to connect to the MQTT broker! \n");
}
// Serial.println(broker);
mqttClient.setServer(broker, port);
if (!mqttClient.connect(broker, mqttuser, mqttpass)) {
if (logging) {
writeFile(SPIFFS, "/error.log", "MQTT connection failed! \n");
}
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.state());
// goToDeepSleepFiveMinutes();
}
if (logging) {
writeFile(SPIFFS, "/error.log", "You're connected to the MQTT broker! \n");
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
bool retained = true;
if (mqttClient.publish(topic_c, buffer_c, retained)) {
Serial.println("Message published successfully");
} else {
Serial.println("Error in Message, not published");
goToDeepSleepFiveMinutes();
}
Serial.println("*****************************************" );
Serial.println();
}
called before making json for mqtt with measure values:
Serial.println("chipId " + chipId); //existing line
mqttSetup("daysOnBattery", chipId);
mqttSetup("battvoltage", chipId, "V");
mqttSetup("bat", chipId, "%");
mqttSetup("lux", chipId, "lx");
mqttSetup("humid", chipId, "%");
mqttSetup("soil", chipId, "%");
mqttSetup("salt", chipId);
mqttSetup("temp", chipId, "°C");
const String topicStr = "homeassistant/sensor/" + device_name + "-" + chipId + "/status"; //existing line
And with this changes measurements show itself in home assistant for me:
still don't know how to make the measurements to be possible to disable, I mean make them with unique entity_id
And question, it seems like there somewhere is log stored and other files, how to access them? I thought there is web server, or it should be visible as a portable drive but I can't find. Can anybody explain how to access log?
one more thing, because it can matter I changed original JSON to this, with one level structure:
StaticJsonDocument<1536> doc;
// Set the values in the document
// Device changes according to device placement
JsonObject plant = doc.to<JsonObject>();
// JsonObject plant = root.createNestedObject("sensor"); //CHANGE!!!!!
plant[device_name] = chipId;
plant["sensorname"] = plant_name;
plant["date"] = config.date;
plant["time"] = config.time;
plant["batchargeDate"] = config.batchargeDate;
plant["daysOnBattery"] = config.daysOnBattery;
// plant["battvolt"] = config.batvolt; //nie
plant["bat"] = config.bat;
plant["battvoltage"] = config.batvoltage;
plant["sleep5Count"] = config.sleep5no;
plant["bootCount"] = config.bootno;
// plant["batcharge"] = config.batcharge; //nie
plant["lux"] = config.lux; //nie
plant["temp"] = config.temp;
plant["humid"] = config.humid;
// plant["pressure"] = config.pressure;
plant["soil"] = config.soil;
// plant["soilTemp"] = config.soilTemp; //nie
plant["salt"] = config.salt;
// plant["saltadvice"] = config.saltadvice;//nie
// plant["plantValveNo"] = plantValveNo; //nie
// plant["wifissid"] = WiFi.SSID(); //nie
plant["rel"] = config.rel;
with two level structure one have to change json templates to read measurements.
entity_id solved. Because there are lot of changes in the file, full content:
void mqttSetup(String identyfikator, String chipId, String uom = "x", String dc = "x" )
{
Serial.println("*****************************************" );
const String topicStr_c = "homeassistant/sensor/" + device_name + "-" + chipId + "/" + identyfikator +"/config";
const char* topic_c = topicStr_c.c_str();
Serial.println(topic_c);
StaticJsonDocument<1536> doc_c;
JsonObject root = doc_c.to<JsonObject>();
root["name"] = device_name +" "+ identyfikator;
if ( dc != "x" ) {
root["device_class"] = dc;
}
root["unique_id"] = chipId +"-"+ identyfikator;
root["state_topic"] = "homeassistant/sensor/" + device_name + "-" + chipId + "/status";
root["value_template"] = "{{ value_json['" + identyfikator +"'] }}";
if ( uom != "x" ) {
root["unit_of_measurement"] = uom;
}
// // Send to mqtt
char buffer_c[1536];
serializeJson(doc_c, buffer_c);
Serial.print("Sending message to topic: \n");
if (logging) {
writeFile(SPIFFS, "/error.log", "Sending message to topic: \n");
}
// Serial.println(buffer_c);
serializeJsonPretty(doc_c, Serial);
Serial.println();
// Connect to mqtt broker
Serial.print("Attempting to connect to the MQTT broker: ");
if (logging) {
writeFile(SPIFFS, "/error.log", "Attempting to connect to the MQTT broker! \n");
}
// Serial.println(broker);
mqttClient.setServer(broker, port);
if (!mqttClient.connect(broker, mqttuser, mqttpass)) {
if (logging) {
writeFile(SPIFFS, "/error.log", "MQTT connection failed! \n");
}
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.state());
// goToDeepSleepFiveMinutes();
}
if (logging) {
writeFile(SPIFFS, "/error.log", "You're connected to the MQTT broker! \n");
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
bool retained = true;
if (mqttClient.publish(topic_c, buffer_c, retained)) {
Serial.println("Message published successfully");
} else {
Serial.println("Error in Message, not published");
// goToDeepSleepFiveMinutes();
}
Serial.println("*****************************************" );
Serial.println();
}
// Allocate a JsonDocument
void saveConfiguration(const Config & config) {
// Serial.println(WiFi.macAddress());
// String stringMAC = WiFi.macAddress();
// stringMAC.replace(':', '_');
byte mac[6];
WiFi.macAddress(mac);
// String chipId = String(mac[0], HEX) + String(mac[1], HEX) + String(mac[2], HEX) + String(mac[3], HEX) + String(mac[4], HEX) + String(mac[5], HEX);
String chipId = "";
String HEXcheck = "";
for (int i = 0; i <= 5; i++) {
HEXcheck = String(mac[i], HEX);
if (HEXcheck.length() == 1) {
chipId = chipId + "0" + String(mac[i], HEX);
} else {
chipId = chipId + String(mac[i], HEX);
}
}
Serial.println("chipId " + chipId);
mqttSetup("daysOnBattery", chipId, "x", "duration");
mqttSetup("battvoltage", chipId, "V", "voltage");
mqttSetup("bat", chipId, "%", "battery");
mqttSetup("lux", chipId, "lx", "illuminance");
mqttSetup("humid", chipId, "%", "humidity");
mqttSetup("soil", chipId, "%", "humidity");
mqttSetup("salt", chipId, "x");
mqttSetup("temp", chipId, "°C", "temperature");
const String topicStr = "homeassistant/sensor/" + device_name + "-" + chipId + "/status";
const char* topic = topicStr.c_str();
Serial.println(topic);
Serial.println(ssid);
StaticJsonDocument<1536> doc;
// Set the values in the document
// Device changes according to device placement
JsonObject plant = doc.to<JsonObject>();
// JsonObject plant = root.createNestedObject("sensor");
plant[device_name] = chipId;
plant["sensorname"] = plant_name;
plant["date"] = config.date;
plant["time"] = config.time;
plant["batchargeDate"] = config.batchargeDate;
plant["daysOnBattery"] = config.daysOnBattery;
// plant["battvolt"] = config.batvolt; //nie
plant["bat"] = config.bat;
plant["battvoltage"] = config.batvoltage;
plant["sleep5Count"] = config.sleep5no;
plant["bootCount"] = config.bootno;
// plant["batcharge"] = config.batcharge; //nie
plant["lux"] = config.lux; //nie
plant["temp"] = config.temp;
plant["humid"] = config.humid;
// plant["pressure"] = config.pressure;
plant["soil"] = config.soil;
// plant["soilTemp"] = config.soilTemp; //nie
plant["salt"] = config.salt;
// plant["saltadvice"] = config.saltadvice;//nie
// plant["plantValveNo"] = plantValveNo; //nie
// plant["wifissid"] = WiFi.SSID(); //nie
plant["rel"] = config.rel;
// Send to mqtt
char buffer[1536];
serializeJson(doc, buffer);
Serial.print("Sending message to topic: \n");
if (logging) {
writeFile(SPIFFS, "/error.log", "Sending message to topic: \n");
}
// Serial.println(buffer);
serializeJsonPretty(doc, Serial);
Serial.println();
// Connect to mqtt broker
Serial.print("Attempting to connect to the MQTT broker: ");
if (logging) {
writeFile(SPIFFS, "/error.log", "Attempting to connect to the MQTT broker! \n");
}
Serial.println(broker);
mqttClient.setServer(broker, port);
if (!mqttClient.connect(broker, mqttuser, mqttpass)) {
if (logging) {
writeFile(SPIFFS, "/error.log", "MQTT connection failed! \n");
}
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.state());
goToDeepSleepFiveMinutes();
}
if (logging) {
writeFile(SPIFFS, "/error.log", "You're connected to the MQTT broker! \n");
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
bool retained = true;
if (mqttClient.publish(topic, buffer, retained)) {
Serial.println("Message published successfully");
} else {
Serial.println("Error in Message, not published");
goToDeepSleepFiveMinutes();
}
Serial.println();
}
and nice result in entities
@Maaciej, as I am setting up some new sensors for HA I will give this a try. Is it the intention to replace the content of the file "save-configuration.h" with your code as mentioned above?
Is it the intention to replace the content of the file "save-configuration.h" with your code as mentioned above?
yes this whole save-configuration file content customized (unnecessary fields of json commented out and changed json structure) for my needs, with added mqtt autodiscovery for ha. I guess you change your file to above and should see that ha will detect configured metrics.
Indeed, worked like a charm. Thanks a lot for the effort!
And question, it seems like there somewhere is log stored and other files, how to access them? I thought there is web server, or it should be visible as a portable drive but I can't find. Can anybody explain how to access log?
files in question are:
Listing directory: /
FILE: favicon.ico SIZE: 13532
FILE: index.html SIZE: 482
FILE: app.js.gz SIZE: 198509
FILE: batinfo.conf SIZE: 21
FILE: error.log SIZE: 1342
FILE: soil.conf SIZE: 9
FILE: name.conf SIZE: 9
files with extension .conf are from the program, and unknown are the first three.
I suspect now they are from the original firmware.
They cannot be removed by "remove",
but I erased all files with SPIFFS.format()
and now have only new files:
FILE: name.conf SIZE: 9
FILE: batinfo.conf SIZE: 21
FILE: soil.conf SIZE: 9
Hi, i found a few issues with the above. Mainly having a device and sensor names. So updated some, added some,.
https://github.com/elgansayer/TTGO-T-HIGrow/tree/feature/AutoDiscovery