Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <util/crc16.h>
  3. #include <TinyGPS.h>
  4. #include <OneWire.h>
  5. #define EN 11
  6. #define TX0 7
  7. #define TX1 5
  8.  
  9. OneWire ds(9); // DS18x20 Temperature chip i/o One-wire
  10.  
  11. //Tempsensor variables
  12. byte address0[8] = {
  13.   0x28, 0x73, 0xCD, 0xF5, 0x2, 0x0, 0x0, 0xAE}; // External DS18B20 Temp Sensor
  14. int temp0 = 0;
  15.  
  16. // gets temperature data from onewire sensor network, need to supply byte address, it'll check to see what type of sensor and convert
  17. // appropriately
  18. int getTempdata(byte sensorAddress[8]) {
  19.   int HighByte, LowByte, TReading, SignBit, Tc_100, Whole;
  20.   byte data[12], i, present = 0;
  21.  
  22.   ds.reset();
  23.   ds.select(sensorAddress);
  24.   ds.write(0x44,1); // start conversion, with parasite power on at the end
  25.  
  26.   delay(3000); // maybe 750ms is enough, maybe not
  27.   // we might do a ds.depower() here, but the reset will take care of it.
  28.  
  29.   present = ds.reset();
  30.   ds.select(sensorAddress);
  31.   ds.write(0xBE); // Read Scratchpad
  32.  
  33.   for ( i = 0; i < 9; i++) { // we need 9 bytes
  34.     data[i] = ds.read();
  35.   }
  36.   LowByte = data[0];
  37.   HighByte = data[1];
  38.   TReading = (HighByte << 8) + LowByte;
  39.   SignBit = TReading & 0x8000; // test most sig bit
  40.   if (SignBit) // negative
  41.   {
  42.     TReading = (TReading ^ 0xffff) + 1; // 2's comp
  43.   }
  44.  
  45.   if (sensorAddress[0] == 0x10) {
  46.     Tc_100 = TReading * 50; // multiply by (100 * 0.0625) or 6.25
  47.   }
  48.   else {
  49.     Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
  50.   }
  51.  
  52.  
  53.   Whole = Tc_100 / 100; // separate off the whole and fractional portions
  54.  
  55.   if (SignBit) // If its negative
  56.   {
  57.     Whole = Whole * -1;
  58.   }
  59.   return Whole;
  60. }
  61.  
  62. TinyGPS gps;
  63.  
  64. char msg[120];
  65. int count = 1;
  66.  
  67. uint16_t crccat(char *msg)
  68. {
  69.   uint16_t x;
  70.   for(x = 0xFFFF; *msg; msg++)
  71.     x = _crc_xmodem_update(x, *msg);
  72.   snprintf(msg, 8, "*%04X\n", x);
  73.   return(x);
  74. }
  75.  
  76. void setup()
  77. {
  78.   // Setup the GPS serial port
  79.   Serial.begin(9600);
  80.  
  81.   count = 1;
  82.  
  83.   // Set up the pins used to control the radio module and switch
  84.   // it on
  85.  
  86.   pinMode(EN, OUTPUT);
  87.   pinMode(TX0, OUTPUT);
  88.   pinMode(TX1, OUTPUT);
  89.   digitalWrite(EN, HIGH);
  90.  
  91.   rtty_send(".... Starting PicoChu-1 MK2 ....\n");
  92. }
  93.  
  94. void loop()
  95. {
  96.   long lat, lng;
  97.   unsigned long time;
  98.  
  99.   /* Got any data yet? */
  100.   if(Serial.available() <= 0) return;
  101.   if(!gps.encode(Serial.read())) return;
  102.  
  103.   /* Yes, prepare the string */
  104.   gps.get_position(&lat, &lng, NULL);
  105.   gps.get_datetime(NULL, &time, NULL);
  106.   int numbersats = 99;
  107.   numbersats = gps.sats();
  108.  
  109.  
  110.   snprintf(msg, 120,
  111.   "$$PICOCHU-1,%i,%02li:%02li:%02li,%s%li.%05li,%s%li.%05li,%li,%i,%i",
  112.   count++, time / 1000000, time / 10000 % 100, time / 100 % 100,
  113.   (lat >= 0 ? "" : "-"), labs(lat / 100000), labs(lat % 100000),
  114.   (lng >= 0 ? "" : "-"), labs(lng / 100000), labs(lng % 100000),
  115.   gps.altitude() / 100,
  116.   numbersats,
  117.   getTempdata(address0)
  118.  
  119.  
  120.   );
  121.  
  122.   /* Append the checksum, skipping the $$ prefix */
  123.   crccat(msg + 2);
  124.  
  125.   /* Transmit it! */
  126.  
  127.   digitalWrite(EN, HIGH); /*Power on the ntx2*/
  128.   rtty_send(msg); /*Send the gps data*/
  129.  
  130.     if(count % 200 == 0)
  131.   {
  132.     int i;
  133.     digitalWrite(EN, LOW); /*Power off the ntx2*/
  134.     for(i = 0; i < 600; i++) delay(1000);
  135.     digitalWrite(EN, HIGH); /*Power on the ntx2*/
  136.   }
  137.  
  138. }
  139.  
  140. // ---------------------------------------------------------------------------------
  141. // RTTY Code
  142. //
  143. // Code to send strings via RTTY. The RTTY parameters are defined by constants
  144. // below.
  145. // ---------------------------------------------------------------------------------
  146.  
  147. // The number of bits per character (7), number of start bits (1), number of stop bits (2)
  148. // and the baud rate.
  149.  
  150. #define ASCII 7
  151. #define START 1
  152. #define STOP 2
  153. #define BAUD 50
  154. #define INTER_BIT_DELAY (1000/BAUD)
  155.  
  156. // rtty_send: sends a null-terminated string via radio to the ground trackers
  157. void rtty_send( char * s ) // The null-terminated string to transmit
  158. {
  159.   char c;
  160.   while ( c = *s++ ) {
  161.     int i;
  162.     for ( i = 0; i < START; ++i ) {
  163.       rtty_bit(0);
  164.     }
  165.  
  166.     int b;
  167.     for ( i = 0, b = 1; i < ASCII; ++i, b *= 2 ) {
  168.       rtty_bit(c&b);
  169.     }
  170.  
  171.     for ( i = 0; i < STOP; ++i ) {
  172.       rtty_bit(1);
  173.     }
  174.   }
  175.  
  176.   // Note that when idling RTTY specifies that it be in the 'mark' state (or 1). This
  177.   // is achieved by the stop bits that were sent at the end of the last character.
  178. }
  179.  
  180. // rtty_bit: sends a single bit via RTTY
  181. void rtty_bit(int b) // Send 0 if b is 0, 1 if otherwise
  182. {
  183.   digitalWrite(TX0,(b>0)?HIGH:LOW);
  184.   digitalWrite(TX1,(b>0)?LOW:HIGH);
  185.   delay(INTER_BIT_DELAY);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement