This commit is contained in:
richonguzman 2023-03-26 09:12:27 -03:00
parent eaf170ede3
commit fd534ec533
5 changed files with 228 additions and 26 deletions

39
data/igate_conf.json Normal file
View File

@ -0,0 +1,39 @@
{
"callsign": "CD2RXU-11",
"wifi": {
"active": true,
"AP": [
{ "SSID": "Richon", "password": "k4fPnmg5qnyf"}
]
},
"beacon": {
"comment": "LoRa_APRS_iGate https://github.com/richonguzman/LoRa_APRS_iGate",
"positions": [
{ "latitude": -33.0337121, "longitude": -71.5738217}
],
"timeout": 15
},
"aprs_is": {
"active": true,
"passcode": "23201",
"server": "radioaficion.pro",
"port": 14580
},
"lora": {
"frequency_rx": 433775000,
"gain_rx": 0,
"frequency_tx": 433775000,
"power": 20,
"spreading_factor": 12,
"signal_bandwidth": 125000,
"coding_rate4": 5,
"tx_enable": false
},
"display": {
"always_on": true,
"timeout": 10,
"overwrite_pin": 0,
"turn180": true
},
"ntp_server": "pool.ntp.org"
}

View File

@ -4,21 +4,30 @@
#include <WiFi.h>
#include "iGate_config.h"
#include "pins_config.h"
#define VERSION "V.0.0.9" //BETA STILL
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "display.h"
WiFiClient espClient;
uint32_t lastTxTime = 0;
static bool beacon_update = true;
unsigned long previousWiFiMillis = 0;
static uint32_t lastRxTxTime = millis();
static bool displayEcoMode = true;
String firstLine, secondLine, thirdLine, fourthLine;
void load_config() {
Serial.println("cargando configuracion desde SPIFFS");
}
void setup_wifi() {
int status = WL_IDLE_STATUS;
Serial.print("\nConnecting to '"); Serial.print(WIFI_SSID); Serial.println("' WiFi ...");
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);
@ -28,13 +37,10 @@ void setup_wifi() {
}
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) {
@ -65,30 +71,39 @@ void APRS_IS_connect(){
if (count == 20) {
Serial.println("Tried: " + String(count) + " FAILED!");
} else {
Serial.println("Connected with Server: " + String(AprsServer) + " Port: " + String(AprsServerPort));
Serial.println("Connected with Server: '" + String(AprsServer) + "' (port: " + String(AprsServerPort)+ ")");
aprsauth = "user " + iGateCallsign + " pass " + iGatePasscode + " vers " + AprsSoftwareName + " " + AprsSoftwareVersion + " filter " + AprsFilter + "\n\r";
espClient.write(aprsauth.c_str());
delay(200);
}
}
String process_loraPacket(String unprocessedPacket) {
String createAPRSPacket(String unprocessedPacket) {
String callsign_and_path_tracker, payload_tracker, processedPacket;
int two_dots_position = unprocessedPacket.indexOf(':');
callsign_and_path_tracker = unprocessedPacket.substring(3, two_dots_position);
payload_tracker = unprocessedPacket.substring(two_dots_position);
processedPacket = callsign_and_path_tracker + ",qAO," + iGateCallsign + payload_tracker + "\n";
Serial.print("Message uploaded : "); Serial.println(processedPacket);
return processedPacket;
}
void validate_lora_packet(String packet) {
String packetStart, aprsPacket;
packetStart = packet.substring(0, 3);
if (packetStart == "\x3c\xff\x01") {
void validate_and_upload(String packet) {
String aprsPacket;
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.substring(4, 5) != "}")) {
Serial.println(" ---> Valid LoRa Packet!");
aprsPacket = process_loraPacket(packet);
aprsPacket = createAPRSPacket(packet);
display_toggle(true);
lastRxTxTime = millis();
espClient.write(aprsPacket.c_str());
Serial.print("Message uploaded : "); Serial.println(aprsPacket);
if (aprsPacket.indexOf("::") >= 10) {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " MESSAGE", 1000);
} else if (aprsPacket.indexOf(":>") >= 10) {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " NEW STATUS", 1000);
} else {
show_display("LoRa iGate: " + iGateCallsign, secondLine, String(aprsPacket.substring(0,aprsPacket.indexOf(">"))) + " GPS BEACON", 1000);
}
} else {
Serial.println(" ---> Not Valid LoRa Packet (Ignore)");
}
@ -105,13 +120,20 @@ String process_aprsisPacket(String aprsisMessage) {
void setup() {
Serial.begin(115200);
Serial.println("Starting iGate: " + iGateCallsign + "\n");
load_config();
setup_display();
setup_wifi();
btStop();
setup_lora();
Serial.println("Starting iGate: " + iGateCallsign + "\n");
}
void loop() {
void loop() {
String wifiState, aprsisState;
firstLine = "LoRa iGate: " + iGateCallsign;
secondLine = " ";
thirdLine = " ";
fourthLine = " ";
unsigned long currentWiFiMillis = millis();
if ((WiFi.status() != WL_CONNECTED) && (currentWiFiMillis - previousWiFiMillis >= WifiCheckInterval)) { // if WiFi is down, try reconnecting
@ -121,21 +143,39 @@ void loop() {
WiFi.reconnect();
previousWiFiMillis = currentWiFiMillis;
}
if (!espClient.connected()) {
APRS_IS_connect();
}
if (WiFi.status() == WL_CONNECTED) wifiState = "OK"; else wifiState = "--"; display_toggle(true); lastRxTxTime = millis();
if (espClient.connected()) aprsisState = "OK"; else aprsisState = "--"; display_toggle(true); lastRxTxTime = millis();
secondLine = "WiFi: " + wifiState + "/ APRS-IS: " + aprsisState;
show_display(firstLine, secondLine, thirdLine, fourthLine, 0);
while (espClient.connected()) {
uint32_t lastRxTx = millis() - lastRxTxTime;
if (displayEcoMode) {
if (lastRxTx >= EcoModeDisplayTime) {
display_toggle(false);
}
}
thirdLine = " ";
fourthLine = " ";
show_display(firstLine, secondLine, thirdLine, fourthLine, 0);
uint32_t lastTx = millis() - lastTxTime;
if (lastTx >= BeaconInterval) {
beacon_update = true;
}
if (beacon_update) {
if (beacon_update) {
display_toggle(true);
Serial.println("---- Sending iGate Beacon ----");
espClient.write(iGateBeaconPacket.c_str());
lastTxTime = millis();
display_toggle(true);
lastRxTxTime = millis();
show_display(firstLine, secondLine, thirdLine, "*SENDING iGate BEACON", 1000);
beacon_update = false;
}
@ -147,11 +187,11 @@ void loop() {
loraPacket += (char)inChar;
}
Serial.print("\nReceived Lora Message : " + String(loraPacket));
validate_lora_packet(loraPacket);
validate_and_upload(loraPacket);
}
if (espClient.available()) {
String aprsisData, aprsisPacket, newLoraMessage;
String aprsisData, aprsisPacket, newLoraMessage, Sender, AddresseAndMessage, Addressee, Message;
aprsisData = espClient.readStringUntil('\n');
aprsisPacket.concat(aprsisData);
if (!aprsisPacket.startsWith("#")){
@ -165,9 +205,15 @@ void loop() {
LoRa.write((const uint8_t *)newLoraMessage.c_str(), newLoraMessage.length());
LoRa.endPacket();
Serial.println("packet LoRa enviado!");
display_toggle(true);
lastRxTxTime = millis();
Sender = newLoraMessage.substring(1,newLoraMessage.indexOf(">"));
AddresseAndMessage = newLoraMessage.substring(newLoraMessage.indexOf("::")+2);
Addressee = AddresseAndMessage.substring(0, AddresseAndMessage.indexOf(":"));
Message = AddresseAndMessage.substring(AddresseAndMessage.indexOf(":")+1);
show_display(firstLine, secondLine, Sender + " --> " + Addressee, Message, 2000);
}
}
}
}
}
}

101
src/display.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "display.h"
#include "pins_config.h"
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// cppcheck-suppress unusedFunction
void setup_display() {
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(0);
display.setCursor(0, 0);
display.print("LORA iGate by CD2RXU");
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
}
// cppcheck-suppress unusedFunction
void display_toggle(bool toggle) {
if (toggle) {
display.ssd1306_command(SSD1306_DISPLAYON);
} else {
display.ssd1306_command(SSD1306_DISPLAYOFF);
}
}
// cppcheck-suppress unusedFunction
void show_display(String line1, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(line1);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String line1, String line2, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(line1);
display.setCursor(0, 8);
display.println(line2);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String line1, String line2, String line3, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(line1);
display.setCursor(0, 8);
display.println(line2);
display.setCursor(0, 16);
display.println(line3);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}
// cppcheck-suppress unusedFunction
void show_display(String line1, String line2, String line3, String line4, int wait) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(line1);
display.setCursor(0, 8);
display.println(line2);
display.setCursor(0, 16);
display.println(line3);
display.setCursor(0, 24);
display.println(line4);
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(1);
display.display();
delay(wait);
}

18
src/display.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef DISPLAY_H_
#define DISPLAY_H_
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
void setup_display();
void display_toggle(bool toggle);
void show_display(String line1, int wait = 0);
void show_display(String line1, String line2, int wait = 0);
void show_display(String line1, String line2, String line3, int wait = 0);
void show_display(String line1, String line2, String line3, String line4, int wait = 0);
#endif

View File

@ -3,16 +3,15 @@
#include <Arduino.h>
#define LORA_SCK 5 // GPIO5 - SX1276 SCK
#define LORA_MISO 19 // GPIO19 - SX1276 MISO
#define LORA_MOSI 27 // GPIO27 - SX1276 MOSI
#define LORA_CS 18 // GPIO18 - SX1276 CS ---> NSS
#define LORA_RST 14 // GPIO14 - SX1276 RST
#define LORA_RST 23 // GPIO14 - SX1276 RST
#define LORA_IRQ 26 // GPIO26 - SX1276 IRQ ---->DIO0
/*
SX1278-------------------> ESP32
SX1278-------------------> ESP32 TTGO LILYGO 2.1 or 1.6.1
GND GND
DIO1 -
DIO2 -
@ -27,5 +26,4 @@ REST 14
GND -
*/
#endif