Mqtt library and pinmode error

Hi.

I have a problem I’m using the mqtt library to send the state of a pir sensor to a server.
If I set the PIR pin D0 in to the input mode pinMode(pinpir, INPUT); during the loop the mqtt client lose the connection and going to the reconnection part of the code.

If I don’t set the pin with pinMode(pinpir, INPUT); the client doesn’t lose the connection and i’m also able to read the pin state.

is this solution a valid solution?

// This #include statement was automatically added by the Spark IDE.
#include "MQTT.h"
#include <ArduinoJson.h>


void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192,168,1,20 }; //192.168.1.xx is ip of rpi mosquitto broker
MQTT client(server, 1883, callback);
byte bytebuffer[30];
long loopcounter;
//const char* ndata;

int led1 = D7;
int pinpir = D5;

bool button = false;
byte mac[6];
char macStr[18];
uint32_t lastTime;
bool pir_state;


void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);
    String t(topic);
    if (t.equals("ledstatus")){
    if (message.equals("ON")){
        button=true;
      }
    else if (message.equals("OFF")){
        button=false;
      }
    }

}

void reconnect() {
  while (Spark.connected() == false) {
    Spark.connect();
    delay(1000);
  }
}

void publishjson(const char* id, long value) {
  StaticJsonBuffer<200> jsonBuffer;

  JsonObject& root = jsonBuffer.createObject();
  root["MACADDRESS"] = macStr;
  root["sensor"] = id;
  float value2 = value;
  JsonArray& data = root.createNestedArray("data");
  data.add((value2), 6);  // 6 is the number of decimals to print
  char buffer[200];
  root.prettyPrintTo(buffer, sizeof(buffer));
  client.publish("json", buffer);
}


void pir_controller(){

  if (digitalRead(pinpir) == HIGH && !pir_state){
    publishjson("PIR", 1);
    pir_state = true;
  }
  else if (digitalRead(pinpir) == LOW && pir_state){
    publishjson("PIR", 0);
    pir_state = false;

  }

}


void setup() {
    Spark.syncTime();
    Time.zone(1);
    // connect to the server
    client.connect("clientS");
    Spark.process();
    // publish/subscribe
    if (client.isConnected()) {
        Spark.process();
        String message6 = Time.timeStr();
        char  cstr[24];
        strcpy(cstr, message6.c_str());
        Spark.process();
        client.publish("lasttime", (uint8_t *)cstr,24,true); //publish time starts up
        client.subscribe("ledstatus");

        WiFi.macAddress(mac);
        snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

        delay(100);
    }

    loopcounter=0;
    pinMode(led1, OUTPUT);
    pinMode(pinpir, INPUT);
}

void loop() {
    Spark.process();
    if (client.isConnected()) {

        client.loop();
        delay(100);
        if(button){
          digitalWrite(D7, HIGH);
          String message = "ledON";
          message.getBytes(bytebuffer, 6);
          client.publish("photon/debug",bytebuffer, 6);
          delay(100);
        }
        else{
          digitalWrite(D7, LOW);
          String message9 = "ledOFF";
          message9.getBytes(bytebuffer, 6);
          client.publish("photon/debug",bytebuffer, 6);
          delay(100);
        }

        delay(100);
        loopcounter++;
        String string2(loopcounter,(uint8_t)10);
        string2.getBytes(bytebuffer, string2.length()+1);
        client.publish("loopcounter",bytebuffer,string2.length()+1,true); //publish number of loop cycles


        pir_controller();

        delay(100);

    }
    else
        {
        Spark.process();
        reconnect();
        Spark.process();
        client.connect("clientS");
        Spark.process();
        String message7 = Time.timeStr();
        char  cstr[24];
        strcpy(cstr, message7.c_str());
        Spark.process();
        client.publish("lasttime", (uint8_t *)cstr,24,true); //publish time core reconnected
        delay(100);
        }
}

if use pinMode(pinpir, INPUT_PULLUP); it doesn’t going to reconnect.

First Spark should be replaced with Particle, since Spark will be deprecated.
Next what system version has your device?
And then this

void reconnect() {
  while (Spark.connected() == false) {
    Spark.connect();
    delay(1000);
  }
}

That’s not the best way to do that, since retriggering Particle.connect() while still connecting can cause the connection to fail.
Rather use

void reconnect() {
  if(!Particle.connected()) {
    Particle.connect();
    waitUntil(Particle.connected);
  }
}

The pin mode itself won’t make the difference in behaviour, so you’d need to find out what behaves differently in your code when you set different pin modes (or none).

BTW, I’d write your pir_controller() like this

void pir_controller() {
  int currState = digitalRead(pinpir);
  static int prevState = !currState;                  // initialize only on first run
    
  if (currState != prevState) {
    prevState = currState;                            // set the flag immediately to "prevent" race condition in async scenarios
    Serial.printlnf("%d (%8u)", currState, millis()); // for debugging to see if multiple triggers happen
    publishjson("PIR", currState);
  }
}

Thanks for the answer

i’ve the 5.2 firmware
using the particle instead of spark i’ve resolve the problem