Skip to content

Instantly share code, notes, and snippets.

@aryamansharda
Last active August 30, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aryamansharda/abd1b260edcda97bbdfc9e170f2ab233 to your computer and use it in GitHub Desktop.
Save aryamansharda/abd1b260edcda97bbdfc9e170f2ab233 to your computer and use it in GitHub Desktop.
Code involved in automating my studio apartment's appliances.
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <IRutils.h>
#define WIFI_SSID "YOUR_NETWORK_NAME"
#define WIFI_PASS "YOUR_NETWORK_PASSWORD"
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "YOUR_ADAFRUIT_USERNAME"
#define MQTT_PASS "YOUR_ADAFRUIT_PASS_KEY"
int led = 2;
#define IR_SEND_PIN D2
IRsend irsend(IR_SEND_PIN);
IRrecv irrecv(14);
decode_results results;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/toggleTelevision");
uint16_t FAN_POWER[] = {1340,364,1340,362,488,1214,1340,362,1340,362,486,1214,488,1214,488,1214,488,1214,488,1214,488,1214,1338,7330,1340,364,1338,364,486,1214,1338,364,1338,362,488,1214,486,1214,488,1214,488,1214,486,1214,488,1214,1338,7330,1338,364,1338,364,486,1214,1338,364,1340,362,486,1214,486,1214,486,1214,486,1216,486,1214,486,1214,1340,7330,1338,364,1338,364,488,1214,1338,364,1338,364,486,1214,486,1214,486,1214,488,1214,488,1214,486,1214,1338,7330,1340,362,1338};
uint16_t FAN_ROTATE[] = {1346,358,1346,358,492,1208,1346,358,1346,356,494,1208,494,1208,1346,358,492,1208,494,1208,494,1208,494,8178,1346,356,1346,356,494,1208,1344,356,1346,356,494,1208,494,1208,1346,356,494,1206,494,1208,494,1206,494,8176,1346,356,1346,358,492,1208,1346,356,1346,356,494,1208,494,1208,1346,356,494,1206,494,1206,494,1208,494,8176,1346,356,1346,356,494,1208,1344,358,1344,358,494,1208,492,1208,1346,356,494,1208,494,1208,494,1208,492,8176,1344,358,1344};
uint16_t FAN_DELAY[] = {1304,376,1326,376,474,1228,1326,376,1326,376,472,1230,472,1228,472,1228,1326,376,474,1228,472,1228,472,8196,1324,376,1326,376,474,1228,1324,376,1326,378,472,1230,472,1228,472,1230,1324,376,474,1228,474,1228,474,8194,1324,376,1326,378,474,1228,1324,376,1326,376,474,1228,472,1228,472,1228,1324,376,474,1228,472,1228,474,8194,1324,376,1326,376,474,1228,1324,376,1326,376,474,1228,472,1228,472,1228,1326,376,474,1228,472,1228,472,8194,1322,376,1326};
void setup() {
irsend.begin();
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
irrecv.enableIRIn();
//Connect to WiFi
Serial.print("\n\nConnecting Wifi>");
WiFi.begin(WIFI_SSID, WIFI_PASS);
digitalWrite(LED_BUILTIN, LOW);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(">");
delay(50);
}
Serial.println("OK!");
//Subscribe to the onoff topic
mqtt.subscribe(&onoff);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
MQTT_connect();
if (irrecv.decode(&results)) {
serialPrintUint64(results.value, HEX);
irrecv.resume();
delay(150);
}
//Read from our subscription queue until we run out, or
//wait up to 5 seconds for subscription to update
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoff) {
String lastRead = String((char*)onoff.lastread);
Serial.println("Command: " + lastRead);
if (lastRead.equals("on")) {
irsend.sendNEC(0x57E3E817, 32);
} else if (lastRead.equals("off")) {
irsend.sendNEC(0x57E3E817, 32);
} else if (lastRead.equals("netflix")) {
irsend.sendNEC(0x57E34AB5, 32);
} else if (lastRead.equals("volume up")) {
irsend.sendNEC(0x57E3F00F, 32);
} else if (lastRead.equals("volume down")) {
irsend.sendNEC(0x57E308F7, 32);
} else if (lastRead.equals("play")) {
irsend.sendNEC(0x57E332CD, 32);
} else if (lastRead.equals("pause")) {
irsend.sendNEC(0x57E332CD, 32);
} else if (lastRead.equals("up")) {
irsend.sendNEC(0x57E39867, 32);
} else if (lastRead.equals("down")) {
irsend.sendNEC(0x57E3CC33, 32);
} else if (lastRead.equals("left")) {
irsend.sendNEC(0x57E37887, 32);
} else if (lastRead.equals("right")) {
irsend.sendNEC(0x57E3B44B, 32);
} else if (lastRead.equals("enter")) {
irsend.sendNEC(0x57E354AB, 32);
} else if (lastRead.equals("rewind")) {
irsend.sendNEC(0x57E31EE1, 32);
} else if (lastRead.equals("back")) {
irsend.sendNEC(0x57E36699, 32);
} else if (lastRead.equals("hulu")) {
irsend.sendNEC(0x57E3B24D,32);
} else if (lastRead.equals("home")) {
irsend.sendNEC(0x57E3C03F);
} else if (lastRead.equals("fan on")) {
irsend.sendRaw(FAN_POWER, sizeof(FAN_POWER) / sizeof(int), 38);
} else if (lastRead.equals("fan rotate")) {
irsend.sendRaw(FAN_ROTATE, sizeof(FAN_ROTATE) / sizeof(int), 38);
} else if (lastRead.equals("sleep timer")) {
for( int x = 0; x < 7; x++) {
irsend.sendRaw(FAN_DELAY, sizeof(FAN_DELAY) / sizeof(int), 38);
delay(100);
}
} else if (lastRead.equals("clean up")) {
for (int x = 0; x < 6; x++) {
irsend.sendNEC(0x23ABFA21, 32);
delay(10000);
}
} else if (lastRead.equals("dock")) {
// Try every 10 seconds for a minute to make sure you catch the signal
for (int x = 0; x < 6; x++) {
irsend.sendNEC(0xFE48D1D, 32);
delay(10000);
}
}
}
}
}
void MQTT_connect() {
// // Stop if already connected
if (mqtt.connected() && mqtt.ping()) {
return;
}
int8_t ret;
mqtt.disconnect();
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
// Connect will return 0 for connected
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
ESP.reset();
}
}
Serial.println("MQTT Connected!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment