#include <WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros

#define SECRET_SSID "Wokwi-GUEST"		// replace MySSID with your WiFi network name
#define SECRET_PASS ""	// replace MyPassword with your WiFi password

#define SECRET_CH_ID_WEATHER_STATION 2626681	          	//MathWorks weather station
 

#define SECRET_READ_APIKEY_STATUS "DYLCWR68EPSCE1PM"	//API Key for Test channel



char ssid[] = SECRET_SSID;   // your network SSID (name) 
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

// Weather station channel details
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
unsigned int temperatureFieldNumber = 4;

// Counting channel details
//unsigned long counterChannelNumber = SECRET_CH_ID_COUNTER;
const char * myStatusReadAPIKey = SECRET_READ_APIKEY_STATUS;
unsigned int statusFieldNumber = 1; 

int RedLED = 2;
int GreenLED = 4;
int BlueLED = 16;

void setup() {
  Serial.begin(115200);  //Initialize serial
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(BlueLED, OUTPUT);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }
  
  WiFi.mode(WIFI_STA);   
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {

  int statusCode = 0;
  
  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected");
  }

  // // Read in field 4 of the public channel recording the temperature
  // float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber);  

  // // Check the status of the read operation to see if it was successful
  // statusCode = ThingSpeak.getLastReadStatus();
  // if(statusCode == 200){
  //   Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F");
  // }
  // else{
  //   Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); 
  // }
  
  // delay(15000); // No need to read the temperature too often.

  // Read in field 1 of the private channel which is a counter  
  String humidityStatus = ThingSpeak.readStringField(weatherStationChannelNumber, statusFieldNumber, myStatusReadAPIKey);  

   // Check the status of the read operation to see if it was successful
  statusCode = ThingSpeak.getLastReadStatus();
  if(statusCode == 200){
    Serial.println("Greenhouse state: " + String(humidityStatus));
    if (humidityStatus == "Dry"){
      digitalWrite(RedLED, HIGH);
      digitalWrite(GreenLED, LOW);
      digitalWrite(BlueLED, LOW);
    }
    else if (humidityStatus == "Humid"){
      digitalWrite(RedLED, LOW);
      digitalWrite(GreenLED, LOW);
      digitalWrite(BlueLED, HIGH);
    }
    else {
      digitalWrite(RedLED, LOW);
      digitalWrite(GreenLED, HIGH);
      digitalWrite(BlueLED, LOW);

    }
      
  }
  else{
    Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); 
  }
  
  delay(15000); // No need to read the counter too often.
  
}
$abcdeabcde151015202530fghijfghij