I am super new to coding and am trying to create a people counter that detects data from an MQTT broker. The data recieved from mqtt will be values, if they change to values <30, it it effects an internal count. How do I make an internal count which will increase if a change in sensor L is detected first and decrease when sensor R is detected first?
Additionally, stopping further values being detected by L or R`` for a few seconds to not break the count.
Very specific, but I'd use all the help I could get.
Cheers:
#include <***>
#include <MQTT.h>
const char ssid[] = "***";
const char pass[] = "";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
// Add variables for counting and time tracking
int count = 0;
unsigned long lastUpdateTimeL = 0; // Keep track of the last time MonkeysensorL was updated for comparison against
unsigned long lastUpdateTimeR = 0; // Keep track of the last time MonkeysensorR was updated
const long ignoreTime = 2000; // Creates an ignore time for sensors to stop overlap when people are walking in
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
// Print the client ID ("MonkeysensorL") to the serial monitor
Serial.print("Connecting to MQTT with client ID: ");
Serial.println("MonkeysensorL");
// Change "mySessionID" to your own identifier (name, project etc)
while (!client.connect("MonkeysensorL", "", "")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/mySensor");
}
void messageReceived(String &topic, String &payload) {
// Connects to the topic on the MQTT broker and returns the most recent data in that topic.
// Data is held in the variable "payload"
Serial.println("incoming: " + topic + " - " + payload);
int myVal = payload.toInt(); // Convert the payload to an integer (if it actually is an integer)
//
int gate = 1;
if (MonkeysensorL < 30)}
int total += 1;
if (MonkeysensorR > 30)}
int total -= 1;
if gate = 0
Serial.println(int total)
lastUpdateTimeL = millis(); // Update the last update time for MonkeysensorL
}
}
if (topic == "MonkeysensorR") {
// Ignore updates to MonkeysensorL for 2 seconds after MonkeysensorR update
if (millis() - lastUpdateTimeR > ignoreTime) {
if (myVal < 30) {
count -= 1; // Decrement the count for MonkeysensorR
Serial.println("MonkeysensorR updated, count = " + String(count));
}
lastUpdateTimeR = millis(); // Update the last update time for MonkeysensorR
}
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
client.begin("test.mosquitto.org", net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected()) {
connect();
}
}
The sketch you posted has some things that look like trying to do what you want, but is also full of mistakes that would prohibit testing it.
This
int gate = 1;
if (MonkeysensorL < 30)}
int total += 1;
if (MonkeysensorR > 30)}
int total -= 1;
if gate = 0
Serial.println(int total)
lastUpdateTimeL = millis(); // Update the last update time for MonkeysensorL
}
}
is nonsense, but the part that you might not know about is that if you just fix the braces, you still have multiple variables named total.
Learn about or review the concept of scope as it relates to variables in a C/C++ program.
Who wrote this? Can you fix it so it compiles and runs?
Please post some corrected code, which at least compiles.
What is the sample rate of the ultrasonic sensors. How often do they update the values per second?
Of course you can operate with a time to disable multiple detections during transition. But I would sugget to observe, if the values went back to certain values with a hystheresis.
Can you provide a little drawing how Sensor A and B are positioned to the passage?
Here is how to make the worst program evAr, using the same variable name in parts of the program will cause confusion to programmers and could cause unexpected results in the program.
// Variable scope shown on the Serial Monitor output.
int a = 9600; // global "a"
void setup() {
Serial.begin(a); // using the global variable value of "a" = 9600
Serial.println(a); // global "a" = 9600
int a = 2; // this new "a = 2" is locally defined here, in "setup()" Global "a" is still 9600
for (int a = 7; a < 9; a++) { // this new "a" is local only inside "for"
Serial.println(a); // this "a" is local to this "for" loop
}
Serial.println(a); // this "a" is from the definition inside "setup" with a global "a" still 9600
}
void loop() {
Serial.println(a); // this is the global "a" without an over-riding, local variable "a"
while(1); //stay here
}
Here is some altered code, It compiles but still does not work.
@xfpd How would I implement this into my code exactly? I am really unsure about a lot of this haha.
> #include <ESP8266WiFi.h>
> #include <MQTT.h>
>
> const char ssid[] = "StudentResidences";
> const char pass[] = "";
> WiFiClient net;
> MQTTClient client;
> unsigned long lastMillis = 0;
>
> // Add variables for counting and time tracking
> int count = 0;
> unsigned long lastUpdateTimeL = 0; // Keep track of the last time MonkeysensorL was updated for comparison against
> unsigned long lastUpdateTimeR = 0; // Keep track of the last time MonkeysensorR was updated
> const long ignoreTime = 2000; // Creates an ignore time for sensors to stop overlap when people are walking in
>
> void connect() {
> Serial.print("checking wifi...");
> while (WiFi.status() != WL_CONNECTED) {
> Serial.print(".");
> delay(1000);
> }
> Serial.print("\nconnecting...");
> // Print the client ID ("MonkeysensorL") to the serial monitor
> Serial.print("Connecting to MQTT with client ID: ");
> Serial.println("MonkeysensorL");
>
> // Change "mySessionID" to your own identifier (name, project etc)
> while (!client.connect("MonkeysensorL", "", "")) {
> Serial.print(".");
> delay(1000);
> }
> Serial.println("\nconnected!");
> client.subscribe("/mySensor");
> }
>
> void messageReceived(String &topic, String &payload) {
> // Connects to the topic on the MQTT broker and returns the most recent data in that topic.
> // Data is held in the variable "payload"
> Serial.println("incoming: " + topic + " - " + payload);
> int myVal = payload.toInt(); // Convert the payload to an integer (if it actually is an integer)
>
> // Determine if the message is for the left or right sensor
> if (topic == "MonkeysensorL") {
> // Ignore updates to MonkeysensorR for 2 seconds after MonkeysensorL update
> if (millis() - lastUpdateTimeL > ignoreTime) {
> if (myVal < 30) {
> count += 1; // Increment the count for MonkeysensorL
> Serial.println("MonkeysensorL updated, count = " + String(count));
> }
> lastUpdateTimeL = millis(); // Update the last update time for MonkeysensorL
> }
> }
>
> if (topic == "MonkeysensorR") {
> // Ignore updates to MonkeysensorL for 2 seconds after MonkeysensorR update
> if (millis() - lastUpdateTimeR > ignoreTime) {
> if (myVal < 30) {
> count -= 1; // Decrement the count for MonkeysensorR
> Serial.println("MonkeysensorR updated, count = " + String(count));
> }
> lastUpdateTimeR = millis(); // Update the last update time for MonkeysensorR
> }
> }
> }
>
> void setup() {
> Serial.begin(115200);
> WiFi.begin(ssid, pass);
> client.begin("test.mosquitto.org", net);
> client.onMessage(messageReceived);
> connect();
> }
>
> void loop() {
> client.loop();
> delay(10); // <- fixes some issues with WiFi stability
>
> if (!client.connected()) {
> connect();
> }
> }
Also, it won't allow me to upload a diagram, but essentially the two ultrasonic sensors are on a table next to each other about 30 cm apart, facing the same direction. Super basic.
The code I showed was a demonstration of poor variable name use (for example, "a") in multiple scope blocks, confusing anyone reading the program. The compiler uses its own substitution, so it never mistakes scope for "a" or "a" or "a" or "a". Your program exhibited the same poor naming.
You should not be using code that you do not understand. You must learn to write and code from "Hello, World!" to wherever you need to be. If you do not understand the code you are using, then you are putting the burden of your problems on others. So, scrap this project and start again with "Hello, World!" or "blink" and work your way toward your goal.