-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_station.ino
181 lines (141 loc) · 4.26 KB
/
base_station.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
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
#include <SparkFun_Qwiic_Humidity_AHT20.h>
#include <Adafruit_BMP280.h>
#include <NRFLite.h>
// BASE STATION
// 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
LiquidCrystal_I2C lcd(0x27,20,4); // Set address to 0x27 for a 16 X 2 display
// Instantiate a transceiver
NRFLite _radio;
const static uint8_t RADIO_ID = 0; // This transceiver
const static uint8_t DESTINATION_RADIO_ID = 1; // Other transceiver
const static uint8_t PIN_RADIO_CE = 9;
const static uint8_t PIN_RADIO_CSN = 10;
struct RadioPacket // Packet to be received
{
float temp;
float pres;
float hum;
};
enum TypeOfVal {temp, pres, hum}; // Determines which display function to use
void setup(){
Serial.begin(115200);
Wire.begin();
// 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);
}
bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X2, // Temperature oversampling
Adafruit_BMP280::SAMPLING_X16, // Pressure oversampling
Adafruit_BMP280::FILTER_X16, // Filtering
Adafruit_BMP280::STANDBY_MS_500); // Standby time
Serial.println("BMP280 setup finished");
// Setup display
lcd.init();
lcd.backlight();
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();
// Receive values
RadioPacket outdoor_data = receiveData();
RadioPacket indoor_data;
indoor_data.temp = bmp280temp;
indoor_data.pres = bmp280pres / 100; // Pressure in hPa
indoor_data.hum = aht20hum;
// Print temperatures on display
printData(indoor_data.temp, outdoor_data.temp, temp);
delay(4000);
lcd.clear();
// Print pressures on display
printData(indoor_data.pres, outdoor_data.pres, pres);
delay(4000);
lcd.clear();
// Print humidities on display
printData(indoor_data.hum, outdoor_data.hum, hum);
delay(4000);
lcd.clear();
}
RadioPacket receiveData(){
RadioPacket _radioData = {-99.0, -99.0, -99.0};
if (_radio.hasData())
{
_radio.readData(&_radioData);
}
return _radioData;
}
void printData(float indoor_val, float outdoor_val, TypeOfVal type_of_val){
if (type_of_val == temp)
{
lcd.setCursor(4,0);
lcd.print(indoor_val);
lcd.setCursor(10,0);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(4,1);
lcd.print(outdoor_val);
lcd.setCursor(10,1);
lcd.print((char)223);
lcd.print("C");
}
else if (type_of_val == pres)
{
lcd.setCursor(3,0);
lcd.print(indoor_val);
lcd.setCursor(9,0);
lcd.print(" hPa");
lcd.setCursor(3,1);
lcd.print(outdoor_val);
lcd.setCursor(9,1);
lcd.print(" hPa");
}
else if (type_of_val == hum)
{
lcd.setCursor(5,0);
lcd.print(indoor_val);
lcd.setCursor(10,0);
lcd.print("%");
lcd.setCursor(5,1);
lcd.print(outdoor_val);
lcd.setCursor(10,1);
lcd.print("%");
}
else
{
lcd.setCursor(0,1);
lcd.print("Unknown data type!");
}
}