////////////////////////////////////////////////////////////////////////////////
// Name:       ESP13                                                          //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB rboek2@gmail.com July 2016 GPL copyrights                 //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=186          //
// to test run: http://robotigs.com/robotigs/mobile/ESP13-2.php               //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
// Initialse conditional compiling, uncomment to include, comment to exclude ---
// Do comment for runtime versions
#define RS232                     //Uncomment to include Serial Monitor sections

// Define precompiler settings PINS --------------------------------------------
#define ledRed  3        //3 Color LED, to which PWM pin this Color is connected
#define ledGre  4        //3 Color LED, to which PWM pin this Color is connected
#define ledBlu  5        //3 Color LED, to which PWM pin this Color is connected
#define relay1  6                 //Relay to which an 220 Vac light is connected

// Define the needed header files for the precompiler, no charge if not used ---

// Define user settings --------------------------------------------------------

// Define variables ------------------------------------------------------------
bool ledBinVal = LOW;         //You can chose HIGH-on or LOW-off for LED_BUILTIN
bool relay1Val = LOW;             //You can chose HIGH-on or LOW-off for light 1
byte Red =   0;             //Brightness of this Color, set by PWM 0=min 255=max
byte Gre =   2;             //Brightness of this Color, set by PWM 0=min 255=max
byte Blu =   0;             //Brightness of this Color, set by PWM 0=min 255=max
byte inByte = 0;                                      //For incoming serial data
String inStri   = "";
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void setup() { //Setup runs once ***********************************************
  disable_jtag();              //Disable jtag to free port C, enabled by default
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(ledRed, OUTPUT);                //Make the LED connections output pins
  pinMode(ledGre, OUTPUT);                //Make the LED connections output pins
  pinMode(ledBlu, OUTPUT);                //Make the LED connections output pins
  pinMode(relay1, OUTPUT);               //Make the lamp connections output pins

  analogWrite(ledRed, Red);   //Set the brightness of this LED and illuminate it
  analogWrite(ledGre, Gre);   //Set the brightness of this LED and illuminate it
  analogWrite(ledBlu, Blu);   //Set the brightness of this LED and illuminate it
  toggle_ledBin();                                           //Show we are awake
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  
  //CHECK IF WE RECEIVED A HTTP REQUEST ****************************************
  String inStri = "";                                     //Reset receive string
  while (Serial.available() > 0) {   //Check if any request is made by a browser
    inStri = String(inStri + Serial.readString());     //Read incoming character
  } //End of if (Serial.available() > 0)              Entire block has been read
  
  if (inStri == "") {
    delay(50);         //Needed for IR receiver Wait milliseconds, TIME TO SPARE 
  }else{
    Serial.println("HTTP/1.1 200 OK");                   //Answer to the request
    Serial.println("Connection: close");
    Serial.println("Content-Type: text/html");
    Serial.println("<html>");
    Serial.println("t1=21,7&Lu=873&mo=11&li1=0&pr=1");
    Serial.println("</html>");
    Serial.println("");
  } //End of if (inStri <> "")
 
  toggle_ledBin();                  //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


  
void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------


void toggle_ledBin(void) { //Toggles the LED_BUILTIN on-board LED on or off ****
  ledBinVal = !ledBinVal;                                         //Toggle value
  Gre = !Gre;                                                     //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
  analogWrite(ledGre, Gre);   //Set the brightness of this LED and illuminate it
} //Exit toggle_ledBin ---------------------------------------------------------


////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS                                                            //
// Prog=List=Chip                                                             //
// A0  = 97 = PF0 ADC0             =                                      ADC //
// A1  = 96 = PF1 ADC1             =                                      ADC //
// A2  = 95 = PF2 ADC2             =                                      ADC //
// A3  = 94 = PF3 ADC3             =                                      ADC //
// A4  = 93 = PF4 ADC4/TMK         =                                      ADC //
// A14 = 83 = PK6 ADC14/PCINT22    = Battery monitor H=1/2 6Vdc pack      ADC //
// A15 = 82 = PK7 ADC15/PCINT23    = Battery monitor H=1/3 9Vdc battery   ADC //
// 0   =  2 = PE0 RXD0/PCINT8      = Serial monitor, also on-board LED    RX0 //
// 1   =  3 = PE1 TXD0             = Serial monitor, also on-board LED    TX0 //
// 2   =  6 = PE4 OC3B/INT4        = IR TV remote control receiver        INT //
// 3   =  7 = PE5 OC3C/INT5        = Motorshield                          INT //
// 4   =  1 = PG5 OCOB             = Motorshield                          PWM //
// 5   =  5 = PE3 OC3A/AIN1        = Motorshield M3 => Buzzer             PWM //
// 6   = 15 = PH3 OC4A             = Motorshield M4 => FREE PWM           PWM //
// 7   = 16 = PH4 OC4B             = Motorshield                          PWM //
// 8   = 17 = PH5 OC4C             = Motorshield                          PWM //
// 9   = 18 = PH6 OC2B             = Motorshield                          PWM //
// 10  = 23 = PB4 OC2A/PCINT4      = Motorshield                          PWM //
// 11  = 24 = PB5 OC1A/PCINT5      = Motorshield                          PWM //
// 12  = 25 = PB6 OC1B/PCINT6      = Motorshield                          PWM //
// 13  = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low   PWM //
// 18  = 46 = PD2 TXD1/INT3        = Speed encoder Right                  TX1 //
// 19  = 45 = PD2 RXD1/INT2        = Speed encoder Left                   RX1 //
// 20  = 44 = PD1 SDA/INT1         = SDA Yellow SRF10                     TWI //
// 21  = 43 = PD0 SCL/INT0         = SCL White  SRF10                     TWI //
// 44  = 40 = PL5 OC5C             = 3 Color led Blue                     PWM //
// 45  = 39 = PL4 OC5B             = 3 Color led Red                      PWM //
// 46  = 38 = PL3 OC5A             = 3 Color led Green                    PWM //
// 50  = 22 = PB3 MISO/PCINT3      =                                      SPI //
// 51  = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// 52  = 20 = PB1 SCK/PCINT1       =                                      SPI //
// 53  = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898