From 1b9941f2fd17366f6f0c680f215189139acec330 Mon Sep 17 00:00:00 2001 From: richonguzman Date: Thu, 2 Mar 2023 08:51:38 -0300 Subject: [PATCH] firstBETA1 --- .DS_Store | Bin 6148 -> 6148 bytes platformio.ini | 4 +- src/ESP32_APRS_Weather_Report.cpp | 262 ------------------------------ src/LoRa_APRS_iGate_DIY.cpp | 140 ++++++++++++++++ src/config.h | 37 ----- src/iGate_config.h | 15 ++ src/pins_config.h | 31 ++++ 7 files changed, 189 insertions(+), 300 deletions(-) delete mode 100644 src/ESP32_APRS_Weather_Report.cpp create mode 100644 src/LoRa_APRS_iGate_DIY.cpp delete mode 100644 src/config.h create mode 100644 src/iGate_config.h create mode 100644 src/pins_config.h diff --git a/.DS_Store b/.DS_Store index 3f0e0f73652b444e42e6f537a3c65e5bd2302d97..1123d91804832582bac6178965f437a34dd92e1a 100644 GIT binary patch delta 31 ncmZoMXfc@J&nU4mU^g?P#AF_p>dpUIIG86ERBUGF_{$FfqS^{y delta 156 zcmZoMXfc@J&nUAoU^g?P%w!&x>UtIiJ%$2?OosfNbi?4}{M-TtAYihpDg{z-x!img zm!zEhB%l~ax|l(>?&%|rsPZXz;V diff --git a/platformio.ini b/platformio.ini index d96c6dc..5d794bd 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,6 +13,8 @@ platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 -lib_deps = bblanchon/ArduinoJson@^6.20.1 +lib_deps = + bblanchon/ArduinoJson@^6.20.1 + sandeepmistry/LoRa@^0.8.0 platform_packages = platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git diff --git a/src/ESP32_APRS_Weather_Report.cpp b/src/ESP32_APRS_Weather_Report.cpp deleted file mode 100644 index c3c34b1..0000000 --- a/src/ESP32_APRS_Weather_Report.cpp +++ /dev/null @@ -1,262 +0,0 @@ -#include -#include -#include -#include -#include "time.h" -#include "config.h" - -#define VERSION "V.0.0.9" // 2023.03.02 BETA - -WiFiClient espClient; -HTTPClient http; -uint32_t lastTxTime = 0; -static bool beacon_update = true; -unsigned long previousWiFiMillis = 0; - - -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()); -} - -void APRS_IS_connect(){ - int count = 0; - String aprsauth; - Serial.println("Connecting to APRS-IS ..."); - while (!espClient.connect(AprsServer.c_str(), AprsServerPort) && count < 20) { - Serial.println("Didn't connect with server..."); - delay(1000); - espClient.stop(); - espClient.flush(); - Serial.println("Run client.stop"); - Serial.println("Trying to connect with Server: " + String(AprsServer) + " AprsServerPort: " + String(AprsServerPort)); - count++; - Serial.println("Try: " + String(count)); - } - if (count == 20) { - Serial.println("Tried: " + String(count) + " FAILED!"); - } else { - Serial.println("Connected with Server: " + String(AprsServer) + " Port: " + String(AprsServerPort)); - aprsauth = "user " + WeatherReportCallsign + " pass " + WeatherReportPasscode + " vers " + AprsSoftwareName + " " + AprsSoftwareVersion + " filter " + AprsFilter + "\n\r"; - espClient.write(aprsauth.c_str()); - delay(200); - } -} - -String getDateTime() { - struct tm timeinfo; - String currentTime, year, month, day, hour, minute, seconds; - if(!getLocalTime(&timeinfo)){ - Serial.println("Failed to obtain time"); - return "no time info... restarting"; - ESP.restart(); - } - - year = (String)(timeinfo.tm_year + 1900); - - month = (String)(timeinfo.tm_mon+1); - if (month.length() == 1) { - month = "0" + month; - } - - day = (String)(timeinfo.tm_mday); - if (day.length() == 1) { - day = "0" + day; - } - - hour = (String)(timeinfo.tm_hour); - if (hour == 0) { - hour = "00"; - } else if (hour.length() == 1) { - hour = "0" + hour; - } - - minute = (String)(timeinfo.tm_min); - if (minute == 0) { - minute = "00"; - } else if (minute.length() == 1) { - minute = "0" + minute; - } - - seconds = (String)(timeinfo.tm_sec); - if (seconds == 0) { - seconds = "00"; - } else if (seconds.length() == 1) { - seconds = "0" + seconds; - } - - currentTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + seconds; - //Serial.println(currentTime); - return currentTime; -} - -String getWeatherForecast(String infoJson){ - StaticJsonDocument<700> doc; - DeserializationError error = deserializeJson(doc, infoJson); - if (error) { // Test if parsing succeeds. - Serial.print(F("deserializeJson() failed: ")); - Serial.println(error.f_str()); - return "No Location info for Callsign"; - } - String latitude = doc["entries"][0]["lat"]; - String longitude = doc["entries"][0]["lng"]; - latitude = latitude.substring(0,latitude.indexOf(".")+3); - longitude = longitude.substring(0,longitude.indexOf(".")+3); - - //openweather api key - String OpenWeatherApiKey; - OpenWeatherApiKey = "ac33704cb73d416103ede5e5d86aabd5"; - //OpenWeatherApiKey = "d732610ae742d5d2d057c10825e3e7eb"; - String request_info; - request_info = "https://api.openweathermap.org/data/2.5/weather?lat=" + (String)latitude + "&lon=" + (String)longitude + "&appid=" + OpenWeatherApiKey + "&units=metric"; - - String payload; - int httpResponseCode; - - http.begin(request_info.c_str()); - httpResponseCode = http.GET(); - if (httpResponseCode > 0) { // Serial.print("HTTP Response code: "); //Serial.println(httpResponseCode); - payload = http.getString(); // Serial.println(payload); - } else { // Serial.print("Error code: "); //Serial.println(httpResponseCode); - payload = "0"; - } - http.end(); - - if (payload != "0") { - StaticJsonDocument<1000> doc2; - DeserializationError error2 = deserializeJson(doc2, payload); - if (error2) { // Test if parsing succeeds. - Serial.print(F("deserializeJson() failed: ")); - Serial.println(error2.f_str()); - return "No WeatherForcast Available"; - } - - String resumen = doc2["weather"][0]["description"]; - String temperatura = doc2["main"]["temp"]; - String presion = doc2["main"]["pressure"]; - String humedad = doc2["main"]["humidity"]; - String viento = doc2["wind"]["speed"]; - String lugar = doc2["name"]; - String respuesta = lugar + ", " + resumen + ",T:" + temperatura.substring(0,temperatura.indexOf(".")) + ",P:" + presion + ",H:" + humedad + ",W:" + viento.substring(0,viento.indexOf(".")); - return respuesta; - } -} - -String GetWeatherForecast(String callsign) { - String request_info, payload, weatherForecast; - int httpResponseCode; - callsign.trim(); - request_info = "https://api.aprs.fi/api/get?name=" + callsign + "&what=loc&apikey=" + APRS_API_KEY + "&format=json"; - http.begin(request_info.c_str()); - httpResponseCode = http.GET(); - if (httpResponseCode > 0) { //Serial.print("HTTP Response code: "); //Serial.println(httpResponseCode); - payload = http.getString(); //Serial.println(payload); - } else { - //Serial.print("Error code: "); //Serial.println(httpResponseCode); - payload = "0"; - } - http.end(); - if (payload != "0") { - weatherForecast = getWeatherForecast(payload); - return weatherForecast; - } else { - return "No WeatherForcast Available"; - } -} - -void setup() { - Serial.begin(115200); - setup_wifi(); - btStop(); - configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); - getDateTime(); - Serial.println("Starting Weather Report APRS\n"); -} - -void loop() { - 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(); - } - - while (espClient.connected()) { - while (espClient.available()) { - uint32_t lastTx = millis() - lastTxTime; - - if (lastTx >= BeaconInterval) { - beacon_update = true; - } - if (beacon_update) { - Serial.println("\n---- Sending WeatherReport Beacon ----\n"); - espClient.write(WeatherReportBeaconPacket.c_str()); - lastTxTime = millis(); - beacon_update = false; - } - - String aprsisData, packet, subpacket1, receivedMessage, questioner, answerMessage, ackNumber, ackMessage, currentDate, weatherForecast; - - aprsisData = espClient.readStringUntil('\n'); - packet.concat(aprsisData); - if (!packet.startsWith("#")){ - Serial.println(packet); - 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 == "hora") { - currentDate = getDateTime(); - answerMessage = "WRCLP>APRS,TCPIP*,qAC,CHILE::" + questioner + ":" + currentDate + "\n"; - } else if (receivedMessage == "clima") { - 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("\n-------> " + answerMessage + "\n"); - espClient.write(answerMessage.c_str()); - } - } - } - } - } -} \ No newline at end of file diff --git a/src/LoRa_APRS_iGate_DIY.cpp b/src/LoRa_APRS_iGate_DIY.cpp new file mode 100644 index 0000000..8d635bd --- /dev/null +++ b/src/LoRa_APRS_iGate_DIY.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include "iGate_config.h" +#include "pins_config.h" + +WiFiClient espClient; +int status = WL_IDLE_STATUS; +uint32_t lastTxTime = 0; + +#define txInterval 15 * 60 * 1000 +const String LAT = "3302.03S"; // por corregir // write your latitude +const String LON = "07134.42W"; //por corregir +const String IGATE = "CD2RXU-10"; +const String Mensaje_iGate = "DIY ESP32 - LoRa APRS iGATE"; + + +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"); +} + +void setup_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); + } + Serial.print("Connected as "); + Serial.println(WiFi.localIP()); +} + +void connect_and_upload_to_APRS_IS(String packet) { + 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()) { + delay(1000); + aprsauth = "user " + callsign_igate + " pass " + passcode_igate + "\n"; //info igate + espClient.write(aprsauth.c_str()); //Serial.println("Run client.connected()"); + delay(500); + espClient.write(packet.c_str()); //Serial.println("Send client.write=" + aprsauth); + delay(500); //Serial.println("Send espClient.write = " + packet_para_APRS_IS); + Serial.println("Packet uploaded =)\n"); + espClient.stop(); + espClient.flush(); //Serial.println("(Telnet client disconnect)\n"); + } + } +} + +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); + packet_para_APRS_IS = callsign_y_path_tracker + ",qAO," + callsign_igate + payload_tracker + "\n"; + //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); +} + +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)"); + } +} + +void setup() { + Serial.begin(115200); + setup_wifi(); + btStop(); + setup_lora(); + Serial.println("Starting iGate\n"); +} + +void loop() { + String mensaje_recibido = ""; + String mensaje_beacon_estacion = IGATE + ">APLG01,TCPIP*,qAC,T2BRAZIL:=" + LAT + "L" + LON + "&" + Mensaje_iGate+ "\n"; // + uint32_t lastTx = millis() - lastTxTime; + bool valida_inicio; + int packetSize = LoRa.parsePacket(); + if (packetSize) { + while (LoRa.available()) { + int inChar = LoRa.read(); + mensaje_recibido += (char)inChar; + } + valida_y_procesa_packet(mensaje_recibido); //Serial.println("Mensaje Recibido : " + String(mensaje_recibido)); + } + if (lastTx >= txInterval) { + Serial.println("enviando Beacon Estacion/iGate"); + connect_and_upload_to_APRS_IS(mensaje_beacon_estacion); + lastTxTime = millis(); + } +} + \ No newline at end of file diff --git a/src/config.h b/src/config.h deleted file mode 100644 index 7b68995..0000000 --- a/src/config.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef IGATE_CONFIG_H_ -#define IGATE_CONFIG_H_ - -#include - -#define WIFI_SSID "Richon" -//#define WIFI_SSID "Jimenita" -#define WIFI_PASSWORD "k4fPnmg5qnyf" -//#define WIFI_PASSWORD "mg6wyMhqRnxk" -#define APRS_API_KEY "172828.INx7f7KxnpajX3" - -#define BeaconInterval 900000 // 15 minutes = 900000 seg -#define WifiCheckInterval 60000 // wificheck after one minute - -const String WeatherReportCallsign = "WRCLP"; -const String WeatherReportPasscode = "14332"; -const String AprsServer = "radioaficion.pro"; // write the address of the aprs server //const String SERVER = "brazil.aprs2.net"; -const int AprsServerPort = 14580; // 14579 port is allready filtered so use 14580 -const String AprsSoftwareName = "ESP32_TEST"; -const String AprsSoftwareVersion = "0.1.0"; -const int ReportingDistance = 30; // kms -const String AprsFilter = "t/poms/CD2RXU-10/50"; //cambio a : "t/poms/" + WeatherReportCallsign + "/" + String(ReportingDistance) - -const String WeatherReportComment = "LoRa APRS Weather Report https://github.com/richonguzman/ESP32_APRS_Weather_Report"; - -const String LAT = "3302.04S"; // por corregir // write your own latitude!!!! -const String LON = "07134.43W"; // por corregir - -String WeatherReportBeaconPacket = WeatherReportCallsign + ">APRS,TCPIP*,qAC,CHILE:=" + LAT + "/" + LON + "?" + WeatherReportComment + "\n"; - -const char* ntpServer = "pool.ntp.org"; -const long GMT = -3; // GMT -3 -const long gmtOffset_sec = GMT*60*60; -const int daylightOffset_sec = 3600; - - -#endif \ No newline at end of file diff --git a/src/iGate_config.h b/src/iGate_config.h new file mode 100644 index 0000000..0d662a0 --- /dev/null +++ b/src/iGate_config.h @@ -0,0 +1,15 @@ +#ifndef IGATE_CONFIG_H_ +#define IGATE_CONFIG_H_ + +#include + +#define WIFI_SSID "Richon" +#define WIFI_PASSWORD "k4fPnmg5qnyf" + +String callsign_igate = "CD2RXU-10"; +String passcode_igate = "23201"; + +const String SERVER = "brazil.aprs2.net"; // write the address of the aprs server +const int APRSPORT = 14579; // write the aprs server APRSPORT + +#endif diff --git a/src/pins_config.h b/src/pins_config.h new file mode 100644 index 0000000..6be6a20 --- /dev/null +++ b/src/pins_config.h @@ -0,0 +1,31 @@ +#ifndef PINS_CONFIG_H_ +#define PINS_CONFIG_H_ + +#include + + +#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_IRQ 26 // GPIO26 - SX1276 IRQ ---->DIO0 + +/* +SX1278-------------------> ESP32 +GND GND +DIO1 - +DIO2 - +DIO3 - +VCC 3.3V +MISO 19 +MOSI 27 +SCLK 5 +NSS 18 +DIO0 26 +REST 14 +GND - +*/ + + +#endif