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);
}
}