-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemperatureMonitor.ino
54 lines (38 loc) · 1008 Bytes
/
TemperatureMonitor.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
#include "Setup.h"
#include <PubSubClient.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
String ip;
char charBuf[40];
void setup()
{
Serial.begin(SERIAL_SPEED);
ensureWifiConnected();
setupWebUpdater();
setupMqtt();
}
void loop()
{
char tempStr[10];
ensureWifiConnected();
loopMqtt();
loopWebUpdater();
sensors.requestTemperatures();
// Sensor 1
float temp1 = sensors.getTempCByIndex(0);
Serial.print("Temperature 1: "); Serial.print(temp1); Serial.println(" °C");
dtostrf(temp1, 2, 2, tempStr);
client.publish("mqtt.0.tempmon.temp1", tempStr);
// Sensor 2
float temp2 = sensors.getTempCByIndex(1);
Serial.print("Temperature 2: "); Serial.print(temp2); Serial.println(" °C");
dtostrf(temp2, 2, 2, tempStr);
client.publish("mqtt.0.tempmon.temp2", tempStr);
delay(5000);
}