-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWeather.ino
195 lines (161 loc) · 5.38 KB
/
getWeather.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
//based on Weather Station by Chris Figge, https://www.youtube.com/watch?v=KMwVNuzghsY&t=429s
//and original code, available at https://github.com/flazer/weather-station
//SNTP library by Andreas Spiess, see https://github.com/SensorsIot/SNTPtime/
//added NTP server to ensure posting of weather at exact times of 0, 15, 30 and 45 minutes every hour.
//after each post, the esp8266 goes into deep sleep and wakes up after 15 minutes. When it wakes up,
//it connects to Wifi and get the current time from the NTP server, gets the sensor data and sends it
//to the post script on the webserver. Then it goes back to sleep to save battery. One minute is ample
//time to connect to wifi and send the data. Both indoor and outdoor weather stations report their
//values at the same time (or at leat within the same minute - usually they are only a second appart).
//by Tina Keil, March 2020
#include <SNTPtime.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include "settings.h"
ESP8266WiFiMulti WiFiMulti;
BME280I2C bme;
#define SERIAL_BAUD 115200
#define FORCE_DEEPSLEEP
SNTPtime NTPch(ntpServer);
strDateTime dateTime;
void setup() {
Serial.begin(SERIAL_BAUD);
Wire.begin();
splashScreen();
Serial.println("---");
Serial.println("Searching for sensor:");
Serial.print("Result: ");
while (!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
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!");
}
startWIFI();
Serial.print("Getting time from NTP Server: ");
Serial.println(ntpServer);
while (!NTPch.setSNTPtime()) Serial.print("."); // set internal clock
Serial.println("Time set successfully!");
}
void loop() {
dateTime = NTPch.getTime(1.0, 1); // get time from internal clock
byte actualMinute = dateTime.minute;
delay(1000); //wait for 1 second;
if (actualMinute == 0 || actualMinute == 15 || actualMinute == 30 || actualMinute == 45) {
sendSensorData(); //send data
goToBed(minutes2sleep); //sending into deep sleep
}
}
/**
Establish WiFi-Connection
If connection times out (threshold 50 sec)
device will sleep for 5 minutes and will restart afterwards.
*/
void startWIFI() {
Serial.println("---");
WiFi.mode(WIFI_STA);
Serial.println("(Re)Connecting to Wifi-Network with following credentials:");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Key: ");
Serial.println(password);
Serial.print("Device-Name: ");
Serial.println(espName);
WiFi.hostname(espName);
WiFiMulti.addAP(ssid, password);
int tryCnt = 0;
while (WiFiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tryCnt++;
if (tryCnt > 100) {
Serial.println("Could not connect to WiFi. Sending device to bed.");
goToBed(5);
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
/**
Building http-POST-request and send all necessary data
*/
void sendSensorData () {
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_bar);
bme.read(pres, temp, hum, tempUnit, presUnit);
pres = pres * 1000; // convert to millibar
String payload = "token=" + token + "&temperature=" + String(temp) + "&humidity=" + String(hum) + "&pressure=" + String(pres);
Serial.println("---");
Serial.println("[HTTP] Start connection info request...");
Serial.println("[HTTP] Sending data.");
HTTPClient http;
int httpCode = -1;
if (ssl_enabled == true) {
http.begin(url, fingerprint);
} else {
http.begin(url);
}
Serial.print("[HTTP] URL: ");
Serial.println(url);
Serial.print("[HTTP] Payload: ");
Serial.println(payload);
Serial.println("[HTTP] Requesting...");
http.setUserAgent(userAgent + " " + clientVer);
http.addHeader("Content-Type", contenttype);
httpCode = http.POST(payload);
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
Serial.println("[HTTP] Result: OK.");
}
}
Serial.println("[HTTP] End connection.");
Serial.println("---");
}
/**
Sending device into deep sleep
*/
void goToBed (int minutes) {
#ifdef FORCE_DEEPSLEEP
Serial.print("GeeWiz. I'm tired. Going back to bed for ");
Serial.print(minutes);
Serial.print(" minutes. Good night!");
ESP.deepSleep(minutes * 60 * 1000000);
#endif
}
/**
Dump some information on startup.
*/
void splashScreen() {
for (int i = 0; i <= 5; i++) Serial.println();
Serial.println("#######################################");
Serial.print("# ");
Serial.print(userAgent);
Serial.print(" - v. ");
Serial.println(clientVer);
Serial.println("# -----------");
Serial.println("# Tina Keil (tkeil69575)");
Serial.println("# Mail: tina.keil@tk-doku.de");
Serial.println("# -----------");
Serial.print("# DeviceName: ");
Serial.println(espName);
Serial.print("# Configured Endpoint: ");
Serial.println(url);
Serial.println("#######################################");
for (int i = 0; i < 2; i++) Serial.println();
}