#include <DHT.h>  
#include <ESP8266WiFi.h>  
#include <WiFiClient.h>  
#include <ThingSpeak.h>  
#define Ventil D5
#define DHTPIN1 D6
#define DHTPIN2 D7
#define DHTTYPE DHT11  
  
DHT dht1(DHTPIN1, DHTTYPE); 
DHT dht2(DHTPIN2, DHTTYPE); 
const char* ssid = "xxxxxxxxx";  
const char* password = "xxxxxxxxx";  
WiFiClient client;  
unsigned long myChannelNumber = xxxxxxxx;  
const char * myWriteAPIKey = "xxxxxxxxxxx";  

uint8_t temperature1, humidity1; 
uint8_t temperature2, humidity2; 

const int valueOpen = 850;
const int valueClose = 750;
bool stateOpen = false;

void setup()  
{  
  Serial.begin(9600);  

 pinMode(Ventil, OUTPUT);
    
  dht1.begin();
  dht2.begin();
  delay(10);  
  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED)  
  {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Print the IP address  
  Serial.println(WiFi.localIP());  
  ThingSpeak.begin(client);  
}  
void loop()   
{  
  static int data_state = 0;  
  int vState = 0;
  int sensorValue;
  
  temperature1 = dht1.readTemperature();  
  humidity1 = dht1.readHumidity();  
  Serial.print("Temperature Value 1 is :");  
  Serial.print(temperature1);  
  Serial.println("C");  
  Serial.print("Humidity Value 1 is :");  
  Serial.print(humidity1);  
  Serial.println("%"); 
  
  temperature2 = dht2.readTemperature();  
  humidity2 = dht2.readHumidity();  
  Serial.print("Temperature Value 2 is :");  
  Serial.print(temperature2);  
  Serial.println("C");  
  Serial.print("Humidity Value 2 is :");  
  Serial.print(humidity2);  
  Serial.println("%"); 

  sensorValue = analogRead(A0);
    Serial.println("Vlaznost " + sensorValue );
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different  
  // pieces of information in a channel. Here, we write to field 1.  
  if( (data_state % 6) == 0)  
  {  

    if (sensorValue > valueOpen){
      digitalWrite(Ventil, LOW);
      ThingSpeak.writeField(myChannelNumber, 1, 1, myWriteAPIKey); 
      Serial.println("Ventil otvoren.");
      delay(60000);
      digitalWrite(Ventil, HIGH);
    
    }
      else if (sensorValue < valueClose) {
          ThingSpeak.writeField(myChannelNumber, 1, 0, myWriteAPIKey); 
      Serial.println("Ventil zatvoren.");
      delay(5000);
      
      }

  }  
  else if( (data_state % 6) == 1) 
  {  
    sensorValue = analogRead(A0);
   
   ThingSpeak.writeField(myChannelNumber, 2, sensorValue, myWriteAPIKey);  
     
  } else if( (data_state % 6) == 2)  
  {  
   ThingSpeak.writeField(myChannelNumber, 3, temperature1, myWriteAPIKey);  
     
  }  
  else if( (data_state % 6) == 3) 
  {  
   ThingSpeak.writeField(myChannelNumber, 4, humidity1, myWriteAPIKey);  
     
  }else if( (data_state % 6) == 4)  
  {  
   ThingSpeak.writeField(myChannelNumber, 5, temperature2, myWriteAPIKey);  
     
  }  
  else if( (data_state % 6) == 5) 
  {  
   ThingSpeak.writeField(myChannelNumber, 6, humidity2, myWriteAPIKey);  
     
  } 
  data_state++;
 delay(600000); // ThingSpeak will only accept updates every 15 seconds.  
}