Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "LoRaWan_APP.h"
- #include "Arduino.h"
- #include <Wire.h>
- #include "softSerial.h"
- #include "TinyGPS++.h"
- /*
- * set LoraWan_RGB to 1,the RGB active in loraWan
- * RGB red means sending;
- * RGB green means received done;
- */
- #ifndef LoraWan_RGB
- #define LoraWan_RGB 0
- #endif
- #define RF_FREQUENCY 915000000 // Hz
- #define TX_OUTPUT_POWER 14 // dBm
- #define LORA_BANDWIDTH 0 // [0: 125 kHz,
- // 1: 250 kHz,
- // 2: 500 kHz,
- // 3: Reserved]
- #define LORA_SPREADING_FACTOR 8 // [SF7..SF12]
- #define LORA_CODINGRATE 4 // [1: 4/5,
- // 2: 4/6,
- // 3: 4/7,
- // 4: 4/8]
- #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
- #define LORA_SYMBOL_TIMEOUT 0 // Symbols
- #define LORA_FIX_LENGTH_PAYLOAD_ON false
- #define LORA_IQ_INVERSION_ON false
- #define LORA_SYNC_WORD 0x12 //sync word to join two LoRa devices
- #define RX_TIMEOUT_VALUE 1000
- #define BUFFER_SIZE 30 // Define the payload size here
- char txpacket[BUFFER_SIZE];
- char rxpacket[BUFFER_SIZE];
- #define timetosleep 1000
- #define timetowake 20000
- static TimerEvent_t sleep;
- static TimerEvent_t wakeup;
- uint8_t lowpower;
- int gpslowpower;
- static RadioEvents_t RadioEvents;
- double txNumber;
- int gtx = GPIO5;
- int grx = GPIO3;
- int16_t rssi,rxSize;
- void DoubleToString( char *str, double double_num,unsigned int len);
- softSerial ss(grx, gtx);
- TinyGPSPlus gps;
- 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};
- uint8_t reset_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0x87, 0x01, 0x00, 0x95, 0xF7};
- uint8_t low_power_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x16, 0x74};
- uint8_t fast_gps[] = {0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xF4, 0x01, 0x01, 0x00, 0x01, 0x00, 0x0B, 0x77};
- uint8_t cold_start_gps[] = {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0xFF, 0xFF, 0x02, 0x00, 0x0E, 0x61};
- 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,
- 0x00, 0x00, 0x4F, 0x82, 0xB5, 0x62, 0x06, 0x24, 0x00, 0x00, 0x2A, 0x84};
- 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};
- 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};
- uint8_t GPSStandby[] = {0xB5, 0x62, 0x02, 0x41, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x4D, 0x3B};
- //the pins that the GPS is connected to
- void setup() {
- delay(500);
- Serial.begin(115200);
- ss.begin(9600);
- rssi=0;
- lowpower = 0;
- Radio.Init( &RadioEvents ); //start the radio with the parameters define at the top
- Radio.SetChannel( RF_FREQUENCY );
- Radio.SetSyncWord(LORA_SYNC_WORD);
- Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
- LORA_SPREADING_FACTOR, LORA_CODINGRATE,
- LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
- true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
- TimerInit( &sleep, OnSleep );
- TimerInit( &wakeup, OnWakeup );
- delay(500);
- OnWakeup();
- }
- void loop()
- {
- if(lowpower > 0)
- {
- //note that LowPower_Handler() run six times the mcu into lowpower mode;
- lowPowerHandler();
- //ss.flush();
- }
- }
- void DoubleToString( char *str, double double_num,unsigned int len) {
- double fractpart, intpart;
- fractpart = modf(double_num, &intpart);
- fractpart = fractpart * (pow(10,len));
- sprintf(str + strlen(str),"%d", (int)(intpart)); //Integer part
- sprintf(str + strlen(str), ".%d", (int)(fractpart)); //Decimal part
- }
- static void smartDelay(unsigned long ms)
- {
- unsigned long start = millis();
- do
- {
- while (ss.available())
- gps.encode(ss.read());
- } while (millis() - start < ms);
- }
- void OnSleep()
- {
- Serial.printf("[lowp] lowpower mode %d ms\r\n",timetowake);
- Serial.println("---------------------------zzzzzzzz----------------------------");
- turnOffRGB();
- lowpower = 1;
- TimerSetValue( &wakeup, timetowake );
- TimerStart( &wakeup );
- }
- void GPSWakeUp()
- {
- if (gpslowpower > 0)
- {
- Serial.println("Waking up GPS");
- //ss.flush();
- ss.println();
- gps = TinyGPSPlus();
- gpslowpower = 0;
- }
- }
- void GPSStandBy()
- {
- if (gpslowpower < 1)
- {
- Serial.println("Putting GPS in Low Power");
- ss.write(GPSStandby,sizeof(GPSStandby));
- //ss.flush();
- //ss.read();
- //ss = softSerial(grx, gtx);
- //ss.begin(9600);
- gpslowpower = 1;
- }
- }
- void OnWakeup()
- {
- lowpower = 0;
- Serial.printf("[wkup] wake up, %d ms later into lowpower mode.\r\n",timetosleep);
- GPSWakeUp();
- uint32_t currentTime=TimerGetCurrentTime()/1000;
- uint16_t voltage = getBatteryVoltage();
- Serial.println(voltage);
- Serial.printf("[time] upTime: %d sec. \r\n",currentTime);
- txNumber=0;
- while (!gps.location.isValid()) //while data is available
- {
- Serial.println("waiting for GPS");
- Serial.println(gps.satellites.value());
- Serial.println(gps.charsProcessed());
- Serial.println(txNumber);
- txNumber += 1;
- if (txNumber == 180)
- {
- Serial.println("Resetting GPS Object...");
- gps = TinyGPSPlus();
- //ss.read();
- //ss.write(reset_gps,sizeof(reset_gps));
- }
- if (txNumber >= 200)
- {
- Serial.println("Resetting GPS Module...");
- ss.write(reset_gps,sizeof(reset_gps));
- gps = TinyGPSPlus();
- //ss.read();
- txNumber = 0;
- }
- smartDelay(1000);
- }
- if (gps.location.isValid()) //check whether gps location is valid
- {
- char c_lat[10];
- char c_lng[10];
- 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
- dtostrf(gps.location.lng(),8,6,c_lng);
- sprintf(txpacket,"%s,%s", c_lat, c_lng); //start a package
- turnOnRGB(COLOR_RECEIVED,0); //change rgb color
- Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket)); //more debugging
- Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
- GPSStandBy();
- Radio.Sleep();
- TimerSetValue( &sleep, timetosleep );
- TimerStart( &sleep );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment