-
Notifications
You must be signed in to change notification settings - Fork 4
/
PressureSensor-BMP280.ino
196 lines (163 loc) · 5.54 KB
/
PressureSensor-BMP280.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
195
196
/**
This Sketch is based on the MySensors Pressure Sensor Sketch which uses an older version of the Sensor (BMP085).
https://github.com/mysensors/MySensorsArduinoExamples/tree/master/examples/PressureSensor
Original Headers:
Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
Copyright (C) 2013-2015 Sensnology AB
Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
Documentation: http://www.mysensors.org
Support Forum: http://forum.mysensors.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*******************************
REVISION HISTORY
Version 1.0 - Henrik Ekblad
Version 1.1 - Alex Muthmann - https://dev-eth0.de
Version 1.2 - Alex Muthmann - https://dev-eth0.de
DESCRIPTION
Temperature and Pressure Sensor using Bosch's BMP280. It uses the Adafruit Library and provides both Temperature and current Pressure in seperate readings.
NOTE
If you want to run your Arduino with batteries, you should disable the brownout using an ISP:
bin\avrdude.exe -Cetc\avrdude.conf -v -patmega328p -cstk500v1 -PCOM9 -b19200 -U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0xff:m
*/
//////////////////////////////////////////////////////
///////////////// NODE CONFIGURATION /////////////////
//////////////////////////////////////////////////////
#define MY_DEBUG // Enable debug prints to serial monitor
//#define BME280; // Enable if you are using a BME280 instead of BMP280
// disable this if you don't want to measure the voltage
#define BATTERY_SENSE_PIN A0 // select the input pin for the battery sense point
#define SEND_VOLTAGE; // Enable if you want to send the voltage explicit
#define VMIN 1.9 // Battery Level 0%
#define VMAX 3.3 // Batter Level 100%
#define VBAT_PER_BITS 0.003363075 // Volts per bit on the A0 pin. For 470k and 1M Ohm @ 3.3V
// Enable and select radio type attached
#define MY_RADIO_NRF24
#define MY_RF24_CE_PIN 9
#define MY_RF24_CS_PIN 10
// If debugging is enabled, ignore issues with a missing gateway
#ifdef MY_DEBUG
#define MY_TRANSPORT_WAIT_READY_MS 1
#endif
#include <math.h>
#include <SPI.h>
#include <MySensors.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#ifdef BME280
#include <Adafruit_BME280.h>
Adafruit_BME280 sensor;
uint8_t addr = 0x76;
#else
#include <Adafruit_BMP280.h>
Adafruit_BMP280 sensor;
uint8_t addr = 0x76;
#endif
// Sleep time between reads (in milliseconds)
#ifdef MY_DEBUG
const unsigned long SLEEP_TIME = 5000; // debug? update every 5 seconds
#else
const unsigned long SLEEP_TIME = 300000; // once every 5 minutes
#endif
float lastPressure = -1;
float lastTemp = -1;
float lastVolt = -1;
float lastHumidity = -1;
bool initialSetup = true;
// IDs used
#define CHILD_ID_TEMP V_TEMP
#define CHILD_ID_BARO V_PRESSURE
#define CHILD_ID_BAT V_VOLTAGE
#define CHILD_ID_HUM V_HUM
MyMessage tempMsg(CHILD_ID_TEMP, V_TEMP);
MyMessage pressureMsg(CHILD_ID_BARO, V_PRESSURE);
MyMessage pressureMsgPrefix(CHILD_ID_BARO, V_UNIT_PREFIX);
#ifdef BATTERY_SENSE_PIN
#ifdef SEND_VOLTAGE
MyMessage voltMsg(CHILD_ID_BAT, V_VOLTAGE);
#endif
#endif
#ifdef BME280
MyMessage humidityMsg(CHILD_ID_HUM, V_HUM);
#endif
void setup()
{
Serial.begin(57600);
Serial.println("setup");
if (!sensor.begin(addr))
{
Serial.println("Could not find a valid Sensor, check wiring!");
while (1) {}
}
analogReference(INTERNAL);
}
void presentation() {
Serial.println("presentation");
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Pressure Sensor", "1.3");
// Register sensors to gw
present(CHILD_ID_TEMP, S_TEMP);
present(CHILD_ID_BARO, S_BARO);
#ifdef BATTERY_SENSE_PIN
present(CHILD_ID_BAT, S_MULTIMETER);
#endif
#ifdef BME280
present(CHILD_ID_HUM, S_HUM);
#endif
}
void loop()
{
Serial.println("loop");
// Read pressure as mBar and temperature
float pressure = sensor.readPressure()/100;
float temperature = sensor.readTemperature();
#ifdef BME280
float humidity = sensor.readHumidity();
#endif
#ifdef BATTERY_SENSE_PIN
// Read voltage
int sensorValue = analogRead(BATTERY_SENSE_PIN); // Battery monitoring reading
float volt = sensorValue * VBAT_PER_BITS;
int batteryPcnt = sensorValue / 10;
#endif
// Debug Output
#ifdef MY_DEBUG
Serial.print("Temperature = "); Serial.print(temperature); Serial.println(" *C");
Serial.print("Pressure = "); Serial.print(pressure); Serial.println(" hPa");
#ifdef BME280
Serial.print("Humidity = "); Serial.print(humidity); Serial.println(" %");
#endif
#ifdef BATTERY_SENSE_PIN
Serial.print("A0 analogRead = "); Serial.println(sensorValue);
Serial.print("Battery Voltage= "); Serial.print(volt); Serial.println(" V");
Serial.print("Battery percent= "); Serial.print(batteryPcnt); Serial.println(" %");
#endif
#endif
// Send values to gateway
if(!isnan(temperature)){
send(tempMsg.set(temperature, 1));
initialSetup = false;
}
if(!isnan(pressure)){
send(pressureMsgPrefix.set("hPa")); // Set fixed unit
send(pressureMsg.set(pressure, 2));
initialSetup = false;
}
#ifdef BME280
if(!isnan(humidity)){
send(humidityMsg.set(humidity, 0));
initialSetup = false;
}
#endif
#ifdef BATTERY_SENSE_PIN
#ifdef SEND_VOLTAGE
send(voltMsg.set(volt, 2));
#endif
sendBatteryLevel(batteryPcnt);
#endif
// only sleep, if initial values were send
if(!initialSetup){
sleep(SLEEP_TIME);
}
}