-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySensors.cpp
121 lines (105 loc) · 3.19 KB
/
MySensors.cpp
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
#include "MySensors.h"
#define PIN_ENABLE_MIC 14
#define PIN_TOUT 17
#define MIC_SAMPLE_MS 1000
#define MIC_LEVEL_SCALE 64
#define STRING_BUFFER 64
MySensors::MySensors(void)
{
pTsl = new Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT);
pBmp = new Adafruit_BMP280();
pinMode(PIN_ENABLE_MIC, OUTPUT);
digitalWrite(PIN_ENABLE_MIC, LOW);
}
MySensors::~MySensors()
{
delete pTsl;
delete pBmp;
digitalWrite(PIN_ENABLE_MIC, LOW);
}
void MySensors::activate(void)
{
Wire.begin();
if (pTsl->begin()) {
pTsl->enableAutoRange(true); // Auto-gain ... switches automatically between 1x and 16x
pTsl->setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); // medium resolution and speed
} else {
dprintln(F("Couldn't find TSL2561."));
}
if (pBmp->begin(BMP280_ADDRESS_ALT)) {
pBmp->setSampling(
Adafruit_BMP280::MODE_FORCED, // Operating Mode
Adafruit_BMP280::SAMPLING_X1, // Temp. oversampling
Adafruit_BMP280::SAMPLING_NONE, // Pressure oversampling
Adafruit_BMP280::FILTER_OFF, // Filtering
Adafruit_BMP280::STANDBY_MS_4000); // Standby time
} else {
dprintln(F("Couldn't find BMP280."));
}
digitalWrite(PIN_ENABLE_MIC, HIGH);
isActive = true;
}
bool MySensors::doSensing(RECORD_T &record)
{
if (!isActive) return false;
uint16_t broadband, ir;
pTsl->getLuminosity(&broadband, &ir);
record.illum = pTsl->calculateLux(broadband, ir);
record.temp = pBmp->readTemperature();
record.sound = doSensingSound(MIC_SAMPLE_MS);
dprint(F("Luminosity = "));
dprint(record.illum);
dprintln(F(" lux"));
dprint(F("Temperature = "));
dprint(record.temp);
dprintln(F(" *C"));
dprint(F("Sound level = "));
dprintln(record.sound);
dprintln();
return true;
}
float MySensors::doSensingSound(long duration)
{
long targetMillis = millis() + duration;
long diffTotal = 0;
int lastValue = analogRead(PIN_TOUT), samples = 0;
while (millis() < targetMillis) {
delay(1);
int currentValue = analogRead(PIN_TOUT);
diffTotal += abs(currentValue - lastValue);
lastValue = currentValue;
samples++;
}
return diffTotal * MIC_LEVEL_SCALE / (float)samples;
}
void MySensors::inactivate(void)
{
isActive = false;
}
String MySensors::convert2JSON(RECORD_T &record)
{
char buf[STRING_BUFFER];
sprintf(buf, "{\"illum\":%.2f,\"temp\":%.2f,\"sound\":%.2f}", record.illum, record.temp, record.sound);
return String(buf);
}
void MySensors::scanI2C(void)
{
Wire.begin();
dprintln(F("----- Scan I2C Devices -----"));
int nDevices = 0;
for (byte adrs = 1; adrs < 127; adrs++) {
Wire.beginTransmission(adrs);
byte error = Wire.endTransmission();
if (error == 0) {
dprint(F("I2C device found at address 0x"));
dprintf("%02X !\r\n", adrs);
nDevices++;
} else if (error == 4) {
dprint(F("Unknown error at address 0x"));
dprintf("%02X !\r\n", adrs);
}
}
dprint(nDevices);
dprintln(F(" device(s) is found."));
dprintln();
}