-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDustSensorExample.ino
More file actions
72 lines (56 loc) · 1.9 KB
/
DustSensorExample.ino
File metadata and controls
72 lines (56 loc) · 1.9 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
/**
* @brief Uses the built in Mama Duck with a GP2YDustSensor
*
* @date 2020-09-21
*
* @copyright Copyright (c) 2020
*
*/
#include <MamaDuck.h>
#include <arduino-timer.h>
#include <string>
#ifdef SERIAL_PORT_USBVIRTUAL
#define Serial SERIAL_PORT_USBVIRTUAL
#endif
// Include for DustSensor
#include <GP2YDustSensor.h>
const uint8_t SHARP_LED_PIN = 22; // Sharp Dust/particle sensor Led Pin
const uint8_t SHARP_VO_PIN = 0; // Sharp Dust/particle analog out pin used for reading
GP2YDustSensor dustSensor(GP2YDustSensorType::GP2Y1010AU0F, SHARP_LED_PIN, SHARP_VO_PIN);
// create a built-in mama duck
MamaDuck duck;
auto timer = timer_create_default();
const int INTERVAL_MS = 60000;
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::vector<byte> devId;
devId.insert(devId.end(), deviceId.begin(), deviceId.end());
// Use the default setup provided by the SDK
duck.setupWithDefaults(devId);
Serial.println("MAMA-DUCK...READY!");
// Dust sensor
dustSensor.begin();
// 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();
}
bool runSensor(void *) {
//Dust sensor
String sensorVal = "Current dust concentration: " + dustSensor.getDustDensity();
sensorVal += " ug/m3";
duck.sendData(topics::gp2y, sensorVal);
return true;
}