-
Notifications
You must be signed in to change notification settings - Fork 3
/
TempHumLight-Sensor.ino
187 lines (144 loc) · 4.96 KB
/
TempHumLight-Sensor.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
/* Built to run on ESP-01 for being small and powered by 3.3v directly.
*
* This file includes:
SHT21 Temperature and humidity sensor
GY49 LUX sensor
OTA support
MQTT support
Made by Daniel Römer 2019 for homeautomation.
*/
#include <ESP8266WiFi.h> //For ESP8266
#include <PubSubClient.h> //For MQTT
#include <ESP8266mDNS.h> //For OTA
#include <WiFiUdp.h> //For OTA
#include <ArduinoOTA.h> //For OTA
#include <MAX44009.h>
#include "SparkFun_Si7021_Breakout_Library.h"
//WIFI configuration
#define wifi_ssid "Esperyd"
#define wifi_password "Esperyd4"
#define board_name "ESP_Ella"
//MQTT configuration
#define mqtt_server "192.168.10.100"
#define mqtt_user "mqtt"
#define mqtt_password "mqtt"
String mqtt_client_id="ESP-"; //This text is concatenated with ChipId to get unique client_id
//MQTT Topic configuration
String mqtt_base_topic="sensor";
#define humidity_topic "/humidity"
#define temperature_topic "/temperature"
#define lux_topic "/lux"
//MQTT client
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
//Necesary to make Arduino Software autodetect OTA device
WiFiServer TelnetServer(8266);
MAX44009 light;
Weather sensor;
void setup_wifi() {
delay(10);
Serial.print("Connecting to ");
Serial.print(wifi_ssid);
WiFi.hostname(board_name);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("OK");
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
Serial.println("\r\nBooting...");
setup_wifi();
Serial.print("Configuring OTA device...");
TelnetServer.begin(); //Necesary to make Arduino Software autodetect OTA device
ArduinoOTA.setHostname(board_name);
ArduinoOTA.onStart([]() {Serial.println("OTA starting...");});
ArduinoOTA.onEnd([]() {Serial.println("OTA update finished!");Serial.println("Rebooting...");});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {Serial.printf("OTA in progress: %u%%\r\n", (progress / (total / 100)));});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("OK");
Serial.println("Configuring MQTT server...");
mqtt_client_id=board_name;
mqtt_base_topic=mqtt_base_topic+"/" + mqtt_client_id;
mqtt_client.setServer(mqtt_server, 1883);
Serial.printf(" Server IP: %s\r\n",mqtt_server);
Serial.printf(" Username: %s\r\n",mqtt_user);
Serial.println(" Cliend Id: "+mqtt_client_id);
Serial.println(" MQTT configured!");
Wire.begin(0,2);
sensor.begin();
light.begin();
Serial.println("Setup completed! Running app...");
}
void mqtt_reconnect() {
// Loop until we're reconnected
while (!mqtt_client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (mqtt_client.connect(mqtt_client_id.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return(true);
return newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
}
long now =0; //in ms
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;
int min_timeout=6000; //in ms
float lux = 0;
void loop() {
ArduinoOTA.handle();
if (!mqtt_client.connected()) {
mqtt_reconnect();
}
mqtt_client.loop();
now = millis();
if (now - lastMsg > min_timeout) {
lastMsg = now;
now = millis();
//getData();
float newTemp = sensor.getTemp();
float newHum = sensor.getRH();
float lux = light.get_lux();
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("Sent ");
Serial.print(String(temp).c_str());
Serial.println(" to "+mqtt_base_topic+temperature_topic);
mqtt_client.publish((mqtt_base_topic+temperature_topic).c_str(), String(temp).c_str(), true);
}
if (checkBound(newHum, hum, diff)) {
hum = newHum;
Serial.print("Sent ");
Serial.print(String(hum).c_str());
Serial.println(" to "+mqtt_base_topic+humidity_topic);
mqtt_client.publish((mqtt_base_topic+humidity_topic).c_str(), String(hum).c_str(), true);
}
mqtt_client.publish((mqtt_base_topic+lux_topic).c_str(), String(lux).c_str(), true);
}
}