#include <OneWire.h>                                        	                  	 // library for the oneWire digital pins
#include <SPI.h>                                                     		 // A Brief Introduction to the Serial Peripheral Interface 
#include <Ethernet.h>                                                        		 // library for ethernet functions
#include "SPI.h"
//#include "Ethernet.h"
#include "sha1.h"
//#include "mysql.h"
//=============Preferences============
//-------------Pins-------------------
OneWire ds(2);                                                               		 // declare 1Wire digital pins
//--------------Network-Arduino-------
//static char user[] = "arduino";                                             	// SQL-Nutzer direct link about SQL-Connector
//static char password[] = "pi";                                              	// SQL-Kennwort direct link about SQL-Connector
static IPAddress server_addr(192, 168, 1, 1);                                	// IP-Adresse Server
static byte my_mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x9B, 0xD1 };       // MAC-Adress Arduino 
static IPAddress ip(192, 168, 1, 200);                                         	// IP-Adress Arduino
//byte subnet[]  = { 255, 255, 0, 0 };
//const unsigned int t_delay=1000;                                           	// Pausenzeit
EthernetClient client;                                                       		// EthernetClient - Instance build
//onnector my_conn;                                                           		// Connector - Instance build
//--------------PhP--Adress-----------
char host[]    = "www.sonds.org";                                             	// Domain from Server
char url[]     = "/Transmit_Data_analog.php";                                 	// Path from the PHP-File
char key[]     = "MEINPASSWORT123";                                           	// Keyword from PHP-File
char c;                                                                      		// Variable for answer from Servers
long Interval  = 5;                                                          		// Upload-Interval
//-------------Settings/Variable------
byte addr_oneWire_next[8];                                                   	 // buffer for Address 1Wire
int addr_onewire_counter = 0;                                                 	// Variable to loop the Values for Address from 1Wire
byte addr_oneWire[10][8];                                                     	// Array for oneWire Adress
byte present = 0;                                                             		// counter for return text Server
byte data_oneWire[10][12];                                                    	// Array Values from 1Wire
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;               // Variable to convert the Hex-Values into a decimal value
char buffer1[10];                                                             		// buffer to translate the Hex-code from 1wire
char buffer2[10];                                                             		// buffer to translate the Hex-code from 1wire
char minus[2] = "-";                                                         		// charset to translate the hex-code from 1wire 
char point[2] = ",";                                                          		// charset to translate the hex-code from 1wire (german)
char values_float[10][20];                                                    	// values 1wire in °C and decimal
int datapoint = 0;                                                           		// counter for sequence transmit the values 
int analog_port_counter = 4;                                                   	// number analog ports 0-3
//-------------Initialize-------------
void setup() 
   {
   Ethernet.begin(my_mac, ip);                                                	// Ethernet start
   Serial.begin(115200);                                                      		// Serial start
   delay(2000);                                                              		// wait for Ethernet and Serial
   Serial.println("Connecting...");
   analogReference(DEFAULT);
   delay(2000);
   Serial.println("analogReference DEFAULT");
   }
//--------------Controller------------
void loop(void)                                                               		// control-loop
   {
   for(int i=0; i< analog_port_counter; i++)
      {
       int value_analog = Analog_read(i);                                      	// Read Analog Port
       float value_analog_direct = value_analog*0.009766;            	// 10bar = 10Voltage  / 1024  = 0.09765625                     
       Send_data_php_mysql_analog(value_analog_direct, i);     	//send to server
       delay(2000);                                     	                     	// wait 2000ms
     
       byte maxReads = 10;                                       		// Seconds
       while ((maxReads-- > 0) && client.connected())                        	// wait and read answer from database
          {
          delay(1000);                                                     		// wait
          while (client.available())                                           
             {
             char response = client.read();
             Serial.print(response);
             }
          }
       client.stop();                                                          		//stop client com.
       Serial.println("Done.");               
       client.flush();              
      }
    delay(600000);                                                             		// walt 1 min to repeat the control loop
    }  

 
void Monitor(){                                                                		// Sende Data on Monitor
    for(int j=0; j<10; j++)                                                                                           
       {
       if(j < addr_onewire_counter)                                            	// with void arrays counter
          {  
          Serial.print("Adress:     ");
          for(int i = 0; i < 10; i++)                                 
             {
             Serial.print(addr_oneWire[j][i], HEX);                           
             Serial.print(" ");
             }
          Serial.print("      Value:      ");
          for(int i = 0; i < 8; i++) 
             {
             Serial.print(data_oneWire[j][i], HEX);
             Serial.print(" ");
             }   
          Serial.print("      ");    
          Serial.print(values_float[j]);
          Serial.print("\n");  
          }          
     }
}
//===================================
//API : GET /Transmit_Data.php?key=MEINPASSWORT123&mac_addr_arduino=0xmy_mac[0]&sensor_addr=0xaddr_oneWire[datapoint][0]&value_hex=0xdata_oneWire[datapoint][0]&value_direct=0xvalues_float[datapoint][0])  \r\n" HTTP/1.0 Host: www.sonds.org \r\n
//===================================
 
void Send_data_php_mysql(int datapoint)                 		// Sende data to Database                   
   {  
   if (client.connect(server_addr, 80))                                        	// Verbindung zum Server aufbauen
      {
      Serial.println("Verbunden, Sende Daten...");           
      client.print("GET /Transmit_Data.php");                                  	// Outside connection
      client.print("?key=MEINPASSWORT123");                                    	// key = password Database
      client.print("&mac_addr_arduino=");                                      	// Start transmit Data     
      for(int i = 0; i<6; i++)                               
         {
         client.print(" 0x");
         client.print(my_mac[i], HEX);
         if(i<5)
            {
             client.print("_"); 
            }  
         }   
      client.print("&sensor_addr=");
      for(int i = 0; i<8; i++)
         {
         client.print("0x");
         client.print(addr_oneWire[datapoint][i], HEX); 
         if(i<7)
            {
             client.print("_"); 
            }  
         }   
      client.print("&value_hex=");
      for(int i = 0; i<12; i++)
         {
         client.print("0x");
         client.print(data_oneWire[datapoint][i], HEX);  
         if(i<11)
            {
             client.print("_"); 
            } 
         }         
      client.print("&value_direct=");
      for(int i = 0; i<6; i++)
         {
         client.print("0x");
         client.print(values_float[datapoint][i]);
         if(i<5)
            {
             client.print("_"); 
            } 
         }    
      client.print("  \r\n");                                                    		// Settings coming....
      client.print("HTTP/1.0");                                                  	// Protocoltyp 
      client.print("Host: www.sonds.org \r\n");                                  	// URL from Database
      client.println();                                                          		// Transmit end !!
      }
      else
      {
      Serial.println(" ***** VERBINDUNG KANN NICHT HERGESTELLT WERDEN *****");
      }
}
void Send_data_php_mysql_analog(float value_analog_direct, int analog_port)      // Sende data to Database                   
   {  
   if (client.connect(server_addr, 80))                                          	// Verbindung zum Server aufbauen
      {
      Serial.println("Verbunden, Sende Daten...");           
      client.print("GET /Transmit_Data_analog.php");                      	// Outside connection
      client.print("?key=MEINPASSWORT123");                                     // key = password Database
      client.print("&mac_addr_arduino=");                                        	// Start transmit Data     
      
      for(int i = 0; i<6; i++)                               
         {
         
         client.print(my_mac[i], HEX);  
      //   if(i<5)
      //     {
      //      client.print("_"); 
      //     } 
         }   
      client.print("&sensor_addr=");
      client.print("A");
      client.print(analog_port);                 
      client.print("&value_direct=");
      client.print(value_analog_direct);
      
      client.print("  \r\n");                                                    		// Settings coming.... 
      client.print("HTTP/1.0");                                                  	// Protocoltyp 
      client.print("Host: www.sonds.org \r\n");                                  	// URL from Database
      client.println();                                                          		// Transmit end !!
      }
      else
      {
      Serial.println(" ***** VERBINDUNG ANALOG KANN NICHT HERGESTELLT WERDEN *****");
      }
}

 //============================================================================
 //return the value from the port number where you give inside
 //inside byte the analog ports that you want read
 //ten bit measurement
 //return the bit value in an integer 
 //============================================================================     
  int Analog_read(int analog_port)
   {
   int value_bit = analogRead(analog_port);  
   return value_bit;   
   } 
 //============================================================================
 //inside the byte value and the offset factor
 //inside analog port number
 //return on monitor the value from sensor in integer
 //============================================================================ 
 /*
 void Analog_monitor(int value, float offest_factor, byte port)
   {
    Serial.print(port);
    Serial.print("          ");
    Serial.print(value);
    Serial.print(" bit");
    Serial.print("          ");
    Serial.print(value_1); 
    Serial.print(" mV");
    Serial.print("          ");
    Serial.print(value_1/1000); 
    Serial.print(" V");
    Serial.print("          ");
    Serial.print(value_bit_1*0.0097656); 
    Serial.println(" bar"); 
   } 
   */   
