-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoWeatherStation.ino
More file actions
152 lines (126 loc) · 4.8 KB
/
ArduinoWeatherStation.ino
File metadata and controls
152 lines (126 loc) · 4.8 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
//===========================================================================
//============================= Libraries ===================================
//===========================================================================
#include "LowPower.h"
#include <Adafruit_BME280.h>
#include <Wire.h>
//===========================================================================
//============================= Settings ====================================
//===========================================================================
#define sleepTimeHours 0
#define sleepTimeMinutes 10
#define sleepTimeSeconds 0
#define temperature true
#define humidity true
#define pressure true
const String deviceId = "A01";
//===========================================================================
//============================= Variables ===================================
//===========================================================================
int errorCode;
struct WeatherStation {
String id;
float temp;
float hum;
float pres;
};
Adafruit_BME280 bme; // I2C
#define ledPinRed 2
#define ledPinGreen 3
#define powerRadio 7
#define powerBmeSensor 8
//===========================================================================
//============================== SETUP ======================================
//===========================================================================
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(ledPinRed,OUTPUT);
pinMode(ledPinGreen,OUTPUT);
pinMode(powerRadio,OUTPUT);
pinMode(powerBmeSensor,OUTPUT);
}
//===========================================================================
//============================= Start Code =================================
//===========================================================================
void loop() {
doSleep(); // After finished loop, put all system to sleep.
turnOnLed();
digitalWrite(powerBmeSensor,HIGH);
digitalWrite(powerRadio,HIGH);
delay(100);
sendSensorData();
delay(1000);
turnOffLed();
digitalWrite(powerBmeSensor,LOW);
digitalWrite(powerRadio,LOW);
}
//===========================================================================
//============================= End Code ====================================
//===========================================================================
//===========================================================================
//============================= LED CONTROL =================================
//===========================================================================
void turnOnLed(){
if(errorCode > 0){ // Turn on the RED LED if there is an error.
digitalWrite(ledPinRed,HIGH);
}
else{ // If no error, turn on the GREEN LED.
digitalWrite(ledPinGreen,HIGH);
}
}
void turnOffLed(){
digitalWrite(ledPinGreen,LOW);
digitalWrite(ledPinRed,LOW);
}
//===========================================================================
//=============================== SLEEP =====================================
//===========================================================================
void doSleep() {
#if sleepTimeSeconds > 60
#error sleepTimeSeconds is greater than 60!
#endif
#if sleepTimeSeconds < 0
#error sleepTimeSeconds is negative, must be positive!
#endif
#if sleepTimeMinutes > 60
#error sleepTimeMinutes is greater than 60!
#endif
#if sleepTimeMinutes < 0
#error sleepTimeMinutes is negative, must be positive!
#endif
#if sleepTimeHours < 0
#error sleepTimeHours is negative, must be positive!
#endif
long secondsToSleep = (sleepTimeSeconds) + (sleepTimeMinutes*60) + (sleepTimeHours*3600);
for (long s = 0; s < secondsToSleep; s++) {
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}
}
//===========================================================================
//============================= SEND DATA ===================================
//===========================================================================
void sendSensorData(){
int bmpError = bme.begin(); // error 0 = no com. with bme280.
if(bmpError == 0){ // No contact with BMP 280 sensor, set errorCode bit no.0 HIGH.
bitSet(errorCode,0); // Activate bit no.0
}
else{ // Contact with BMP 280 Sensor, set errorCode bit no.1 LOW
bitClear(errorCode,0); // Deactivate bit no.0
}
Serial.print("id:" + deviceId + ",e:" + String(errorCode));
delay(100);
if( temperature && (bmpError > 0) ){
Serial.print("id:" + deviceId + ",t:" + String(bme.readTemperature()));
delay(100);
}
if( humidity && (bmpError > 0) ){
Serial.print("id:" + deviceId + ",h:" + String(bme.readHumidity()));
delay(100);
}
if( pressure && (bmpError > 0) ){
float bme280_pressure = bme.readPressure()/100.0F;
Serial.print("id:" + deviceId + ",p:" + String(bme280_pressure));
delay(100);
}
}