-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteorology.ino
116 lines (102 loc) · 2.69 KB
/
meteorology.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
#include "DHTesp.h"
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <Adafruit_BME280.h>
#include <ESP8266HTTPClient.h>
const char* ssid = ""; //Set your wifi network name(ssid)
const char* password = ""; //Set your router password
const int LDRPin = 17;
#define SEALEVELPRESSURE_HPA (1013.25)
HTTPClient http;
Adafruit_BME280 bme;
String SERVER_URL = "http://192.168.1.141:6789/weather";
/** Initialize DHT sensor */
DHTesp dht;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LDRPin, INPUT);
// Connect D0 to RST to wake up
pinMode(D0, WAKEUP_PULLUP);
enableLed(false);
connectToWifi();
//setupOTA();
bool status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void loop()
{
Serial.println();
Serial.print("Free heap: ");
Serial.print(ESP.getFreeHeap());
int sleepSeconds = 900;
enableLed(true);
sendPost(SERVER_URL, createJSONFromSensors());
enableLed(false);
Serial.println();
Serial.println("Going to sleep! I will wake up in ");
Serial.print(sleepSeconds);
Serial.print(" seconds");
deepSleep(sleepSeconds);
}
void sendPost(String URL, String JSONBodyData){
Serial.println();
Serial.println("Sending data to server: ");
Serial.print(URL);
Serial.println();
Serial.println(JSONBodyData);
http.begin(URL);
http.addHeader("Content-Type", "application/json");
http.POST(JSONBodyData);
//http.writeToStream(&Serial);
http.end();
}
void connectToWifi()
{
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Print the IP address
Serial.print("IP: ");
Serial.print(WiFi.localIP());
}
void deepSleep(int seconds)
{
ESP.deepSleep(seconds * 1000000);
}
String createJSONFromSensors(){
String result = "{\"temperature\": ";
result+=bme.readTemperature();
result+=",\"humidity\": ";
result+=bme.readHumidity();
result+=",\"pressure\": ";
result = result + (bme.readPressure() / 100.0F);
result+=",\"altitude\": ";
result+=bme.readAltitude(SEALEVELPRESSURE_HPA);
result+=",\"lightLevel\": ";
result+= analogRead(LDRPin);
result+="}";
return result;
}
void reconnect(){
WiFi.persistent(false);
WiFi.mode(WIFI_OFF); // this is a temporary line, to be removed after SDK update to 1.5.4
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
void enableLed(boolean value){
digitalWrite(LED_BUILTIN, !value);
}