#include <UbidotsYUN.h>
#include <SimpleDHT.h>
#include <FileIO.h>


#define TOKEN "Insert Your API Token Here"
#define TEMP "Insert Your Variable Token Here"
#define HUMID "Insert Your Variable Token Here"
#define LIGHT "Insert Your Variable Token Here"


int pinDHT11 = 2;
SimpleDHT11 dht11;

int  sensorPin  =  A0;
int  lightlevel =  0;  // variable to  store  the value  coming  from  the sensor

Ubidots client(TOKEN);

void setup() {
  client.init();
  Serial.begin(9600);
  Bridge.begin();
  FileSystem.begin();
}

void loop() {
// Get Date and Time from Yun Linux Side
  String dataString;
  dataString += getTimeStamp();
  //dataString += " = ";
  
  // read with raw Temperature and Humidity data.
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    //Serial.print("Read DHT11 failed");
    return;
  }

 // Read Light Level Data from Photoresistor
 lightlevel =  analogRead(sensorPin);
 // Convert to 100 point low to high scale
 lightlevel = map(lightlevel, 0, 1023, 100, 0);
 
 // Update Ubidots
client.add(TEMP, temperature);
client.add(HUMID, humidity);
client.add(LIGHT, lightlevel);
client.sendAll(); 
}


// *********** FUNCTIONS ************
// Functon to get Time Stamp from Yun Linux Side
String getTimeStamp() {
  String result;
  Process time;
  time.begin("date");
  //time.addParameter("+%D-%T");  
  time.run(); 

  while(time.available()>0) {
    char c = time.read();
    if(c != '\n')
      result += c;
  }


  return result;
}