 
 void sensorTimerFinished(void *pArg) {
   isSensorTimerComplete = true;
+}
+
+void wifiConnectTimerFinished(void *pArg) {
+  isWifiConnectTimerComplete = true;
 }
 
 void sendSensorData() {

    ArduinoOTA.begin();
    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.print("Test: ");
    Serial.println(WiFi.localIP());
}


  }
}

// Returns true if the sensor was initialised successfully
bool initSensor() {
  Serial.print("Initialising SHT31 sensor...");
  if (!sht31.begin(0x44)) {
    Serial.println("NOT FOUND");
    return false;
  } else {
    Serial.println("FOUND");
    return true;
  }
}

void setup()  {
  // Configure serial port
  Serial.begin(115200);
  Serial.println("Server: "+String(server));
  Serial.println("ClientID:"+String(clientId));
  pinMode(D3, OUTPUT);
//  digitalWrite(D1, 1); // switch off
//  delay(3000);
  digitalWrite(D3, 1);
  if (initSensor() == false) {
    // if we failed to initialise the sensor
    // then just return and don't do anything
    // TODO: Send message to system to alert parent there is an issue
    return;
  }
  // initialise WiFi but don't connect yet
  initWiFi();
  initWiFiConnectTimer();
  //connectWithBroker();
  // Start the sensor timer

  initSensorTimer();
  // startSensorTimer();
}

void loop() {

  // if wifi not connected
  // rescan wifi
  // if not found recognised wifi then sleep for 5 mins
  // else connect to recognised wifi

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected");
    stopSensorTimer();
    // Scan Network
    if (scanWiFiNetworks() == true) {
      // found a network, lets connect to it
      if (connectWiFiNetwork() == true) {
        // configure Over The Air updates
        configureOTA();
        isWaitingForWiFi = false;
        startSensorTimer();
        //stopWiFiConnectTimer();
      } else {
        // couldn't connect to the wifi network
        Serial.println("Couldn't connect to the WiFi network");
        isWaitingForWiFi = true;
        // trigger timer before we try again
        //startWifiConnectTimer();

        // Sleep for 1 minute
        ESP.deepSleep(uint32_t time_us, optional RFMode mode)
      }
    } else {
      // Couldn't find a known network to connect to
      isWaitingForWiFi = true;
      //stopSensorTimer();
      // trigger timer before we try again
      //startWifiConnectTimer();

      // Sleep for 1 minute

    }
  } else {
    // We are connected to a network
    // Only if we were waiting for wifi do these
    // otherwise just carry on
    if (isWaitingForWiFi == true) {
      isWaitingForWiFi = false;
      configureOTA();
      startSensorTimer();
      //stopWiFiConnectTimer();
    }
  }

  // Check to see we are connected to the broker
  if ((client.connected() == false)) {
    connectWithBroker();
  }

  // when the timer has completed and we aren't waiting for wifi
  if (isSensorTimerComplete == true) {
    isSensorTimerComplete = false;
    readSensorData();
    sendSensorData();
  }


  // if (!!!client.connected()) {
  //   connectWithBroker();
  // }



  // read analog temp sensor for battery

