//bibliotecas 
#include <ESP8266WiFi.h>
#include<WiFiClientSecure.h>
#include <DHTesp.h>
#include <Wire.h>

//PINOS
#define espConectado 03
#define wifiConectado 01
#define serverAtivo 16
#define pinLed 00
#define pinBotao 12
#define pinDHT 13

//DHT 
#define DHTTYPE DHT 11 //DHT 11
DHTesp  dhtSensor;

//WIFI 
#define ssid "AAPM2"
#define password "aapnsenai"
//#define channel 6
int status =WL_IDLE_STATUS;

//Server & Client
WiFiServer server (80);
int estadoServidor ;
String mensagem ; // mensagem cliente 
WiFiClient client; // instância cliente 

//Variaveis 
int estadoLed =LOW;
int leiLed=0;
float temp;//valor temperatura
float hum;//valor umidade 
byte stateBotao =LOW;
char mensagem;

//Planilha Google & ID
int httpsPort=443;
WiFiClientSecure client;//->Create a WiFiClientSecure objet.
String GAS_ID ="AKfycbxcGpvFsrta9jlq7M-ZQuyRN1ujwSlQ2uIcI4USb62lg5v9_zaYh15FGzWFMZ90cg";



void setup() {
  //Inicializa terminal 
  Serial.begin(115200);
  delay(500);
  pinMode(pinLed, OUTPUT);//LED
  pinMode(pinBotao, INPUT);//botão
  digitalWrite(pinLed, LOW);


//inicializa dispositivos 
dhtSensor.setup(pinDHT,DHTesp::DHTTYPE);
pinMode(espConectado, OUTPUT);
pinMode(wifiConectado, OUTPUT);
pinMode(serverAtivo, OUTPUT);
digitalWrite(espConectado, LOW);
digitalWrite(wifiConectado, LOW);
digitalWrite(serverAtivo, LOW);
delay(100);
Serial.println("Configuração pronta");
digitalWrite(espConectado, HIGH);
delay(100);

//Inicializa WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.print("Conectando na rede WiFi");
Serial.println(ssid);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED)
{
  delay(100);
  Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
digitalWrite(wifiConectado,HIGH);
Serial.print("IP address:");
Serial.println(WiFi.localIP());
Serial.print("Mac Address:");
Serial.println(WiFi.macAddress());
Serial.print("Subnet:");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP:");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS :");
Serial.println(WiFi.dnsIP());
server.begin();
delay(1000);
digitalWrite(serverAtivo,HIGH);
mensagem ="";
Serial.println("Server conectado.");
delay(10);
}

void loop() {
  
  Serial.print("Rede:");
  Serial.println(ssid);
  Serial.print("IP address:");
  Serial.println(WiFi.localIP());
  //DHT
  TempAndHumidity data  = dhtSensor.getTempAndHumidity();
  temp=data.temperature;
  hum=data.humidity;
  leitLed=digitalRead(pinLed);

  //Acende Led
  if (Serial.available()>0){
    mensagem=Serial.read();
    Serial.println("Recebido:");
    Serial.println(mensagem);

    if (mensagem=='L')
    {
      stateBotao=!stateBotao;
    }
  }
  if (digitalRead(pinBotao)==LOW){
    stateBotao =!stateBotao;
    delay(400);
  }
  digitalWrite(pinLed,stateBotao);
  //Checar leitura 
  if (isnan(hum) || isnan(temp)){
    Serial.println("Failed to read from DHT sensor !");
    delay(500);
    return;
  }

  Serial.println("Temp :" + String (data.temperature,1)+"C");
  Serial.println("Humidity:"+String (data.humidity,0)+"%");
  string LED = "Led :" + String(leiLed);
  Serial.println(LED);
  Serial.println("------");
  sendData(temp,hum,leitLed);//->calls the sendData subroutine
  delay(6000);
  delay(1000);
}