-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd1_mqtt_dht22.ino
227 lines (192 loc) · 6.06 KB
/
d1_mqtt_dht22.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <EEPROM.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
// MQTT library
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// DHT Setup
#include "DHTesp.h"
DHTesp dht;
// Variables
unsigned long previousMillis = 0;
//const long interval = 60000;
const long interval = 5000;
const int TEMP_ADDR = 0;
const int HUM_ADDR = 4;
float temperature;
float oldtemperature;
float humidity;
float oldhumidity;
//const char* mqtt_server = "m23.cloudmqtt.com";
//const int mqtt_port = 17822;
//const char* mqtt_user = "hhwlirgn";
//const char* mqtt_pass = "FXwQWwN1fZhw";
// const char* mqtt_server = "192.168.168.120";
// const int mqtt_port = 1883;
// const char* mqtt_user = "mosquitto";
// const char* mqtt_pass = "mosquittopass";
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "aburnsni"
#define AIO_KEY "aad58026fc614464b68e4df7f4b2a44a"
char temp2[5];
char humidity2[5];
// Initialise pubsubslient
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish worktemp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/work.temp");
Adafruit_MQTT_Publish workhumidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/work.humidity");
void MQTT_connect();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
EEPROM.begin(512); // Needed for ESP8266
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
//set custom ip for portal
//wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
dht.setup(2); // data pin 2
oldtemperature = readFloat(TEMP_ADDR);
oldhumidity = readFloat(HUM_ADDR);
}
// void reconnect() {
// // Loop until we're reconnected
// while (!client.connected()) {
// Serial.print("Attempting MQTT connection...");
// // Attempt to connect
// if (client.connect("ESP8266Client", mqtt_user, mqtt_pass)) {
// Serial.println("connected");
// // Once connected, publish an announcement...
// client.publish("connected", "hello world");
// // ... and resubscribe
// // client.subscribe("inTopic");
// } 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() {
// Serial.println ("Old Values");
// Serial.print(readFloat(TEMP_ADDR));
// Serial.print("\t");
// Serial.println(readFloat(HUM_ADDR));
MQTT_connect();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if (!client.connected()) {
// reconnect();
// }
// client.loop();
// put your main code here, to run repeatedly:
//delay(dht.getMinimumSamplingPeriod());
temperature = dht.getTemperature();
humidity = dht.getHumidity();
if (temperature != oldtemperature || humidity != oldhumidity) {
// temperature = ((int)(temperature * 100)) / 100.0;
// humidity = ((int)(humidity * 100)) / 100.0;
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(temperature);
Serial.print("\t");
Serial.println(humidity);
if (! worktemp.publish(temperature)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
if (! workhumidity.publish(humidity)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// dtostrf(temperature, 0, 1, temp2);
// String payload = temp2;
// client.publish("work/temp", (char*) payload.c_str(), true);
// dtostrf(humidity, 0, 1, humidity2);
// payload = humidity2;
// client.publish("work/humidity", (char*) payload.c_str(), true);
if (temperature != oldtemperature) {
writeFloat(TEMP_ADDR,temperature);
Serial.println("Writing temp to EEPROM");
oldtemperature = temperature;
Serial.print("Old temp is now: ");
Serial.println(oldtemperature);
}
if (humidity != oldhumidity) {
writeFloat(HUM_ADDR,humidity);
Serial.println("Writing humidity to EEPROM");
oldhumidity = humidity;
Serial.print("Old humidity is now: ");
Serial.println(oldtemperature);
}
}
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
float readFloat(unsigned int addr)
{
union
{
byte b[4];
float f;
} data;
for(int i = 0; i < 4; i++)
{
data.b[i] = EEPROM.read(addr+i);
}
return data.f;
}
void writeFloat(unsigned int addr, float x)
{
union
{
byte b[4];
float f;
} data;
data.f = x;
for(int i = 0; i < 4; i++)
{
EEPROM.write(addr+i, data.b[i]);
}
EEPROM.commit(); // Needed for ESP8266
}