﻿const char ssid[] = "StudentResidences"; //Works within University
const char pass[] = ""; // no password for univeristy wifi so left blank
const char myBroker[] = "134.36.5.199"; //Internal MQTT broker
 
 
 
 
#include <ESP8266WiFi.h> // Include the library for WiFi connectivity
#include <MQTT.h> // Include the library for MQTT server
#include "conns.h" // include the wifi and MQTT server details in seperate tab above
 
WiFiClient net;
MQTTClient client;
 
unsigned long lastMillis = 0;
const int sensorPin = A0; // defines the sensor pin as pin AO on wemos D1
const int ledPin = D7; // defines the LED pin as pin D7 on wemos D1
const int adcMax = 1023;
const float Vcc = 5.0;
 
float readDensity() {
  digitalWrite(ledPin, LOW);
  delayMicroseconds(280);
  int adc = analogRead(sensorPin); 
  float v0 = Vcc * adc / adcMax;
  float density = 0.170 * v0 - 0.1;  // mg/m^3
  digitalWrite(ledPin, HIGH);
  return density * 1000;  // Convert to ug/m^3
}
 
void connect() { // Print to serial monitor that WiFi connection is being checked
  Serial.print("Checking WiFi..."); // Print to serial monitor that WiFi connection is being checked
  while (WiFi.status() != WL_CONNECTED) {
        Serial.print("."); // Print a dot in serial monitor to show Wifi connection attempt
        delay(1000); // Wait for 1 second before retrying
  }
  Serial.print("\nConnecting to MQTT..."); // Print in serial monitor that the board is connecting to MQTT
 
  while (!client.connect("Benjikershawdust", "", "")) { // Connect to the MQTT server using the client ID "Benjikershawdust"
        Serial.print("."); // Print a dot in serial monitor to show MQTT connection attempt
        delay(1000);  // Wait for 1 second before retrying
  }
  Serial.println("\nConnected!");  // Print a message in serial monitor once MQTT connection is successful
}
 
void setup() {
  Serial.begin(115200); // Start serial communication at a rate of 115200 baud
  WiFi.begin(ssid, pass); // Begin WiFi connection with university "studentresidencies" wifi defined in "conns.h"
  client.begin(myBroker, net); // Initialize the MQTT client using the broker address and network connection
 connect();
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  float dust = abs(readDensity());  // Get the possitive dust density value from the sensor
  Serial.print("Dust Density (ug/m^3): "); // Print the dust density value to the serial monitor
  Serial.println(dust);
 
  client.loop();
  delay(10);  // 10 millisecond delay to fix some issues with WiFi stability
 
 
  if (!client.connected()) {  // If MQTT client is not connected, run function 'connect()'
        connect();
  }
 
 
  if (millis() - lastMillis > 2000) { // Publish dust density roughly every 2 seconds
        lastMillis = millis();
 
        
        client.publish("Benjikershawdust", String(dust)); // Publish the dust value to the MQTT topic "Benjikershawdust"
        Serial.println("Published dust value to MQTT");  // Print confirmation that the value has been uploaded to the serial monitor
  }
  delay(2000); // Wait for 2 seconds before reading the sensor again and publishing the next value
}