Hi, I know this is an arduino Forum. But i really did not find any help on mqtt forums. So just thought i would give it a try here.
I am working with the arduino PubSubClient library. Simple code. But it seems to not functions.
The client is not publishing messages to the broker. I cannot understand why. There is definitely a problem with the code. Because the client is able to publish when i put it at the end of the void loop().
Here is my code. Any kind of help is appreciated. Thank you.
void loop()
{
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
int xyz = 2;
if (abc == 1){
Serial.println("Starting Sensor");
digitalWrite(5, HIGH);
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
digitalWrite(12, HIGH);
Serial.println("wait till module gains stability");
delay(5000);
//for (int x=0; x<20; x++){
while (xyz != 1){
Serial.println("Started Detection");
if(digitalRead(pirPin) == HIGH){
//digitalWrite(ledPin, HIGH);
Serial.println("motion detected ");
xyz = 1;
digitalWrite(14, HIGH);
}
delay (1000);
}
abc = 2;
if(xyz > 0){
mqttClient.publish("Daccess/MotionSensor/status","Motion Detected");
}
}
}