Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Feature request: Use a single MQTT publisher topic and ArduinoJson.h #24

Open
JannickBlmndl opened this issue Aug 23, 2021 · 1 comment

Comments

@JannickBlmndl
Copy link

JannickBlmndl commented Aug 23, 2021

Hi Andrea,

Here I'm with a suggestion on how the code could be improved or functionality can be added to your code. I thought of using a single MQTT publish topic and publish all the data in json format to that. Instead of using 3 topics. That way configuring can be a bit more easy. I have already something set up. The idea is using global variables, instead of passing everything data related as a parameter to the specific functions. You would need to define those variables tho. So, for example.

/******************************** MQTT TOPIC(s) change these to your liking *******************************/
#define mqtt_topic "/peopleCounter/" // one publisher topic 

/******************************** VARIABLES FOR 'CARRYING' THE DATA *******************************/

String Passage_Event;

uint16_t Distance;

int LocalPeopleCount;

The general publish function can something like this.

void publishData(){
  StaticJsonDocument<300> doc;

  doc["passage_event"] = Passage_Event;

  JsonObject distance = doc.createNestedObject("distance");
  distance["zone"] = Zone;
  distance["measurement"] = Distance;

  doc["local_people_count"] = LocalPeopleCount;

  char buffer[300];
  serializeJson(doc, buffer);
  Serial.print(String(buffer));

  client.publish(mqtt_topic, buffer, true);
}

The data first gets formatted into a json before it gets published to the MQTT server.
Then after an entry (peoplecounter.ino line 295) you can add these lines to publish the data.

        if ((PathTrack[1] == 1)  && (PathTrack[2] == 3) && (PathTrack[3] == 2)) {
          // this is an entry
          Passage_Event = "entry";
          LocalPeopleCount++;
          publishData();
          }

You can use the same method for for the exit event and distance measuring.

And an example of the JSON data can consequently look like this.

{"passage_event":"entry",
"distance":{"zone":1,"measurement":2115}
"local_people_count":1}

Maybe I'm completely wrong and does this change only make things more difficult. Also it depends on how the users use the code of course. Anyway, it's just a suggestion. If it would help I can also share my 'version' of your code, where I did some stuff differently. (Using a cheap aliexpress time of flight sensor which has a different I2C address.)

@AhmedFaizaan
Copy link

could you share your code? i am interested.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants