main setup:
an esp32 board connected through two pins to the dx lr02 mini board which is connected to a lora antenna.
On the other hand we have another dxlr02 mini board with the lora antenna, connected to a dxsmart mini board which is then conected via serial to a computer. Where we can open the serial monitor and make at+ commands.
we tested this:
1. if we forget about the esp32 board and connect both lora antennas via the mini dx smart boards, to two computers (each antenna to one) we have no problem in sending a receiving messages both ways.
2. if we now connect one of the antennas to an arduino board, still everything both perfecty we receive and send messages without problem.
problem:
if instead of arduino we use esp32, no matter which pins we select for the lora or gps connexion, it doesnt work. THe most we receive is a unique message of "message received" followed by a single weird symbol. And then, no other message every. we have to recompile and resend the program to the board to receive the weird symbol again.
Here is our code for the esp32 board:
(we tried with pins also 16 and 17 for lora, change uart 1 to uart2...) we don't know anymore if its us or the board that doesn't work.
// LORA 14 RECEPTOR CONSOLIDADO. PRUEVAS
#include <TinyGPS++.h>
#include <HardwareSerial.h>
// =================== CONFIG PINS ===================
// UART para LoRa
#define LORA_RX 19 // ESP32 RX conectado al TX del LoRa
#define LORA_TX 22 // ESP32 TX conectado al RX del LoRa
// UART para GPS
#define GPS_RX 16 // ESP32 RX conectado al TX del GPS
#define GPS_TX 17 // ESP32 TX conectado al RX del GPS
#define GPSBaud 9600
// Voltímetro (canal ADC)
#define VoltimetroinPin 22 // GPIO22 es ADC
int pmwOutPin ; // value PMW salida de voltaje
int voltimetrout;
// RELE PILA ACCESORIA canal
//#define ReleOutPin 14 // GPIO14 es ADC
bool ReleOutPin = false;
// Pines de motores (salida PWM)
#define entrada1 33 //GPIO33
#define entrada2 25 //GPIO25
#define entrada3 26 //GPIO26
#define entrada4 27 //GPIO15
// =================== VARIABLES ===================
// Serial por hardware conexion
HardwareSerial loraSerial(1);
HardwareSerial gpsSerial(2);
// GPS OBJETO
TinyGPSPlus gps;
// Destino prefijado
#define des_LAT 41.42262
#define des_LNG 4.20644
String InstGral = "0";
// Canales PWM
int canal1 = 0; //canal PWM 1
int canal2 = 1;
int canal3 = 2; //canal PWM 2
int canal4 = 3;
/*int canal5 = 4; // canal de voltimetro
int canal6 = 5; // canal de Relé*/
// Valores PWM motores
int pwmValue1 = 0;
int pwmValue2 = 0;
int pwmValue3 = 0;
int pwmValue4 = 0;
//================AÑADIDOS DEL PROVA 10============
// potencies enviades per lemisor, de cada motor: motor1,motor2
int potmot1; // QUE SON LAS POTENTVAL1 Y 2 DEL EMISOR
int potmot2;
unsigned long lastUpdateTime = 0;
unsigned long lastUpdateTiempo = 0;
int intEnviat;
// VARIABLES PARA GUARDAR COORDENADAS DE SALIDA
float latitudInicial = 0.0;
float longitudInicial = 0.0;
bool coordenadesGuardades = false;
void setup() {
Serial.begin(9600);
// Inicialización LoRa en UART1
loraSerial.begin(9600, SERIAL_8N1, LORA_RX, LORA_TX);
// Inicialización GPS en UART2
gpsSerial.begin(GPSBaud, SERIAL_8N1, GPS_RX, GPS_TX);
pinMode(entrada1, OUTPUT); // salida a motor n1, PMW 1
pinMode(entrada2, OUTPUT); // salida DIR1 a motor n1 , reward
pinMode(entrada3, OUTPUT); // salida a motor n2, PMW 2
pinMode(entrada4, OUTPUT); // salida DIR 2 a motor n2, reward
// ENTRADA DE VOLTIMETRO.
pinMode(VoltimetroinPin, INPUT); // ENTRADA VOLTIMETRO PIN 6
pinMode(ReleOutPin,OUTPUT);
// Configuración PWM
// ledcSetup(canal1, 5000, 8); // canal, frecuencia, resolución (8 bits = 0-255)
// ledcSetup(canal2, 5000, 8);
// ledcSetup(canal3, 5000, 8);
// ledcSetup(canal4, 5000, 8);
// ledcAttachPin(entrada1, canal1);
// ledcAttachPin(entrada2, canal2);
// ledcAttachPin(entrada3, canal3);
// ledcAttachPin(entrada4, canal4);
ledcAttach(entrada1, 5000, 8);
ledcAttach(entrada2, 5000, 8);
ledcAttach(entrada3, 5000, 8);
ledcAttach(entrada4, 5000, 8);
/*ledcAttach(VoltimetroinPin,5000, 10);
ledcAttach(pmwRelePin;5000,10);*/
Serial.println("ESP32 listo con LoRa + GPS + PWM + Voltímetro");
}
void loop() {
// =================== LoRa Comunicación ===================
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
loraSerial.println(cmd);
}
/* if (loraSerial.available()) {
String resposta = loraSerial.readStringUntil('\n');
resposta.trim();
InstGral = resposta;
Serial.print("Instrucción general: ");
Serial.println(resposta);
}*/
// Si el mòdul contesta, ho mostrem al monitor sèrie
if (loraSerial.available()) {
Serial.println(" serial lora disponible: ");
String resposta = loraSerial.readStringUntil('\n');
resposta.trim();
InstGral = String(resposta); // InstGral ES UN STRING
Serial.print("Instruccio general: "); //7 nuevo sustitut linea 121
Serial.println(resposta); // Mostra el string rebut
if(resposta == "1"|| resposta == "2"|| resposta == "3"|| resposta == "4" || resposta == "5" || resposta == "6" )
// RESPOSTA "1"ES CONTROL MANUAL;"2" IRA DESTINO CON GPS;"3" IR A PUNTO ORIGEN;"4" ENCENDER LED; ECT
{
Serial.print("Canvi en Instruccio general: ");
InstGral = resposta;
Serial.println(InstGral); // CAMVI DE RESPOSTA VARIABLE A INSTGRAL
delay(50);
}
if(InstGral != "0")
{
if( InstGral=="1") // si el caracter es el numero 1
{
Serial.println("hem entrat en 1");
//CONTROL MOTORES CON POTENCIOMETROS MANUAL DESDE TIERRA
pwmValue1 = 0;
pwmValue2 = 0;
pwmValue3 = 0;
pwmValue4 = 0;
//Espera aquí fins que arribi una nova línia
while (!loraSerial.available())
{
// Opcionalment pots fer un timeout o un LED parpellejant aquí
}
// Quan arriba, llegeix els valors
String valors = loraSerial.readStringUntil('\n');
valors.trim();
int separador = valors.indexOf(' ');
if (separador != -1) {
potmot1 = valors.substring(0, separador).toInt();
potmot2 = valors.substring(separador + 1).toInt();
Serial.print("Rebut valors: ");
Serial.print(potmot1); Serial.print(" i "); Serial.println(potmot2);
//POTMOT1 Y 2 ES POTENCIA DE MOTORES
// ara pots fer ledcWrite/ledcWrite amb aquests valors
} else {
Serial.println("Format incorrecte.");
}
// anem cap endavant motor dreta, motor numero 1
if(potmot1 >=1 && potmot1 <=300) {
Serial.println("motor dreta endavant");
// Map the potentiometer value to PWM range (0 to 250 forward motor 1 y 2)
pwmValue1 = map(potmot1, 1, 300, 0, 300); // PRIMER MOTOR MOSET
}
// anem cap endarrere motor dreta
if(potmot1 >= 301 && potmot1 <= 400) {
Serial.println("motor dreta endarrere");
// Map pot enciometer value PWM range 191 to 250 backward motor 1 y 2 )
pwmValue2 = map(potmot1, 301, 400, 0, 100);
}
// anem cap endavant motor esquerra
if(potmot2 >= 10 && potmot2 <= 300) {
// Map the potentiometer value to PWM range (0 to 190 forward motor 1 y 2)
pwmValue3 = map(potmot2, 1, 300, 0, 300); // SEGUNDO MOTOR MOSET
}
// anem cap endarrere motor esquerra
if(potmot2 >=301 && potmot2 <= 400) {
// Map potenciometer value PWM range 191 to 250 backward motor 1 y 2 )
pwmValue4 = map(potmot2, 301, 400, 0, 100);
}
// ENTRADAS A MOTORES
ledcWrite(canal1, pwmValue1); // PIN 33, Pwm potenciometro 1
ledcWrite(canal2, pwmValue2); // PIN 25, poten 1, INVERSOR
ledcWrite(canal3, pwmValue3); // PIN 26, Pwm potenciometro 2
ledcWrite(canal4, pwmValue4); // PIN 27, poten 2, INVERSOR
// INFORMACION DE LA MARCHA DEL BARCO.
Serial.println("canal1, PIN 33, ADELANTE: "); Serial.print("POTENCIA1: ");
Serial.println(pwmValue1);
delay (30);
Serial.println("canal2, PIN 25, ATRAS: ");Serial.print("POTENCIA2: ");
Serial.println(pwmValue2);
delay (30);
Serial.println("canal3, PIN 26, ADELANTE: ");Serial.print("POTENCIA3: ");
Serial.println(pwmValue3);
delay (30);
Serial.println("canal4,PIN 27, ATRAS: ");Serial.print("POTENCIA4: ");
Serial.println(pwmValue4);
delay (30);
if (InstGral=="2")
// si 2, GPS IR A DESTINO PREFIJADO
{
// If any characters have arrived from the GPS,
// send them to the TinyGPS++ object
Serial.println("Hem Entrat En: 2, ir a destino prefijado");
while (gpsSerial.available() > 0)
gps.encode(gpsSerial.read());
// Every 7 seconds, do update.
// +++++++++++para actualizar desde el principio
if (millis() - lastUpdateTime >= 7000)
// SI VA A 15KM-H, SE ACTUALIZA CADA 28 METROS
{
lastUpdateTime = millis();
Serial.println();
// Establish our current status
double distanceToDestination = TinyGPSPlus::distanceBetween(gps.location.lat(), gps.location.lng(), des_LAT, des_LNG);
double courseToDestination = TinyGPSPlus::courseTo(gps.location.lat(), gps.location.lng(), des_LAT, des_LNG);
const char *directionToDestination = TinyGPSPlus::cardinal(courseToDestination);
int courseChangeNeeded = (int)(360 + courseToDestination - gps.course.deg()) % 360;
// CAMBIO DE CURSO DE NAVEGACION PARA LLEGAR A UN DESTINO QUE ES DES_LAT DES_LNG
// debug
Serial.print("Rumbo hacia la meta: ");
Serial.print(courseToDestination);
Serial.print(" Rumbo actual: ");
Serial.println(gps.course.deg());
Serial.print(" Cardinal E,O,N,S: ");
Serial.print(directionToDestination);
Serial.print(" Cambio necesario de rumbo: "); // CURSO A TOMAR PARA EL DESTINO
Serial.print(courseChangeNeeded);
Serial.print(" CurSpd: ");
Serial.println(gps.speed.kmph());
delay(5000);
float l = gps.location.lat();
//float 2 = gps.location.lng();
Serial.println();
Serial.print("Lat: "); Serial.print(l,6); Serial.print(" Lon: "); Serial.println(gps.location.lng(),6);
Serial.print(" Angulo a meta: "); Serial.println(atan2(gps.location.lat(), gps.location.lng())*180/M_PI);
// Within 20 meters of destination? We're here!
if (distanceToDestination <= 20.0)
{
Serial.println("CONGRATULATIONS: You've arrived!");
exit(1);
}
Serial.print("DISTANCE PARA LLEGAR: ");
Serial.print(distanceToDestination);
Serial.println(" meters to go.");
Serial.print("INSTRUCTION: ");
// Standing still? Just indicate which direction to go.
if (gps.speed.kmph() > 2.0)
{
Serial.print("DIRECCION HACIA DESTINO ");
Serial.print(directionToDestination);
Serial.println(".");
delay(100);
//return;
} // CURSO NECESARIO PARA LLEGAR AL DESTINO
if (courseChangeNeeded >= 345 || courseChangeNeeded < 15)
{
Serial.println("Keep on straight ahead!");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 315 && courseChangeNeeded < 345)
{
Serial.println("Veer slightly to the left.");
ledcWrite(canal1, 160); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 15 && courseChangeNeeded < 45)
{
Serial.println("Veer slightly to the right.");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 160); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 255 && courseChangeNeeded < 315)
{
Serial.println("Turn to the left.");
ledcWrite(canal1, 130); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 45 && courseChangeNeeded < 105)
{
Serial.println("Turn to the right.");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 130); // PIN 9, poten 1, MOTOR2
}
else
{
Serial.println("Turn completely around.");
ledcWrite(canal1, 180); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 60); // PIN 9, poten 1, MOTOR2
}
delay(100);
}
}
if(InstGral=="3") // si el caracter es el numero 3
{
// VUELTA A PUNTO DE ORIGEN CON GPS POR MANADATO DE TIERRA
// Every 5 seconds, do an update.
Serial.println("Hem Entrat En: 3 , ir a Punto de Salida");
if (millis() - lastUpdateTime >= 5000)
{
lastUpdateTime = millis();
Serial.println();
if (gps.location.isValid() && !coordenadesGuardades) {
latitudInicial = gps.location.lat();
longitudInicial = gps.location.lng();
coordenadesGuardades = true;
Serial.println("Coordenades inicials guardades!");
}
// Establish our current status
double distanceToDestination = TinyGPSPlus::distanceBetween(gps.location.lat(), gps.location.lng(),latitudInicial, longitudInicial);
double courseToDestination = TinyGPSPlus::courseTo(gps.location.lat(), gps.location.lng(), latitudInicial, longitudInicial);
const char *directionToDestination = TinyGPSPlus::cardinal(courseToDestination);
int courseChangeNeeded = (int)(360 + courseToDestination - gps.course.deg()) % 360;
// debug
Serial.print("DEBUG: Course2Dest: ");
Serial.print(courseToDestination);
Serial.print(" CurCourse: ");
Serial.print(gps.course.deg());
Serial.print(" Dir2Dest: ");
Serial.print(directionToDestination);
Serial.print(" RealCourse: ");
Serial.print(courseChangeNeeded);
Serial.print(" CurSpd: ");
Serial.println(gps.speed.kmph());
float l = gps.location.lat();
Serial.println();
Serial.print("Lat: "); Serial.print(l,6); Serial.print(" Lon: "); Serial.println(gps.location.lng());
Serial.print("current Angle: "); Serial.println(atan2(gps.location.lat(), gps.location.lng())*180/M_PI);
// Within 20 meters of destination? We're here!
if (distanceToDestination <= 20.0)
{
Serial.println("CONGRATULATIONS: You've arrived!");
exit(1);
}
Serial.print("DISTANCE: ");
Serial.print(distanceToDestination);
Serial.println(" meters to go.");
Serial.print("INSTRUCTION: ");
// Standing still? Just indicate which direction to go.
if (gps.speed.kmph() > 2.0)
{
Serial.print("Head ");
Serial.print(directionToDestination);
Serial.println(".");
delay(100);
//return;
}
if (courseChangeNeeded >= 345 || courseChangeNeeded < 15)
{
Serial.println("Keep on straight ahead!");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 315 && courseChangeNeeded < 345)
{
Serial.println("Veer slightly to the left.");
ledcWrite(canal1, 160); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 15 && courseChangeNeeded < 45)
{
Serial.println("Veer slightly to the right.");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 160); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 255 && courseChangeNeeded < 315)
{
Serial.println("Turn to the left.");
ledcWrite(canal1, 130); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 200); // PIN 9, poten 1, MOTOR2
}
else if (courseChangeNeeded >= 45 && courseChangeNeeded < 105)
{
Serial.println("Turn to the right.");
ledcWrite(canal1, 200); // PIN 5, poten 1, MOTOR1
ledcWrite(canal2, 130); // PIN 9, poten 1, MOTOR2
}
else
{
Serial.println("Turn completely around.");
ledcWrite(canal1, 60); // PIN 5, poten 1, MOTOR1
ledcWrite(canal3, 180); // PIN 9, poten 1, MOTOR2
}
delay(100);
}
}
}
}
}
// SI SE PIERDE LA COMUNICACION VOLVER A SITIO INICIO AUTOMATICAMENTE
// int lastUpdateTiempo=0;
if (millis() - lastUpdateTiempo >= 30000) {
lastUpdateTiempo = millis(); }
//lastUpdateTiempo = 0;
if (!loraSerial.available()&& lastUpdateTiempo >=29000 )
{ /*Serial.println(" No comunicacion, volvemos a sitio INICIO EN: 29 segundos" ); */
}
// =================== GPS Lectura ===================
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
if (gps.location.isUpdated()) {
Serial.print("Latitud: "); Serial.println(gps.location.lat(), 6);
Serial.print("Longitud: "); Serial.println(gps.location.lng(), 6);
delay(5000);
}
// =================== Motores PWM (ejemplo simple) ===================
/* Aquí pondrías tu lógica real
pwmValue1 = 0;
pwmValue2 = 0;
pwmValue3 = 0;
pwmValue4 = 0;
ledcWrite(canal1, pwmValue1);
ledcWrite(canal2, pwmValue2);
ledcWrite(canal3, pwmValue3);
ledcWrite(canal4, pwmValue4);
delay(1000);*/
// =================== Voltímetro ===================
pmwOutPin = analogRead(VoltimetroinPin); // ADC de 0-4095
voltimetrout = map(pmwOutPin, 0, 4095, 0, 13); // Escalar a voltios (ejemplo)
Serial.print("Voltaje pila central: ");
Serial.print(voltimetrout);
Serial.println(" V");
if (voltimetrout <= 9) {
Serial.println("
delay(1000);
}
//=============Relé obertura de pila secundaria==============
if (voltimetrout <= 9) {
ReleOutPin = true;
Serial.println("
delay(1000);
}
}