#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* SSID = "smartroom";
const char* PSK = "smarthome";
const char* MQTT_BROKER = "192.168.252.1";
WiFiClient espClient;
PubSubClient client(espClient);

// defines pins numbers
int ledPin = D1;  // LED on Pin 1 of ESP
int pirPin1 = D0; // Input for HC-SR501  #1
int pirPin2 = D2; // Input for HC-SR501  #2
int pirValueNeu1; // Place to read PIR Value
int pirValueAlt1 = 0; //Place to store read PIR Value
int pirAn1;
int pirAus1;
int pirValueNeu2; // Place to read PIR Value
int pirValueAlt2 = 0; //Place to store read PIR Value
int pirAn2;
int pirAus2;

char msg[50];


void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(MQTT_BROKER, 1883);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin1, INPUT);
  pinMode(pirPin2, INPUT);
  digitalWrite(ledPin, LOW);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PSK);

  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() {
  while (!client.connected()) {
    Serial.print("Reconnecting...");
    if (!client.connect("ESP8266Client")) {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" retrying in 5 seconds");
      delay(5000);
    }
  }
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  pirValueNeu1 = digitalRead(pirPin1);
  pirValueNeu2 = digitalRead(pirPin2);
  digitalWrite(ledPin, (pirValueNeu1 | pirValueNeu2));
  pirAn1 = ((pirValueNeu1 == 1) && (pirValueAlt1 == 0));
  pirAn2 = ((pirValueNeu2 == 1) && (pirValueAlt2 == 0));
  pirAus1 = ((pirValueNeu1 == 0) && (pirValueAlt1 == 1));
  pirAus2 = ((pirValueNeu2 == 0) && (pirValueAlt2 == 1));

  if (pirAn1) {
    Serial.println("Steigende Flanke von PIR1");
    snprintf (msg, 50, "%c%c", 49, 48); //Codierung PIR1 steigende Flanke: 10
    Serial.print("Publish Motion: ");
    Serial.println(msg);
    client.publish("/gso/bb/104/Pir/1", msg);
  }
  if (pirAn2) {
    Serial.println("Steigende Flanke von PIR2");
    snprintf (msg, 50, "%c%c", 49, 49); //Codierung PIR2 steigende Flanke: 11
    Serial.print("Publish Motion: ");
    Serial.println(msg);
    client.publish("/gso/bb/104/Pir/2", msg);
  }
  if (pirAus1) {
    Serial.println("Fallende Flanke von PIR1");
    snprintf (msg, 50, "%c%c", 48, 48); //Codierung PIR1 fallende Flanke: 00
    Serial.print("Publish Motion: ");
    Serial.println(msg);
    client.publish("/gso/bb/104/Pir/1", msg);
  }
  if (pirAus2) {
    Serial.println("Fallende Flanke von PIR2");
    snprintf (msg, 50, "%c%c", 48, 49); //Codierung PIR2 fallende Flanke: 01
    Serial.print("Publish Motion: ");
    Serial.println(msg);
    client.publish("/gso/bb/104/Pir/2", msg);
  }

  pirValueAlt1 = pirValueNeu1;
  pirValueAlt2 = pirValueNeu2;
}
