-
Notifications
You must be signed in to change notification settings - Fork 2
/
WeMos-WiFi-MQTT-relay.ino
125 lines (106 loc) · 2.98 KB
/
WeMos-WiFi-MQTT-relay.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// i'm loading settings from this file
#include "credentials.h"
// but you can set them here instead
//const char* ssid = "";
//const char* password = "";
//const char* mqtt_server = "";
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 5
#define CLIENT_ID "qm9"
#define IN_PIN 5
#define RELAY_PIN 4
const char* willTopic = "$CONNECTED/"CLIENT_ID;
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
float temp = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid); //We don't want the ESP to act as an AP
WiFi.mode(WIFI_STA);
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());
}
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();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(RELAY_PIN, HIGH);
client.publish("node/"CLIENT_ID"/relay", "1", true);
} else {
digitalWrite(RELAY_PIN, LOW);
client.publish("node/"CLIENT_ID"/relay", "0", true);
}
}
void reconnect() {
// Loop until we're reconnected
digitalWrite(LED_BUILTIN, LOW);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CLIENT_ID, willTopic, 0, true, "0")) {
Serial.println("connected");
client.publish(willTopic, "1", true);
client.subscribe("node/"CLIENT_ID"/relay/set");
digitalWrite(LED_BUILTIN, HIGH);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(IN_PIN, INPUT);
sensors.begin();
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 60000) {
lastMsg = now;
sensors.setResolution(12);
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
Serial.println(temp);
client.publish("node/"CLIENT_ID"/sys/temp", String(temp).c_str(), false);
}
}