TTGO-T-HIGrow
TTGO-T-HIGrow copied to clipboard
Proposition to add RSSI measurement
It appears 7 meters with two walls is too much for LILYGO TTGO-T-HIGrow to maintain WiFi connection and I think possibility to monitor the RSSI counter can be useful.
Example of implementation:
I added it in save-configuration.h (I would name the file rather "send-measurements.h") in the last possible place before actual sending:
plant["RSSI"] = WiFi.RSSI(); //wifiRSSI;
also with MQTT autodiscovery there is possibility to define entity in Home Assistant with proposed before function:
mqttSetup("RSSI", chipId, "dBm", "signal_strength");
and there is new version of whole file, with only one time connect to MQTT server (faster, simpler, probably saves some energy) and with variable which decide to or not print all MQTT configuration definition (print_info):
void mqttSetup(String identyfikator, String chipId, String uom = "x", String dc = "x" )
{
bool print_info = false;
const String topicStr_c = "homeassistant/sensor/" + device_name + "-" + chipId + "/" + identyfikator +"/config";
const char* topic_c = topicStr_c.c_str();
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);
if (logging) {
writeFile(SPIFFS, "/error.log", "Sending message to topic: \n");
}
// nice print of configuration mqtt message
if (print_info) {
Serial.println("*****************************************" );
Serial.println(topic_c);
Serial.print("Sending message to topic: \n");
serializeJsonPretty(doc_c, Serial);
Serial.println();
}
bool retained = true;
if (mqttClient.publish(topic_c, buffer_c, retained)) {
if (print_info) {
Serial.println("Message published successfully");
}
} else {
if (print_info) {
Serial.println("Error in Message, not published");
}
// goToDeepSleepFiveMinutes();
}
if (print_info) {
Serial.println("*****************************************\n" );
}
}
// 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);
// 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();
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");
mqttSetup("RSSI", chipId, "dBm", "signal_strength");
// //testing wifi strength
//wire should be an odd multiple ( i.e. 3/4, 5/4, 7/4 etc.) of the wavelength of the 2.4GHz signal. So it should be ~3.25cm (1.28in) for 1/4 or or ~9.75cm (3.84in) for 3/4 wavelength.
// We saw an increase of WiFi Range by about 2x times versus the internal antenna on the ESP32.
// long rssi = 0;
// for (int i = 0 ; i < 5 ;i++){
// rssi += WiFi.RSSI();
// delay(20);
// }
// float wifiRSSI = rssi/5;
// Serial.print("WiFi RSSI = ");
// Serial.println(wifiRSSI);
const String topicStr = "homeassistant/sensor/" + device_name + "-" + chipId + "/status";
const char* topic = topicStr.c_str();
// 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"] = device_name;
plant["chipId"] = 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["RSSI"] = WiFi.RSSI(); //wifiRSSI;
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: ");
Serial.println(topic);
if (logging) {
writeFile(SPIFFS, "/error.log", "Sending message to topic: \n");
}
// Serial.println(buffer);
serializeJsonPretty(doc, Serial);
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();
}