-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather_station.ino
129 lines (108 loc) · 2.61 KB
/
weather_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
#include <Wire.h>
#include <BME280I2C.h>
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <time.h>
unsigned long myChannelNumber = 1234;
const char * myWriteAPIKey = "";
const char* ssid = "";
const char* password = "";
const char* server = "api.thingspeak.com";
WiFiClient client;
BME280I2C bme;
int sleepTime = 60;
unsigned long curTime;
void setup() {
curTime = millis();
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
if (millis() - curTime > (60 * 1000)) {
goToSleep();
}
Serial.println(millis() - curTime);
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
Wire.begin();
while (!bme.begin())
{
if (millis() - curTime > (60 * 1000)) {
goToSleep();
}
Serial.println(millis() - curTime);
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch (bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
ThingSpeak.begin(client);
measure();
// sleep
WiFi.disconnect(true);
goToSleep();
}
void goToSleep() {
Serial.print("Sleeping for: ");
Serial.print(sleepTime);
Serial.println(" seconds.");
ESP.deepSleep(sleepTime * 1000000);
}
void measure() {
float t(NAN);
float p(NAN);
float h(NAN);
float volt = 0.0;
unsigned int raw = 0;
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(p, t, h, tempUnit, presUnit);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Pressure: ");
Serial.print(p);
Serial.print(" Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");
pinMode(A0, INPUT);
raw = analogRead(A0);
volt = raw / 1023.0;
volt = volt * 4.2;
Serial.print(volt);
if (volt > 4.0) {
sleepTime = 60;
} else if (volt > 3.9) {
sleepTime = 120;
} else if (volt > 3.85) {
sleepTime = 180;
} else if (volt > 3.75) {
sleepTime = 240;
} else {
sleepTime = 60 * 30;
}
// wifi
float rssi = WiFi.RSSI();
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, p);
ThingSpeak.setField(3, h);
ThingSpeak.setField(4, volt);
ThingSpeak.setField(5, rssi);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
void loop() {
}