2023-03-02 12:51:38 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <SPI.h>
|
|
|
|
|
#include <LoRa.h>
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
#include "iGate_config.h"
|
|
|
|
|
#include "pins_config.h"
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
#define VERSION "V.0.1.0"
|
2023-03-02 12:51:38 +01:00
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
WiFiClient espClient;
|
|
|
|
|
uint32_t lastTxTime = 0;
|
|
|
|
|
static bool beacon_update = true;
|
|
|
|
|
unsigned long previousWiFiMillis = 0;
|
2023-03-02 12:51:38 +01:00
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
void setup_wifi() {
|
|
|
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
|
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-03-02 12:51:38 +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);
|
|
|
|
|
Serial.println("LoRa init done!\n");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
void APRS_IS_connect(){
|
2023-03-02 12:51:38 +01:00
|
|
|
int count = 0;
|
|
|
|
|
String aprsauth;
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Connecting to APRS-IS ...");
|
|
|
|
|
while (!espClient.connect(AprsServer.c_str(), AprsServerPort) && count < 20) {
|
|
|
|
|
Serial.println("Didn't connect with server...");
|
2023-03-02 12:51:38 +01:00
|
|
|
delay(1000);
|
|
|
|
|
espClient.stop();
|
|
|
|
|
espClient.flush();
|
|
|
|
|
Serial.println("Run client.stop");
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Trying to connect with Server: " + String(AprsServer) + " AprsServerPort: " + String(AprsServerPort));
|
2023-03-02 12:51:38 +01:00
|
|
|
count++;
|
|
|
|
|
Serial.println("Try: " + String(count));
|
|
|
|
|
}
|
|
|
|
|
if (count == 20) {
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Tried: " + String(count) + " FAILED!");
|
2023-03-02 12:51:38 +01:00
|
|
|
} else {
|
2023-03-03 00:40:59 +01:00
|
|
|
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);
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
String process_packet(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("APRS_IS Message : "); Serial.println(processedPacket);
|
|
|
|
|
return processedPacket;
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
void validate_lora_packet(String packet) {
|
|
|
|
|
String packetStart, aprsPacket;
|
|
|
|
|
packetStart = packet.substring(0, 3);
|
2023-03-02 12:51:38 +01:00
|
|
|
if (packetStart == "\x3c\xff\x01") {
|
2023-03-03 01:40:39 +01:00
|
|
|
Serial.println(" ---> Valid LoRa Packet!");
|
2023-03-03 00:40:59 +01:00
|
|
|
aprsPacket = process_packet(packet);
|
2023-03-03 01:40:39 +01:00
|
|
|
espClient.write(aprsPacket.c_str());
|
2023-03-02 12:51:38 +01:00
|
|
|
} else {
|
2023-03-03 01:40:39 +01:00
|
|
|
Serial.println(" ---> Not Valid LoRa Packet (Ignore)");
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
setup_wifi();
|
|
|
|
|
btStop();
|
|
|
|
|
setup_lora();
|
2023-03-03 00:40:59 +01:00
|
|
|
Serial.println("Starting iGate: " + iGateCallsign + "\n");
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
2023-03-03 00:40:59 +01:00
|
|
|
unsigned long currentWiFiMillis = millis();
|
|
|
|
|
|
|
|
|
|
if ((WiFi.status() != WL_CONNECTED) && (currentWiFiMillis - previousWiFiMillis >= WifiCheckInterval)) { // if WiFi is down, try reconnecting
|
|
|
|
|
Serial.print(millis());
|
|
|
|
|
Serial.println("Reconnecting to WiFi...");
|
|
|
|
|
WiFi.disconnect();
|
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
previousWiFiMillis = currentWiFiMillis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!espClient.connected()) {
|
|
|
|
|
APRS_IS_connect();
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
2023-03-03 00:40:59 +01:00
|
|
|
|
|
|
|
|
while (espClient.connected()) {
|
2023-03-03 01:40:39 +01:00
|
|
|
//while (espClient.available()) {
|
2023-03-03 00:40:59 +01:00
|
|
|
|
2023-03-03 01:40:39 +01:00
|
|
|
uint32_t lastTx = millis() - lastTxTime;
|
|
|
|
|
if (lastTx >= BeaconInterval) {
|
|
|
|
|
beacon_update = true;
|
|
|
|
|
}
|
|
|
|
|
if (beacon_update) {
|
|
|
|
|
Serial.println("---- Sending WeatherReport Beacon ----");
|
|
|
|
|
espClient.write(iGateBeaconPacket.c_str());
|
|
|
|
|
lastTxTime = millis();
|
|
|
|
|
beacon_update = false;
|
|
|
|
|
}
|
2023-03-03 00:40:59 +01:00
|
|
|
|
2023-03-03 01:40:39 +01:00
|
|
|
String loraPacket = "";
|
|
|
|
|
int packetSize = LoRa.parsePacket();
|
|
|
|
|
if (packetSize) {
|
|
|
|
|
while (LoRa.available()) {
|
|
|
|
|
int inChar = LoRa.read();
|
|
|
|
|
loraPacket += (char)inChar;
|
2023-03-03 00:40:59 +01:00
|
|
|
}
|
2023-03-03 01:40:39 +01:00
|
|
|
Serial.print("\nReceived Lora Message : " + String(loraPacket));
|
|
|
|
|
validate_lora_packet(loraPacket);
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
2023-03-03 01:40:39 +01:00
|
|
|
|
|
|
|
|
/*delay(100);
|
|
|
|
|
if (espClient.available()) {
|
|
|
|
|
|
|
|
|
|
String aprsisData, aprsisPacket, subpacket1, receivedMessage, questioner, answerMessage, ackNumber, ackMessage, currentDate, weatherForecast;
|
|
|
|
|
|
|
|
|
|
aprsisData = espClient.readStringUntil('\n');
|
|
|
|
|
aprsisPacket.concat(aprsisData);
|
|
|
|
|
if (!aprsisPacket.startsWith("#")){
|
|
|
|
|
Serial.println("APRS MESSAGE : " + aprsisPacket);
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
/*
|
|
|
|
|
if (packet.indexOf("WRCLP") > 0){
|
|
|
|
|
if (packet.indexOf("::")>0) {
|
|
|
|
|
subpacket1 = packet.substring(packet.indexOf("::")+2);
|
|
|
|
|
receivedMessage = subpacket1.substring(subpacket1.indexOf(":")+1);
|
|
|
|
|
questioner = packet.substring(0,packet.indexOf(">"));
|
|
|
|
|
//Serial.println(receivedMessage);
|
|
|
|
|
|
|
|
|
|
if (receivedMessage.indexOf("{")>0) { // if questioner solicitates ack
|
|
|
|
|
ackNumber = receivedMessage.substring(receivedMessage.indexOf("{")+1);
|
|
|
|
|
for(int i = questioner.length(); i < 9; i++) {
|
|
|
|
|
questioner += ' ';
|
|
|
|
|
}
|
|
|
|
|
ackMessage = "WRCLP>APRS,TCPIP*,qAC,CHILE::" + questioner + ":ack" + ackNumber + "\n";
|
|
|
|
|
//Serial.print("---> " + ackMessage);
|
|
|
|
|
espClient.write(ackMessage.c_str());
|
|
|
|
|
delay(500);
|
|
|
|
|
receivedMessage = receivedMessage.substring(0,receivedMessage.indexOf("{"));
|
|
|
|
|
//Serial.println(receivedMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i = questioner.length(); i < 9; i++) {
|
|
|
|
|
questioner += ' ';
|
|
|
|
|
}
|
|
|
|
|
receivedMessage.trim();
|
|
|
|
|
if (receivedMessage == "utc" || receivedMessage == "Utc" || receivedMessage == "UTC" || receivedMessage == "time"|| receivedMessage == "Time" || receivedMessage == "TIME") {
|
|
|
|
|
currentDate = getDateTime();
|
|
|
|
|
answerMessage = "WRCLP>APRS,TCPIP*,qAC,CHILE::" + questioner + ":" + currentDate + "\n";
|
|
|
|
|
} else if (receivedMessage == "clima" || receivedMessage == "Clima" || receivedMessage == "CLIMA" || receivedMessage == "weather"|| receivedMessage == "Weather" || receivedMessage == "WEATHER") {
|
|
|
|
|
weatherForecast = GetWeatherForecast(questioner);
|
|
|
|
|
answerMessage = "WRCLP>APRS,TCPIP*,qAC,CHILE::" + questioner + ":" + weatherForecast + "\n";
|
|
|
|
|
} else {
|
|
|
|
|
answerMessage = "WRCLP>APRS,TCPIP*,qAC,CHILE::" + questioner + ":" + "hola " + questioner + "\n";
|
|
|
|
|
}
|
|
|
|
|
Serial.print("-------> " + answerMessage);
|
|
|
|
|
espClient.write(answerMessage.c_str());
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-03 00:40:59 +01:00
|
|
|
}
|
2023-03-02 12:51:38 +01:00
|
|
|
}
|
|
|
|
|
|