////////////////////////////////////////////////////////////////////////////////
// Name:       Gateway01.ino                                                  //
// Platform:   Arduino UNO R3                                                 //
// Created by: HARB rboek2@gmail.com august 2020 GPL copyrights               //
// http://robotigs.nl/robots/includes/bots.php?idbot=32                       //
// Met een simpele setting in het programma wordt er 1 van beide de           //
// hoofdzender (ping) die wordt aangesloten op de seriele monitor. De andere  //
// transceiver (pong) wordt enkel op de voedingsspanning aangesloten. Door nu //
// een `T` commando te geven zal ping de huidige tijd naar pong sturen. Pong  //
// stuurt de ontvangen gegevens dan weer terug en ping laat zien hoe lang de  //
// hele cyclus heeft geduurd.                                                 //
// http://tmrh20.github.io/RF24/GettingStarted_8ino-example.html              //
////////////////////////////////////////////////////////////////////////////////
// As outputs the following modules are mounted:                              //
// - Standard Arduino Onboard LED (PWM)                                       //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=185 //
// - 3 color LED (PWM)                                                        //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=293 //
// - Activ loudspeaker / buzzer                                               //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=240 //
//                                                                            //
// As inputs the following modules are mounted:                               //
//                                                                            //
// For communications and statistics are mounted:                             //
// - Standard Serial Monitor output                                           //
//            http://robotigs.nl/robotigs/includes/parts_header.php?idpart=43 //
// - Lan ENC28J60 module                                                      //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=313 //
// - LoRa NRF24L01+ module                                                    //
//           http://robotigs.nl/robotigs/includes/parts_header.php?idpart=344 //
////////////////////////////////////////////////////////////////////////////////


  //Define the needed header files for the precompiler, no charge if not used --
  #include <SPI.h>      //Serial Peripheral Interface requiered by software LORA
                       // http://robotigs.nl/robots/includes/parts.php?idpart=28
  #include <nRF24L01.h>                               //Lijkt nergens voor nodig
  #include <RF24.h>    //TMRH20 Optimized high speed nRF24L01+ driver class LORA
                      // http://robotigs.nl/robots/includes/parts.php?idpart=344
  #include <printf.h>             //Needed to send to monitor radio.printDetails

  //Define PINS ----------------------------------------------------------------
  #define CE_PIN       10          //CE (Chip Enable) is an active-HIGH pin LORA
  #define CSN_PIN       9          //CSN (Chip Select Not) is an active-LOW LORA
  #define ledRedPin     3         //3 Colour LED, which PWM pin connects RED LED
  #define ledGrePin     5       //3 Colour LED, which PWM pin connects GREEN LED
  #define ledBluPin     6        //3 Colour LED, which PWM pin connects BLUE LED
  #define buzActPin     4          //Define DIO output pin connects ACTIV BUZZER
  
  //Define EEPROM variables ----------------------------------------------------
  //Define DATABASE VARIABLES --------------------------------------------------
  //Define variables -----------------------------------------------------------
  int    ledOnBoardVal  =   LOW; //You choose HIGH-on or LOW-off for LED_BUILTIN
  const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
  char dataReceived[10]; // this must match dataToSend in the TX
  bool newData = false;
  char tmp;
  unsigned long countOk, countNok;  //Counters for long term tests on stabillity

  //User configuration RF24 radio ----------------------------------------------
  bool radioNumber    = 1;     //Set this radio as radio number 1=Ping or 0=Pong
  bool role           = 0;                  //Sending=0 or receiving=1 role LORA
  byte addresses[][6] = {"1Node","2Node"};
  bool test           = false;

  //Initialize OBJECTS ---------------------------------------------------------
  RF24 radio(CE_PIN, CSN_PIN);                 //CE=10 CSN=9 Speed settings LORA
//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void setup() { //Setup runs once ***********************************************
  Serial.begin(115200);
  Serial.println("LoRa Gateway setup start Ping");
  Serial.println("");
  
  //pinMode(CE_PIN, OUTPUT);
  //pinMode(CSN_PIN, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  pinMode(ledRedPin, OUTPUT);                 //Set this pin as output to redLED
  pinMode(ledBluPin, OUTPUT);                //Set this pin as output to blueLED
  pinMode(ledGrePin, OUTPUT);               //Set this pin as output to greenLED
  pinMode(buzActPin, OUTPUT);                 //Set this pin as output to BUZZER
    
  radio.begin();

  test = radio.setDataRate(RF24_250KBPS);
  Serial.print("radio.setDataRate succeeded: ");
  Serial.println(test);
  Serial.println("");

  radio.setPALevel(RF24_PA_MIN);             //RF24_PA_MAX=default, HIGH LOW MIN
  //radio.setRetries(4,5);                                    //Delay=3, count=5

  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1,addresses[0]);
  radio.startListening();                  // Start the radio listening for data
  
  //Test hardware and software -------------------------------------------------
  Serial.println("Start Tests");                           //Show the user RS232
  printf_begin();        //Needed to print the radio.printDetails() to a monitor
  radio.printDetails();               //Set of data about the transceiver status
  //show_LAN();                                     //Shows DHCP settings of LAN
  test_LEDs();            //PWM fade in and fade out for all 4 LEDs on board LED
  Serial.println("Setup completed");     //Show the user the setup is done RS232
  beep(1);                        //Create a test beep with KY-012 active BUZZER
} //End of setup ---------------------------------------------------------------




void loop() { //KEEP ON RUNNING THIS LOOP FOREVER  *****************************
   pingout() ;                //Gateway sends out timestamp and waits for return
} //End of void loop() ----------------------- KEEP ON RUNNING THIS LOOP FOREVER




void pingout(void) { //Ping sends out timestamp and waits for return ***********
  radio.stopListening();                   //First stop listening so we can talk
  Serial.println("*** Now sending");            //Show the start of the sequence

  unsigned long start_time = micros();               //Read the time and send it
  if (!radio.write( &start_time, sizeof(unsigned long) )){
    Serial.println("    Error radio.write failed");                      //Error
  } //End of if (!radio.write( &start_time, sizeof(unsigned long) ))

  radio.startListening();                 //Start listening for answer from Pong
  
  unsigned long started_waiting_at = micros();        //Start timer microseconds
  boolean timeoutbool = false;         //Indicate if radio is receiving anything

    while (!radio.available()){                 //As long as nothing is received
      if (micros() - started_waiting_at > 200000 ){     //If waited longer 200ms
          timeoutbool = true;             //Then indicate timeout and exit while
          break;                            //Exit waiting in case of a time-out
      } //End of if (micros() - started_waiting_at > 200000 )
    } //End of while (!radio.available())

    if (timeoutbool){                                     //Describe the results
        countNok++;                                           //One more failure
        Serial.println("    Failed, response timed out.");
    }else{
        unsigned long got_time;
        radio.read( &got_time, sizeof(unsigned long) );//Read response from Pong
        unsigned long end_time = micros();
        countOk++;                           //One more succesfull communication
        Serial.print("    Sent ");
        Serial.print(start_time);
        Serial.print(", Got response ");
        Serial.print(got_time);
        Serial.print(", Round-trip delay ");
        Serial.print(end_time-start_time);     //Compare and send debugging spew
        Serial.println(" microseconds");
    }


  Serial.print("    Succesvol:");
  Serial.print(countOk);
  Serial.print("  Mislukkingen:");
  Serial.print(countNok);
  Serial.println("  Wacht 10 seconden");
  Serial.println("");
  delay(20000);     //Try again x seconds later
} //Exit pingout ---------------------------------------------------------------
//345678911234567892123456789312345678941234567895123456789612345678971234567898



void getData() {
  newData = false;
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
    if (newData == true) {
        Serial.print("Data received ");
        Serial.print(dataReceived);
        newData = false;
    } else {
        Serial.print("Nott received ");
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

void showChipConnected() {
  if (radio.isChipConnected()){
        Serial.println("CHP Connected");
  }else{
        Serial.println("Not connected");
  }
}







void test_LEDs(void){ //PWM fade in and fade out for all 4 LEDs on board *******
  byte brillance = 0;            //Brightness of any color, just to test PWM LED
  byte   msWait  = 1;                   //Test your patience during the test LED

  while (brillance<255){
    analogWrite(ledRedPin, brillance);        //Set LED to desired PWM value RED
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledRedPin, brillance);        //Set LED to desired PWM value RED
    brillance--;
    delay (msWait);
  }
  analogWrite(ledRedPin, 0);            //Set LED to desired PWM value = off RED

  while (brillance<255){
    analogWrite(ledGrePin, brillance);      //Set LED to desired PWM value GREEN
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledGrePin, brillance);      //Set LED to desired PWM value GREEN
    brillance--;
    delay (msWait);
  }
  analogWrite(ledGrePin, 0);          //Set LED to desired PWM value = off GREEN

  while (brillance<255){
    analogWrite(ledBluPin, brillance);       //Set LED to desired PWM value BLUE
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(ledBluPin, brillance);       //Set LED to desired PWM value BLUE
    brillance--;
    delay (msWait);
  }
  analogWrite(ledBluPin, 0);           //Set LED to desired PWM value = off BLUE

  while (brillance<255){
    analogWrite(LED_BUILTIN, brillance);  //Set to desired PWM value LED_BUILTIN
    brillance++;
    delay (msWait);
  }
  while (brillance>0){
    analogWrite(LED_BUILTIN, brillance);  //Set to desired PWM value LED_BUILTIN
    brillance--;
    delay (msWait);
  }
  analogWrite(LED_BUILTIN, 0);  //Set LED to desired PWM value = off LED_BUILTIN
} //Exit test_LEDs -------------------------------------------------------------





void showRadioSettings(void) { //Laat de instellingen zien op een monitor ******
  Serial.println("Radio instellingen ****************************************");
  Serial.println("radio.setRetries(4,5);  //(Delay*250us, Count) ieder max 15");
  Serial.println("radio.setPALevel(RF24_PA_MIN);  //RF24_PA_MAX, HIGH LOW MIN");
  Serial.println("radio.setDataRate(RF24_250KBPS);     //Langzaamste van de 3");
  Serial.println("");
} //Exit showRadioSettings -----------------------------------------------------




void beep(uint8_t ms) {      //Create a beep (x5ms) with KY-012 active BUZZER **
  digitalWrite(buzActPin,HIGH);                                 //Turn on BUZZER
  while (ms > 0){                     //Timer of the duration of the beep BUZZER
    delay(5);                                         //Wait milliseconds BUZZER
    ms--;                              //Countdown untill we reached zero BUZZER
  }                 //Timer of the duration has been counted down to zero BUZZER
  digitalWrite(buzActPin,LOW);                  //Turn annoying sound off BUZZER
} //Exit beep ------------------------------------------------------------------



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




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



////////////////////////////////////////////////////////////////////////////////
// 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  -                           NC //
// SDA   -  27 - PC4 -18/A4- ADC4/SDA/PCINT12  -                           NC //
// AREF  -  21 - REF -     - AREF              -                              //
// GND   -  22 - GND -     - GND               -                              //
// 13    -  19 - PB5 -  13 - SCK/PCINT5        - SCK LoRa groen  BUILT_IN SPI //
// 12    -  18 - PB4 -  12 - MISO/PCINT4       - MISO LoRa paars          SPI //
// ~11   -  17 - PB3 -  11 - MOSI/OC2A/PCINT3  - MOSI Lora blauw      PWM SPI //
// ~10   -  16 - PB2 -  10 - SS/OC1B/PCINT2    - CE LoRa geel         PWM SPI //
// ~9    -  15 - PB1 -   9 - OC1A/PCINT1       - CSN LoRa oranje          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 - Blauwe led               PWM //
// ~5    -  11 - PD5 -   5 - PCINT21/OC0B/T1   - Groene led               PWM //
// 4     -   6 - PD4 -   4 - PCINT20/XCK/T0    - Buzzer                   INT //
// ~3    -   5 - PD3 -   3 - PCINT19/OC2B/INT1 - Rode led                 PWM //
// ~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               -                          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 //
////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////
// 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=0)         //
// 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  //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898