-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTBeam-GPS-Example.ino
More file actions
151 lines (125 loc) · 4.15 KB
/
TBeam-GPS-Example.ino
File metadata and controls
151 lines (125 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file TBeam-GPS-Example.ino
* @brief Uses the built in Mama Duck with some customizations.
*
* This example is a Mama Duck, that has GPS capabilities and will send the GPS data with the GPS topic based on the set timer.
* @date 2020-09-21
*
* @copyright Copyright (c) 2020
* ClusterDuck Protocol
*/
#include <string>
#include "arduino-timer.h"
#include <MamaDuck.h>
#include <vector>
#ifdef SERIAL_PORT_USBVIRTUAL
#define Serial SERIAL_PORT_USBVIRTUAL
#endif
// GPS
#include <TinyGPS++.h>
// Power chip
#include <axp20x.h>
TinyGPSPlus tgps;
HardwareSerial GPS(1);
AXP20X_Class axp;
MamaDuck duck;
auto timer = timer_create_default();
const int INTERVAL_MS = 20000;
char message[32];
int counter = 1;
void setup() {
// We are using a hardcoded device id here, but it should be retrieved or
// given during the device provisioning then converted to a byte vector to
// setup the duck NOTE: The Device ID must be exactly 8 bytes otherwise it
// will get rejected
std::string deviceId("MAMA0001");
std::array<byte,8> devId;
std::copy(deviceId.begin(), deviceId.end(), devId.begin());
// Use the default setup provided by the SDK
duck.setupWithDefaults(devId);
Serial.println("MAMA-DUCK...READY!");
// Setup AXP
Wire.begin(21, 22);
if (!axp.begin(Wire, AXP192_SLAVE_ADDRESS)) {
Serial.println("AXP192 Begin PASS");
} else {
Serial.println("AXP192 Begin FAIL");
}
axp.setPowerOutPut(AXP192_LDO2, AXP202_ON);
axp.setPowerOutPut(AXP192_LDO3, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC2, AXP202_ON);
axp.setPowerOutPut(AXP192_EXTEN, AXP202_ON);
axp.setPowerOutPut(AXP192_DCDC1, AXP202_ON);
GPS.begin(9600, SERIAL_8N1, 34, 12); //17-TX 18-RX
// initialize the timer. The timer thread runs separately from the main loop
// and will trigger sending a counter message.
timer.every(INTERVAL_MS, runSensor);
}
void loop() {
timer.tick();
// Use the default run(). The Mama duck is designed to also forward data it receives
// from other ducks, across the network. It has a basic routing mechanism built-in
// to prevent messages from hoping endlessly.
duck.run();
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (GPS.available())
tgps.encode(GPS.read());
} while (millis() - start < ms);
}
// Getting GPS data
String getGPSData() {
// Encoding the GPS
smartDelay(5000);
// Printing the GPS data
Serial.println("--- GPS ---");
Serial.print("Latitude : ");
Serial.println(tgps.location.lat(), 5);
Serial.print("Longitude : ");
Serial.println(tgps.location.lng(), 4);
Serial.print("Altitude : ");
Serial.print(tgps.altitude.feet() / 3.2808);
Serial.println("M");
Serial.print("Satellites: ");
Serial.println(tgps.satellites.value());
Serial.print("Time : ");
Serial.print(tgps.time.hour());
Serial.print(":");
Serial.print(tgps.time.minute());
Serial.print(":");
Serial.println(tgps.time.second());
Serial.print("Speed : ");
Serial.println(tgps.speed.kmph());
Serial.println("**********************");
// Creating a message of the Latitude and Longitude
String sensorVal = "Lat:" + String(tgps.location.lat(), 5) + " Lng:" + String(tgps.location.lng(), 4) + " Alt:" + String(tgps.altitude.feet() / 3.2808);
// Check to see if GPS data is being received
if (millis() > 5000 && tgps.charsProcessed() < 10)
{
Serial.println(F("No GPS data received: check wiring"));
}
return sensorVal;
}
// Converting String to byte vector
std::vector<byte> stringToByteVector(const String& str) {
std::vector<byte> byteVec;
byteVec.reserve(str.length());
for (unsigned int i = 0; i < str.length(); ++i) {
byteVec.push_back(static_cast<byte>(str[i]));
}
return byteVec;
}
bool runSensor(void *) {
bool result;
String sensorVal = getGPSData();
Serial.print("[MAMA] sensor data: ");
Serial.println(sensorVal);
//Send gps data
std::vector<byte> message = stringToByteVector(sensorVal);
duck.sendData(topics::location, message);
return true;
}