 const char* MY_SSID = "<YOUR-SSID>"; 
 const char* MY_PWD = "<YOUR-PASSWORD>";
 
-const int PIN_CLK = D5;
-const int PIN_SOIL = A0; 
-const int PIN_LED = D7;
-const char* SERVER = "api.thingspeak.com";
-const int TMP_ADDR = 0x48;
+const int PIN_CLK   = D5;
+const int PIN_SOIL  = A0; 
+const int PIN_LED   = D7;
+const int PIN_SWITCH = D8;
+
+// I2C address for temperature sensor
+const int TMP_ADDR  = 0x48;
 
 int sent = 0;
 
 #define SLEEP_TIME 1200 * 1000 * 1000
 
-void setup() {
-  Serial.begin(115200);
-  Wire.begin();
-  pinMode(PIN_CLK, OUTPUT);
-  pinMode(PIN_SOIL, INPUT);
-  pinMode(PIN_LED, OUTPUT);
-  digitalWrite(PIN_LED, HIGH);
+
+float readTemperature() {
+  float temp;
   
-  // device address is specified in datasheet
-  Wire.beginTransmission(TMP_ADDR); // transmit to device #44 (0x2c)
-  Wire.write(byte(0x01));            // sends instruction byte
-  Wire.write(0x60);             // sends potentiometer value byte
-  Wire.endTransmission();     // stop transmitting
-  
-  analogWriteFreq(40000);
-  analogWrite(PIN_CLK, 400);
-  delay(500);
-  connectWifi();
-}
-
-void loop() {
-  digitalWrite(PIN_LED, LOW);
-  delay(100);
-  digitalWrite(PIN_LED, HIGH);
-  float hum=0,temp=0,soil_hum=0;
-  Serial.println("Requesting Temperature...");
-
   // Begin transmission
   Wire.beginTransmission(TMP_ADDR);
   // Select Data Registers


    int rawtmp = msb << 8 |lsb;
    int value = rawtmp >> 4;
    temp = value * 0.0625 ;
  }

  delay(1000);
  soil_hum = readSoilVal(8);
  Serial.print("Soil_Humidity:");
  Serial.println(soil_hum);
  Serial.print("Temperature:");
  Serial.println(temp);
  delay(5000);
  
  sendData(temp, soil_hum);
  ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
}

void connectWifi()
{
  Serial.print("Connecting to " + *MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("");  
}//end connect

void sendData(float temp, float soil_hum) {  
   WiFiClient client;
  
   if (client.connect("api.thingspeak.com", 80)) { // use ip 184.106.153.149 or api.thingspeak.com
       Serial.println("WiFi Client connected ");
       
       String postStr = API_KEY;
