Here is some Working MQTT Code …
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
#include <PubSubClient.h>
#include "Adafruit_DHT.h"
//***********
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = {10,0,0,2 };MQTT client(server, 1883, callback);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
//**********
const int AlarmPinIn = D1;//LowPressure
const int AlarmPinIn1 = D2;//High Pressure
const int AlarmPinIn2 = D3;//Flame Fail
const int AlarmPinIn3 = D4;//CO2 Alarm
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int sensorValue = 0;
int outputValue = 0;
int sensorValue1 = 0;
int outputValue1 = 0;
int sensorValue2 = 0;
int outputValue2 = 0;
int sensorValue3 = 0;
int outputValue3 = 0;
int sensorValue4 = 0;
int outputValue4 = 0;
int sensorValue5 = 0;
int outputValue5 = 0;
int sensorValue6 = 0;
int outputValue6 = 0;
int sensorValue7 = 0;
int outputValue7 = 0;
int sensorValue8 = 0;
int outputValue8 = 0;
int sensorValue9 = 0;
int outputValue9 = 0;
int sensorValue10 = 0;
int outputValue10 = 0;
#define DHTPIN 0 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
//*******************
RGB.control(true);
// connect to the server
client.connect("sparkclient");
// publish/subscribe
if (client.isConnected()) {
client.publish("outTopic/message","hello world");
client.subscribe("inTopic/message");
}
//*********************
pinMode(AlarmPinIn, INPUT);
pinMode(AlarmPinIn1, INPUT);
pinMode(AlarmPinIn2, INPUT);
pinMode(AlarmPinIn3, INPUT);
//********************
}
void loop() {
char message[56];
if (client.isConnected())
client.loop();
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
float h = dht.getHumidity();
// Read temperature as Celsius
float t = dht.getTempCelcius();
// Read temperature as Farenheit
float f = dht.getTempFarenheit();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//*********************
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.getHeatIndex();
float dp = dht.getDewPoint();
float k = dht.getTempKelvin();
Serial.print("Humid: ");
Serial.print(h);
Serial.print("% - ");
Serial.print("Temp: ");
Serial.print(t);
Serial.print("*C ");
// Serial.print(f);
// Serial.print("*F ");
// Serial.print(k);
// Serial.print("*K - ");
// Serial.print("DewP: ");
// Serial.print(dp);
// Serial.print("*C - ");
// Serial.print("HeatI: ");
// Serial.print(hi);
// Serial.println("*C");
// Serial.println(Time.timeStr());
sensorValue4 =(t);
sprintf(message, "%d",sensorValue4);
// Particle.publish("Temperature", message, PRIVATE);
client.publish("mymosquitto/arduino/temp", message);
sensorValue5 =(h);
sprintf(message, "%d",sensorValue5);
// Particle.publish("Humidity", message ,PRIVATE);
client.publish("mymosquitto/arduino/humid", message);
int buttonState = digitalRead(AlarmPinIn);
sensorValue = buttonState;
sprintf(message,"%d",sensorValue);
client.publish("mymosquitto/arduino/BoilerLowPressure",message);
// Particle.publish("BoilerLowWater", message, PRIVATE);
delay(100);
int buttonState1 = digitalRead(AlarmPinIn1);
sensorValue1 = buttonState1;
sprintf(message,"%d",sensorValue1);
Particle.publish("BoilerHighPress", message, PRIVATE);
client.publish("mymosquitto/arduino/BoilerHighPress",message);
delay(100);
int buttonState2 = digitalRead(AlarmPinIn2);
sensorValue3 = buttonState2;
sprintf(message,"%d",sensorValue3);
Particle.publish("BoilerFlameFail", message, PRIVATE);
int buttonState5 = digitalRead(AlarmPinIn2);
sensorValue4 = buttonState2;
sprintf(message,"%d",sensorValue4);
client.publish("mymosquitto/arduino/BoilerFlameFail",message);
delay(100);
int buttonState3 = digitalRead(AlarmPinIn3);
sensorValue5 = buttonState3;
sprintf(message,"%d",sensorValue5);
Particle.publish("CO2Alarm", message, PRIVATE);
int buttonState6 = digitalRead(AlarmPinIn3);
sensorValue6 = buttonState3;
sprintf(message,"%d",sensorValue6);
client.publish("mymosquitto/arduino/CO2Alarm",message);
delay(100);
//******************************//
int buttonState7 = digitalRead(AlarmPinIn);
sensorValue7 = buttonState7;
sprintf(message,"%d",sensorValue7);
if (sensorValue ==LOW) {
client.publish("mymosquitto/Alarm/BoilerLowPressure", "Boiler 188 Low Water");
Particle.publish("BoilerLowWater", message, PRIVATE);
delay(10000);
}
int buttonState8 = digitalRead(AlarmPinIn1);
sensorValue8 = buttonState8;
sprintf(message,"%d",sensorValue8);
if (sensorValue == LOW) {
client.publish("mymosquitto/Alarm/BoilerHighPress", "Boiler 188 High Pressure");
delay(10000);
}
int buttonState9 = digitalRead(AlarmPinIn2);
if (sensorValue == LOW) {
client.publish("mymosquitto/Alarm/BoilerFlameFail","Boiler 188 Flame Fail");
delay(10000);
}
int buttonState10 = digitalRead(AlarmPinIn3);
sensorValue = buttonState10;
if (sensorValue == LOW) {
client.publish("mymosquitto/particle/CO2Alarm", "Boiler 188 CO2 Alarm");
delay(10000);
Particle.publish("CO2Alarm", message, PRIVATE);
}
//******************************//
Serial.println(Time.timeStr());
//******************************//
}