silenius

Untitled

Feb 11th, 2021 (edited)
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Arduino.h"
  2. #include "heltec.h"
  3.  
  4. unsigned int counter = 0;
  5. float snr = 0;
  6. String nema;
  7. short rssi;
  8. short packSize;
  9. String packet;
  10. double latconv = 0;
  11. double longconv = 0;
  12. float dirtylong = 0;
  13. float dirtylat = 0;
  14. volatile bool recieveflag = false;
  15.  
  16. void setup() {
  17.   Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, true /*Serial Enable*/, true /*LoRa use PABOOST*/, 868E6 /*LoRa RF working band*/);
  18.   LoRa.setSpreadingFactor(12);
  19.   LoRa.onReceive(onReceive);
  20.   LoRa.receive();
  21. }
  22.  
  23. void loop() {
  24.   delay(1000);
  25.   if (recieveflag) {
  26.     recieveflag = false;
  27.     counter++;
  28.     convert();
  29.     makestring();
  30.     LoRa.receive();
  31.     displaySendReceive();
  32.   }
  33. }
  34.  
  35. void displaySendReceive()
  36. {
  37.   Heltec.display -> drawString(0, 0, "Received Size " + String(packSize));
  38.   Heltec.display -> drawString(0, 10, String(latconv, 10));
  39.   Heltec.display -> drawString(0, 20, String(longconv, 10));
  40.   Heltec.display -> drawString(0, 40, "RSSI" + String(rssi) + "db " + "  SNR " + String(snr, 2));
  41.   Heltec.display -> drawString(0, 50, "Counter:  " + (String)(counter - 1));
  42.   Heltec.display -> display();
  43.   Heltec.display -> clear();
  44. }
  45.  
  46. void onReceive(int packetSize) {
  47.   recieveflag = true;
  48.   packSize = packetSize;
  49.   snr = LoRa.packetSnr();
  50.   rssi = LoRa.packetRssi();
  51.   packet = "";
  52.   while (LoRa.available()) {
  53.     packet += (char) LoRa.read();
  54.   }
  55. }
  56.  
  57. void convert() {
  58.   short degLo = 0;
  59.   short degLat = 0;
  60.   double minLat = 0;
  61.   double minLo = 0;
  62.   unsigned long latitude = 0;
  63.   unsigned long longitude = 0;
  64.  
  65.   latitude = uint8_t(packet[0]) * 65536 + uint8_t(packet[1]) * 256 + uint8_t(packet[2]);
  66.   latconv = (latitude / 93206.75) - 90;
  67.   degLat = latconv;
  68.   minLat = (latconv - degLat) * 60;
  69.   dirtylat = (degLat * 100) + minLat;
  70.   longitude = uint8_t(packet[3]) * 65536 + uint8_t(packet[4]) * 256 + uint8_t(packet[5]);
  71.   longconv = (longitude / 46603.375) - 180;
  72.   degLo = longconv;
  73.   minLo = (longconv - degLo) * 60;
  74.   dirtylong = (degLo * 100) + minLo;
  75. }
  76.  
  77. void makestring() {
  78.   String filler1 = "GPGGA,000000.00,";
  79.   String filler2 = ",N,";
  80.   String filler3 = ",E,1,05,1.5,200.2,M,-34,M,,";
  81.   char lat[11];
  82.   char lon[12];
  83.   String checksum = "";
  84.   byte sum = 0;
  85.   byte major;
  86.   byte minor;
  87.   String latstr = "";
  88.   String lonstr = "";
  89.   char witch[2];
  90.  
  91.   dtostrf(dirtylat, 1, 3, lat);
  92.   dtostrf(dirtylong, 1, 5, lon);
  93.  
  94.   if (dirtylat < 1000) {
  95.     latstr += "0";
  96.     if (dirtylat < 100) {
  97.       latstr += "0";
  98.       if (dirtylat < 10) {
  99.         latstr += "0";
  100.       }
  101.     }
  102.   }
  103.   latstr += lat;
  104.  
  105.   if (dirtylong < 10000) {
  106.     lonstr += "0";
  107.     if (dirtylong < 1000) {
  108.       lonstr += "0";
  109.       if (dirtylong < 100) {
  110.         lonstr += "0";
  111.         if (dirtylong < 10) {
  112.           lonstr += "0";
  113.         }
  114.       }
  115.     }
  116.   }
  117.   lonstr += lon;
  118.  
  119.   sum = makeCheck(filler1, sum, 16);
  120.   sum = makeCheck(latstr, sum, 8);
  121.   sum = makeCheck(filler2, sum, 3);
  122.   sum = makeCheck(latstr, sum, 11);
  123.   sum = makeCheck(filler3, sum, 27);
  124.  
  125.   Serial.println(checksum);
  126.   major = sum / 16;
  127.   minor = sum % 16;
  128.  
  129.   Serial.println(major);
  130.   Serial.println(minor);
  131.   if (major < 10) {
  132.     witch[0] = '0' + major;
  133.   }
  134.   else {
  135.     witch[0] = '0' + major;
  136.   }
  137.   if (minor < 10) {
  138.     witch[1] = '0' + minor;
  139.   }
  140.   else {
  141.     witch[1] = '0' + minor % 10 + 17;
  142.   }
  143.  
  144.   Serial.print("$");
  145.   Serial.print(filler1);
  146.   Serial.print(latstr);
  147.   Serial.print(filler2);
  148.   Serial.print(lonstr);
  149.   Serial.print(filler3);
  150.   Serial.print("*");
  151.   Serial.println(String(witch));
  152. }
  153.  
  154. byte makeCheck(String nmea, byte foo, byte leng) {
  155.   for (byte i = 0; i < leng; i++) {
  156.     foo = (foo ^ nema.charAt(i));
  157.     return foo;
  158.   }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment