Client not publishing to the broker.

19 views
Skip to first unread message

shantanu...@gmail.com

unread,
Sep 21, 2016, 7:00:46 AM9/21/16
to MQTT
here is my sketch.
void loop()
{
  
  if (!mqttClient.connected()) {
    reconnect();
  }

  
  
  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++){
    int xyz = 2;
    while (xyz != 1){
    Serial.println("Started Detection");
  if(digitalRead(pirPin) == HIGH){
       //digitalWrite(ledPin, HIGH);


Serial.println("motion detected ");
 
    xyz = 1;     
      
  digitalWrite(14, HIGH);
  mqttClient.publish("Daccess/MotionSensor/status","Motion Detected");
  }

delay (1000);
  
  }
  abc = 2;
  
  }
   
  
mqttClient.loop();
}

Its a code for PIR sensor motion detection.

"abc" is initialized to 1 when a "ACTIVATE" message is received from Broker. Only then will the sensor start.

But the Publish request is not working. It is simple but the client (Sesnor) doesn't Publish messages to the broker. I know it is a problem in the placement of the "mqttClient.publish" command. Because I change the position of the command and the client perfectly publishes to the broker. But why not when in loop?

Laing, Michael

unread,
Sep 22, 2016, 10:55:12 AM9/22/16
to mq...@googlegroups.com

Hi Shantanu,

I reformatted your code.

It appears that loop() is never called due to commenting of the left bracket in the second for loop..

Cheers,

ml

--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+unsubscribe@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at https://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

shantanu...@gmail.com

unread,
Sep 23, 2016, 12:17:42 AM9/23/16
to MQTT
Thank You soo much Michael! I really appreciate your help! :)
Reply all
Reply to author
Forward
0 new messages