////////////////////////////////////////////////////////////////////////////////
// Name:       FloraGardenUno-01.ino                                          //
// Platform:   Arduino UNO Rev3                                               //
// Created by: HARB, April 2017, GPL copyrights                               //
// http://robotigs.com/robotigs/includes/bots_header.php?idbot=18             //
// This project used to be called VENSTERBANK.                                //
// An Arduino Uno is used to measure moisture, temperature and light. The data//
// are transmitted by wifi if requested so.                                   //
// Thanks: SR04 created by Rui Santos, http://randomnerdtutorials.com         //
////////////////////////////////////////////////////////////////////////////////


// SET PRECOMPILER OPTIONS *****************************************************
//Define the needed header files for the precompiler, no charge if not used ----
#include <Wire.h>                  //Two Wire Interface library needed for CJMCU
#include <OneWire.h> //Library can be installed through Arduino IDE menu DS18B20
OneWire ds(2);      //DS1820 is connected to pin 2, but may be any DIO input pin

//Define precompiler variables -------------------------------------------------
bool ledBinVal   = LOW;             //Choose HIGH=on or LOW=off for on-board LED
bool moistSwitch1 = HIGH;        //HIGH=L>R or LOW=R<L Needed during measurement
word gardsoimoi1;                    //GREenhouse Soil Moisture sensor 1 = CJMCU
word gardsoimoi2;             //GREenhouse Soil Moisture sensor 2 = RO-043 right
word gardsoimoi3;              //GREenhouse Soil Moisture sensor 3 = RO-043 left
String html;              //HTML Response preapaired and counted into bodylength
unsigned int bodyLength;                                    //HTML answer length
int trigPin = 8;                           //Trig - brown jumper needed for SR04
int echoPin = 9;                         //Echo - purple jumper, needed for SR04
long duration, inches;                                         //Needed for SR04
float cm;                                                      //Needed for SR04
byte addr[8];    //Array with the first 8 bytes of DS1820 ROM, including address
byte i;                                        //Used in DS18B20 initialisiation
byte type_s = 0; //0 always ok, except old DS1820 = 1, results in different calc
byte present = 0;               //Used for DS18B20 oneWire, present = ds.reset()
byte data[12];                              //Used during DS18B20 result reading
float  propboxtmp1;      //Switchbox soil temperature in Celsius by DS18B20 99.9
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(57600);  //Nothing more needed for the Serial Monitor to function
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);           //Needed to start Sonar Ranger04 measuring
  pinMode(echoPin, INPUT);   //Needed to read the output pulse of Sonar Ranger04
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  digitalWrite(LED_BUILTIN, ledBinVal);//Onboard LED switched to default setting
  analogReference(DEFAULT);           //Option may be different for other boards  
  DS1820_init();                                  //Determins the type of DS1820
  //Serial.println("SETUP DS18B20 DONE");
  //Serial.println(" ");
  
  //Wire.begin();                  //Two Wire Interface library needed for CJMCU
  //writeI2CRegister8bit(0x20, 6); //Reset sensor to tell it to act like a slave
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  analogWrite(11, 20);                                   //Turn the green LED on
  read_moisture();           //Reads the moisture of the soil with RO-043 sensor
  //gardsoimoi1 = readI2CRegister16bit(0x20, 0); //Read CJMCU capacitance sensor
  readSR04();             //Read the distance to water level with Sonar Ranger04
  DS1820_read();             //Switchbox read temperature from DS1820 in Celsius
  http_check();              //See if we received a http request and reply if so
  digitalWrite(11,LOW);                                 //Turn the green LED off
  toggle_ledBin();                  //Toggles the default on-board LED on or off
  delay(1000);                                      //Just to test your patience
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER


void readSR04(void) { //Read the distance to water level with Sonar Ranger04 ***
    // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  cm = (duration/2) / 29.1;
  inches = (duration/2) / 74; 
  gardsoimoi1 = cm;
} //Exit readSR04, end of read the distance to water level with Sonar Ranger04--


void DS1820_read(void) {          //Reads the temperature from DS1820 in Celcius
  ds.reset();
  ds.select(addr);
  ds.write(0x44);         // start conversion, with parasite power on at the end    
  delay(800);                                // maybe 750ms is enough, maybe not
  present = ds.reset(); 
  ds.select(addr);    
  ds.write(0xBE);                                             // Read Scratchpad
  for ( i = 0; i < 9; i++) {                                  // we need 9 bytes
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  propboxtmp1 = (float)raw / 16.0;
}


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>Greenhouse</title></head><body>";   
    html += "gardsoimoi1=";
    html += cm;
    html += "&gardsoimoi2=";
    html += propboxtmp1;
    html += "&gardsoimoi3=";
    html += gardsoimoi3;
    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---

void read_moisture(void) { //Reads the moisture of the soil with RO-043 sensor *
  // http://robotigs.com/robotigs/includes/parts_header.php?idpart=66
  if (moistSwitch1) {
    pinMode(A0, INPUT);
    pinMode(A1, OUTPUT);
    digitalWrite(A1, HIGH);
    delay(2);
    gardsoimoi2 = analogRead(A0);                                     //Moisture
    digitalWrite(A1, LOW);
  }else{
    pinMode(A1, INPUT);
    pinMode(A0, OUTPUT);
    digitalWrite(A0, HIGH);
    delay(2);
    gardsoimoi3 = analogRead(A1);                                     //Moisture
    digitalWrite(A0, LOW);
  }
  moistSwitch1 = !moistSwitch1;                      //Toggle value moistSwitch1
} //Exit read_moisture ---------------------------------------------------------


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 writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}


unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(20);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}


void DS1820_init(void) {                          //Determins the type of DS1820
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
    //Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  //Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    //Serial.write(' ');
    //Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      //Serial.println("CRC is not valid!");
      return;
  }
  //Serial.println();
 
  switch (addr[0]) {   // the first ROM byte indicates which chip
    case 0x10:
      //Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      //Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      //Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      //Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  //Serial.print("  Data = ");
  //Serial.print(present, HEX);
  //Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    //Serial.print(data[i], HEX);
    //Serial.print(" ");
  }
  //Serial.print(" CRC=");
  //Serial.print(OneWire::crc8(data, 8), HEX);
  //Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  propboxtmp1 = (float)raw / 16.0;
  //fahrenheit = celsius * 1.8 + 32.0;
  //Serial.print("  Temperature = ");
  //Serial.print(celsius);
  //Serial.print(" Celsius, ");
  //Serial.print(fahrenheit);
  //Serial.println(" Fahrenheit");
}


////////////////////////////////////////////////////////////////////////////////
// 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  - CJMCU Moisture 2 Sensor  TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - CJMCU Moisture 2 Sensor  TWI //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - LED_BUILTIN Arduino      SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - LED 3-color red          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - LED 3-color blue         PWM //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - LED 3-color green        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      - DS1820 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       - Moisture 1 Left          ADC //
// A1    -  24 - PC1 -A1/15- ADC1/PCINT9       - Moisture 1 Right         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