Guest User

Untitled

a guest
Feb 13th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. #include "LoRaWan_APP.h"
  2. #include "Arduino.h"
  3. #include <Wire.h>
  4. #include "softSerial.h"
  5. #include "TinyGPS++.h"
  6.  
  7. /*
  8. * set LoraWan_RGB to 1,the RGB active in loraWan
  9. * RGB red means sending;
  10. * RGB green means received done;
  11. */
  12.  
  13. #ifndef LoraWan_RGB
  14. #define LoraWan_RGB 0
  15. #endif
  16.  
  17. #define RF_FREQUENCY 915000000 // Hz
  18.  
  19. #define TX_OUTPUT_POWER 14 // dBm
  20.  
  21. #define LORA_BANDWIDTH 0 // [0: 125 kHz,
  22. // 1: 250 kHz,
  23. // 2: 500 kHz,
  24. // 3: Reserved]
  25. #define LORA_SPREADING_FACTOR 8 // [SF7..SF12]
  26. #define LORA_CODINGRATE 4 // [1: 4/5,
  27. // 2: 4/6,
  28. // 3: 4/7,
  29. // 4: 4/8]
  30. #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
  31. #define LORA_SYMBOL_TIMEOUT 0 // Symbols
  32. #define LORA_FIX_LENGTH_PAYLOAD_ON false
  33. #define LORA_IQ_INVERSION_ON false
  34. #define LORA_SYNC_WORD 0x12 //sync word to join two LoRa devices
  35.  
  36.  
  37. #define RX_TIMEOUT_VALUE 1000
  38. #define BUFFER_SIZE 30 // Define the payload size here
  39.  
  40. char txpacket[BUFFER_SIZE];
  41. char rxpacket[BUFFER_SIZE];
  42.  
  43. #define timetosleep 1000
  44. #define timetowake 20000
  45. static TimerEvent_t sleep;
  46. static TimerEvent_t wakeup;
  47. uint8_t lowpower;
  48. int gpslowpower;
  49.  
  50. static RadioEvents_t RadioEvents;
  51.  
  52. double txNumber;
  53. int gtx = GPIO5;
  54. int grx = GPIO3;
  55. int16_t rssi,rxSize;
  56. void DoubleToString( char *str, double double_num,unsigned int len);
  57. softSerial ss(grx, gtx);
  58. TinyGPSPlus gps;
  59.  
  60. uint8_t slow_gps[] = {0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0x10, 0x27, 0x01, 0x00, 0x01, 0x00, 0x4D, 0xDD, 0xB5, 0x62, 0x06, 0x08, 0x00, 0x00, 0x0E, 0x30};
  61. uint8_t reset_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0x87, 0x01, 0x00, 0x95, 0xF7};
  62. uint8_t low_power_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x16, 0x74};
  63. uint8_t fast_gps[] = {0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xF4, 0x01, 0x01, 0x00, 0x01, 0x00, 0x0B, 0x77};
  64. uint8_t cold_start_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0xFF, 0x02, 0x00, 0x0E, 0x61};
  65. uint8_t pedest_mode_gps[] = {0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x4F, 0x82, 0xB5, 0x62, 0x06, 0x24, 0x00, 0x00, 0x2A, 0x84};
  67. uint8_t hard_reset_gps[] = {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x07, 0x1F, 0x9E};
  68. uint8_t low_baud_gps[] = {0xb5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xd0, 0x08, 0x00, 0x00, 0xc0, 0x12, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd3, 0xfc, 0xb5, 0x62, 0x06, 0x00, 0x01, 0x00, 0x01, 0x08, 0x22};
  69. uint8_t GPSStandby[] = {0xB5, 0x62, 0x02, 0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x4D, 0x3B};
  70. //the pins that the GPS is connected to
  71.  
  72. void setup() {
  73. delay(500);
  74. Serial.begin(115200);
  75. ss.begin(9600);
  76. rssi=0;
  77. lowpower = 0;
  78.  
  79. Radio.Init( &RadioEvents ); //start the radio with the parameters define at the top
  80. Radio.SetChannel( RF_FREQUENCY );
  81. Radio.SetSyncWord(LORA_SYNC_WORD);
  82. Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
  83. LORA_SPREADING_FACTOR, LORA_CODINGRATE,
  84. LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
  85. true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
  86.  
  87. TimerInit( &sleep, OnSleep );
  88. TimerInit( &wakeup, OnWakeup );
  89. delay(500);
  90. OnWakeup();
  91. }
  92.  
  93. void loop()
  94. {
  95. if(lowpower > 0)
  96. {
  97. //note that LowPower_Handler() run six times the mcu into lowpower mode;
  98. lowPowerHandler();
  99. //ss.flush();
  100. }
  101. }
  102.  
  103. void DoubleToString( char *str, double double_num,unsigned int len) {
  104. double fractpart, intpart;
  105. fractpart = modf(double_num, &intpart);
  106. fractpart = fractpart * (pow(10,len));
  107. sprintf(str + strlen(str),"%d", (int)(intpart)); //Integer part
  108. sprintf(str + strlen(str), ".%d", (int)(fractpart)); //Decimal part
  109. }
  110.  
  111. static void smartDelay(unsigned long ms)
  112. {
  113. unsigned long start = millis();
  114. do
  115. {
  116. while (ss.available())
  117. gps.encode(ss.read());
  118. } while (millis() - start < ms);
  119. }
  120.  
  121.  
  122. void OnSleep()
  123. {
  124. Serial.printf("[lowp] lowpower mode %d ms\r\n",timetowake);
  125. Serial.println("---------------------------zzzzzzzz----------------------------");
  126. turnOffRGB();
  127. lowpower = 1;
  128. TimerSetValue( &wakeup, timetowake );
  129. TimerStart( &wakeup );
  130. }
  131.  
  132. void GPSWakeUp()
  133. {
  134. if (gpslowpower > 0)
  135. {
  136. Serial.println("Waking up GPS");
  137. //ss.flush();
  138. ss.println();
  139. gps = TinyGPSPlus();
  140. gpslowpower = 0;
  141. }
  142. }
  143.  
  144. void GPSStandBy()
  145. {
  146. if (gpslowpower < 1)
  147. {
  148. Serial.println("Putting GPS in Low Power");
  149. ss.write(GPSStandby,sizeof(GPSStandby));
  150. //ss.flush();
  151. //ss.read();
  152. //ss = softSerial(grx, gtx);
  153. //ss.begin(9600);
  154. gpslowpower = 1;
  155. }
  156. }
  157.  
  158. void OnWakeup()
  159. {
  160. lowpower = 0;
  161. Serial.printf("[wkup] wake up, %d ms later into lowpower mode.\r\n",timetosleep);
  162. GPSWakeUp();
  163. uint32_t currentTime=TimerGetCurrentTime()/1000;
  164. uint16_t voltage = getBatteryVoltage();
  165. Serial.println(voltage);
  166. Serial.printf("[time] upTime: %d sec. \r\n",currentTime);
  167. txNumber=0;
  168. while (!gps.location.isValid()) //while data is available
  169. {
  170. Serial.println("waiting for GPS");
  171. Serial.println(gps.satellites.value());
  172. Serial.println(gps.charsProcessed());
  173. Serial.println(txNumber);
  174. txNumber += 1;
  175. if (txNumber == 180)
  176. {
  177. Serial.println("Resetting GPS Object...");
  178. gps = TinyGPSPlus();
  179. //ss.read();
  180. //ss.write(reset_gps,sizeof(reset_gps));
  181. }
  182. if (txNumber >= 200)
  183. {
  184. Serial.println("Resetting GPS Module...");
  185. ss.write(reset_gps,sizeof(reset_gps));
  186. gps = TinyGPSPlus();
  187. //ss.read();
  188. txNumber = 0;
  189. }
  190. smartDelay(1000);
  191. }
  192. if (gps.location.isValid()) //check whether gps location is valid
  193. {
  194. char c_lat[10];
  195. char c_lng[10];
  196. dtostrf(gps.location.lat(),8,6,c_lat); //the gps sends the latitude as a double, I convert it to a char here for the radio
  197. dtostrf(gps.location.lng(),8,6,c_lng);
  198. sprintf(txpacket,"%s,%s", c_lat, c_lng); //start a package
  199.  
  200. turnOnRGB(COLOR_RECEIVED,0); //change rgb color
  201.  
  202. Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket)); //more debugging
  203.  
  204. Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
  205. GPSStandBy();
  206. Radio.Sleep();
  207. TimerSetValue( &sleep, timetosleep );
  208. TimerStart( &sleep );
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment