/*
 *  Send temperature via HTTP GET requests to $host service.
 *  Measure temperature each 10 min. The server decide wich measure will be stored.
 *  Version 1.1.1
 */

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//----------------------------------------------------

const char* ssid              = "TNCAP3F2E03";
const char* password          = "73ABCCAA87";

const char* host              = "datahouse.kingpenguin.tk";
const int httpPort            = 80;
const int DEFAULT_INTERVAL    = 600;

//----------------------------------------------------

String MAC = "";

// broche data du DS18B20
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

// Wait for x second.
void sleepSec(int sec) {
  for (int i=0; i<1000; i++) {
    delay(sec);
  }
}

// Get the MAC address of the device.
String getMacAddress() {
  String macReturn = "";
  unsigned char mac[6];
  if(WiFi.macAddress(mac) != 0) {
    for(int i=0; i<6; i++) {
      if(mac[i]<16) {
        macReturn = "0";
      }
      macReturn += String(mac[i], HEX);
      macReturn += (i < 5) ? ":" : "";
    }
  }
  return macReturn;
}

// Get the temperature.
float getTemp() {
  float t;
  do {
    DS18B20.requestTemperatures();
    t = DS18B20.getTempCByIndex(0);
  } while (t == 85.0 || t == (-127.0));
  return t;
}


void setup() {
  Serial.begin(115200);
  delay(10);

//  WiFi.macAddress(mac);
  MAC = getMacAddress();
  Serial.print("MAC address: ");
  Serial.println(MAC);
  
  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

float temp;
int nextInterval;

void loop() {
  // On mesure la température
  temp = getTemp();
  temp += 0.5;
  int temperature = (int) temp;
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, httpPort)) {
    Serial.print("connection failed on host ");
    Serial.print(host);
    Serial.print(":");
    Serial.println(host);
    return;
  }
  
  // We now create a URI for the request
  String url = "/add/temp/"+MAC+"/"+temperature;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.println("GET "+url+" HTTP/1.1");
  client.println("Host: "+String(host));
  client.println("Connection: keep-alive");
//  client.println("Connection: close");
  client.println();
  delay(10);

  Serial.println();
  String line = "";
  while (client.available()) {
    char c = client.read();
    line += c;
  }
  Serial.println(line);
  
  nextInterval = line.toInt();
  if (nextInterval == 0) {
    nextInterval = DEFAULT_INTERVAL;
  }

  Serial.print("Temperature sent: ");
  Serial.print(temperature);
  Serial.println("°C");

  
  sleepSec(nextInterval);
}


