// Required libraries (sketch -> include library -> manage libraries)
// - PubSubClient by Nick ‘O Leary
// - DHT sensor library by Adafruit
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define wifi_ssid "FistingForJesus"
#define wifi_password "NewWifiWhoDis"
#define mqtt_server "192.168.0.2"
#define client_name "masterBedroom"
#define humidity_topic "humidity"
#define ctemperature_topic "c_temperature"
#define ftemperature_topic "f_temperature"
#define motion_topic "motion"
#define DHTTYPE DHT11
#define DHTPIN 14
#define MPIN 13
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
void setup() {
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT reconnection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
//if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
if (client.connect(client_name)) {
Serial.println("connected");
if (client.connected())
Serial.println("connected");
delay(5000);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}
long lastMsg = 0;
long ft = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 0.5;
long state = LOW;
void loop() {
char tName[40];
if (!client.connected()) {
Serial.println("client not connected");
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
Serial.println("getting new reading");
float newTemp = dht.readTemperature();
float newHum = dht.readHumidity();
long newState = digitalRead(MPIN);
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("New temperature: ");
Serial.print(String(temp).c_str());
Serial.print("C, ");
Serial.print(String(dht.convertCtoF(temp)).c_str());
Serial.println("F");
sprintf(tName, "%s/%s", client_name, ctemperature_topic);
client.publish(tName, String(temp).c_str(), true);
sprintf(tName, "%s/%s", client_name, ftemperature_topic);
client.publish(tName, String(dht.convertCtoF(temp)).c_str(), true);
}
if (checkBound(newHum, hum, diff)) {
hum = newHum;
Serial.print("New humidity: ");
Serial.println(String(hum).c_str());
sprintf(tName, "%s/%s", client_name, humidity_topic);
client.publish(tName, String(hum).c_str(), true);
}
if (newState != state || ft == 0) {
if (ft == 0)
ft = 1;
state = newState;
Serial.print("Motion: ");
if (state == LOW) {
Serial.println("Not detected");
sprintf(tName, "%s/%s", client_name, motion_topic);
client.publish(tName, "FALSE");
} else {
Serial.println("Detected");
sprintf(tName, "%s/%s", client_name, motion_topic);
client.publish(tName, "TRUE");
}
}
}
}