Hi there;
I'm having a problem with an if statement that should be comparing a string of text and only if the text matches it should reset a tick timer for a specific idletimer that the string applies to. The project is running on an ESP32-dev module. All other aspects of the sketch is working as intended. I'm using serial print to see which timer is being reset so I first include serial prints of where the if statement functions as intended, then one where it doesn't.
There are two types of message, an armed message and a fired message, this armed message is functioning as intended.
This is the serial print when a "fired message" is received. It should only reset one idletimer but resets both.
I appreciate that i'm new to coding and therefore my code might be bloated, but as I've said everything BUT the Fired! message filters are working as intended. Where I think the error is is in void MessageRecieved () section. I've included the full code, so thanks in advance for anyone who is able to help, or even taken the time to read this far.
#include <Arduino.h>
#include <WiFi.h>
#include <MQTT.h>
WiFiClient wifiClient;
MQTTClient client;
#define WIFI_NETWORK "redacted"
#define WIFI_PASSWORD "redacted"
#define WIFI_TIMEOUT_MS 20000
//Sets LED data table
int ledCount = 7;
struct led {
String LedName;
int LedPinNumber;
boolean LedPinState;
String LedChannel;
boolean TrapEmailStatus;
String StatusMessage;
String NodeName;
String StatusMessageArmed;
} LED[] = {
{ "trap1A", 19, 0, "/WorkShop/Trap1Status", 0, "WS Trap 1 Fired!", "WorkShop", "WS Trap 1 is armed.", },
{ "trap2A", 5, 0, "/WorkShop/Trap2Status", 0, "WS Trap 2 Fired!", "WorkShop", "WS Trap 2 is armed.", },
{ "Trap3A", 18, 0, "/WorkShop/Trap3Status", 0, "WS Trap 3 Fired!", "WorkShop", "WS Trap 3 is armed.", },
{ "trap1B", 13, 0, "/SummerHouse/Trap1Status", 0, "SH Trap 1 Fired!", "SummerHouse", "SH Trap 1 is armed.", },
{ "trap2B", 12, 0, "/SummerHouse/Trap2Status", 0, "SH Trap 2 Fired!", "SummerHouse", "SH Trap 2 is armed.", },
{ "trap3B", 14, 0, "/SummerHouse/Trap3Status", 0, "SH Trap 3 Fired!", "SummerHouse", "SH Trap 3 is armed.", },
{ "trap4B", 16, 0, "/SummerHouse/Trap4Status", 0, "SH Trap 4 Fired!", "SummerHouse", "SH Trap 4 is armed.", },
};
//Data table MQTT subscriptions
int SubTopicsMax = 7;
struct SubTopics {
String SubTopicName;
} SUBTOPIC[] = {
{"/WorkShop/Trap1", },
{"/WorkShop/Trap2", },
{"/WorkShop/Trap3", },
{"/SummerHouse/Trap1", },
{"/SummerHouse/Trap2", },
{"/SummerHouse/Trap3", },
{"/SummerHouse/Trap4", },
};
//Data table for IDLEcounters
int idleCounters = 2;
struct idleCount {
String IdleName;
int MaxIdleCount;
int CurrentIdleCount;
boolean GreenStatus;
int GreenPinNumber;
int RedPinNumber;
} IDLECOUNT[] = {
{ "idleCountA", 10000, 0, 1, 15, 2, },
{ "idleCountB", 1800000, 0, 1, 4, 21, },
} ;
//Connects to Wifi
void connectToWifi() {
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS) {
Serial.print(".");
delay(200);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println(" Failed!");
} else {
Serial.println(" Connected");
Serial.println(WiFi.localIP());
}
}
//Connects to MQTT
void connectToMQTT() {
Serial.println("Connecting to MQTT Server...");
client.begin("192.168.0.9", wifiClient);
while (!client.connect("ESPHue", "Public", "Public")) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected to MQTT Broker!");
}
//Sets pins for LEDs from Data table
void setpinMode() {
int i = 0;
for (i; i < ledCount; i++) {
pinMode(LED[i].LedPinNumber, OUTPUT);
}
i = 0;
for (i; i < idleCounters; i++) {
pinMode(IDLECOUNT[i].GreenPinNumber, OUTPUT);
pinMode(IDLECOUNT[i].RedPinNumber, OUTPUT);
}
}
//Subscribes to MQTT topics
void subscribeToTopics() {
int i = 0;
for (i; i < SubTopicsMax; i++) {
client.subscribe(SUBTOPIC[i].SubTopicName);
Serial.print("Subscribing to channel");
Serial.println(SUBTOPIC[i].SubTopicName);
}
}
//When message is recieves on MQTT channel, strips topic and payload
void messageRecieved(String &topic, String &payload) {
// String response;
Serial.print("MQTT Message Received on topic [:");
Serial.print(topic);
Serial.print("]");
Serial.print(" MQTT Message: ");
Serial.println(payload);
Serial.print("Idletime A = ");
Serial.print(IDLECOUNT[0].CurrentIdleCount);
Serial.print(". Idletime B = ");
Serial.println(IDLECOUNT[1].CurrentIdleCount);
//Comapares payload to = trap fired messsage, if it hasn't sent an email message it will, but if it has it wont. Should reset NodeName timer
int i = 0;
for (i; i < ledCount; i++) {
if (payload.equals(LED[i].StatusMessage)) {
if (LED[i].TrapEmailStatus == 0) {
client.publish(LED[i].LedChannel, "Mousetrap has fired, please check");
LED[i].TrapEmailStatus = 1;
LED[i].LedPinState = 1;
Serial.print("Sending MQQT message to email ");
Serial.println(LED[i].LedName);
}
}
//Checks node of messsage, and should reset the idle timer of that node.
if (LED[i].NodeName == "WorkShop"); {
IDLECOUNT[0].CurrentIdleCount = 0;
Serial.println("Trap has fired on WS, Resetting WS idle");
}
if (LED[i].NodeName == "SummerHouse"); {
Serial.println("Trap has fired on SH, Resseting SH idle");
IDLECOUNT[1].CurrentIdleCount = 0;
}
}
//Compares payload to = trap armed message, if it matches, resets node timer.
i = 0;
for (i; i < ledCount; i++) {
if (payload.equals(LED[i].StatusMessageArmed)) {
LED[i].LedPinState = 0;
if (LED[i].NodeName == "WorkShop") {
IDLECOUNT[0].CurrentIdleCount = 0;
Serial.println("Armed message recieved via WS, resetting WS idle time.");
}
if (LED[i].NodeName == "SummerHouse") {
IDLECOUNT[1].CurrentIdleCount = 0;
Serial.println("Armed message recieved via SH, reseeting SH idle time.");
}
}
}
}
//Tick counts timers, if timers are overmax, then light the warning light
void idle() {
int i = 0;
for (i; i <= idleCounters; i++) {
IDLECOUNT[i].CurrentIdleCount = IDLECOUNT[i].CurrentIdleCount + 1;
}
i = 0;
for (i; i < idleCounters; i++) {
if (IDLECOUNT[i].CurrentIdleCount >= IDLECOUNT[i].MaxIdleCount) {
IDLECOUNT[i].GreenStatus = 0;
}
else {
IDLECOUNT[i].GreenStatus = 1;;
}
}
}
//Set pin status from data tables
void writepinState() {
int i = 0;
for (i; i < ledCount; i++) {
digitalWrite(LED[i].LedPinNumber, LED[i].LedPinState);
}
i = 0;
for (i; i < idleCounters; i++) {
if (IDLECOUNT[i].GreenStatus == 1) {
digitalWrite(IDLECOUNT[i].GreenPinNumber, HIGH);
digitalWrite(IDLECOUNT[i].RedPinNumber, LOW);
} else {
digitalWrite(IDLECOUNT[i].GreenPinNumber, LOW);
digitalWrite(IDLECOUNT[i].RedPinNumber, HIGH);
}
}
}
void setup() {
delay(5000);
Serial.begin(9600);
Serial.println("");
Serial.println("Booting...");
connectToWifi();
connectToMQTT();
setpinMode();
subscribeToTopics();
Serial.println("Waiting for MQTT message!");
}
void loop() {
client.loop();
delay(10);
if (!client.connected()) {
connectToMQTT();
}
idle();
client.onMessage(messageRecieved);
writepinState();
}
Thanks in advance for anyone who has taken the time to check this out. I appreciate any comments as I'm relatively new to coding.
Thanks
Josh