-
Notifications
You must be signed in to change notification settings - Fork 0
/
IthoEcoFanMQTT.ino
161 lines (141 loc) · 3.83 KB
/
IthoEcoFanMQTT.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
/*
* Author: BertrandR
*/
#include <SPI.h>
#include "IthoCC1101.h"
#include "IthoPacket.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
IthoCC1101 rf;
IthoPacket packet;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// Configuration
// WiFi
const char* ssid = "";
const char* password = "";
// MQTT
const char* mqttServer = "";
const char* mqttStateTopic = "home/house_fan";
const char* mqttCommandTopic = "home/house_fan/set";
const char* mqttUsername = "";
const char* mqttPassword = "";
void setup() {
Serial.begin(115200);
//Serial.begin(74880);
delay(1500);
setup_wifi();
rf.init();
Serial.println("RF setup");
mqttClient.setServer(mqttServer, 1883);
mqttClient.setCallback(callback);
reconnectMQTT();
}
void setup_wifi() {
// Connect to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// I've found that sometimes the unit gets stuck on connecting, mainly after flashing
// It will try for 30x then reboot
int reset = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
reset++;
if (reset > 30) ESP.restart();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnectMQTT() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
// Don't hammer it though
delay(1000);
Serial.print("Attempting MQTT connection...");
// Attempt to connect using either username/password or without
if (strlen(mqttUsername) > 0) {
mqttClient.connect("ESP8266Client",mqttUsername,mqttPassword);
}
else {
mqttClient.connect("ESP8266Client");
}
if (mqttClient.connected()) {
Serial.println("connected");
// Say hello to the channel
String helloMessage = "ESP8226 ";
helloMessage += WiFi.macAddress();
helloMessage += " connected";
char helloMessageChar[helloMessage.length()+1];
helloMessage.toCharArray(helloMessageChar, helloMessage.length()+1);
mqttClient.publish(mqttStateTopic,helloMessageChar);
// Subscribe to the channel
mqttClient.subscribe(mqttCommandTopic);
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
}
}
}
// Send command to RF module, every command is send 3x to be sure it arrives (since we can't check if it has effect)
void sendCommand(IthoCommand command) {
for( int i = 0; i < 3; i++ ) {
rf.sendCommand(command);
delay(1000);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
String s = String((char*)payload);
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
Serial.print(s);
Serial.println();
// Let's not be picky
s.toLowerCase();
if (s == "high") {
sendCommand(full);
mqttClient.publish(mqttStateTopic,"high");
}
else if (s == "medium") {
sendCommand(medium);
mqttClient.publish(mqttStateTopic,"medium");
}
else if (s == "low") {
sendCommand(low);
mqttClient.publish(mqttStateTopic,"low");
}
else if (s == "timer") {
sendCommand(timer1);
mqttClient.publish(mqttStateTopic,"timer");
}
else if (s == "join") {
sendCommand(join);
mqttClient.publish(mqttStateTopic,"join");
}
}
void loop() {
// Check WiFi connection
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi failed, try reconnect in 5 seconds");
delay(5000);
setup_wifi();
}
// Check MQTT connection
if (!mqttClient.connected()) {
Serial.print("MQTT Connection failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try reconnect in 5 seconds");
delay(5000);
reconnectMQTT();
}
// Handle MQTT events
mqttClient.loop();
delay(100);
}