#include <ESP8266WiFi.h>  //Library for to allow the D1 mini to connect to the MQTT throught the WiFi
#include <MQTT.h> // allows to send data to the MQTT 
#include <Adafruit_GFX.h> // library for the OLED screen 
#include <Adafruit_SSD1306.h> // Library for the OLED screen 

const char ssid[] = "StudentResidences"; // Connect to student residence wifi 
const char pass[] = "";
WiFiClient net;
MQTTClient client; 
unsigned long lastMillis = 0;
int myVal = 0; // Declares the name of the data being recieved from the MQTT

const int sensorMin = 0;  // sensor minimum, discovered through experiment
const int sensorMax = 600000; 

//-------------
#define SCREEN_WIDTH 128  // OLED screen display width
#define SCREEN_HEIGHT 64  // OLED screen display height

#define OLED_RESET 0  // 
Adafruit_SSD1306 OLED(OLED_RESET); 

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Connects the SDA and SCL pins to the
// The SDA send that data to the screen from the computer, the SCL syncronises the data tranfer  
//---------

void connect() { 
  Serial.print("checking wifi...");    
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000); // connects the D1 mini to the wifi and tells the computer to print checking wifi in the serial monitor
  }
  Serial.print("\nconnecting...");
  while (!client.connect("ProjectFun", "", "")) { // connects the D1 mini to the wifi 
    Serial.print(".");
    delay(1000);
  }
  Serial.println("\nconnected!");
  client.subscribe("ACameron2"); // subscribes to the topic on the MQTT that the other device is publishing 
}

void messageReceived(String &topic, String &payload) { // returns the data from the MQTT to the serial print 
 
  int myVal = payload.toInt();  
  Serial.println(myVal); // prints the data in the serial monitor 


  if ((myVal >= 0) && (myVal <= 40000)) { // says that if myVal is more than zero and if its less than 40000
    display.clearDisplay(); // clears the screen of any words or designs 
    display.setTextSize(1.5); // sets the lettering size 
    display.setCursor(50, 5); // tells the screen where to place the words the screen X and Y axis 
    display.println(myVal); // prints the changin myVal on the screen 
    display.setTextSize(3); // sets text size 
    display.setCursor(30, 40); // sets position on screen
    display.println("Low"); // tells the screen what to text to print 
    display.display(); // display all of the above on the screen 
    delay(50);
  }

  if ((myVal >= 40000) && (myVal <= 70000)) { // says that if myVal is more than zero and if its less than 40000
    display.clearDisplay(); // clears the screen of any words or designs 
    display.setTextSize(1.5); // sets the lettering size 
    display.setCursor(50, 5);// tells the screen where to place the words the screen X and Y axis 
    display.println(myVal); // prints the changin myVal on the screen 
    display.setTextSize(2); // sets the lettering size 
    display.setCursor(30, 40); // tells the screen where to place the words the screen X and Y axis 
    display.println("Medium"); // tells the screen what to text to print 
    display.display(); // display all of the above on the screen 
    delay(50);
  }

  if ((myVal >= 80000) && (myVal <= 90000)) { // says that if myVal is more than zero and if its less than 40000
    display.clearDisplay(); // clears the screen of any words or designs 
    display.setTextSize(1.5); // sets the lettering size 
    display.setCursor(50, 5); // tells the screen where to place the words the screen X and Y axis 
    display.println(myVal); // prints the changin myVal on the screen 
    display.setTextSize(2); // sets the lettering size 
    display.setCursor(40, 40); // tells the screen where to place the words the screen X and Y axis 
    display.println("High"); // tells the screen what to text to print 
    display.display(); // display all of the above on the screen 
    delay(50);
  }

  if ((myVal >= 90000) && (myVal <= 200000)) { // says that if myVal is more than zero and if its less than 40000
    display.clearDisplay(); // clears the screen of any words or designs 
    display.setTextSize(1.5); // sets the lettering size 
    display.setCursor(50, 5); // tells the screen where to place the words the screen X and Y axis
    display.println(myVal); // prints the changin myVal on the screen 
    display.setTextSize(2); // sets the lettering size 
    display.setCursor(30, 40); // tells the screen where to place the words the screen X and Y axis
    display.println("Extreme"); // tells the screen what to text to print 
    display.display(); // display all of the above on the screen 
    delay(50);
  }
}




void setup() { // will run first before the other functions 
  Serial.begin(115200); // sets the baud rate 
  WiFi.begin(ssid, pass); 
  client.begin("test.mosquitto.org", net); 
  client.onMessage(messageReceived); 
  connect();

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ;
  }
  delay(2000); 
  display.clearDisplay();

  display.setTextSize(2); // same as before, set up the screen with text
  display.setTextColor(WHITE);
  display.setCursor(20, 40);
  display.println("Harmful");
  display.display();

  display.setTextSize(1.5); // same as before, set up the screen with text
  display.setTextColor(WHITE);
  display.setCursor(40, 5);
  display.println("Numbers");
  display.display();
}

void loop() {
  client.loop();
  delay(10);  // <- fixes some issues with WiFi stability

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