// SHT31 configuration
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();

#define SHT31_RST_PIN 16

void resetSensor() {
  pinMode(SHT31_RST_PIN, OUTPUT);
  digitalWrite(SHT31_RST_PIN, LOW);
  delay(10);
  digitalWrite(SHT31_RST_PIN, HIGH);
  delay(10);
}
//
// W5500 configuration
#include <SPI.h>
#include <Ethernet.h>

// Configure MAC address and IP:
byte mac[] = { 0x97, 0x8A, 0xC5, 0x86, 0xA4, 0xEF };
char T[8];
char H[8];
char message[20];


// the real one IPAddress ip(10, 11, 1, 22); // Static IP


// Define CS and RST pins:
#define W5500_CS_PIN 10 // 8 in E LEGO
#define W5500_RST_PIN 9 //10 in E LEGO
//IPAddress serverIP(10, 44, 1, 238);  // Computer
//IPAddress W5500_ip(10, 44, 1, 22);   // Change the last digit
IPAddress serverIP(10, 44, 1, 238);
IPAddress W5500_ip(10, 11, 1, 46);
IPAddress gateway(10, 11, 1, 1);
IPAddress DNS(147,142,19,254);
IPAddress subnet(255, 255, 255, 0);



// Don't change
const int port = 5005;
EthernetClient client;
//


void setup() {
  // put your setup code here, to run once:
  
  // SHT31 First readout
  Serial.begin(9600);
  while (!Serial)
    ;
  Serial.println("Looking for SHT31");

  if (!sht31.begin(0X44)) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(100);
  }
  Serial.println("SHT31 Found");
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (!isnan(t)) {
    Serial.print("Temperature (C)= ");
    Serial.println(t);
  }

  if (!isnan(h)) {
    Serial.print("Humidity = ");
    Serial.println(h);
  }


  // W5500 first readout
  pinMode(W5500_RST_PIN, OUTPUT);
  digitalWrite(W5500_RST_PIN, HIGH);
  delay(250);
  digitalWrite(W5500_RST_PIN, LOW);
  delay(250);
  digitalWrite(W5500_RST_PIN, HIGH);
  delay(1000);

  // Initialize Ethernet:
  Ethernet.init(W5500_CS_PIN);
  Ethernet.begin(mac, W5500_ip, DNS, gateway, subnet);
  
  delay(1500);
  Serial.print("W5500 IP: ");
  Serial.println(Ethernet.localIP());

  // Send a message through socket
  Serial.println("Sending test message");
  delay(1000);
  if (client.connect(serverIP, port)) {
    client.write("Connection check");
    client.stop();
    Serial.println("Test Message sent");
  } else {
    Serial.println("Connection failed");
    while(1);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  // Measure temp. and humidity every 30 seconds
  delay(15000);
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();
  // Send to Eilon's computer
  if (client.connect(serverIP, port)) {
    client.write("Temp");
    client.stop();
  } else {
    Serial.println("Connection failed");
  }
  if (client.connect(serverIP, port)) {
    if (!isnan(t)) {
      dtostrf(t, 6, 2, T);
      snprintf(message, sizeof(message), "T %s", T);
      Serial.println(message);
      //client.write((const uint8_t*)message, strlen(message));  // Send to server
      client.write(T);
      client.stop();
      Serial.println("message sent");
    }

  } else {
    Serial.println("Connection failed");
  }
  delay(15000);
  if (client.connect(serverIP, port)) {
    client.write("Humidity");
    client.stop();
  } else {
    Serial.println("Connection failed");
  }
  
  if (client.connect(serverIP, port)) {
    if (!isnan(h)) {
      dtostrf(h, 6, 2, H);
      snprintf(message, sizeof(message), "H %s", H);
      Serial.println(message);
      client.write(H);
      //client.write((const uint8_t*)message, strlen(message));  // Send to server
      client.stop();
      Serial.println("message sent");
    }
  } else {
    Serial.println("Connection failed");
  }
}
