-
Notifications
You must be signed in to change notification settings - Fork 27
/
mqtt_esp_1.ino
310 lines (262 loc) · 7.46 KB
/
mqtt_esp_1.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Send temperature from ESP8266 with multiple DS18B20 to MQTT server.
* A simple Sketch to read the Temperature from multiple DS18B20 and publish them to a MQTT-Server using a ESP8266.
* Compiles in the Arduino IDE for the ESP8266
*
* For deep sleep support uncomment 'deep sleep' part
* For DHT22 support uncomment 'dht22' part
* OTA currently does not work.
*
*
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
/* dht22
#include <DHT.h>
*/
/* deep sleep
#define SLEEP_DELAY_IN_SECONDS 30
*/
// data cable connected to D4 pin
#define ONE_WIRE_BUS D4
// dht22
/*
// data cable connected to D3
#define DHTPIN D3
#define DHTTYPE DHT22
char tString[6];
char hString[6];
long previousMillis = 0;
long interval = 60000;
*/
//wifi
const char* ssid = "NETWORK_NAME";
const char* password = "NERWORK_PASSWORD";
//mqtt
const char* mqtt_server = "MQTT_SERVER_ADDRESS";
//const char* mqtt_username = "<MQTT_BROKER_USERNAME>";
//const char* mqtt_password = "<MQTT_BROKER_PASSWORD>";
const char* host = "esp1";
// ora
/*
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
*/
//dht22
/*
DHT dht(DHTPIN, DHTTYPE, 20);
*/
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
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.println("IP address: ");
Serial.println(WiFi.localIP());
// dht22
/*
dht.begin();
*/
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
// setup serial port
Serial.begin(115200);
// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// ota
/*
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
*/
// setup OneWire bus
DS18B20.begin();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
//if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} 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();
/*
// UNCOMMENT TO ENABLE DHT22 READINGS
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
dtostrf(h, 2, 2, hString);
// Read temperature as Celsius
float t = dht.readTemperature();
dtostrf(t, 2, 2, tString);
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
previousMillis = currentMillis;
Serial.println("DTH sensor read and transmitted");
Serial << "Sending temperature: " << tString << endl;
Serial << "Sending humidity: " << hString << endl;
client.publish("/sensors/esp1/dht/temperature",tString);
client.publish("/sensors/esp1/dht/humidity",hString);
*/
// ds18b20
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
//Loop through all DS1820
while(oneWire.search(addr))
{
//Serial.print("ROM =");
//Topic is built from a static String plus the ID of the DS18B20
String romcode = "/esp/temp/";
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr[i], HEX);
romcode = romcode + String(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
//Serial.println("Device is not a DS18x20 family device.");
return;
}
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a oneWire.depower() here, but the reset will take care of it.
present = oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read Scratchpad
/*
Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = oneWire.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
*/
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
//convert RAW Temperature to String
String raw_temp = String(raw, DEC);
//convert RAW Temperature to celsius
double temp = raw * 0.0625;
//convert to string
char tempString[6];
dtostrf(temp, 2, 2, tempString);
if (client.publish((char*) romcode.c_str(), tempString)) {
Serial.println("Publish ok : ");
Serial.print( romcode );
Serial.print(":");
Serial.println( tempString );
}
else {
Serial.println("Publish failed");
}
}
//End of the OneWire-Devices, reset Loop
Serial.println("End of Onewire Bus");
oneWire.reset_search();
delay(30000);
return;
// deep sleep
//Serial << "Closing MQTT connection..." << endl;
//client.disconnect();
//Serial << "Closing WiFi connection..." << endl;
//WiFi.disconnect();
delay(100);
// deep sleep
//Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
//ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
//ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
//delay(500);
}