Skip to content

Commit c7e9ec9

Browse files
committed
demo for tak offsite
Signed-off-by: faradaym <[email protected]>
1 parent 8667b5c commit c7e9ec9

File tree

8 files changed

+180
-71
lines changed

8 files changed

+180
-71
lines changed

examples/Basic-Ducks/MamaDuck/MamaDuck.ino

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88
#include <arduino-timer.h>
99
#include <CDP.h>
1010

11+
// GPS Setup
12+
#include <TinyGPS++.h>
13+
TinyGPSPlus tgps;
14+
HardwareSerial GPS(1);
15+
16+
// // Setup BMP180
17+
// #include <Adafruit_BMP085_U.h>
18+
// Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
19+
1120
#ifdef SERIAL_PORT_USBVIRTUAL
1221
#define Serial SERIAL_PORT_USBVIRTUAL
1322
#endif
1423

1524
bool sendData(std::vector<byte> message);
1625
bool runSensor(void *);
1726

27+
static void smartDelay(unsigned long ms);
28+
String getGPSData();
29+
// String getBMPData();
30+
1831
// create a built-in mama duck
1932
MamaDuck duck;
2033

@@ -26,6 +39,19 @@ const int INTERVAL_MS = 60000;
2639
int counter = 1;
2740
bool setupOK = false;
2841

42+
std::string arduinoStringFromHex(byte* data, int size)
43+
{
44+
std::string buf = "";
45+
buf.reserve(size * 2); // 2 digit hex
46+
const char* cs = "0123456789ABCDEF";
47+
for (int i = 0; i < size; i++) {
48+
byte val = data[i];
49+
buf += cs[(val >> 4) & 0x0F];
50+
buf += cs[val & 0x0F];
51+
}
52+
return buf;
53+
}
54+
2955
void setup() {
3056
// We are using a hardcoded device id here, but it should be retrieved or
3157
// given during the device provisioning then converted to a byte vector to
@@ -38,9 +64,21 @@ void setup() {
3864
Serial.println("[MAMA] Failed to setup MamaDuck");
3965
return;
4066
}
41-
4267
setupOK = true;
4368

69+
GPS.begin(9600, SERIAL_8N1, 34, 12);
70+
71+
// // BMP setup
72+
// if (!bmp.begin()) {
73+
// /* There was a problem detecting the BMP085 ... check your connections */
74+
// Serial.print(
75+
// "Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
76+
// while (1)
77+
// ;
78+
// } else {
79+
// Serial.println("BMP on");
80+
// }
81+
4482
// Initialize the timer. The timer thread runs separately from the main loop
4583
// and will trigger sending a counter message.
4684
timer.every(INTERVAL_MS, runSensor);
@@ -72,20 +110,75 @@ void loop() {
72110

73111
bool runSensor(void *) {
74112
bool result;
75-
76-
std::string message = std::string("Counter:") + std::to_string(counter) + " " + std::string("Free Memory:") + std::to_string(freeMemory());
113+
114+
// String bmpData = getBMPData();
115+
String gpsData = getGPSData();
116+
117+
String message = "\"" + String("GPS: ") + gpsData + "\"";
118+
// String message = "\"" + String("BMP: ") + bmpData + "\"";
77119
Serial.print("[MAMA] sensor data: ");
78120
Serial.println(message.c_str());
79121

80-
result = sendData(stringToByteVector(message));
81-
if (result) {
82-
Serial.println("[MAMA] runSensor ok.");
83-
} else {
84-
Serial.println("[MAMA] runSensor failed.");
122+
duck.storeSensorData(stringToByteVector(message.c_str()));
123+
return true;
124+
}
125+
126+
static void smartDelay(unsigned long ms)
127+
{
128+
unsigned long start = millis();
129+
do
130+
{
131+
while (GPS.available())
132+
tgps.encode(GPS.read());
133+
} while (millis() - start < ms);
134+
}
135+
136+
// Getting GPS data
137+
String getGPSData() {
138+
139+
// Encoding the GPS
140+
smartDelay(5000);
141+
142+
// Printing the GPS data
143+
Serial.println("--- GPS ---");
144+
Serial.print("Latitude : ");
145+
Serial.println(tgps.location.lat(), 5);
146+
Serial.print("Longitude : ");
147+
Serial.println(tgps.location.lng(), 4);
148+
Serial.print("Altitude : ");
149+
Serial.print(tgps.altitude.feet() / 3.2808);
150+
Serial.println("M");
151+
Serial.print("Satellites: ");
152+
Serial.println(tgps.satellites.value());
153+
Serial.println("**********************");
154+
155+
// Creating a message of the Latitude and Longitude
156+
String sensorVal = "Lat:" + String(tgps.location.lat(), 5) + " Lng:" + String(tgps.location.lng(), 4);
157+
158+
// Check to see if GPS data is being received
159+
if (millis() > 5000 && tgps.charsProcessed() < 10)
160+
{
161+
Serial.println(F("No GPS data received: check wiring"));
85162
}
86-
return result;
163+
164+
return sensorVal;
87165
}
88166

167+
// String getBMPData() {
168+
169+
// float T, P;
170+
171+
// bmp.getTemperature(&T);
172+
// Serial.println(T);
173+
// bmp.getPressure(&P);
174+
// Serial.println(P);
175+
176+
// String sensorVal = "Temp:" + String(T) + " Pres:" + String(P);
177+
178+
179+
// return sensorVal;
180+
// }
181+
89182
bool sendData(std::vector<byte> message) {
90183
bool sentOk = false;
91184

examples/Basic-Ducks/MamaDuck/platformio.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ lib_deps =
1919
SPI
2020
contrem/arduino-timer@^3.0.1
2121
bblanchon/ArduinoJson@^7.0.3
22+
adafruit/Adafruit BMP085 Unified@^1.1.3
23+
adafruit/Adafruit Unified Sensor@^1.1.14
2224

2325
[env:esp32]
2426
lib_deps =
2527

2628
[env:local_cdp]
2729
lib_deps =
2830
symlink://../../../
31+
adafruit/Adafruit BMP085 Unified@^1.1.3
32+
adafruit/Adafruit Unified Sensor@^1.1.14
2933

3034
[env:release_cdp]
3135
lib_deps =
@@ -100,6 +104,8 @@ monitor_filters = time
100104
lib_deps =
101105
${env:esp32.lib_deps}
102106
${env:local_cdp.lib_deps}
107+
adafruit/Adafruit BMP085 Unified@^1.1.3
108+
adafruit/Adafruit Unified Sensor@^1.1.14
103109

104110
[env:local_ttgo_lora32_v1]
105111
platform = espressif32
@@ -110,3 +116,5 @@ monitor_filters = time
110116
lib_deps =
111117
${env:esp32.lib_deps}
112118
${env:local_cdp.lib_deps}
119+
adafruit/Adafruit BMP085 Unified@^1.1.3
120+
adafruit/Adafruit Unified Sensor@^1.1.14

src/DuckNet.cpp

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
DuckNet::DuckNet(BloomFilter *filter): bloomFilter(filter) {
66
}
77

8-
CircularBuffer atakBuffer = CircularBuffer(CDPCFG_CDP_CHATBUF_SIZE);
8+
CircularBuffer atakMessageBuffer = CircularBuffer(CDPCFG_CDP_CHATBUF_SIZE);
99

1010
#ifndef CDPCFG_WIFI_NONE
1111

@@ -152,46 +152,46 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
152152
}
153153
});
154154

155-
webServer.on("/sendAtakMessage.json", HTTP_POST, [&](AsyncWebServerRequest* request) {
156-
int err = DUCK_ERR_NONE;
157-
158-
std::vector<byte> message;
159-
std::string clientId = "";
160-
161-
const AsyncWebParameter* p = request->getParam(0);
162-
std::string msg = p->value().c_str();
163-
message.insert(message.end(), msg.begin(), msg.end());
164-
std::vector<byte> muid;
165-
166-
txPacket->prepareForSending(bloomFilter, BROADCAST_DUID, DuckType::UNKNOWN, topics::achat, message);
167-
auto atakPacket = txPacket->getBuffer(); //TODO: no auto
168-
169-
addToAtakBuffer(CdpPacket(atakPacket));
170-
err = duckRadio.sendData(atakPacket);
171-
172-
switch (err) {
173-
case DUCK_ERR_NONE:
174-
{
175-
loginfo_ln("success sending packer");
176-
request->send(200, "text/html", "OK.");
177-
}
178-
break;
179-
case DUCKLORA_ERR_MSG_TOO_LARGE:
180-
request->send(413, "text/html", "Message payload too big!");
181-
break;
182-
case DUCKLORA_ERR_HANDLE_PACKET:
183-
request->send(400, "text/html", "BadRequest");
184-
break;
185-
default:
186-
request->send(500, "text/html", "Oops! Unknown error.");
187-
break;
188-
}
189-
});
155+
// webServer.on("/sendAtakMessage.json", HTTP_POST, [&](AsyncWebServerRequest* request) {
156+
// int err = DUCK_ERR_NONE;
157+
158+
// std::vector<byte> message;
159+
// std::string clientId = "";
160+
161+
// const AsyncWebParameter* p = request->getParam(0);
162+
// std::string msg = p->value().c_str();
163+
// message.insert(message.end(), msg.begin(), msg.end());
164+
// std::vector<byte> muid;
165+
166+
// txPacket->prepareForSending(bloomFilter, BROADCAST_DUID, DuckType::UNKNOWN, topics::achat, message);
167+
// auto atakPacket = txPacket->getBuffer(); //TODO: no auto
168+
169+
// addToAtakBuffer(CdpPacket(atakPacket));
170+
// err = duckRadio.sendData(atakPacket);
171+
172+
// switch (err) {
173+
// case DUCK_ERR_NONE:
174+
// {
175+
// loginfo_ln("success sending packer");
176+
// request->send(200, "text/html", "OK.");
177+
// }
178+
// break;
179+
// case DUCKLORA_ERR_MSG_TOO_LARGE:
180+
// request->send(413, "text/html", "Message payload too big!");
181+
// break;
182+
// case DUCKLORA_ERR_HANDLE_PACKET:
183+
// request->send(400, "text/html", "BadRequest");
184+
// break;
185+
// default:
186+
// request->send(500, "text/html", "Oops! Unknown error.");
187+
// break;
188+
// }
189+
// });
190190

191191
webServer.on("/atakHistory", HTTP_GET, [&](AsyncWebServerRequest* request) {
192192
size_t atakSize;
193193

194-
uint8_t* atakBytes = DuckNet::serializeAtakHistoryToBytes(&atakBuffer, &atakSize);
194+
uint8_t* atakBytes = DuckNet::serializeAtakHistoryToBytes(&atakMessageBuffer, &atakSize);
195195

196196
// std::shared_ptr<uint8_t> atakData(atakBytes, [](uint8_t* p) { delete[] p; });
197197

@@ -203,8 +203,7 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
203203
});
204204

205205
webServer.on("/atakChatHistory", HTTP_GET, [&](AsyncWebServerRequest* request){
206-
loginfo_ln("this is what should be called in captive portal");
207-
std::string response = DuckNet::serializeAtakHistoryToJSON(&atakBuffer);
206+
std::string response = DuckNet::serializeAtakHistoryToJSON(&atakMessageBuffer);
208207
const char* res = response.c_str();
209208
request->send(200, "text/json", res);
210209
});
@@ -357,10 +356,14 @@ int DuckNet::setupInternet(std::string ssid, std::string password)
357356

358357
}
359358

360-
void DuckNet::addToAtakBuffer(CdpPacket message) {
361-
if(atakBuffer.findMuid(message.muid) < 0){
362-
message.timeReceived = millis();
363-
atakBuffer.push(message);
359+
void DuckNet::addToAtakBuffer(std::vector<byte> message) {
360+
txPacket->prepareForSending(bloomFilter, BROADCAST_DUID, DuckType::UNKNOWN, topics::achat, message);
361+
auto atakBuffer = txPacket->getBuffer(); //TODO: no auto
362+
CdpPacket atakPacket(atakBuffer);
363+
364+
if(atakMessageBuffer.findMuid(atakPacket.muid) < 0){
365+
atakPacket.timeReceived = millis();
366+
atakMessageBuffer.push(atakPacket);
364367
loginfo_ln("pushed new packet to buffer");
365368
}
366369
}

src/Ducks/Duck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Duck::~Duck() {
2828
delete duckNet;
2929
}
3030

31+
void Duck::storeSensorData(std::vector<byte> sensorData){
32+
duckNet->addToAtakBuffer(sensorData);
33+
}
34+
3135
void Duck::setEncrypt(bool state) {
3236
duckcrypto::setEncrypt(state);
3337
}

src/Ducks/MamaDuck.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ void MamaDuck::handleReceivedPacket() {
107107
loginfo_ln("handleReceivedPacket: packet RELAY DONE");
108108
}
109109
break;
110-
case topics::achat:{
111-
packet.timeReceived = millis();
112-
duckNet->addToAtakBuffer(packet);
113-
114-
std::vector<byte> data;
115-
byte numPairs = 1;
116-
data.insert(data.end(), numPairs);
117-
data.insert(data.end(), packet.muid.begin(), packet.muid.end());
118-
119-
err = duckRadio.relayPacket(rxPacket);
120-
if (err != DUCK_ERR_NONE) {
121-
logerr_ln("====> ERROR handleReceivedPacket failed to relay. rc = %d",err);
122-
} else {
123-
loginfo_ln("handleReceivedPacket: packet RELAY DONE");
124-
}
125-
}
110+
// case topics::achat:{
111+
// packet.timeReceived = millis();
112+
// duckNet->addToAtakBuffer(packet);
113+
114+
// std::vector<byte> data;
115+
// byte numPairs = 1;
116+
// data.insert(data.end(), numPairs);
117+
// data.insert(data.end(), packet.muid.begin(), packet.muid.end());
118+
119+
// err = duckRadio.relayPacket(rxPacket);
120+
// if (err != DUCK_ERR_NONE) {
121+
// logerr_ln("====> ERROR handleReceivedPacket failed to relay. rc = %d",err);
122+
// } else {
123+
// loginfo_ln("handleReceivedPacket: packet RELAY DONE");
124+
// }
125+
// }
126126
break;
127127
default:
128128
err = duckRadio.relayPacket(rxPacket);

src/include/Duck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Duck {
2828
virtual ~Duck();
2929

3030
std::string getCDPVersion() { return duckutils::getCDPVersion(); }
31+
void storeSensorData(std::vector<byte> sensorData);
3132

3233
/**
3334
* @brief Set the Device Name object

src/include/DuckNet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class DuckNet {
119119
return DUCK_ERR_NONE;
120120
}
121121

122-
void addToAtakBuffer(DuckPacket message) {
122+
void addToAtakBuffer(DuckPacket std::vector<byte>) {
123123
logwarn_ln("WARNING addToAtakBuffer skipped, device has no WiFi.");
124124
}
125125
std::string serializeAtakHistoryToJSON(CircularBuffer* buffer) {
@@ -213,7 +213,7 @@ class DuckNet {
213213
/**
214214
* @brief add ATAK message to history buffer
215215
*/
216-
void addToAtakBuffer(CdpPacket message);
216+
void addToAtakBuffer(std::vector<byte> message);
217217

218218
/**
219219
* @brief retrieve all messages from from message circular buffer

src/include/chatPage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const char chat_page[] PROGMEM = R"=====(
3939
card.classList.add("received-message-card");
4040
}
4141
// console.log(newMessage)
42-
card.innerHTML = newMessage.body.body + '</p><span class="duid">FROM DUCKID: '
42+
card.innerHTML = newMessage.body + '</p><span class="duid">FROM DUCKID: '
4343
+ newMessage.sduid + '</span></p><span class="name">';
4444

4545
document.getElementById('message-container').prepend(card);

0 commit comments

Comments
 (0)