//include all everything necessary to read the thermometer and connect to php
#include <Arduino.h>
#include <WiFiMulti.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <lm75.h>
#include <inttypes.h>

WiFiMulti wifiMulti;

//set up the transmission to read from the wire
TempI2C_LM75 termo = TempI2C_LM75(0x48,TempI2C_LM75::nine_bits);

void setup() {
  // put your setup code here, to run once:
  //allow for serial comunication for debugging
Serial.begin(115200);
Serial.setDebugOutput(true);
//count down, wait for a connection
for (uint8_t t = 4; t > 0; t--){
  Serial.printf("[SETUP] WAIT %d...\n", t);
  Serial.flush();
}
//connect to wifi
  wifiMulti.addAP("Kuena","ckeb489(");
  delay(5000);
}



void loop() {
  
  //wait for ESP to connect to OSU_Access
  if((wifiMulti.run() == WL_CONNECTED)){
    Serial.println("Connected");
   HTTPClient http;
   
   float temp = termo.getTemp();
   
   //begin with the exact place this should direct to
   http.begin("https://web.engr.oregonstate.edu/~sakatac/finalproject.php");
   
   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    

    //create a string to be posted to php
    String tempString;
    tempString = "temp=";
    tempString = tempString + termo.getTemp();

    //check data on the serial monitor
    Serial.print("Temperature:");
    Serial.println(termo.getTemp());

    int httpCode = http.POST(tempString);
  
    delay(1000);
    
   if(httpCode >0) {
    if(httpCode == HTTP_CODE_OK) {
      String payload = http.getString();
      Serial.println(payload);
    }
   
    else{
  Serial.printf("[HTTP] GET...code: %d\n", httpCode);
    
   
    }
     http.end(); //end request
   }
  }
   else if ((wifiMulti.run() != WL_CONNECTED)){
Serial.println("Connection Error");
delay(3000);
   }
   }
