//Monarch Lamp Code

//The libraries we used
#include <ESP8266WiFi.h>        //WiFi connection library
#include <MQTT.h>               //MQTT library
#include <Adafruit_NeoPixel.h>  //LED library
#include <Wire.h>               //I2C library
#include <LiquidCrystal_I2C.h>  //LCD screen library
#include "StringSplitter.h"     //String splitter library for breaking down messages from the MQTT

#define PIN D4                  //D4 Standard input
#define NUMPIXELS 100           //the number of pixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

//LCD setup (adjust address if needed, usually 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);

//Setup access to Wifi within UoD
const char ssid[] = //"YourWifiNetwork";
const char pass[] = "";

//Set up a secure WiFi client object named "net"
WiFiClientSecure net;
//Set up an MQTT client object named "client"
MQTTClient client;

unsigned long lastMillis = 0;


//check every second whether the ESP8266 has connected to the WiFi Network
void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  net.setInsecure();

  Serial.print("\nconnecting...");
  while (!client.connect(//"Your Name", "Login Details", "Password")) { //Login info for MQTT
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nconnected!");

  client.subscribe(//"ExampleSubtopic"); //Subscribe from this sub-topic
}


//MQTT message handler
void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);



  int myInt = payload.toInt();
//For uploading data to LCD screen
 lcd.clear();
  lcd.setCursor(0, 0);
  //lcd.print("MQTT Msg:");
  //lcd.setCursor(0, 1);
  lcd.print(payload);

    //If butterflies are detected a 1 will be published in the sub-topic, the lamp will subscribe from it and turn on the LED to yellow.
  if (myInt == 1) {
    for (int i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(100, 100, 0));
    }
    pixels.show();
    //Otherwise the LEDs will be turned off
  } else {
    for (int i = 0; i < NUMPIXELS; i++) {
      pixels.setPixelColor(i, pixels.Color(0, 0, 0));
    }
    pixels.show();
  }
}


void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  //String splitter used to break down messages with commas in them from the MQTT.
  Serial.println(F("StringSplitter Library Test"));

  String strTest = "123,456,789,abc";
  Serial.println("Test String: " + strTest);

  StringSplitter *splitter = new StringSplitter(strTest, ',', 3);  // new StringSplitter(string_to_split, delimiter, limit)
  int itemCount = splitter->getItemCount();
  Serial.println("Item count: " + String(itemCount));

  for(int i = 0; i < itemCount; i++){
    String item = splitter->getItemAtIndex(i);
    Serial.println("Item @ index " + String(i) + ": " + String(item));
  }

  Serial.println(F("End of program."));
  Serial.end();
  

  pinMode(D4, OUTPUT);
  pixels.begin();

  //LCD init
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Connecting...");

  client.begin(//"MQTT address", net);
  client.onMessage(messageReceived);

  connect();

  lcd.clear();
  lcd.print("Connected!");
}

void loop() {

  client.loop();
  delay(10);

  if (!client.connected()) {
    connect();
  }
}