#include <SPI.h>
#include <Ethernet.h>
#include "LiquidCrystal_1602_RUS.h"
#include <EEPROM.h>
#include <avr/interrupt.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <avr/wdt.h>
LiquidCrystal_1602_RUS lcd(8, 9, 4, 5, 6, 7 );
OneWire ds(15); DallasTemperature sensors(&ds); float temp; 
String readString = String(300); 
String message;
int opros=0; int sends=0; int sendperiod=0; int dog=0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "test.domain.ru";    
IPAddress ip(10, 0, 0, 177);
EthernetClient client;
void setup() 
{
  lcd.begin(16, 2);
  TCCR2A = 0; 
  TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; 
  TIMSK2 = 1<<TOIE2; 
  sei();
  wdt_enable(WDTO_8S);  
}
ISR(TIMER2_OVF_vect) 
{
   opros++;
    if(opros==7500) 
    {
      sensors.requestTemperatures();
      temp = sensors.getTempCByIndex(0);
       lcd.setCursor(0,0); lcd.print(message); 
       lcd.setCursor(0,1); if(60-sendperiod<10){lcd.print(0);} lcd.print(60-sendperiod);
       lcd.setCursor(9,1); lcd.print(temp);  lcd.print(L"°C");
      opros=0;
     if(sends!=0){dog++;} else {dog=0;}
      if(dog<600){wdt_reset();}  
     
      if (sends==0 && sendperiod==0){sends=1;}  
      sendperiod++; if(sendperiod==60){ sendperiod=0;}  
          
    }
   
}
void loop() 
{
if (sends==1) 
{
  message="Connect         ";
  lcd.setCursor(0,0); lcd.print(L"Connect         ");
  sends=2; 
  
  Serial.begin(9600);
  while (!Serial) 
  {
    ; 
  }  
  
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    
    Ethernet.begin(mac, ip);
  }
  
  delay(1000);
  Serial.println("connecting...");
  
  if (client.connect(server, 80)) 
    {
    Serial.println("connected");
    
    
    client.print("GET /info/temp.php?t=");
    client.print(temp);
    client.println(" HTTP/1.1");
    client.println("Host: test.domain.ru");
    client.println("Connection: close");
    client.println();
    } 
  else 
    {
    
    Serial.println("connection failed");
    }
  sends=3;
}  
  
if(sends==3)
 { 
  if (client.available()) 
  {
    
    char c = client.read();
    Serial.print(c);
      if (c != '\n'){readString+= c;}
 }
  if (!client.connected()) 
  {
    Serial.println();
    Serial.println("disconnecting.");
    if(readString.indexOf("z666")>0)
     {message="OK ";message+= readString.substring(readString.indexOf("z666")+5,readString.indexOf("z666")+19);}
     else {message="Send error";}
    readString="";
    client.stop();
    sends=0;
  } 
 }
}