#include <UIPEthernet.h> // Used for Ethernet
#include <DHT.h> //Needed for DHT22 and DHT11 Temperature and humidity sensors
             // http://robotigs.nl/robotigs/includes/parts_header.php?idpart=252


             
  #define DHTPIN        4                   //Which DIO input pin connects DHT22
  #define DHTTYPE   DHT22     //What sensor is connected (AM2302) (AM2321) DHT22
  
// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };                                      
// For the rest we use DHCP (IP address and such)

  String domoticz       = "";                //Creating response string INTERNET
  
//Domoticz
 const char * domoticz_server = "192.168.2.30"; // IP Adres (or name) of server to dump Domoticz data to
 int port = 8080;                               //Domoticz port
 int idx = 3;                                   //IDX for this virtual sensor, found in Setup -> Devices
 float humidity = 0;
 float temp = 0;
 int  interval = 10000; // Wait between dumps  
  
  EthernetClient client;



  DHT dht(DHTPIN, DHTTYPE);                     //Initialize sensor object DHT22

void setup() {
  Serial.begin(115200);
  Ethernet.begin(mac);
  dht.begin();                               //Start sensor object running DHT22
  
  Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask       : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP     : ");
  Serial.println(Ethernet.dnsServerIP());
}

void loop() {
    temp = dht.readTemperature();     //Read temperature as Celsius DHT22
    humidity = dht.readHumidity();      //Reading takes 250 milliseconds DHT22

  refreshMessage();  //Refresh the push message by a new one DOMOTICZ
  if (client.connect(domoticz_server, port)) {  // if you get a connection, report back via serial:
    //Serial.println(domoticz);
    //client.println(domoticz); 
    Serial.println("-> Connected");
    // Make a HTTP request:
        client.print("GET /json.htm?type=command&param=udevice&idx=");
        client.print(idx);
        client.print("&nvalue=0&svalue=");
        client.print(temp);
        client.print(";");
        client.print(humidity);
        client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
    client.stop();
  } else {
    Serial.println("--> connection failed/n"); // you didn't get a connection to the server:
  }
  delay(interval);
}


void refreshMessage(void) { //Refresh the push message by a new one DOMOTICZ ***
   domoticz  = "GET /json.htm?type=command&param=udevice&idx=";        //Request
   domoticz += String(idx);                          //Device number in domoticz
   domoticz += "&nvalue=0&svalue=";                                    //Request
   domoticz += String(temp); 
   domoticz += ";";
   domoticz += String(humidity);
   domoticz += ";0";
   domoticz += " HTTP/1.1\n";
   domoticz += "Host: ";
   domoticz += domoticz_server;
   domoticz += ":";
   domoticz += port;
   domoticz += "\n";
   domoticz += "User-Agent: Arduino-ethernet\n";
   domoticz += "Connection: close\n";
} //Exit refreshMessage ---------------------------------------------------------