 #define TEMP_SENSOR D6
 #define PH_ADDRESS 99
 #define ORP_ADDRESS 98
+#define SAMPLE_INTERVAL 2500
+#define WAKE_INTERVAL 60000;
 
 // Set external antenna (remembered after power off)
 STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));
 
 DS18B20 ds18b20(TEMP_SENSOR);
 double celsius;
-unsigned int Metric_Publish_Rate = 30000;
-unsigned int MetricnextPublishTime;
-int nextSampleTime, nextSleepTime;
-int SAMPLE_INTERVAL = 2500;
-int SLEEP_INTERVAL = 60000 * 5;
-int WAKE_INTERVAL = 60000;
+int nextSampleTime, nextSleepTime, sleepInterval;
 
 double pH = 0;
 double ORP = 0;


void setup() {
  Serial.begin(9600);
  Wire.begin();
  pinMode(TEMP_SENSOR, INPUT);

  lipo.begin();

}

void loop() {
  if (millis() > nextSampleTime) {
    getTemp();
    getPh();
    getORP();
    nextSampleTime = millis() + SAMPLE_INTERVAL;
    soc = lipo.getSOC();
    wifi = WiFi.RSSI();
  }
  if(millis() > nextSleepTime) {
    Particle.publish("waterdata", water_data(), PRIVATE);
    if(soc <= 10)
      sleepInterval = 60 * 15;
    else if(soc <= 10)
      sleepInterval = 60 * 10;
    else
      sleepInterval = 60 * 5;
    nextSleepTime = millis() + (sleepInterval * 1000) + WAKE_INTERVAL;
    executeRequest(PH_ADDRESS,"Sleep");
    executeRequest(ORP_ADDRESS,"Sleep");
    System.sleep(SLEEP_MODE_DEEP,sleepInterval);
  }
}

String water_data() {
  return String::format("{\"temperature\":%f,\"ph\":%f,\"orp\":%f,\"soc\":%f}",celsius,pH,ORP,soc);
}

void getPh() {
  String pH_data = executeRequest(PH_ADDRESS,"R");
  //Serial.print("Ph:");
  //Serial.println(pH_data);
  if (isdigit(pH_data[0]))
    pH = String(pH_data).toFloat();
 }

void getORP() {
  String orp_data = executeRequest(ORP_ADDRESS,"R");
  //Serial.print("ORP:");
  //Serial.println(orp_data);
  if (isdigit(orp_data[0]))
    ORP = orp_data.toFloat();
}
