Skip to content

Commit 413a942

Browse files
committed
add serial reading and writing
Signed-off-by: faradaym <[email protected]>
1 parent c7e9ec9 commit 413a942

File tree

6 files changed

+545
-0
lines changed

6 files changed

+545
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* @file MamaDuck.ino
3+
* @brief Uses the built in Mama Duck.
4+
*/
5+
6+
#include <string>
7+
#include <vector>
8+
#include <arduino-timer.h>
9+
#include <CDP.h>
10+
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+
20+
#ifdef SERIAL_PORT_USBVIRTUAL
21+
#define Serial SERIAL_PORT_USBVIRTUAL
22+
#endif
23+
24+
bool sendData(std::vector<byte> message);
25+
bool runSensor(void *);
26+
27+
static void smartDelay(unsigned long ms);
28+
String getGPSData();
29+
// String getBMPData();
30+
31+
// create a built-in mama duck
32+
MamaDuck duck;
33+
34+
// create a timer with default settings
35+
auto timer = timer_create_default();
36+
37+
// for sending the counter message
38+
const int INTERVAL_MS = 60000;
39+
int counter = 1;
40+
bool setupOK = false;
41+
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+
55+
void setup() {
56+
// We are using a hardcoded device id here, but it should be retrieved or
57+
// given during the device provisioning then converted to a byte vector to
58+
// setup the duck NOTE: The Device ID must be exactly 8 bytes otherwise it
59+
// will get rejected
60+
std::string deviceId("MAMAMPU5");
61+
std::array<byte,8> devId;
62+
std::copy(deviceId.begin(), deviceId.end(), devId.begin());
63+
if (duck.setupWithDefaults(devId) != DUCK_ERR_NONE) {
64+
Serial.println("[MAMA] Failed to setup MamaDuck");
65+
return;
66+
}
67+
setupOK = true;
68+
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+
82+
// Initialize the timer. The timer thread runs separately from the main loop
83+
// and will trigger sending a counter message.
84+
Serial1.begin(9600, SERIAL_8N1, 4, 0);
85+
86+
timer.every(INTERVAL_MS, runSensor);
87+
Serial.println("[MAMA] Setup OK!");
88+
89+
}
90+
91+
std::vector<byte> stringToByteVector(const std::string& str) {
92+
std::vector<byte> byteVec;
93+
byteVec.reserve(str.length());
94+
95+
for (unsigned int i = 0; i < str.length(); ++i) {
96+
byteVec.push_back(static_cast<byte>(str[i]));
97+
}
98+
99+
return byteVec;
100+
}
101+
102+
void loop() {
103+
if (!setupOK) {
104+
return;
105+
}
106+
timer.tick();
107+
// Use the default run(). The Mama duck is designed to also forward data it receives
108+
// from other ducks, across the network. It has a basic routing mechanism built-in
109+
// to prevent messages from hoping endlessly.
110+
duck.run();
111+
}
112+
113+
bool runSensor(void *) {
114+
bool result;
115+
116+
// String bmpData = getBMPData();
117+
String gpsData = getGPSData();
118+
119+
String message = "\"" + String("DuckGPS: ") + gpsData + "\"";
120+
// String message = "\"" + String("DuckBMP: ") + bmpData + "\"";
121+
Serial.print("[MAMA] sensor data: ");
122+
Serial.println(message.c_str());
123+
Serial1.println(message.c_str());
124+
duck.storeSensorData(stringToByteVector(message.c_str()));
125+
return true;
126+
}
127+
128+
static void smartDelay(unsigned long ms)
129+
{
130+
unsigned long start = millis();
131+
do
132+
{
133+
while (GPS.available())
134+
tgps.encode(GPS.read());
135+
} while (millis() - start < ms);
136+
}
137+
138+
// Getting GPS data
139+
String getGPSData() {
140+
141+
// Encoding the GPS
142+
smartDelay(5000);
143+
144+
// Printing the GPS data
145+
Serial.println("--- GPS ---");
146+
Serial.print("Latitude : ");
147+
Serial.println(tgps.location.lat(), 5);
148+
Serial.print("Longitude : ");
149+
150+
// Creating a message of the Latitude and Longitude
151+
String sensorVal = "Lat:" + String(tgps.location.lat(), 5) + " Lng:" + String(tgps.location.lng(), 4);
152+
153+
// Check to see if GPS data is being received
154+
if (millis() > 5000 && tgps.charsProcessed() < 10)
155+
{
156+
Serial.println(F("No GPS data received: check wiring"));
157+
}
158+
159+
return sensorVal;
160+
}
161+
162+
// String getBMPData() {
163+
164+
// float T, P;
165+
166+
// bmp.getTemperature(&T);
167+
// Serial.println(T);
168+
// bmp.getPressure(&P);
169+
// Serial.println(P);
170+
171+
// String sensorVal = "Temp:" + String(T) + " Pres:" + String(P);
172+
173+
174+
// return sensorVal;
175+
// }
176+
177+
bool sendData(std::vector<byte> message) {
178+
bool sentOk = false;
179+
180+
int err = duck.sendData(topics::status, message);
181+
if (err == DUCK_ERR_NONE) {
182+
counter++;
183+
sentOk = true;
184+
}
185+
if (!sentOk) {
186+
std::string errMessage = "[MAMA] Failed to send data. error = " + std::to_string(err);
187+
Serial.println(errMessage.c_str());
188+
}
189+
return sentOk;
190+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
src_dir = .
13+
default_envs = prod_ttgo_lora32_v1
14+
description = MamaDuck CDP examples
15+
16+
[env]
17+
lib_deps =
18+
WIRE
19+
SPI
20+
contrem/arduino-timer@^3.0.1
21+
bblanchon/ArduinoJson@^7.0.3
22+
adafruit/Adafruit BMP085 Unified@^1.1.3
23+
adafruit/Adafruit Unified Sensor@^1.1.14
24+
25+
[env:esp32]
26+
lib_deps =
27+
28+
[env:local_cdp]
29+
lib_deps =
30+
symlink://../../../
31+
adafruit/Adafruit BMP085 Unified@^1.1.3
32+
adafruit/Adafruit Unified Sensor@^1.1.14
33+
34+
[env:release_cdp]
35+
lib_deps =
36+
symlink://../../../
37+
38+
[env:prod_heltec_wifi_lora_32_V2]
39+
platform = espressif32
40+
board = heltec_wifi_lora_32_V2
41+
framework = arduino
42+
monitor_speed = 115200
43+
monitor_filters = time
44+
lib_deps =
45+
${env:esp32.lib_deps}
46+
${env:release_cdp.lib_deps}
47+
48+
[env:prod_heltec_wifi_lora_32_V3]
49+
platform = espressif32
50+
board = heltec_wifi_lora_32_V3
51+
framework = arduino
52+
monitor_speed = 115200
53+
monitor_filters = time
54+
lib_deps =
55+
${env:esp32.lib_deps}
56+
${env:release_cdp.lib_deps}
57+
58+
[env:prod_lilygo_t_beam_sx1262]
59+
platform = espressif32
60+
board = ttgo-t-beam
61+
framework = arduino
62+
monitor_speed = 115200
63+
monitor_filters = time
64+
lib_deps =
65+
${env:esp32.lib_deps}
66+
${env:release_cdp.lib_deps}
67+
68+
[env:prod_ttgo_lora32_v1]
69+
platform = espressif32
70+
board = ttgo-lora32-v1
71+
framework = arduino
72+
monitor_speed = 115200
73+
monitor_filters = time
74+
lib_deps =
75+
${env:esp32.lib_deps}
76+
${env:release_cdp.lib_deps}
77+
78+
[env:local_heltec_wifi_lora_32_V2]
79+
platform = espressif32
80+
board = heltec_wifi_lora_32_V2
81+
framework = arduino
82+
monitor_speed = 115200
83+
monitor_filters = time
84+
lib_deps =
85+
${env:esp32.lib_deps}
86+
${env:local_cdp.lib_deps}
87+
88+
[env:local_heltec_wifi_lora_32_V3]
89+
platform = espressif32
90+
board = heltec_wifi_lora_32_V3
91+
framework = arduino
92+
monitor_speed = 115200
93+
monitor_filters = time
94+
lib_deps =
95+
${env:esp32.lib_deps}
96+
${env:local_cdp.lib_deps}
97+
98+
[env:local_lilygo_t_beam_sx1262]
99+
platform = espressif32
100+
board = ttgo-t-beam
101+
framework = arduino
102+
monitor_speed = 115200
103+
monitor_filters = time
104+
lib_deps =
105+
${env:esp32.lib_deps}
106+
${env:local_cdp.lib_deps}
107+
adafruit/Adafruit BMP085 Unified@^1.1.3
108+
adafruit/Adafruit Unified Sensor@^1.1.14
109+
110+
[env:local_ttgo_lora32_v1]
111+
platform = espressif32
112+
board = ttgo-lora32-v1
113+
framework = arduino
114+
monitor_speed = 115200
115+
monitor_filters = time
116+
lib_deps =
117+
${env:esp32.lib_deps}
118+
${env:local_cdp.lib_deps}
119+
adafruit/Adafruit BMP085 Unified@^1.1.3
120+
adafruit/Adafruit Unified Sensor@^1.1.14
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch

0 commit comments

Comments
 (0)