// This #include statement was automatically added by the Particle IDE.
// This code is designed for Particle Photon to work with the SHT30 and MQ9 I2C Mini Module available from   dcubestore.com
// This code is written for Fire and Carbon Monoxide Alert System
// Ubidots using Particle Webhooks.

/****************************************
   Include Libraries
 ****************************************/
#include <Ubidots.h>
#include <application.h>
#include <spark_wiring_i2c.h>

/****************************************
   Define Instances and Constants
 ****************************************/

#define interval 1000
#define Addr 0x44
#define Addr2 0x50
double cTemp = 0.0, fTemp = 0.0, humidity = 0.0;
int raw_adc_MQ9 = 0;
double ppm_MQ9 = 0.0;
int relay = D7;
int i = 0;

const char* WEBHOOK_NAME = "Ubidots";

Ubidots ubidots("webhook", UBI_PARTICLE);



/****************************************
   Main Functions
 ****************************************/


void setup() {
  Serial.begin(115200);
  ubidots.setDebug(true);  // Uncomment this line for printing debug messages

  // Set variable for SHT30
  Particle.variable("i2cdevice", "SHT30");
  Particle.variable("cTemp", cTemp);
  Particle.variable("humidity", humidity);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);
  delay(300);



  // Set variable for MQ9
  Particle.variable("i2cdevice", "ADC121C_MQ9");
  Particle.variable("PPM", ppm_MQ9);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);
  delay(300);
  pinMode(relay, OUTPUT);

}





void loop() {

  // SHT30
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, crc, hum msb, hum lsb, crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
  delay(500);

  // Convert the data
  cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
  fTemp = (cTemp * 1.8) + 32;
  humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);

  // Output data to dashboard
  Particle.publish("Temperature in Celsius: ",  String(cTemp));
  Particle.publish("Temperature in Fahrenheit: ", String(fTemp));
  Particle.publish("Relative Humidity: ",  String(humidity));

  float value2 = humidity ;
  float value1 = cTemp ;
  if (cTemp >= 60)
  {
    digitalWrite(relay, HIGH);
    delay(interval);
    digitalWrite(relay, HIGH);
    delay(interval);
  } else {
    digitalWrite(relay, LOW);
    delay(interval);
    digitalWrite(relay, LOW);
    delay(interval);
  }
  delay(1000);


  // MQ9

  unsigned int data2[2];

  // Start I2C transmission
  Wire.beginTransmission(Addr2);
  // Select data register
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();

  // Request 2 bytes of data
  Wire.requestFrom(Addr2, 2);

  // Read 2 bytes of data
  // raw_adc msb, raw_adc lsb
  if (Wire.available() == 2)
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
  }
  delay(300);

  // Convert the data to 12-bits
  raw_adc_MQ9 = ((data2[0] & 0x0F) * 256) + data2[1];
  float sensor_voltage =  raw_adc_MQ9 / 1024.0 * 5.0;
  float RS_gas = (5.0 - sensor_voltage) / sensor_voltage;
  float ratio = RS_gas / 3.78;

  // Output data to dashboard
  Particle.publish("Carbon Monoxide Concentration : ", String(ratio));
  float value3 = ratio;
  delay(1000);


  ubidots.add("Variable_Name_One", value1);  // Change for your variable name
  ubidots.add("Variable_Name_Two", value2);
  ubidots.add("Variable_Name_Three", value3);
  bool bufferSent = false;
  bufferSent = ubidots.send(WEBHOOK_NAME, PUBLIC);  // Will use particle webhooks to send data

  if (bufferSent) {
    // Do something if values were sent properly
    Serial.println("Values sent by the device");
  }

  delay(5000);

}
