/* fenster-helligkeits-sensor */

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

const char* SSID = "";
const char* PSK = "";
const char* MQTT_BROKER = "";
const char* MQTT_USER = "";
const char* MQTT_PASS = "";

int pin_fenster_left = 13;
int pin_fenster_right = 12;

int pin_fenster_left_status = 2;
int pin_fenster_right_status = 2;
int last_send_helligkeits_sensor = 0;

int loop_since_last_send_helligkeitssensor = 0; //Loop run ~every secound, count to 2 minutes

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  //Turn Status LED On (while connecting)
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  //Pin mode for hall-sensor
  pinMode(pin_fenster_right, INPUT);
  pinMode(pin_fenster_left, INPUT);

  //Serial Console for Debug Infos
  Serial.begin(115200);

  //Connecting to WiFi
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(SSID);

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

  //Setup MQTT Broker Connection
  client.setServer(MQTT_BROKER, 1883);

  //Turn Status LED Off
  digitalWrite(LED_BUILTIN, HIGH);

}

//Reconnect to MQTT Broker if connection failed
void reconnect() {
  while (!client.connected()) {
    // Turn Status LED on while connecting
    digitalWrite(LED_BUILTIN, LOW);
    //Reconnecting
    Serial.print("Reconnecting...");
    if (!client.connect("ESP8266Client-Fenster-Helligkeits-Sensor", MQTT_USER, MQTT_PASS, "/sensor/fenster/status", 1, true, "0")) {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" retrying in 5 seconds");
      delay(5000);
    }
  }
  client.publish("/sensor/fenster/status", "1", true);
  digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
  //Check MQTT Connection
  if (!client.connected()) {
    reconnect();
  }
  //Loop MQTT Connection
  client.loop();

  //Check if window left status changed
  if(pin_fenster_left_status != digitalRead(pin_fenster_left)) {
    //Convert Value to char array
    char cc[6];
    String(digitalRead(pin_fenster_left)).toCharArray(cc, 6);
    //Send value to mqtt
    client.publish("/sensor/fenster-links/value", cc, true);
    //Set last value to current value
    pin_fenster_left_status = digitalRead(pin_fenster_left);
  }

  //Check if window right status changed
  if(pin_fenster_right_status != digitalRead(pin_fenster_right)) {
    //Convert Value to char array
    char cc[6];
    String(digitalRead(pin_fenster_right)).toCharArray(cc, 6);
    //Send value to mqtt
    client.publish("/sensor/fenster-rechts/value", cc, true);
    //Set last value to current value
    pin_fenster_right_status = digitalRead(pin_fenster_right);
  }


  //Get Helligkeitssensor
  int sensorValue = analogRead(A0);
  int outputValue = map(sensorValue, 0, 1024, 0, 255);
  int dif = outputValue - last_send_helligkeits_sensor;

  if(dif > 15 || dif < -15 || loop_since_last_send_helligkeitssensor > 300) {
    char cc[6];
    String(outputValue).toCharArray(cc, 6);
    client.publish("/sensor/helligkeit/1/value", cc, true);
    //Set last value to current value
    last_send_helligkeits_sensor = outputValue;
    //Set loop since count to 0, so the next regular message will be send in 5 minutes
    loop_since_last_send_helligkeitssensor = 0;
  }

  loop_since_last_send_helligkeitssensor = loop_since_last_send_helligkeitssensor + 1;
  delay(1000);

}
