 // WIFI Client Lib
-#include <ssl_client.h>
 #include <WiFiClientSecure.h>
 // MQTT Lib
 #include <PubSubClient.h>
 // OLED Display
-#include <OLEDDisplay.h>
-#include <OLEDDisplayFonts.h>
-#include <OLEDDisplayUi.h>
 #include <SSD1306.h>
-#include <SSD1306Wire.h>
 // include IR Lib
 #include <esp32_rmt.h>
-// Rotary Encoder Lib
-#include <ky-040.h>
+
+
+// Include Images
+#include "images.h"
+//#include "bat.h"
 
 //-------- Customise these values -----------
-const char* ssid = "uberssid";
-const char* password = "securewlanpass";
-const char* mqttServer = "mqtt.server.local";
+const char* ssid = "ubserssid";
+const char* password = "passw0rd";
+const char* mqttServer = "172.16.1.10";
 const int   mqttPort = 8883;
-const char* mqttUser = "mqttuserName";
-const char* mqttPassword = "mqttpassw0rd";
-const char* mqttDeviceName = "Panel-";
+const char* mqttUser = "iot";
+const char* mqttPassword = "passw0rd";
 byte mac[6];
+boolean bRFlag, bLFlag;
+boolean bRTrig, bLTrig;
 
 // Initialize the OLED display using Wire library
 SSD1306  display(0x3c, 5, 4);


void setup() {
  // put your setup code here, to run once:
  // Init Display
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  // Init WIFI
  initWiFi();
  // Init MQTT
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
  while (!client.connected()) {
     if (client.connect(mqttDeviceName + mac[3] + mac[4] + mac[5], mqttUser, mqttPassword )) {
     } else {
       delay(1000);
     }
  }
  client.subscribe("test");
  // Init Rotary
  encoder1.AddRotaryCounter(ROTARY_ID1, 10, -100, 100, 1, true );
  encoder1.SetRotary(ROTARY_ID1);
  encoder1.SetChanged(ROTARY_ID1); // This way we can force an update the first time through
  // Init IR
  rem1.begin(17,1);
  rem2.begin(16,1);
}

void loop() {

  client.loop();

  // ir transmit loop
  if (Serial.available() > 0)
  {
    switch (Serial.read())
    {
      case '0': Serial.println("power"); rem2.necSend(0x3000, 0xfd02); break;
      case '1': Serial.println("volume up"); rem2.necSend(0x3000, 0x7d82); break;
      case '2': Serial.println("volume down"); rem2.necSend(0x3000, 0x7c83); break;
      default: break;
    }

  }

  // Encoder loop
  if (bRTrig == true)
  {
    Serial.println("Int Rechts: ");
    bRFlag = false;
    bLFlag = false;
    bRTrig = false;
  }
  if (bLTrig == true)
  {
    Serial.println("Int Links: ");
    bRFlag = false;
    bLFlag = false;
    bLTrig = false;
  }
  // End Main Loop
}

void initWiFi() {
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  if (strcmp (WiFi.SSID().c_str(), ssid) != 0) {
    Serial.println("Connecting...");
    WiFi.begin(ssid, password);
    WiFi.macAddress(mac);
  }
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    // wait 0.5 second for re-trying
    delay(500);
  }

  Serial.print("Connected to ");
  Serial.println(ssid);
}

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Message arrived in topic: ");
  Serial.println(topic);

  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  display.clear();
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 0, String((char *)payload));
  display.display();
  Serial.println();
  Serial.println("-----------------------");

}

void int_25() {
  bRFlag = true;
  if (bLFlag == true)
  {
    bRTrig = true;
  }
}

void int_26() {
  state = !state;
  Serial.println("pressed!");
}
void int_2() {
  bLFlag = true;
  if (bRFlag == true)
  {
    bLTrig = true;
  }
}