     }
   }
 
-  // battery voltage is good, boot system
-  pinMode(D7, OUTPUT);
-  digitalWrite(D7, HIGH);
+  daytime = true;
+  if (solarVoltage < 1000)
+    daytime = false;
 
-  delay(500);
+  delay(100);
   uvIndex.begin();
   tempSensor.begin();
 }

    { NULL, NULL } // NOTE: Always terminate headers with NULL
};

int dataReady = 0;
int connectionStarted = 0;
int transmitData = 0;

void loop()
{
  // read data from sensors
  if (dataReady == 0)
  {
    temperature = tempSensor.readTemperature();
    humidity = tempSensor.readHumidity();
    uv = uvIndex.readUV();
    ir = uvIndex.readIR();
    visible = uvIndex.readVisible();
    dataReady = 1;
  }

  // start up Internet connection
  if (connectionStarted == 0)
  {
    RGB.control(false);
    digitalWrite(D7, LOW);
    Spark.connect();
    connectionStarted = 1;
  }

  if (Spark.connected())
  {
    // process any background tasks for wifi
    Spark.process();

    if (transmitData == 0)
    {
      transmitData = 1;
      request.hostname = IPADDRESS;
      request.port = 80;
      request.path = URL;

      request.body =  "field1=" + String((int)temperature, DEC)
                       + "&" + "field2=" + String((int)humidity, DEC) // Do not omit
                       + "&" + "field3=" + String(uv, DEC) // the &
                       + "&" + "field4=" + String(ir, DEC)
                       + "&" + "field5=" + String(visible, DEC)
                       + "&" + "field6=" + String(solarVoltage, DEC)
                       + "&" + "field7=" + String(batteryVoltage, DEC);
      http.post(request, response, headers);
    }
  }

  // stay on for only 30 seconds maximum
  if (millis() > 30000)
  {
    // did not connect to the Internet
    if (Spark.connected() == false)
    {
      if (batteryVoltage > 2250 && solarVoltage > 2400)
      {
        // strong battery and solar voltage - power back on in 60 seconds
        System.sleep(SLEEP_MODE_DEEP, 60);
        goto exit;
      }
      // else fall thru and test for day/night
    }

    // connection successul - come back in 5 minutes during daytime, 10 minutes at night
    if (solarVoltage < 1000)
    {
      System.sleep(SLEEP_MODE_DEEP, 10*60);
    }
    else
    {
      System.sleep(SLEEP_MODE_DEEP, 5*60);
    }
  }
  exit:;
}


void setup()
{
  pinMode(D4, INPUT_PULLUP);
  currentState = STATE_BOOT;

  // test if input at D4 is LOW, enter program mode if so
  // turn on LED, and start connection process
  if (digitalRead(D4) == LOW)
  {
    currentState = STATE_PROGRAM;
    pinMode(D7, OUTPUT);
    digitalWrite(D7, HIGH);
    Spark.connect();
    return;
  }

  // turn off main led to save power, also use our own LED colors for states
  RGB.control(true);
  RGB.color(0,0,0);

