I have a variable in arduino IDE called "count" and it updates every time it passes through two ir sensors basically a bidirectional visitor counter and I want that data to send it to a dashboard in arduino cloud that updates the value of a gauge widget to display the count of visitors in a room.
Arduino IDE Code:
/* ========= PINS ========= */
#define IR_IN 22
#define IR_OUT 9
#define PIR_PIN 4
#define LIGHT_PIN 2
#define AC_PIN 3
enum State { IDLE, IN_FIRST, OUT_FIRST };
State state = IDLE;
int count = 0;
unsigned long stateTime = 0;
const unsigned long TIMEOUT = 800;
/* ===== MANUAL OVERRIDE ===== */
bool manualOverride = false; // true = MANUAL, false = AUTO
bool manualLightState = LOW;
bool manualACState = LOW;
int lastSentCount = -1; // For sending count only if changed
bool lastLight = LOW;
bool lastAC = LOW;
bool lastMode = false;
void setup() {
pinMode(IR_IN, INPUT_PULLUP);
pinMode(IR_OUT, INPUT_PULLUP);
pinMode(PIR_PIN, INPUT);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(AC_PIN, OUTPUT);
Serial.begin(9600); // Debug
Serial1.begin(9600); // ESP32 communication
}
/* ===== SEND VISITOR COUNT ===== */
void sendCount() {
if (count != lastSentCount) {
Serial1.print("COUNT:");
Serial1.println(count);
lastSentCount = count;
}
}
/* ===== SEND STATUS ===== */
void sendStatus() {
bool currLight = digitalRead(LIGHT_PIN);
bool currAC = digitalRead(AC_PIN);
bool currMode = !manualOverride;
if (currLight != lastLight || currAC != lastAC || currMode != lastMode) {
Serial1.print("STATUS:");
Serial1.print(currLight);
Serial1.print(",");
Serial1.print(currAC);
Serial1.print(",");
Serial1.println(currMode); // 1=AUTO, 0=MANUAL
lastLight = currLight;
lastAC = currAC;
lastMode = currMode;
Serial.println("SENT");
}
}
/* ===== IR VISITOR LOGIC ===== */
void handleIR() {
bool inBlocked = digitalRead(IR_IN) == LOW;
bool outBlocked = digitalRead(IR_OUT) == LOW;
switch (state) {
case IDLE:
if (inBlocked && !outBlocked) { state = IN_FIRST; stateTime = millis(); }
else if (outBlocked && !inBlocked) { state = OUT_FIRST; stateTime = millis(); }
break;
case IN_FIRST:
if (outBlocked && !inBlocked) {
count++;
state = IDLE;
sendCount();
}
else if (millis() - stateTime > TIMEOUT) state = IDLE;
break;
case OUT_FIRST:
if (inBlocked && !outBlocked) {
if (count > 0) count--;
state = IDLE;
sendCount();
}
else if (millis() - stateTime > TIMEOUT) state = IDLE;
break;
}
}
void loop() {
handleIR();
Serial.println(count);
/* ===== RECEIVE COMMANDS FROM ESP32 ===== */
if (Serial1.available()) {
String cmd = Serial1.readStringUntil('\n');
cmd.trim();
if (cmd == "AUTO_MODE") {
manualOverride = false;
}
else if (cmd == "MANUAL_MODE") {
manualOverride = true;
}
else if (cmd == "LIGHT_ON") {
manualOverride = true;
manualLightState = HIGH;
}
else if (cmd == "LIGHT_OFF") {
manualOverride = true;
manualLightState = LOW;
}
else if (cmd == "AC_ON") {
manualOverride = true;
manualACState = HIGH;
}
else if (cmd == "AC_OFF") {
manualOverride = true;
manualACState = LOW;
}
else if (cmd.startsWith("SET:")) {
count = cmd.substring(4).toInt();
manualOverride = false; // switch to AUTO after manual set
sendCount();
}
}
/* ===== OUTPUT CONTROL ===== */
if (manualOverride) {
digitalWrite(LIGHT_PIN, manualLightState);
digitalWrite(AC_PIN, manualACState);
}
else {
if (count > 0) {
digitalWrite(LIGHT_PIN, HIGH);
digitalWrite(AC_PIN, HIGH);
}
else {
digitalWrite(LIGHT_PIN, LOW);
analogWrite(AC_PIN, 50); // low AC power when no visitors
}
}
// Send updated status if any changes
sendStatus();
}
Arduino Cloud code:
#include "thingProperties.h"
// Mega connection pins
#define MEGA_RX 16
#define MEGA_TX 17
unsigned long lockUntil = 0; // Prevent overwrite during manual set
void setup() {
Serial.begin(9600); // Debug
Serial2.begin(9600, SERIAL_8N1, MEGA_RX, MEGA_TX); // Mega hardware serial
delay(1500); // Allow Serial to stabilize
// Cloud setup
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
// Keep Cloud updated
ArduinoCloud.update();
// Read incoming data from Mega
if (millis() > lockUntil && Serial2.available()) {
String msg = Serial2.readStringUntil('\n');
msg.trim();
Serial.println(msg);
// === Visitor Count ===
if (msg.startsWith("COUNT:")) {
int receivedCount = msg.substring(6).toInt();
if (visitorCount != receivedCount) {
visitorCount = receivedCount; // This triggers Gauge update
}
}
// === Status of lights/AC and mode ===
else if (msg.startsWith("STATUS:")) {
int c1 = msg.indexOf(',');
int c2 = msg.indexOf(',', c1 + 1);
light = msg.substring(7, c1).toInt();
aircon = msg.substring(c1 + 1, c2).toInt();
autoMode = msg.substring(c2 + 1).toInt(); // 1 = AUTO, 0 = MANUAL
}
}
}
/* ===== CLOUD CALLBACKS ===== */
// Stepper to manually set visitor count
void onVisitorSetChange() {
Serial2.print("SET:");
Serial2.println(visitorSet);
lockUntil = millis() + 2000; // prevent instant overwrite from Mega
}
// AUTO / MANUAL toggle
void onAutoModeChange() {
Serial2.println(autoMode ? "AUTO_MODE" : "MANUAL_MODE");
}
// Manual light control (only works in MANUAL mode)
void onLightChange() {
if (!autoMode)
Serial2.println(light ? "LIGHT_ON" : "LIGHT_OFF");
}
// Manual AC control (only works in MANUAL mode)
void onAirconChange() {
if (!autoMode)
Serial2.print
ln(aircon ? "AC_ON" : "AC_OFF");
}
thingsProperties.h:
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char DEVICE_LOGIN_NAME[] = "d1447613-5ba5-4a3e-99bf-d98e4975e5d2";
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[] = SECRET_DEVICE_KEY; // Secret device password
void onVisitorSetChange();
void onAirconChange();
void onAutoModeChange();
void onLightChange();
int visitorCount;
int visitorSet;
bool aircon;
bool autoMode;
bool light;
void initProperties(){
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(visitorCount, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(visitorSet, READWRITE, ON_CHANGE, onVisitorSetChange);
ArduinoCloud.addProperty(aircon, READWRITE, ON_CHANGE, onAirconChange);
ArduinoCloud.addProperty(autoMode, READWRITE, ON_CHANGE, onAutoModeChange);
ArduinoCloud.addProperty(light, READWRITE, ON_CHANGE, onLightChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);