-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_IR_IoT_hub.ino
168 lines (145 loc) · 5.16 KB
/
ESP32_IR_IoT_hub.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
/****************************************************************************
IR ESP32 IoT hub
See: https://mansfield-devine.com/speculatrix/category/projects/home-control/esp32-ir-hub/
Using an ESP 32 board as an IR receiver, to take IR remote control commands
and respond by sending out MQTT messages.
Uses the 1838T receiver connected to pin RECV_PIN.
Requires the file wifi-credentials.h to be in the same location as this
sketch. See the wifi-credentials-template.h file, which you can edit and
rename to suit your wifi environment.
Use (on my Mac):
Board: ESP32 Dev Module
Upload speed: 921600
CPU freq: 240MHz
Flash freq: 80MHz
Flash mode: QIO
Port: /dev/cu.SLAB_USBtoUART
****************************************************************************/
#include <WiFi.h>
#include "wifi-credentials.h"
#include <IRremote.h>
#include "irdecode.h"
#include <PubSubClient.h>
#define LED 33
#define RECV_PIN 25
#define SERVER_ERR_LIMIT 5
#define WIFI_MAX_TRIES 10
#define MQTT_SERVER_ADDR "10.0.1.59" // replace with the address of your MQTT server
#define MQTT_DEVICE_ID "ESP32_IR_hub" // use whatever name you want
#define MQTT_TOPIC "home/lights" // whatever topic you want
// --- IR SENSOR --------------------------------------------------------------------------
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
// --- WIFI -------------------------------------------------------------------------------
int wifi_status = WL_IDLE_STATUS;
IPAddress ip;
uint8_t server_errors = 0;
void wifiConnect() {
uint8_t ssid_idx = 0;
uint8_t connect_counter = 0;
WiFi.mode(WIFI_STA);
while (connect_counter < WIFI_MAX_TRIES) {
Serial.print("Attempting to connect to "); Serial.println(ssid[ssid_idx]);
WiFi.begin(ssid[ssid_idx], WLAN_PASS); // try to connect
// delay to allow time for connection - flash lights to show this is happening
flashLED(5, 100);
wifi_status = WiFi.status();
connect_counter++;
if (wifi_status != WL_CONNECTED) {
ssid_idx = 1 - ssid_idx; // swap APs
} else {
Serial.println("Wifi connected.");
connect_counter = WIFI_MAX_TRIES; // to break out of the loop
ip = WiFi.localIP();
Serial.print("IP: "); Serial.println(ip);
server_errors = 0;
}
}
if (wifi_status != WL_CONNECTED) { // wifi connection failed
server_errors = SERVER_ERR_LIMIT;
}
}
void flashLED(uint8_t times, int pulseLen) {
for (uint8_t i = 0; i < times; i++) {
digitalWrite(LED, HIGH);
delay(pulseLen);
digitalWrite(LED, LOW);
delay(pulseLen);
}
}
// --- MQTT -------------------------------------------------------------------------------
WiFiClient mqttWifiClient;
PubSubClient mqtt(mqttWifiClient);
/**
Callback function for incoming MQTT messages.
**/
void mqttCallback(char* topic, byte* message, unsigned int length) {
// Topic is a char array. Easiest to cast it to a string to test
// for specific topics.
if (String(topic) == MQTT_TOPIC) {
/* Just a placeholder to show how this works. This is where
you'd respond to the message - perhaps by setting a flag that
could be dealt with in the main loop */
}
// The message is a byte array. We can build a string from it with:
String msg;
for (int i = 0; i < length; i++) {
msg += (char)message[i];
}
}
void mqttReconnect() {
// Loop until we're reconnected
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection... ");
// Attempt to connect
if (mqtt.connect(MQTT_DEVICE_ID)) {
Serial.println("MQTT connected");
mqtt.subscribe(MQTT_TOPIC); // only if you want to respond to incoming messages
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Serial.println(" - trying again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// ***************************************************************************************
// *** SETUP ***
// ***************************************************************************************
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
// connect to wifi
wifiConnect();
// set up MQTT
mqtt.setServer(MQTT_SERVER_ADDR, 1883);
mqtt.setCallback(mqttCallback);
// Enable the IR Receiver
irrecv.enableIRIn();
Serial.println("Listening for signals...");
}
// ***************************************************************************************
// *** LOOP ***
// ***************************************************************************************
void loop() {
if (!mqtt.connected()) {
mqttReconnect();
}
mqtt.loop();
if (irrecv.decode(&results)) {
flashLED(2, 10);
char resultsBuf[40];
decodeDetails(resultsBuf, results);
Serial.println(resultsBuf);
// Send MQTT message according to code received
switch (results.value) {
case 0x807FC03F:
Serial.println("- sending dim message");
mqtt.publish(MQTT_TOPIC, "dim");
break;
}
irrecv.resume();
} // if irrecv.decode
}