//    ,     
#include <Wire.h>
#include <ESP8266WiFi.h> //     ,    
float index_comfort=0; //   
//+++++++++++++++++++++++++++++++++++++++++++++++++++
//   ,    
//   
#include "DHT.h"
#define DHTTYPE DHT22  //   - Grove DHT22
#define DHTPIN 14     //  14  ESP (   D5)  . 
//       
float humidity_room = 0.0; //     
//  humidity_room = dht.readHumidity();
float temp_room = 0.0; //     
//  temp_room = dht.readTemperature();
//    .     
//     
DHT dht(DHTPIN, DHTTYPE);
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//     
#include <Digital_Light_TSL2561.h>
//      
float light_room=0.0;
//  light_room=TSL2561.readVisibleLux();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  C02  
int CO2; //     
int pin_CO2 = 13; //  13,   7 
//  2
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//    . 
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
float pressure =0.0; //   
//  pressure=bmp.readPressure(); 
//   !
// 1013.25 millibar = 101325  = 760  ..   ? ...
//      ,  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
//        -
//     thingspeak.com , , ..,  
#define myPeriodic 300; //        
const char* server = "184.106.153.149"; //   thingspeak.com
String apiKey ="1K******************GM"; //   
const char* MY_SSID = "P********x"; //  Wi-Fi  
const char* MY_PWD = ""; //  ,    ,  ""
int sent = 0; //   ()  .  ?  ,  ...
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//    ,  
void setup() {
  Serial.begin(9600); //    
 
  Serial.println("Go! Go! Go!");
  
  Wire.begin(); 
 
  dht.begin(); //    
 
  TSL2561.init(); //    
 
  pinMode(pin_CO2, INPUT); //    CO2  
 
  //   
 if (!bmp.begin()) {
 Serial.println("Promlem with sensor bmp180!");
  while (1) {}
  }
  //  wi-Fi
 connectWifi();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//       
void connectWifi() 
{
  Serial.print("Connecting to "+*MY_SSID);
 WiFi.begin(MY_SSID, MY_PWD);
 while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
 Serial.print(".");
  }
  
 Serial.println("");
 Serial.println("Connected");
 Serial.println(""); 
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  //   
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  //    
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm"); 
}//end connect
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//    
void send_info(float temp_in, float temp_out, float humidity_in, int CO2_in, float light_in, float pressure_all )
{  
  WiFiClient client;
  
   if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
   Serial.println("WiFi Client connected ");
   
   //        
   String postStr = apiKey; //  
   postStr += "&field1=";
   postStr += String(temp_in); //    
   postStr += "&field2=";
   postStr += String(temp_out); //   
   postStr += "&field3=";
   postStr += String(humidity_in); //    
   postStr += "&field4=";
   postStr += String(CO2_in); // 2   
   postStr += "&field5=";
   postStr += String(light_in); //    
   postStr += "&field6=";
   postStr += String(pressure_all); //   
   
   postStr += "\r\n\r\n"; //    
   
   client.print("POST /update HTTP/1.1\n");
   client.print("Host: api.thingspeak.com\n");
   client.print("Connection: close\n");
   client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
   client.print("Content-Type: application/x-www-form-urlencoded\n");
   client.print("Content-Length: ");
   client.print(postStr.length());
   client.print("\n\n");
   client.print(postStr);
   delay(1000);  
   }//end if
   sent++; //   
  
 client.stop();
 Serial.println("transmition closed ");
}//end send
//+++++++++++++++++++++++++++++++++++++++++++++++++++++
//    ,   ? 
void loop()
{
 delay(5000);
 // 
 dht.begin();
 humidity_room = dht.readHumidity();
delay(500);
//   
 temp_room = dht.readTemperature();
delay(500);
//   
light_room=TSL2561.readVisibleLux();
delay(500);
// CO2  
 while(digitalRead(pin_CO2)==HIGH){;}
float duration_h = pulseIn(pin_CO2,HIGH)/1000;
 
 CO2= int(5000*(duration_h-2)/(duration_h+(1004-duration_h)-4)); //   
 delay(500);
//  
bmp.begin();
pressure=bmp.readPressure(); 
pressure=int((pressure/101325)*760);
delay(500);
//    
if (temp_room<18) {
  index_comfort=(2*light_room/300)+(400/CO2)+humidity_room/40;
  }
if (temp_room>25) {
  index_comfort=(2*light_room/300)+(400/CO2)+humidity_room/40;
  }
  index_comfort=1+(2*(light_room/300)+(400/CO2)+humidity_room/40);
  if (index_comfort>5){
    index_comfort=5;
    }
  
//    
send_info(temp_room, index_comfort, humidity_room, CO2, light_room, pressure);
  
  //.      
  int count = myPeriodic;
  while(count--)
  delay(1000);
  // ,  .   ...
  // , 
  //
  //       i2c    ,   
  // .     . 
  //     .  .    
  //  ,   .    ,   . 
  //     ,  DHT22,    . 
  // --,     ... 
  // 15  2016 .    
}