trigBoardV8_BaseFirmware icon indicating copy to clipboard operation
trigBoardV8_BaseFirmware copied to clipboard

allow MQTT messages to be published as JSON

Open lordmundi opened this issue 3 years ago • 2 comments

This is a request to allow the MQTT messages to be published as JSON instead of just a text string. Parsing the options out of the string becomes a bit of a headache sometimes in things like homeassistant. If we had another flag in the configurator to enable MQTT JSON output, then I think the mqtt function could just become (modified code indented here):

void mqtt() {
  if (strcmp(config.mqttEnable, "t") == 0) {//only if enabled
    
    Serial.println("sending mqtt");

    WiFiClient espClient;
    PubSubClient client(espClient);
    client.setServer(config.mqttServer, config.mqttPort);
    client.setCallback(callback);
    unsigned long mqttStart = millis();
    while (!client.connected()) {
      if (millis() - mqttStart > config.wifiTimeout) {
        return;
      }
      Serial.print("Attempting MQTT connection...");
      // Attempt to connect

      if (strcmp(config.mqttSecureEnable, "t") == 0) {
        client.connect(config.trigName, config.mqttUser, config.mqttPW);
      }
      else {
        client.connect(config.trigName);
      }

      if (client.connected()) {
        Serial.println("connected");
        // Once connected, publish an announcement...
        char mqttMessage[100];
		
		// If MQTT JSON option is enabled, generate a JSON str instead of 
		// using pushMessage.
		char sensorString[7];
		char lowBattString[6];
		char timerWakeString[6];
		
		if (strcmp(config.mqttJSONEnable, "t") == 0) {
			if ( contactLatchClosed ) {
				sprintf(sensorString, "Closed");
			} else {
				sprintf(sensorString, "Open");
			}
			if (lowBattery) {
				sprintf(lowBattString, "true"); 
			} else {
				sprintf(lowBattString, "false"); 
			}
			if (timerWake) {
				sprintf(timerWakeString, "true"); 
			} else {
				sprintf(timerWakeString, "false"); 
			}     
			sprintf(mqttMessage, "{\"sensor\": \"%s\", \"batt_v\": %s, \"low_batt\": %s, \"timer_wake\": %s}", 
								  sensorString, 
								  batCharString, 
								  lowBattString,
								  timerWakeString);
		} else {
			sprintf(mqttMessage, "%s %s", config.trigName, pushMessage);
		}

	Serial.println(client.publish(config.mqttTopic, mqttMessage));
        delay(20);
        // ... and resubscribe
        //client.subscribe("inTopic");
        return;
      } else {
        Serial.print("failed, rc=");
        Serial.println(client.state());
        
        delay(100);
      }
    }
  }
}

Not sure if the sensor using that contactLatchClosed is enough here - i see you have some more logic depending on whether it is a wake or not and the fast support stuff... hoping you might know the right thing to do here to just report the latest value of the sensor as that is all most people will care about I think.

I would try testing this out but I don't have a compile environment set up for the trigboard at the moment. Thanks for considering!

lordmundi avatar Mar 10 '21 03:03 lordmundi

hey are you in the discord? I like this idea, but would be good to chat more about it - send me a message for the link: https://www.kdcircuits.com/#contact

krdarrah avatar Mar 10 '21 06:03 krdarrah

sent a message there

lordmundi avatar Mar 10 '21 16:03 lordmundi