-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_temperature.ino
179 lines (156 loc) · 4.3 KB
/
esp32_temperature.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
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// DEFINES
//#define STATION1
#define STATION2
//#define STATION3
#define DHTPIN 4
#define DHTTYPE DHT22
// SSID/Password combination for WiFi connection
const char* ssid = "ENTER SSID";
const char* password = "ENTER PASSWORD";
// MQTT Borker IP address and credentials
const char* mqtt_server = "ENTER ADDRESS";
const char* mqtt_user = "ENTER USERNAME";
const char* mqtt_pwd = "@ENTER PASSWORD";
#ifdef STATION1
const char* mqtt_topic_temp = "station1/temperature";
const char* mqtt_topic_humid = "station1/humidity";
const char* mqtt_topic_enable = "station1/enable";
#endif
#ifdef STATION2
const char* mqtt_topic_temp = "station2/temperature";
const char* mqtt_topic_humid = "station2/humidity";
const char* mqtt_topic_enable = "station2/enable";
#endif
#ifdef STATION3
const char* mqtt_topic_temp = "station3/temperature";
const char* mqtt_topic_humid = "station3/humidity";
const char* mqtt_topic_enable = "station3/enable";
#endif
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
float temperature = 0;
float humidity = 0;
float heatIndex = 0;
bool enable = false;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
Serial.println("=====================================");
#ifdef STATION1
Serial.println("========== ESP32 Station 1 ==========");
#endif
#ifdef STATION2
Serial.println("========== ESP32 Station 2 ==========");
#endif
#ifdef STATION3
Serial.println("========== ESP32 Station 3 ==========");
#endif
Serial.println("=====================================");
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(mqtt_callback);
dht.begin();
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void mqtt_callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Process read MQTT topic ?
if (String(topic) == String(mqtt_topic_enable)) {
Serial.print("Changing output to ");
if(messageTemp == "true"){
Serial.println("true");
enable = true;
}
else if(messageTemp == "false"){
Serial.println("false");
enable = false;
}
else {
Serial.println("(nothing, not recognised value)");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
//Attemp to connect
#ifdef STATION1
if (client.connect("ESP32Client-station1", mqtt_user, mqtt_pwd)) {
#endif
#ifdef STATION2
if (client.connect("ESP32Client-station2", mqtt_user, mqtt_pwd)) {
#endif
#ifdef STATION3
if (client.connect("ESP32Client-station3", mqtt_user, mqtt_pwd)) {
#endif
Serial.println("Connected");
// Subscribe
client.subscribe(mqtt_topic_enable);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (enable) {
long now = millis();
if ((lastMsg == 0) || (abs(now - lastMsg) > 120000)) {
lastMsg = now;
// Temperature in Celsius
temperature = dht.readTemperature();
humidity = dht.readHumidity();
char tempString[8];
char humidString[8];
dtostrf(temperature, 1, 2, tempString);
dtostrf(humidity, 1, 2, humidString);
Serial.print("Temperature: ");
Serial.println(tempString);
Serial.print("Humidity: ");
Serial.println(humidString);
client.publish(mqtt_topic_temp, tempString, true);
client.publish(mqtt_topic_humid, humidString, true);
}
} else {
lastMsg = 0;
}
}