-
Notifications
You must be signed in to change notification settings - Fork 1
/
outdoor_sensor.ino
194 lines (147 loc) · 4.75 KB
/
outdoor_sensor.ino
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
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_SSD1306.h>
#include <SparkFun_Qwiic_Humidity_AHT20.h>
#include <Adafruit_BMP280.h>
#include <NRFLite.h>
// OUTDOOR SENSOR
volatile byte button_pressed = LOW; // Global variable for the ISR to monitor button
const byte button_compare = HIGH; // Byte to compare button_pressed to
const byte interrupt_pin = 2; // Using digital pin 2 as interrupt pin
// Instantiate an AHT20 sensor
AHT20 aht20;
// Instantiate a BMP280 sensor
Adafruit_BMP280 bmp280; // Use I2C interface
Adafruit_Sensor *bmp_temp = bmp280.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp280.getPressureSensor();
// Instantiate a display
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); // Set display size
// Instantiate a transceiver
NRFLite _radio;
const static uint8_t RADIO_ID = 1; // This transceiver
const static uint8_t DESTINATION_RADIO_ID = 0; // Other transceiver
const static uint8_t PIN_RADIO_CE = 9;
const static uint8_t PIN_RADIO_CSN = 10;
struct RadioPacket // Packet to be sent
{
float temp;
float pres;
float hum;
};
void setup(){
Serial.begin(115200);
Wire.begin();
// Setup interrupt service routine
pinMode(interrupt_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interrupt_pin), buttonPressed, FALLING);
// Setup AHT20 sensor
if (!aht20.begin())
{
Serial.println("Could not find a valid AHT20 sensor, check wiring"
" or try a different address!");
while (1) delay(500);
}
Serial.println("AHT20 setup finished");
// Setup BMP280 sensor
if (!bmp280.begin())
{
Serial.println("Could not find a valid BMP280 sensor, check wiring"
" or try a different address!");
while (1) delay(500);
}
Serial.println("BMP280 setup finished");
bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL, // Normal mode
Adafruit_BMP280::SAMPLING_X2, // Temperature oversampling
Adafruit_BMP280::SAMPLING_X16, // Pressure oversampling
Adafruit_BMP280::FILTER_X16, // Filtering
Adafruit_BMP280::STANDBY_MS_500); // Standby time
// Setup display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) // Address 0x3C for 128x32
{
Serial.println(("SSD1306 allocation failed"));
while (1) delay(500);
}
display.clearDisplay();
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
Serial.println("SSD1306 setup finished");
// Setup transceiver
if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN))
{
Serial.println("Can't communicate with radio!");
while (1) delay(500);
}
Serial.println("nRF24L01 setup finished");
}
void loop(){
// AHT20 measurements
float aht20hum = aht20.getHumidity();
float aht20temp = aht20.getTemperature();
// BMP280 measurements
float bmp280temp = bmp280.readTemperature();
float bmp280pres = bmp280.readPressure();
float temp = bmp280temp;
float pres = bmp280pres / 100; // Pressure in hPa
float hum = aht20hum;
// Print values on display if button is pressed
if (button_pressed == button_compare)
{
printData(temp, pres, hum);
button_pressed = LOW;
Serial.println("Button pressed, display activated");
}
delay(1000);
// Send values to base station
sendData(temp, pres, hum);
}
void printData(float temp, float pres, float hum){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
// Display temperature
display.setCursor(0, 0);
display.print(temp, 1);
display.print(" C");
display.display();
delay(3000);
display.clearDisplay();
// Display pressure
display.setCursor(0, 0);
display.print(pres, 1);
display.print(" hPa");
display.display();
delay(3000);
display.clearDisplay();
// Display humidity
display.setCursor(0, 0);
display.print(hum, 1);
display.print("%");
display.display();
delay(3000);
display.clearDisplay();
display.print("");
display.display();
}
void sendData(float temp, float pres, float hum){
static RadioPacket _radioData;
_radioData.temp = temp;
_radioData.pres = pres;
_radioData.hum = hum;
Serial.println("Sending...");
Serial.print(_radioData.temp);
Serial.print(_radioData.pres);
Serial.print(_radioData.hum);
if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData)))
{
Serial.println("...Success");
}
else
{
Serial.println("...Failed");
}
}
void buttonPressed(){
button_pressed = HIGH;
}