krdarrah / trigBoardV8_BaseFirmware

MIT License
21 stars 17 forks source link

allow MQTT messages to be published as JSON #2

Open lordmundi opened 3 years ago

lordmundi commented 3 years ago

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!

krdarrah commented 3 years ago

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

lordmundi commented 3 years ago

sent a message there