Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15544

Trouble with subscribing/unsubscribing Esp32MQTTClient.h

$
0
0

Hello all!

I'm working with a tool called JMRI, or JAVA Model Railroad Interface.

My goal is to create a sensor using the MQTT interface method supported in JRMI.

I can get an elemental sensor to work, publish, etc.. But here's my problem. JMRI, on initialization, doesn't know what the sensor state is - you can send a query via MQTT, but that query payload is the same payload as an "active" sensor, ie: track/sensor/1 - ACTIVE.

sensor sends when triggered: ACTIVE
sensor sends when deactivated: INACTIVE
JMRI sends a query as: ACTIVE

Where I'm getting fouled up is in this "same payload" to query as to indicate active state. If I send an active state, that causes me to RECEIVE a payload as if the switch were being queried for current state.

A rational person would say, well, this is easy - just unsubscribe for a moment, send your update, then re-subscribe, right? This would avoid a continuous loop of send active state, hear "active" to query, send state (still active) - rinse wash repeat over and over..

In using ChatGPT examples, I ended up with code I don't quite grok and does NOT have an explicit subscribe statement, but rather this little tid bit:

// This is called via "client.loop()" in void.loop() - this is function required by MQTT library!
// use it to receive and parse MQTT payloads which in JMRI are simple text statements that 
// related to the type of device.
void onConnectionEstablished()
{
  // Subscribe to "mytopic/test" and print select received MQTT payload to console
    client.subscribe(mqtt_topic, [](const String & payload) {   // create a payload string, the hard part for weenies like me..
      Serial.println(payload);                                  //print it so I know it for sure..
      if (payload == "ACTIVE"){
        if (lastPublish == false){
        Serial.println("Publishing current state becuase received ACTIVE from JMRI over MQTT");
        publishState();
        } //else {
        //  Serial.println("unsetting lastPublish now...");
        //  lastPublish = false;
        //}
      Serial.println("unsetting lastPublish now...");
          lastPublish = false;
      }
    });  
}

void publishState() {
  lastPublish = true;
  delay(100);
  client.publish(mqtt_topic, (sensorState ? "ACTIVE" : "INACTIVE"));
  delay(100);
  Serial.println(lastPublish);
}

I think my code is essentially asynchronous, so I can't easily use boolean states to control what's happening?

Would anyone have any ideas on re-structuring my mqtt subscibe method so I can turn it off and on when needed?

The gist would be:

Sensor is MOSTLY just sitting watching sensor and listening to associated topic.
When sensor is triggered, send ACTIVE as payload.
When sensor is released, send INACTIVE as payload.
When RECEIVING topic/payload of ACTIVE, report current status.

Like I mentioned, I seem to be getting screwed up having the query payload being the same as the sensor active payload.

Thanks so much for any thoughts on how to restructure my code to solve this issue..

Sincerely,

Eric

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15544

Trending Articles