Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.02 KB | None | 0 0
  1. #include <LibAPRS.h>
  2.  
  3. #include <LM75A.h>
  4. // Create I2C LM75A instance
  5. LM75A lm75a_sensor(false,  //A0 LM75A pin state
  6.                    false,  //A1 LM75A pin state
  7.                    false); //A2 LM75A pin state
  8. // Equivalent to "LM75A lm75a_sensor;"
  9.  
  10. #include <Wire.h>
  11. #include <MS5611.h>
  12. MS5611 ms5611;
  13. double referencePressure;
  14.  
  15. #include "DHT.h"
  16. #define DHTPIN 30     // what digital pin we're connected to
  17. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  18. DHT dht(DHTPIN, DHTTYPE);
  19.  
  20. char byteString[80]; //Char Array holding up to 80 bytes filled with the $GNGGA NMEA sentence
  21.  
  22. char timeUTC[10]; //array holding time
  23. char latitudeGPS[12]; // array holding latitude decimal form as chars
  24. char dirLat[2]; //array holding latitude hemisphere
  25. char longitudeGPS[12]; // array holding longitude decimal form as chars
  26. char dirLon[2];  //array holding longitude hemesphere
  27.  
  28. char fixNum[2]; //array holding fix quality 0-8
  29. char satNum[3]; // array holding total number of tracked satellites
  30. char dilution[4];  //array holding horizontal dilution of position
  31. char altMeters[10]; //array holding  Altitude, Meters, above mean sea level
  32.  
  33. float altFeet = 0;
  34. char temp2[6];
  35. char baroP[4];
  36.  
  37. double realTemperature = 0;
  38. long realPressure =  0;
  39.  
  40. char intT1Char[8];
  41. char intT2Char[8];
  42. char intT3Char[8];
  43. char gpsAltFeetChar[11];
  44. char barChar[11];
  45. char humChar[3];
  46.  
  47. char stringOut[64]; //libAPRS max 64 char
  48.  
  49. float dhtH = 0; // Read humidity in %
  50. float dhtT = 0; // Read temperature as Celsius (the default)
  51. float dhtF = 0;// Read temperature as Fahrenheit (isFahrenheit = true)
  52.  
  53. float temperature_in_degreesC = 0;
  54. float temperature_in_degreesF = 0;
  55. float absoluteAltitude = 0;
  56. float relativeAltitude = 0;
  57.  
  58. int battV = 0;
  59.  
  60. char combLat[15]; //array holding the final input into LibAPRS's locationUpdate() func - latitude
  61. char combLon[15];//array holding the final input into LibAPRS's locationUpdate() func - longitude
  62.  
  63. boolean gotGGA = false; //make our if statement possible I think this can be simplified TO DO
  64.  
  65. long previousMillis = 0;        //timer define counter
  66.  
  67. long rfInterval = 90000; // every 30 seconds approx SEND OUT RF PACKET locationUpdate()
  68.  
  69. #define ADC_REFERENCE REF_5V // OR #define ADC_REFERENCE REF_3V3
  70.  
  71. #define OPEN_SQUELCH false //// You can also define whether your modem will be running with an open squelch radio:
  72.  
  73. //boolean gotPacket = false; //Get rid of soon.
  74. //uint8_t *packetData; //make sure we dont need em.
  75.  
  76. void aprs_msg_callback(struct AX25Msg *msg) {
  77. }
  78.  
  79. void setup() {
  80.   Serial.begin(115200);
  81.   Serial1.begin(9600);
  82.   Serial3.begin(9600);
  83.  
  84.   dht.begin();
  85.   ms5611.begin();
  86.  
  87.   // Get reference pressure for relative altitude
  88.   referencePressure = ms5611.readPressure();
  89.  
  90.   APRS_init(ADC_REFERENCE, OPEN_SQUELCH);  // Initialise APRS library - This starts the modem
  91.  
  92.   APRS_setCallsign("KM4PGH", 9);  //   You must at a minimum configure your callsign and SSID
  93.  
  94.   APRS_setDestination("BEACON", 0);  // You don't need to set the destination identifier, but can.
  95.  
  96.   APRS_setPath1("WIDE1", 1);   // Path parameters are set to sensible values by default, but this is how you can configure them:
  97.  
  98.   APRS_setPath2("WIDE2", 2);
  99.  
  100.   APRS_setPreamble(150);    // You can define preamble and tail like this:
  101.   APRS_setTail(50);   // You can define preamble and tail like this:
  102.  
  103.   // APRS_useAlternateSymbolTable(false);  // You can use the normal or alternate symbol table:
  104.  
  105.   APRS_setSymbol('O');    // And set what symbol you want to use:
  106.  
  107.   APRS_printSettings();   // We can print out all the settings
  108.   Serial.print(F("Free RAM:     ")); Serial.println(freeMemory()); //debug memory problems
  109. }
  110.  
  111. void getGPS() {
  112.   while (Serial1.available()) {
  113.  
  114.     Serial1.find("$GNGGA"); //reads Serial1 untill we find $GNGGA
  115.     if (gotGGA = true) {  //set Flag if so gotGGA
  116.       Serial.println("we recieved $GNGGA !"); //Debug to serial mon
  117.       Serial1.readBytesUntil('\n', byteString, 80); //lost word $GNGGA during the find, but store the rest into char array byteString 80 bytes (76 needed?)
  118.     }
  119.     else {
  120.       Serial.println("we did NOT get $GNGGA!");
  121.     }
  122.   }
  123. }
  124.  
  125. void parseGPS() {
  126.  
  127.   // split the data into its parts with strtok()
  128.  
  129.   char * strtokIndx; // this is used by strtok() as an index
  130.  
  131.   strtokIndx = strtok(byteString, ",");     // get the first part - the string - TIME
  132.   strcpy(timeUTC, strtokIndx); // copy it to timeUTC
  133.  
  134.   strtokIndx = strtok(NULL, ","); //get next word of NMEA sentence until next delimiter, "," comma
  135.   memcpy(latitudeGPS, strtokIndx, 7); //fill our array with only part of the NMEA word because LibAPRS requires it
  136.  
  137.   strtokIndx = strtok(NULL, ","); //get next word of NMEA sentence until next delimiter, "," comma
  138.   strcpy(dirLat, strtokIndx); // fill our array with the NMEA word for the hemisphere indicator N/S
  139.  
  140.   strtokIndx = strtok(NULL, ","); //get next word of NMEA sentence until next delimiter, "," comma
  141.   memcpy(longitudeGPS, strtokIndx, 8); //fill our array with only part of the NMEA word because LibAPRS requires it
  142.  
  143.   strtokIndx = strtok(NULL, ","); //get next word of NMEA sentence until next delimiter, "," comma
  144.   strcpy(dirLon, strtokIndx);  //fill our array with the NMEA word for the hemisphere indicator E/W
  145.  
  146.   strcat(combLat, latitudeGPS); //combine trunicated latitude to the hemisphere indicator N/S
  147.   strcat(combLat, dirLat);
  148.  
  149.   strcat(combLon, longitudeGPS); //combine trunicated longitude to the hemisphere indicator E/W
  150.   strcat(combLon, dirLon);
  151.  
  152.   strtokIndx = strtok(NULL, ",");  //get next word of NMEA sentence until next delimiter, "," comma
  153.   strcpy(fixNum, strtokIndx); // fill our arrary with NMEA word for Satellite Fix quaity.
  154.  
  155.   //0 = invalid (nofix) 1 = GPS fix (SPS) 2 = DGPS fix 3 = PPS fix 4 = Real Time Kinematic
  156.   //5 = Float RTK 6 = estimated (dead reckoning) (2.3 feature) 7 = Manual input mode 8 = Simulation mode
  157.  
  158.   strtokIndx = strtok(NULL, ",");  //get next word of NMEA sentence until next delimiter, "," comma
  159.   strcpy(satNum, strtokIndx); // fill our arrary with NMEA word for Number of tracked satellites
  160.  
  161.   strtokIndx = strtok(NULL, ",");  //get next word of NMEA sentence until next delimiter, "," comma
  162.   strcpy(dilution, strtokIndx); // fill our arrary with NMEA word for  Horizontal dilution of position
  163.  
  164.   strtokIndx = strtok(NULL, ",");  //get next word of NMEA sentence until next delimiter, "," comma
  165.   strcpy(altMeters, strtokIndx); // fill our arrary with NMEA word for Altitude, Meters, above mean sea level
  166.  
  167.   altFeet = atof (altMeters) * 3.2808;
  168. }
  169.  
  170. void getBaro() {
  171.   // Read raw values
  172.   uint32_t rawTemp = ms5611.readRawTemperature();
  173.   uint32_t rawPressure = ms5611.readRawPressure();
  174.  
  175.   // Read true temperature & Pressure
  176.   realTemperature = ms5611.readTemperature();
  177.   realPressure =  ms5611.readPressure()  ;
  178.  
  179.   // Calculate altitude
  180.   absoluteAltitude = ms5611.getAltitude(realPressure);
  181.   relativeAltitude = ms5611.getAltitude(realPressure, referencePressure);
  182.  
  183. }
  184.  
  185. void getDHT22() {
  186.   // Reading temperature or humidity takes about 250 milliseconds!
  187.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  188.   dhtH = dht.readHumidity();
  189.   // Read temperature as Celsius (the default)
  190.   dhtT = dht.readTemperature();
  191.   // Read temperature as Fahrenheit (isFahrenheit = true)
  192.   dhtF = dht.readTemperature(true);
  193.  
  194.   // Check if any reads failed and exit early (to try again).
  195.   if (isnan(dhtH) || isnan(dhtT) || isnan(dhtF)) {
  196.     Serial.println("Failed to read from DHT sensor!");
  197.     return;
  198.   }
  199. }
  200.  
  201. void getLM75A() {
  202.   temperature_in_degreesC = lm75a_sensor.getTemperatureInDegrees();
  203.   temperature_in_degreesF = (LM75A::degreesToFahrenheit(temperature_in_degreesC ));
  204.  
  205.   if (temperature_in_degreesC == INVALID_LM75A_TEMPERATURE) {
  206.     Serial.println("Error while getting temp LM75A");
  207.   } else {
  208.   }
  209. }
  210.  
  211. void battMon() {
  212.   // read the input on analog pin 0:
  213.   battV = analogRead(A1);
  214. }
  215.  
  216. void buildComment() {
  217.  
  218.   strcpy(stringOut, "{ ALT");
  219.   dtostrf(altFeet, 7, 0, gpsAltFeetChar);
  220.   strcat(stringOut, gpsAltFeetChar);
  221.   strcat(stringOut, "ft");
  222.  
  223.   strcat(stringOut, " SAT");
  224.   strcat(stringOut, satNum);
  225.  
  226.   //strcat(stringOut, " FIX");
  227.   //strcat(stringOut, fixNum);
  228.  
  229.   strcat(stringOut, " B ");
  230.   ltoa(realPressure, barChar, 10);
  231.   strcat(stringOut, barChar);
  232.   //strcat(stringOut, "P");
  233.  
  234.   strcat(stringOut, " H");
  235.   dtostrf(dhtH, 2, 1, humChar);
  236.   strcat(stringOut, humChar);
  237.   strcat(stringOut, "%");
  238.  
  239.   strcat(stringOut, " T1");
  240.   dtostrf((realTemperature * 1.8) + 31, 5, 1, intT1Char);
  241.   strcat(stringOut, intT1Char);
  242.   strcat(stringOut, "F");
  243.  
  244.   strcat(stringOut, " T2");
  245.   dtostrf(dhtF, 5, 1, intT2Char);
  246.  
  247.   strcat(stringOut, intT2Char);
  248.   strcat(stringOut, "F");
  249.  
  250.   strcat(stringOut, " T3");
  251.   dtostrf(temperature_in_degreesF, 5, 1, intT3Char);
  252.   strcat(stringOut, intT3Char);
  253.   strcat(stringOut, "F");
  254.  
  255.   //Serial.println(stringOut);
  256. }
  257.  
  258. void serialOut() {
  259.   if (strcmp(fixNum, "1") == 0) { //Check location FIX status if no fix report it
  260.  
  261.     Serial.println();
  262.     Serial.print("LAT : ");
  263.     Serial.println(combLat); // filled with formatted strcat() array for LibAPRS locationUpdate - latitude
  264.     Serial.print("LON : ");
  265.     Serial.println(combLon); // filled with formatted strcat() array for LibAPRS locationUpdate - longitude
  266.     Serial.print("TIME : ");
  267.     Serial.println(timeUTC); // time is in raw format. UTC HHMMSS.ss format TBD per APRS packet format
  268.  
  269.     Serial.print("FIX : ");
  270.     Serial.println(fixNum); // filled with formatted strcat() array for satellite fix quality
  271.     Serial.print("SATS : ");
  272.     Serial.println(satNum); // filled with formatted strcat() array for Number of satellites tracked
  273.     Serial.print("ALT : ");
  274.     Serial.print(altMeters); // filled with formatted strcat() array for Altitude, Meters, above mean sea level
  275.     Serial.print(" meters ");
  276.     Serial.print(altFeet, 1); //filled with altFeet = atof (altMeters) * 3.2808;  in parse function
  277.     Serial.println(" feet");
  278.   }
  279.  
  280.   else {
  281.     Serial.println("NO GPS FIX!"); //Report lost GPS fix. keep polling tho
  282.   }
  283.  
  284.   //APRS_printSettings();   // We can print out all the settings
  285.   Serial.print(F("Free RAM:     ")); Serial.println(freeMemory()); //debug memory problems
  286.  
  287.   Serial.print("EXT-TEMP1: ");
  288.   Serial.print(temperature_in_degreesC);
  289.   Serial.print("*C ");
  290.   Serial.print(LM75A::degreesToFahrenheit(temperature_in_degreesC ));
  291.   Serial.println("*F");
  292.  
  293.   Serial.print("HUM: ");
  294.   Serial.print(dhtH);
  295.   //Serial.print("%\t");
  296.   Serial.print("%");
  297.   Serial.print(" EXT-TEMP2: ");
  298.   Serial.print(dhtT);
  299.   Serial.print("*C ");
  300.   Serial.print(dhtF);
  301.   Serial.println("*F\t");
  302.  
  303.   Serial.print("INT-TEMP: ");
  304.   Serial.print(realTemperature);
  305.   Serial.print("*C ");
  306.   Serial.print((realTemperature * 1.8) + 31);
  307.   Serial.println("*F");
  308.   Serial.print("BARO: ");
  309.   Serial.print(realPressure);
  310.   Serial.print(" Pa ");
  311.   Serial.print(realPressure / 6894.76);
  312.   Serial.println(" psi");
  313.   Serial.print("BARO-ALT: ");
  314.   Serial.print(absoluteAltitude);
  315.   Serial.print(" m ");
  316.   Serial.print(absoluteAltitude * 3.2808);
  317.   Serial.println(" ft");
  318.  
  319.   Serial.print("A1: ");
  320.   Serial.print(battV);
  321.   Serial.print(" Volts: ");
  322.   Serial.println(battV / 113.2);
  323.  
  324.   Serial.println(stringOut);
  325.   Serial.println();
  326. }
  327.  
  328. void logData() {
  329.  
  330.     Serial.print(F("Free RAM:     ")); Serial.println(freeMemory()); //debug memory problems
  331.  
  332.   //Out Data to send out
  333.   Serial3.print("DATA:");
  334.   Serial3.println(stringOut);
  335.  
  336.   //MS5611
  337.   Serial3.print("MS5611: ");
  338.   Serial3.print(referencePressure);
  339.   Serial3.print(" Pa Start");
  340.   Serial3.print(realTemperature);
  341.   Serial3.print("*C ");
  342.   Serial3.print(realPressure);
  343.   Serial3.print(" Pa ");
  344.   Serial3.print(realPressure / 6894.76);
  345.   Serial3.print(" psi");
  346.   Serial3.print(absoluteAltitude);
  347.   Serial3.print(" m ");
  348.   Serial3.print(absoluteAltitude * 3.2808);
  349.   Serial3.println(" ft");
  350.  
  351.   //DHT22
  352.   Serial3.print("DHT: ");
  353.   Serial3.print(dhtH);
  354.   // Serial3.print("%\t ");
  355.   Serial3.print("%");
  356.   Serial3.print(dhtT);
  357.   Serial3.print("*C ");
  358.   Serial3.print(dhtF);
  359.   Serial3.println("*F\t");
  360.  
  361.   //LM75A
  362.   Serial3.print(temperature_in_degreesC);
  363.   Serial3.print("*C ");
  364.   Serial3.print(temperature_in_degreesF);
  365.   //Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees));
  366.   Serial3.println("*F");
  367.  
  368.   //BATT
  369.   Serial3.print("A1: ");
  370.   Serial3.print(battV);
  371.   Serial3.print(" Volts ");
  372.   Serial3.println(battV / 113.2);
  373. }
  374.  
  375. void locationUpdate() {
  376.   // Let's first set our latitude and longtitude.
  377.   // These should be in NMEA format!
  378.   APRS_setLat(combLat);   //APRS_setLat("5530.80N");
  379.   APRS_setLon(combLon);    //APRS_setLon("01143.89E");
  380.  
  381.   char *comment = stringOut;  // We'll define a comment string
  382.  
  383.   APRS_sendLoc(comment, strlen(comment)); // And send the update
  384.  
  385. }
  386.  
  387. //MAIN LOOP
  388. void loop() {
  389.  
  390.   getGPS(); // Poll GPS data for our string to parse out
  391.  
  392.   Serial.print("$GNGGA");
  393.   Serial.println(byteString); //print NMEA Sentence MUST BE BEFORE PARSE strtok() breaks string after DEBUG
  394.   Serial3.print("$GNGGA");
  395.   Serial3.println(byteString); //print NMEA Sentence MUST BE BEFORE PARSE strtok() breaks string after DEBUG
  396.  
  397.   parseGPS(); // Break up our GPS data in this function.
  398.  
  399.   getBaro();
  400.   getLM75A();
  401.   getDHT22();
  402.   battMon();
  403.  
  404.   serialOut(); // Send data out on Serial1 for debug
  405.   logData(); //Print out data on Serial3 for SD card
  406.  
  407.   buildComment();
  408.  
  409.   unsigned long currentMillis = millis(); //timer define counter
  410.   if (currentMillis - previousMillis > rfInterval) {
  411.     // save the last time you sent RF out
  412.     previousMillis = currentMillis;
  413.     locationUpdate();
  414.     Serial.println("WE SENT OUT RF PACKET!!!!!");
  415.     Serial.println();
  416.     currentMillis = 0;
  417.   }
  418.   else {
  419.     //nada
  420.   }
  421.  
  422.   //APRS_printSettings();
  423.   combLat[0] = '\0'; //MAKE LAST THING TO DO BEFOR DELAY else get nada! makes sure LibAPRS gets fresh data             CHANGED THIS!!!! NOT SURE IF NEEDED
  424.   combLon[0] = '\0'; //MAKE LAST THING TO DO BEFOR DELAY else get nada! makes sure LibAPRS gets fresh data
  425.  
  426.   delay(5000); //slow loop to approx 5Hz
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement