|
| 1 | +#include <TinyGPS.h> |
| 2 | +#include <SPI.h> |
| 3 | +#include <LoRa.h> |
| 4 | +#include <SoftwareSerial.h> |
| 5 | + |
| 6 | +TinyGPS gps; |
| 7 | +SoftwareSerial ss(4, 3); // Arduino RX, TX to conenct |
| 8 | + |
| 9 | +static void smartdelay(unsigned long ms); |
| 10 | +unsigned int count = 0; //For times count |
| 11 | + |
| 12 | +float flat, flon; |
| 13 | +char flat_1[10]={"\0"},flon_1[10]={"\0"}; |
| 14 | +char *node_id = "<5678>"; //From LG01 via web Local Channel settings on MQTT.Please refer <> dataformat in here. |
| 15 | +uint8_t datasend[80]; |
| 16 | + |
| 17 | +void setup() |
| 18 | +{ |
| 19 | + Serial.begin(9600); |
| 20 | + while (!Serial); |
| 21 | + ss.begin(9600); |
| 22 | + Serial.println(F("Start MQTT Example of Ubidots")); |
| 23 | + if (!LoRa.begin(868000000)) //868000000 is frequency |
| 24 | + { |
| 25 | + Serial.println("Starting LoRa failed!"); |
| 26 | + while (1); |
| 27 | + } |
| 28 | + LoRa.setSyncWord(0x34); |
| 29 | + Serial.println("LoRa init succeeded."); |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +void GPSRead() |
| 34 | +{ |
| 35 | + unsigned long age; |
| 36 | + gps.f_get_position(&flat, &flon, &age); |
| 37 | + flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;//save six decimal places |
| 38 | + flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6; |
| 39 | + if(flon!=1000.000000) |
| 40 | + { |
| 41 | + Serial.print(F("########### ")); |
| 42 | + Serial.print(F("NO.")); |
| 43 | + Serial.print(count); |
| 44 | + Serial.println(F(" ###########")); |
| 45 | + Serial.println(F("The longtitude and latitude and altitude are:")); |
| 46 | + Serial.print(F("[")); |
| 47 | + Serial.print(flat,3); |
| 48 | + Serial.print(F(",")); |
| 49 | + Serial.print(flon,3); |
| 50 | + Serial.print(F("]")); |
| 51 | + Serial.println(F("")); |
| 52 | + count++; |
| 53 | + } |
| 54 | +} |
| 55 | +void GPSWrite() |
| 56 | +{ |
| 57 | + char data[80] = "\0"; |
| 58 | + |
| 59 | + for(int i = 0; i < 50; i++) |
| 60 | + { |
| 61 | + data[i] = node_id[i]; |
| 62 | + } |
| 63 | + |
| 64 | + dtostrf(flon,0,3,flon_1); |
| 65 | + dtostrf(flat,0,3,flat_1); |
| 66 | + |
| 67 | + strcat(data,"{"); |
| 68 | + strcat(data,"\"location\":"); |
| 69 | + strcat(data,"{"); |
| 70 | + strcat(data,"\"value\":1"); |
| 71 | + strcat(data,","); |
| 72 | + strcat(data,"\"context\":"); |
| 73 | + strcat(data,"{"); |
| 74 | + strcat(data,"\"lat\":"); |
| 75 | + strcat(data,flat_1); |
| 76 | + strcat(data,","); |
| 77 | + strcat(data,"\"lng\":"); |
| 78 | + strcat(data,flon_1); |
| 79 | + strcat(data,"}"); |
| 80 | + strcat(data,"}"); |
| 81 | + strcat(data,"}"); |
| 82 | + strcpy((char *)datasend,data); |
| 83 | + |
| 84 | + // Serial.println((char *)datasend); |
| 85 | + delay(1000); |
| 86 | +} |
| 87 | + |
| 88 | +static void smartdelay(unsigned long ms) |
| 89 | +{ |
| 90 | + unsigned long start = millis(); |
| 91 | + do |
| 92 | + { |
| 93 | + while (ss.available()) |
| 94 | + { |
| 95 | + gps.encode(ss.read()); |
| 96 | + } |
| 97 | + } while (millis() - start < ms); |
| 98 | +} |
| 99 | + |
| 100 | +void SendData() |
| 101 | +{ |
| 102 | + LoRa.beginPacket(); |
| 103 | + LoRa.print((char *)datasend); |
| 104 | + LoRa.endPacket(); |
| 105 | + Serial.println("The packet is send successful"); |
| 106 | + delay(60000); // one minute |
| 107 | +} |
| 108 | + |
| 109 | +void loop() |
| 110 | +{ |
| 111 | + smartdelay(1000); |
| 112 | + GPSRead(); |
| 113 | + GPSWrite(); |
| 114 | + SendData(); |
| 115 | +} |
| 116 | + |
0 commit comments