Andrea-Fox / peopleCounter

Code for a cheap people counter based on VL53L1X sensor and ESP32/8266
The Unlicense
96 stars 27 forks source link

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

Open JannickBlmndl opened 3 years ago

JannickBlmndl commented 3 years ago

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 commented 2 years ago

could you share your code? i am interested.