////////////////////////////////////////////////////////////////////////////////
// Name:       FloraGreenhouseUno-01.ino                                      //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB, April 2017, GPL copyrights                               //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=7              //
// An Arduino Uno is used to measure moisture, temperature and light. The data//
// are transmitted by wifi if requested so.                                   //
////////////////////////////////////////////////////////////////////////////////


// SET PRECOMPILER OPTIONS *****************************************************
//Define the needed header files for the precompiler, no charge if not used ----
#include <DHT.h>   //Needed for DHT22 and DHT11 Temperature and humidity sensors

//Define precompiler variables -------------------------------------------------
bool ledBinVal   = LOW;             //Choose HIGH=on or LOW=off for on-board LED
#define DHTPIN 2                        //What digital pin is DHT22 connected to
#define DHTTYPE DHT22                                 //DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);                               //Initialize DHT sensor1

//Declare variables ------------------------------------------------------------
String html;                   //Response preapaired and counted into bodylength
unsigned int bodyLength;                                    //HTML answer length
float airtemp0;                               //Temperature as measured by DHT22
float humidity0;                                 //Humidity as measured by DHT22
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(57600);  //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  digitalWrite(LED_BUILTIN, ledBinVal);//Onboard LED switched to default setting
  dht.begin();                                                //Start DHT sensor
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  airtemp0 = dht.readTemperature();                //Read temperature as Celsius
  humidity0 = dht.readHumidity();        //Reading sensor takes 250 milliseconds
  http_check();              //See if we received a http request and reply if so
  toggle_ledBin();                  //Toggles the default on-board LED on or off
  delay(1000);
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER


void toggle_ledBin(void) { //Toggles the default on-board LED on or off ********
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------

void http_check(void) { //See if we received a http request and reply if so ****
  String Request = "";                                    //Reset receive string
  while (Serial.available() > 0) {   //Check if any request is made by a browser
    Request = String(Request + Serial.readString());   //Read incoming character
  } //End of if (Serial.available() > 0)              Entire block has been read
  if (Request != "") {                         //Did I really receive a request?
    //Serial.println(Request);                                //Show the request

    html = "";                              //Always start answer with this line
    html += "<html><head><title>Garden</title></head><body>";   
    html += "airtmp1=";
    html += String(airtemp0, 1);
    html += "&airhum1=";
    html += String(humidity0, 1);
    html += "</body></html>";

    
    bodyLength = html.length();  //Calculate the number of characters to be sent
    bodyLength = bodyLength + 17;                           //Add the basic text
    Serial.println("HTTP/1.1 200 OK");                   //Answer to the request
    Serial.println("Connection: close");          //Close after html is finished
    Serial.print("Content-Length: ");        //Finish html after amount of chars
    Serial.println (bodyLength);
    Serial.println("Content-Type: text/html");         //Needed to be compatible
    Serial.println(" /n \n");                //Needed to end the headers somehow
    Serial.println("<!DOCTYPE HTML>");    //Tell the browser what we are sending
    Serial.println(html);                                         //Entire tekst
  } //End of if (inStri <> "")
} //Exit http_check, end of See if we received a http request and reply if so---

////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS TABLE ARDUINO UNO                                          //
// Board -Atmel- PIN - IDE - Function          - Connection               ALT //
//                                                                            //
// CONNECTIONS RAILS TOP LEFT: DIGITAL PWM<~> ******************************* //
// SCL   -  28 - PC5 -19/A5- ADC5/SCL/PCINT13  -                          TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                          TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        -                          SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       -                          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  -                          PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    -                          PWM //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       -                          PWM //
// 8     -  14 - PB0 -   8 - PCINT0/CLK0/ICP1  -                          DIO //
//                                                                            //
// CONNECTIONS RAILS TOP RIGHT: DIGITAL PWM<~> ****************************** //
// 7     -  13 - PD7 -   7 - PCINT23/AIN1      -                          DIO //
// ~6    -  12 - PD6 -   6 - PCINT22/OCA0/AIN0 -                          PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   -                          PWM //
// ~4    -   6 - PD4 -   4 - PCINT20/XCK/T0    -                          PWM //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 -                          INT //
// ~2    -   4 - PD2 -   2 - PCINT18/INT0      - DHT22 temperature        INT //
// TX->1 -   3 - PD1 -   1 - PCINT17/TXD       - Serial monitor           TXD //
// RX<-0 -   2 - PD0 -   0 - PCINT16/RCD       - Serial Monitor           RCD //
//                                                                            //
// CONNECTIONS RAILS BOTTOM LEFT: POWER ************************************* //
// 5V    -   7 - VCC -      - VCC              - Output to breadboard     VCC //
// RES   -   1 - RES -      - PCINT14/RESET    -                          RES //
// 3.3V  -     -     -     -                   -                              //
// 5V    -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// GND   -     -     -     -                   -                              //
// Vin   -     -     -     -                   -                              //
//                                                                            //
// CONNECTIONS RAILS BOTTOM RIGHT: ANALOG IN ******************************** //
// A0    -  23 - PC0 -A0/14- ADC0/PCINT8       -               t          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       -                          ADC //
// A2    -  25 - PC2 -A2/16- ADC2/PCINT10      -                          ADC //
// A3    -  26 - PC3 -A3/17- ADC3/PCINT12      -                          ADC //
// A4    -  27 - PC4 -A4/18- ADC4/SDA/PCINT12  -                          TWI //
// A5    -  28 - PC5 -A5/19- ADC5/SCL/PCINT13  -                          TWI //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898