// Get ESP8266 going with Arduino IDE

// Adafruit ESP8266 Huzzah https://learn.adafruit.com/adafruit-feather-huzzah-esp8266
// Board: ESP8266: Adafruit Feather Huzzah ESP8266

// ESP8266
// Board: ESP8266: NodeMCU 1.0 (ESP-12E)

// ESP32
// Board: ESP32 Dev Module

// from https://gist.github.com/balloob/1176b6d87c2816bd07919ce6e29a19e9
// - https://github.com/esp8266/Arduino#installing-with-boards-manager
// Required libraries (sketch -> include library -> manage libraries)
// - PubSubClient by Nick ‘O Leary
// - DHT sensor library by Adafruit
// Changed from custom Weather station to Domoticz
// changes based on https://www.domoticz.com/forum/viewtopic.php?f=12&t=27540&p=213547&hilit=MQTT#p213547

// THis sketch has options for Huzzah, ESP8266, and ESP32
// It is currently configured for Huzzah

// uncomment for ESP32
//#include "WiFi.h"

//uncomment for ESP8266 or Huzzah
#include <ESP8266WiFi.h>

#include "PubSubClient.h"
#include <DHT.h>

#define wifi_ssid "AR300M"
#define wifi_password "fuzzysoxfuzzy"

#define mqtt_server "192.168.8.112"  //this is the weather RPi
#define mqtt_user ""
#define mqtt_password ""

#define DHTTYPE DHT22

//Uncomment For ESP8266 Huzzah
#define DHTPIN  14 // 14
#define LEDPIN 0 // onboard LED

//Uncomment For ESP8266
//#define DHTPIN  13 // D7
//#define LEDPIN 5 // D1

//Uncommnt For ESP32
//#define DHTPIN 21 // GPIO21 / P21
//#define LEDPIN 22 // GPIO22 / P22

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266

char msg[128];  // the msg to Domoticz

void setup() {
  pinMode (LEDPIN, OUTPUT);
  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 connection...");
    // 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)) {
      Serial.println("connected");
    } 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 fiveMinutes = -300000;
long ledToggle = 0;
float temp = 0.0;
float ftemp = 0.0;
float hum = 0.0;
float diff = 0.2;

void loop() {

  long now = millis();
  if (now - lastMsg > 500) {
    lastMsg = now;
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
    // blink LED
    if (ledToggle == 0) {
      digitalWrite(LEDPIN, HIGH);
      ledToggle = 1;
    } else {
      digitalWrite(LEDPIN, LOW);
      ledToggle = 0;
    }

    float newTemp = dht.readTemperature();
    float newHum = dht.readHumidity();
    //Serial.print(temperature_topic);
    //Serial.print(":");
    //ftemp = newTemp * 1.8 + 32;
    //Serial.print(ftemp);
    //Serial.println(" F");

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      Serial.print("New temperature:");
      Serial.println(String(temp).c_str());
      ftemp = temp * 1.8 + 32;
      Serial.print(" in F:");
      Serial.println(ftemp);
    }

    if (checkBound(newHum, hum, diff)) {
      hum = newHum;
      Serial.print("New humidity:");
      Serial.println(String(hum).c_str());
    }

// for MQTT to Domoticz
//    client.publish("domoticz/in", msg, true); // Final message that goes to Domoticz
  
    // send temp every 5 minutes
    if (now - fiveMinutes > 300000) {
      fiveMinutes = now;
      
// change value after idx to match your configuration
      snprintf(msg, 128, " {\"idx\":1,\"nvalue\":0,\"svalue\":\"%f;%f;0\"}", temp, hum);  
      
        // This is where the Domoticz mqtt magic is happening
      Serial.print("Publish message: ");
      Serial.println(msg); // Just for logging purpose
      client.publish("domoticz/in", msg, true); // Final message that goes to Domoticz
    }

  }
}
