-
Notifications
You must be signed in to change notification settings - Fork 18
/
ESP8266_MqttPubClientTemp.ino
51 lines (43 loc) · 1.21 KB
/
ESP8266_MqttPubClientTemp.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
#include <ESP8266WiFi.h> // v2.4.2
#include <ESP8266MQTTClient.h> // v1.0.4
#include "DHTesp.h"
const int dhtPin = 5; // Grove adapter I2C_1 or _2 used as D6
const DHTesp::DHT_MODEL_t dhtModel = DHTesp::DHT11;
DHTesp dht;
const char *ssid = "MY_SSID"; // TODO
const char *password = "MY_PASSWORD"; // TODO
const char *host = "mqtt://LOCAL_IP"; // TODO
MQTTClient client;
volatile int connected = 0;
void handleConnected() {
Serial.println("Connected to broker");
connected = 1;
}
void setup() {
Serial.begin(115200);
dht.setup(dhtPin, dhtModel);
Serial.print("\nConnecting to network ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100); // keeps watchdog happy
}
Serial.print("Connected to network, local IP = ");
Serial.println(WiFi.localIP());
client.onConnect(handleConnected);
client.begin(host);
}
void loop() {
client.handle();
if (connected) {
// Readings take about 250 ms, may be up to 2 s old
float temp = dht.getTemperature(); // *C
if (!isnan(temp)) {
String tempStr = String(temp, 2);
Serial.println(tempStr);
client.publish("temp", tempStr);
}
delay(1000);
}
}