-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
210 lines (162 loc) · 4.81 KB
/
main.cpp
File metadata and controls
210 lines (162 loc) · 4.81 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <Arduino.h>
#include <CayenneLPP.h>
#include <Wire.h>
#include "SensorCollection.h"
#include "LoRaWAN.h"
#include "PrgButton.h"
#include "config.h"
#include "lorawan-keys.h"
constexpr uint8_t kLoRaWANMaxPayloadSize = 51;
constexpr uint8_t kMaxSensors = 4;
#ifdef USE_DISPLAY
#include "Display.h"
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, DISPLAY_RESET_PIN, DISPLAY_CLOCK_PIN, DISPLAY_DATA_PIN);
Display display(&u8g2);
DisplayState displayState = {
.batteryVoltage = 0,
.devEUI = devEUI,
.isJoined = false,
.fcntUp = 0};
#endif // USE_DISPLAY
#ifdef USE_HELTEC_BATTERY
#include "HeltecBatterySensor_U.h"
HeltecBatterySensor_U batterySensor(HELTEC_BATTERY_SENSOR_ID, HELTEC_BATTERY_BATTERY_PIN, HELTEC_BATTERY_DRAIN_PIN);
#endif // USE_HELTEC_BATTERY
#ifdef USE_HTU
#include "HTU21DF_Humidity_U.h"
#include "HTU21DF_Temperature_U.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
HTU21DF_Humidity_U humiditySensor(HTU_HUMIDITY_SENSOR_ID, &htu);
HTU21DF_Temperature_U temperatureSensor(HTU_TEMPERATURE_SENSOR_ID, &htu);
#endif // USE_HTU
#ifdef USE_BME280
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
#endif // USE_BME280
#ifdef USE_SR04
#include "HCSR04_U.h"
HCSR04_U distanceSensor(SR04_SENSOR_ID, SR04_TRIGGER_PIN, SR04_ECHO_PIN);
#endif // USE_SR04
#ifdef USE_DS18
#include "DallasTemperature_U.h"
OneWire oneWire(DS18_ONEWIRE_PIN);
DallasTemperature tempSensors(&oneWire);
DallasTemperature_U *temperatureSensors[DS18_SENSOR_NUM];
#endif // USE_DS18
SensorCollection sensors(kMaxSensors, kLoRaWANMaxPayloadSize);
CayenneLPP *lpp;
PrgButton prgButton;
LoRaWAN loraNode(&joinEUI, &devEUI, nwkKey, appKey);
#ifdef DEBUG_PRINT_SENSOR_VALUES
DynamicJsonDocument jsonBuffer(1024);
JsonObject root = jsonBuffer.to<JsonObject>();
#endif // DEBUG_PRINT_SENSOR_VALUES
void sleep()
{
prgButton.sleep();
loraNode.sleep(LORAWAN_UPLINK_INTERVAL);
}
void work()
{
#ifdef USE_BME280
bme.takeForcedMeasurement();
#endif // USE_BME280
lpp = sensors.update();
#ifdef DEBUG_PRINT_SENSOR_VALUES
root.clear();
lpp->decodeTTN(lpp->getBuffer(), lpp->getSize(), root);
serializeJsonPretty(root, Serial);
Serial.println();
#endif // DEBUG_PRINT_SENSOR_VALUES
loraNode.send(LORAWAN_F_PORT, lpp);
sleep();
}
void setup()
{
Serial.begin(115200);
#if defined(ARDUINO_HELTEC_WIFI_LORA_32_V2)
// See https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series/issues/62
Wire.begin(4, 15);
#endif
#ifdef USE_DISPLAY
display.begin();
#endif // USE_DISPLAY
#ifdef USE_HELTEC_BATTERY
batterySensor.begin();
sensors.addSensor(&batterySensor);
#endif // USE_HELTEC_BATTERY
#ifdef USE_HTU
humiditySensor.begin();
temperatureSensor.begin();
sensors.addSensor(&humiditySensor);
sensors.addSensor(&temperatureSensor);
#endif // USE_HTU
#ifdef USE_BME280
bme.begin(BME280_I2C_ADDRESS);
/* Settings are according to BME280 datasheet recommended modes of operation
* in section 3.5.1: weather monitoring.
*/
bme.setSampling(
Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1,
Adafruit_BME280::SAMPLING_X1,
Adafruit_BME280::SAMPLING_X1,
Adafruit_BME280::FILTER_OFF);
sensors.addSensor(bme.getTemperatureSensor(), BME280_TEMPERATURE_SENSOR_ID);
sensors.addSensor(bme.getHumiditySensor(), BME280_HUMIDITY_SENSOR_ID);
sensors.addSensor(bme.getPressureSensor(), BME280_PRESSURE_SENSOR_ID);
#endif // USE_BME280
#ifdef USE_SR04
distanceSensor.begin();
sensors.addSensor(&distanceSensor);
#endif // USE_SR04
#ifdef USE_DS18
tempSensors.begin();
for (uint8_t i = 0; i < DS18_SENSOR_NUM; i += 1)
{
temperatureSensors[i] = new DallasTemperature_U(DS18_SENSOR_ID + i, i, &tempSensors);
temperatureSensors[i]->begin();
sensors.addSensor(temperatureSensors[i]);
}
#endif // USE_DS18
prgButton.begin();
loraNode.begin();
esp_sleep_wakeup_cause_t wakeupCause = esp_sleep_get_wakeup_cause();
if (wakeupCause == ESP_SLEEP_WAKEUP_TIMER || wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED)
{
// Start an uplink after wakeup from regular reset or timer
work();
}
}
void loop()
{
unsigned long prgBtnDuration = prgButton.update();
if (prgBtnDuration > 0)
{
Serial.printf("Button pressed for: %lu ms\n", prgBtnDuration);
if (prgBtnDuration > 5000)
{
Serial.println(F("Reset device"));
loraNode.reset();
}
else if (prgBtnDuration > 1000)
{
Serial.println(F("Immediate uplink"));
work();
}
else
{
Serial.println(F("Display device info"));
#ifdef USE_HELTEC_BATTERY
sensors_event_t event;
batterySensor.getEvent(&event);
displayState.batteryVoltage = event.voltage;
#endif // USE_HELTEC_BATTERY
displayState.isJoined = loraNode.getNode()->isJoined();
displayState.fcntUp = loraNode.getNode()->getFcntUp();
display.print(&displayState);
delay(10000);
}
sleep();
}
}