/*****************************************************************************
*                                                                            *
*      NOMBRE:      Ejemplo básico lectura serial.                           *
*      FECHA:       27 de enero de 2023.                                     *
*      VERSIÓN:     1.0.                                                     *
*                                                                            *
*      AUTOR:       Esli Castellanos Berján.                                 *
*      E-MAIL:      esli_castellanos@ucol.mx.                                *
*      COMPAÑÍA:    Universidad de Colima - Facultad de Telemática.          *
*                                                                            *
*      uC:          ESP32-D0WD Dual Core.                                    *
*      Nombre:      ESP32-S.                                                 *
*                                                                            *
******************************************************************************
*                                                                            *
*      DESCRIPCIÓN DEL PROGRAMA:                                             *
*      Arquitectura de software para leer información del puerto serial.     *
*                                                                            *
******************************************************************************/
#include <ArduinoJson.h>
#define BAUDRATE 115200
String temp;
void setup ( void ) {
  // put your setup code here, to run once:
  Serial.begin ( BAUDRATE );
  Serial.println ( F ( "Ejemplo lectura serial" ) );

}

void loop ( void ) {
  // put your main code here, to run repeatedly:
  if ( Serial.available ( ) ) {
leer_puerto_serial ( 2 );
JsonDocument doc;
deserializeJson(doc, temp);

const char* sensor = doc["sensor"];
long time          = doc["time"];
double latitude    = doc["data"][0];
double longitude   = doc["data"][1];
Serial.println(sensor);
Serial.println(time);
Serial.println(latitude);
Serial.println(longitude);
  }
      
    

}

void leer_puerto_serial ( uint8_t opcion ) {

  switch ( opcion ) {

    case 1 : 
      byte_x_byte ( ); break;

    case 2: string_completo ( ); break;

    case 3: transformar_texto_a_numero ( ); break;

  };

/*  Serial.available( ) devuelve la cantidad de bytes recibidos en el buffer serial */
  
  
  
  delay(10); // this speeds up the simulation

}

void byte_x_byte ( void ) {

      String mensaje = "";
      uint16_t nbyte = 0;

      while ( Serial.available ( ) != 0 ) {

        nbyte++;
        mensaje += (char)Serial.read ( ) ; 

      }

      Serial.println ( F ( "Cantidad de bytes recibidos: " ) );
      Serial.println( nbyte  );

      Serial.print ( "El mensaje recibido fue: " );
      Serial.println ( mensaje );
      mensaje = "";
      nbyte = 0;

}

void string_completo ( void ) {

  while ( Serial.available ( ) != 0 ) {

        Serial.print ( "El mensaje recibido fue: " );
        temp = Serial.readString();

      }

}

void transformar_texto_a_numero ( ) {

   while ( Serial.available ( ) != 0 ) {

        Serial.print ( "El mensaje recibido fue: " );
        Serial.println ( Serial.parseInt ( ) );
        // Serial.println ( Serial.parseInt ( SKIP_WHITESPACE, ' ' ) );

      }

}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23