|
| 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 | +} |
0 commit comments