Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino demo source code for LoRa "Receiver"
#6
I was too optimistic and full-bodied. Unfortunately, HA MQTT doesn't work the way I want it to. Somehow I didn't quite understand the HA topics, as some are not written to mqtt broker. Can you help me please to get my code right? I tried to re-code your topics which I see in the serial and in the mqtt broker via explorer:

Here is my code. and only the status is written, not the sensor parameters, which I do not understand
// Register new device and send discovery topics
void registerDevice(int address, const LoRaData& data) {
    if (registeredNodes[address]) return;

    Serial.printf("New device %08X to be registered in MQTT Broker:\n", address);

    char configTopic[180];
    char payload[1024];
    char stateTopic[50];
    char commandTopic[60];

    // State-& Command-Topics for switch & Sensors
   
    //sprintf(stateTopic,  "homeassistant/sensor/alrha%08X/params/state", address);
    //sprintf(commandTopic, "homeassistant/sensor/alrha%08X/params/set", address);
    sprintf(commandTopic, "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/set", address);
    sprintf(stateTopic,  "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/state", address);


    // Relay
    sprintf(configTopic, "homeassistant/switch/alr_ALR_switch/config");
    snprintf(payload, sizeof(payload),
      "{\"name\":\"relay\","
      "\"state_on\":1,"
      "\"state_off\":0,"
      "\"payload_on\":\"1\","
      "\"payload_off\":\"0\","
      "\"value_template\":\"{{ value_json.relay.on | string }}\","
      "\"state_topic\":\"%s\","
      "\"command_topic\":\"%s\","
      "\"unique_id\":\"alr_switch\","
      "\"device\":{"
        "\"identifiers\":[\"alr\"],"
        "\"name\":\"alr evb\","
        "\"model\":\"ALR\","
        "\"manufacturer\":\"KinCony\""
      "}}",
      stateTopic,
      commandTopic);

    Serial.printf("Config topic: %s\nPayload: %s\n", configTopic, payload);
    mqttClient.publish(configTopic, payload);
    Serial.println("MQTT Switch topic configured");
   
   
    // Sensors
    const char* sensorNames[] = {"inword", "outword", "analog_A1", "analog_A2", "analog_A3", "analog_A4", "DS18B20"};
    const char* valueTemplates[] = {"{{ value_json.inword }}", "{{ value_json.outword }}", "{{ value_json.analog_f[0] }}",
                                    "{{ value_json.analog_f[1] }}", "{{ value_json.analog_f[2] }}", "{{ value_json.analog_f[3] }}",
                                    "{{ value_json.wire1_data0 }}"};

    for (int i = 0; i < 7; i++) {
        delay(800);
        //sprintf(configTopic,  "homeassistant/sensor/alrha%08X/alr_ALR_param%d/config", address, i);
        sprintf(configTopic,  "homeassistant/sensor/alr_ALR_param%d/config", i);

        snprintf(payload, sizeof(payload),
        "{\"name\":\"%s\","
        "\"unit_of_measurement\":\"%s\","
        "\"value_template\":\"%s\","
        "\"state_topic\":\"%s\","
        "\"command_topic\":\"%s\","
        "\"unique_id\":\"alr_param%d\","
        "\"device\":{"
            "\"identifiers\":[\"alr\"],"
            "\"name\":\"alr evb\","
            "\"model\":\"ALR\","
            "\"manufacturer\":\"KinCony\""
            "}}",
        sensorNames[i],                          // Name des Sensors
        (i > 1 && i < 6) ? "V" : " ",            // Einheit (z. B. Volt) für 4 ADs
        valueTemplates[i],                      // Template für den Wert
        stateTopic,                              // State Topic
        commandTopic,                            // Command Topic
        i);                                      // Eindeutige ID
       
        Serial.printf("Config topic: %s\nPayload: %s\n", configTopic, payload);
        mqttClient.publish(configTopic, payload);
   
    }

    registeredNodes[address] = true;
    Serial.printf("All Device %08X Sonsor Topics registered.\n", address);
}




// update MQTT-State values
void updateMQTTState(int address, const LoRaData& data) {
    StaticJsonDocument<256> jsonDoc;
    jsonDoc["self_addr"] = data.self_addr;
    jsonDoc["inword"] = data.inword;
    jsonDoc["outword"] = data.outword;
    JsonArray analogF = jsonDoc.createNestedArray("analog_f");
    for (int i = 0; i < 4; i++) analogF.add(data.analog_f[i]);
    jsonDoc["wire1_data0"] = data.wire1_data0;
    jsonDoc["relay"]["on"] = data.relay_on;

    char buffer[512];
    serializeJson(jsonDoc, buffer, sizeof(buffer));

    char stateTopic[50];
    //sprintf(stateTopic, "homeassistant/sensor/alrha%08X/params/state", address);
    sprintf(stateTopic, "homeassistant/sensor/alrhaDCDA0C7B7BD4/params/state");
   
    // MQTT-Payload serial ausgeben
    Serial.print("MQTT State Updated:");
    Serial.println(buffer);
    Serial.println(); // Fügt einen CR ein


    // MQTT-Payload senden
    mqttClient.publish(stateTopic, buffer);
}
Reply


Messages In This Thread
RE: Arduino demo source code for LoRa "Receiver" - by maso - 12-03-2024, 08:50 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)