LoRa_APRS_iGate/src/Lora_APRS_iGate_DIY.cpp

137 lines
4.2 KiB
C++
Raw Normal View History

2023-02-10 11:56:22 +01:00
#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include "iGate_config.h"
2023-02-10 12:31:56 +01:00
#include "pins_config.h"
2023-02-10 11:56:22 +01:00
WiFiClient espClient;
2023-02-10 20:18:50 +01:00
uint32_t lastTxTime = 0;
2023-02-10 11:56:22 +01:00
void setup_lora() {
Serial.println("Set LoRa pins!");
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
long freq = 433775000;
Serial.print("frequency: ");
Serial.println(String(freq));
if (!LoRa.begin(freq)) {
Serial.println("Starting LoRa failed!");
while (true) {
}
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125000);
LoRa.setCodingRate4(5);
LoRa.enableCrc();
LoRa.setTxPower(20);
2023-02-10 12:31:56 +01:00
Serial.println("LoRa init done!\n");
2023-02-10 11:56:22 +01:00
}
void setup_wifi() {
2023-02-11 21:05:49 +01:00
int status = WL_IDLE_STATUS;
2023-02-10 11:56:22 +01:00
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("\nConnecting to '"); Serial.print(WIFI_SSID); Serial.println("' WiFi ...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.print("Connected as ");
Serial.println(WiFi.localIP());
}
2023-02-10 12:39:12 +01:00
void connect_and_upload_to_APRS_IS(String packet) {
2023-02-10 11:56:22 +01:00
int count = 0;
String aprsauth;
Serial.println("Conectando a APRS-IS");
while (!espClient.connect(SERVER.c_str(), APRSPORT) && count < 20) {
Serial.println("Didn't connect with server: " + String(SERVER) + " APRSPORT: " + String(APRSPORT));
delay(1000);
espClient.stop();
espClient.flush();
Serial.println("Run client.stop");
Serial.println("Trying to connect with server: " + String(SERVER) + " APRSPORT: " + String(APRSPORT));
count++;
Serial.println("Try: " + String(count));
}
if (count == 20) {
Serial.println("Tried: " + String(count) + " don't send the packet!");
} else {
Serial.println("Connected with server: " + String(SERVER) + " APRSPORT: " + String(APRSPORT));
while (espClient.connected()) {
2023-02-11 20:47:08 +01:00
delay(200);
aprsauth = "user " + iGate_Callsign + " pass " + passcode_igate + "\n"; //info igate
2023-02-10 12:39:12 +01:00
espClient.write(aprsauth.c_str()); //Serial.println("Run client.connected()");
2023-02-11 20:47:08 +01:00
delay(200);
2023-02-10 12:39:12 +01:00
espClient.write(packet.c_str()); //Serial.println("Send client.write=" + aprsauth);
2023-02-11 20:47:08 +01:00
delay(200); //Serial.println("Send espClient.write = " + packet_para_APRS_IS);
2023-02-10 11:56:22 +01:00
Serial.println("Packet uploaded =)\n");
espClient.stop();
2023-02-10 12:39:12 +01:00
espClient.flush(); //Serial.println("(Telnet client disconnect)\n");
2023-02-10 11:56:22 +01:00
}
}
}
2023-02-10 12:39:12 +01:00
void procesa_y_sube_APRS_IS(String mensaje) {
String packet_para_APRS_IS = "";
String callsign_y_path_tracker = "";
String payload_tracker;
int posicion_dos_puntos = mensaje.indexOf(':');
callsign_y_path_tracker = mensaje.substring(3, posicion_dos_puntos);
payload_tracker = mensaje.substring(posicion_dos_puntos);
2023-02-11 20:47:08 +01:00
packet_para_APRS_IS = callsign_y_path_tracker + ",qAO," + iGate_Callsign + payload_tracker + "\n";
2023-02-10 12:39:12 +01:00
//Serial.print("Mensaje APRS_IS : "); Serial.println(packet_para_APRS_IS);
//packet = "CD2RXU-9>APLT00,qAO,CD2RXU-10:=" + LAT + "/" + LON + ">" + "\n"; // ejemplo!!!
connect_and_upload_to_APRS_IS(packet_para_APRS_IS);
}
2023-02-10 11:56:22 +01:00
void valida_y_procesa_packet(String mensaje) {
String packetStart = "";
Serial.print("MENSAJE RECIBIDO!!! ");
Serial.print("(Validando inicio ---> ");
packetStart = mensaje.substring(0, 3);
if (packetStart == "\x3c\xff\x01") {
Serial.println("Packet Valido)");
procesa_y_sube_APRS_IS(mensaje);
} else {
Serial.println("Packet NO Valido)");
}
}
2023-02-10 12:39:12 +01:00
void setup() {
Serial.begin(115200);
setup_wifi();
btStop();
setup_lora();
Serial.println("Starting iGate\n");
}
2023-02-10 11:56:22 +01:00
void loop() {
2023-02-11 21:05:49 +01:00
String receivedPacket = "";
2023-02-10 20:18:50 +01:00
uint32_t lastTx = millis() - lastTxTime;
2023-02-11 20:47:08 +01:00
bool beacon_update = true;
2023-02-10 11:56:22 +01:00
int packetSize = LoRa.parsePacket();
if (packetSize) {
while (LoRa.available()) {
int inChar = LoRa.read();
2023-02-11 21:05:49 +01:00
receivedPacket += (char)inChar;
2023-02-10 11:56:22 +01:00
}
2023-02-11 21:05:49 +01:00
valida_y_procesa_packet(receivedPacket); //Serial.println("Mensaje Recibido : " + String(receivedPacket));
2023-02-10 11:56:22 +01:00
}
2023-02-11 20:47:08 +01:00
2023-02-11 21:05:49 +01:00
if (lastTx >= BeaconInterval) {
2023-02-11 20:47:08 +01:00
beacon_update = true;
}
if (beacon_update) {
2023-02-10 20:18:50 +01:00
Serial.println("enviando Beacon Estacion/iGate");
2023-02-11 21:05:49 +01:00
connect_and_upload_to_APRS_IS(iGateBeaconPacket);
2023-02-10 20:18:50 +01:00
lastTxTime = millis();
2023-02-11 20:47:08 +01:00
beacon_update = false;
}
2023-02-10 11:56:22 +01:00
}