////////////////////////////////////////////////////////////////////////////////
// Name:       ESP14-2                                                        //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB, June 2016, GPL copyrights                                //
// http://uwvervoerder.nl/robotigs/includes/parts_header.php?idpart-43        //
// Connect your Arduino to your PC and open the serial monitor.               //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************
// Define precompiler variables ------------------------------------------------
#define DEBUG true
bool ledBinVal = LOW;       //You can choose HIGH-on or LOW-off as startposition

//Declare variables ------------------------------------------------------------
int M_NRSTpin = 2;  //Reset pin of ESP14 must be connected to any digital output
int inByte;                               //Value to read from serial connection
char myChar = 65;                           //Change inByte in a ASCII character
String tmpData;
  
#define BUFFER_SIZE 128
char buffer[BUFFER_SIZE];

//Define the needed header files for the precompiler, no charge if not used ----
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void setup() { //Setup runs once ***********************************************
  pinMode(LED_BUILTIN, OUTPUT);          //Arduino boards contain an onboard LED
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  Serial1.begin(9600);   //Nothing more needed for serial connection to function
  pinMode(M_NRSTpin, OUTPUT);  //Reset pin of ESP14, connected to digital output
  Serial.println("Welcome at ESP14 tester");
  clearSerialBuffer();
  hardReset();
    
  Serial.print("AT : ");                           //Test if the module is ready
  Serial.println(GetResponse("AT",100));
  delay(100);
  
  Serial.print(F("AT+CIFSR = IP address: "));
  Serial.println( GetResponse("AT+CIFSR", 100) );
  delay(100);

  Serial.print("AT : ");                           //Test if the module is ready
  Serial.println(GetResponse("AT",100));
  delay(100);
  
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************
  toggle_ledBin();                  //Toggles the default on-board LED on or off
  delay(1000);                             //You can test your own patience here
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


String GetResponse(String AT_Command, int wait){  //Send command and return data
  tmpData = "Test 1:";
  Serial1.println(AT_Command);
  delay(wait);
  while (Serial1.available() > 0 ) {
    inByte = Serial1.read();
    Serial.print(inByte, HEX);
    Serial.print("-");
    myChar = inByte;
    tmpData += myChar;
    //if ( tmpData.indexOf(AT_Command) > -1 )         
    //  tmpData = "";
    //else
    //  tmpData.trim();
    //}
  }
  return tmpData;
}

boolean hardReset() {
  Serial.println("- Hardware reset ESP14");
  digitalWrite(M_NRSTpin,LOW);
  delay(100);
  digitalWrite(M_NRSTpin,HIGH);
  delay(1000);
}

void readSerialInput(void) {   //Receive and proces data from the Serial Monitor
  if (Serial.available() > 0) {           //Send data only when you receive data
    inByte = Serial.read();                             //Read the incoming byte
  }//End of  if (Serial.available() > 0
} //End of SUBserialInput    Receives and processes data from the Serial Monitor


void clearSerialBuffer(void) {
  Serial.println("- Clear serial buffer ESP14");
  while ( Serial1.available() > 0 ) {
    Serial1.read();
  }
}

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 ---------------------------------------------------------


////////////////////////////////////////////////////////////////////////////////
// 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  - White cable to GY-521    TWI //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  - Yellow cable to GY-521   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       -                          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      -                          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       -                          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