Last active
January 14, 2019 18:53
-
-
Save avseng/90c961a20cf0243b04d913526a92ef32 to your computer and use it in GitHub Desktop.
ESP-8266 IR send
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ---------------------------------------------------------------- | |
* Description: This code will send the IR encoded raw data to | |
* IR home appliencees. Here those are my Music | |
* System and AC. | |
* | |
* Date: 15-Jan-2019 | |
* Author: Avijit Sengupta | |
* ---------------------------------------------------------------- | |
*/ | |
#include <PubSubClient.h> | |
#include <ESP8266WiFi.h> | |
#ifndef UNIT_TEST | |
#include <Arduino.h> | |
#endif | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
#define IR_LED 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2). | |
#define MQTT_SERVER "192.168.1.11" // MQTT Server | |
const char* ssid = "NETGEAR69"; //SSID | |
const char* password = "**************************"; //PASSWORD | |
IRsend irsend(IR_LED); | |
//IR raw data for Music System remote control | |
uint16_t rawDataMS_ON1[37] = {2752, 794, 512, 822, 488, 396, 514, 372, 488, 846, 956, 366, 516, 372, 458, 428, 512, 372, 484, 402, 982, 790, 512, 372, 926, 398, 456, 878, 482, 402, 512, 372, 898, 426, 514, 372, 484}; | |
uint16_t rawDataMS_ON2[37] = {2718, 848, 512, 822, 512, 374, 486, 398, 486, 850, 926, 396, 512, 372, 486, 398, 486, 400, 484, 400, 930, 844, 484, 402, 954, 368, 486, 848, 482, 402, 512, 372, 960, 364, 542, 344, 510}; | |
uint16_t rawDataMS_ON3[37] = {2720, 850, 540, 794, 484, 400, 516, 370, 516, 818, 958, 368, 486, 398, 488, 398, 518, 370, 512, 372, 956, 816, 514, 372, 956, 368, 514, 822, 476, 408, 514, 372, 956, 368, 510, 372, 486}; | |
uint16_t rawDataMS_ON4[37] = {2720, 850, 512, 822, 514, 370, 484, 400, 484, 848, 928, 396, 486, 400, 512, 372, 516, 372, 484, 402, 898, 874, 512, 350, 950, 396, 480, 852, 486, 400, 486, 400, 956, 368, 482, 402, 486}; | |
uint16_t rawDataMS_OFF1[35] = {2754, 790, 514, 820, 490, 394, 490, 396, 1396, 1260, 490, 396, 488, 396, 486, 400, 486, 400, 984, 786, 488, 398, 958, 366, 512, 822, 514, 370, 542, 344, 928, 396, 514, 370, 514}; | |
uint16_t rawDataMS_OFF2[35] = {2698, 848, 486, 848, 482, 402, 486, 400, 1370, 1288, 516, 370, 490, 396, 486, 398, 488, 398, 954, 818, 486, 400, 928, 396, 486, 848, 486, 398, 486, 402, 926, 394, 516, 370, 488}; | |
uint16_t rawDataMS_OFF3[35] = {2696, 850, 486, 848, 484, 402, 486, 400, 1372, 1286, 486, 400, 486, 400, 486, 400, 488, 398, 928, 844, 486, 400, 926, 398, 488, 846, 488, 396, 484, 402, 928, 396, 484, 400, 486}; | |
uint16_t rawDataMS_OFF4[35] = {2722, 846, 488, 846, 490, 396, 544, 340, 1398, 1260, 492, 392, 516, 348, 538, 368, 516, 370, 958, 814, 488, 398, 960, 364, 516, 818, 516, 368, 518, 368, 952, 372, 488, 396, 514}; | |
uint16_t rawDataAC_ON[199] = {4716, 4308, 732, 1484, 728, 380, 732, 1484, 752, 1464, 728, 378, 694, 416, 756, 1460, 720, 388, 758, 350, 706, 1514, 756, 348, 736, 372, 760, 1456, 760, 1456, 730, 376, 728, 1488, 696, 1520, 732, 376, 730, 1486, 692, 1524, 756, 1462, 782, 1432, 724, 1492, 728, 1490, 716, 392, 730, 1486, 760, 348, 734, 374, 762, 346, 760, 348, 754, 354, 754, 354, 728, 380, 760, 346, 696, 1520, 760, 348, 758, 350, 730, 378, 726, 384, 726, 382, 730, 1484, 754, 1464, 724, 384, 762, 1456, 758, 1456, 784, 1434, 756, 1460, 758, 1460, 762, 5150, 4718, 4308, 722, 1494, 732, 376, 764, 1452, 722, 1496, 790, 316, 732, 378, 730, 1486, 790, 316, 732, 376, 758, 1458, 790, 318, 722, 384, 730, 1488, 756, 1460, 724, 384, 726, 1492, 752, 1462, 760, 346, 728, 1488, 726, 1492, 736, 1482, 724, 1490, 756, 1460, 758, 1460, 752, 354, 762, 1454, 702, 406, 728, 378, 784, 322, 730, 380, 734, 374, 698, 412, 724, 382, 752, 356, 760, 1456, 728, 378, 758, 350, 726, 382, 780, 326, 694, 414, 722, 1492, 698, 1518, 696, 414, 820, 1396, 720, 1496, 728, 1488, 728, 1488, 788, 1430, 754}; | |
uint16_t rawDataAC_OFF[199] = {4656, 4346, 692, 1526, 718, 390, 696, 1522, 690, 1526, 694, 414, 692, 416, 694, 1524, 692, 416, 694, 414, 746, 1470, 718, 390, 692, 416, 690, 1526, 718, 1496, 694, 416, 692, 1522, 656, 452, 722, 1494, 692, 1526, 722, 1496, 692, 1524, 692, 416, 716, 1500, 690, 1526, 724, 1494, 694, 416, 690, 416, 692, 416, 694, 414, 690, 1526, 692, 416, 694, 414, 722, 1494, 718, 1498, 694, 1522, 696, 412, 692, 416, 694, 414, 718, 390, 692, 416, 692, 416, 718, 390, 696, 412, 692, 1524, 724, 1492, 690, 1526, 692, 1524, 690, 1526, 694, 5218, 4654, 4372, 692, 1524, 692, 416, 694, 1522, 694, 1522, 694, 414, 694, 414, 722, 1494, 696, 412, 692, 418, 718, 1498, 692, 416, 720, 386, 694, 1522, 696, 1522, 694, 414, 720, 1496, 694, 414, 718, 1498, 694, 1524, 718, 1498, 720, 1496, 694, 414, 720, 1496, 664, 1552, 720, 1496, 694, 414, 692, 414, 692, 416, 692, 416, 692, 1524, 690, 418, 720, 388, 718, 1498, 694, 1522, 694, 1524, 722, 386, 692, 416, 720, 386, 692, 416, 692, 414, 694, 414, 720, 386, 696, 412, 720, 1496, 694, 1522, 692, 1526, 692, 1524, 722, 1494, 696}; | |
//IR raw data for AC remote control | |
char* TopicAC = "AS-SmartIoT/AS-SmartIoT-IR001"; //AC topic | |
char* TopicMS = "AS-SmartIoT/AS-SmartIoT-IR002"; //Music System topic | |
WiFiClient wifiClient; | |
PubSubClient client(wifiClient); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
String topicStr = topic; | |
Serial.println("Callback update."); | |
Serial.print("Topic: "); | |
Serial.println(topicStr); | |
Serial.println((char)payload[0]); | |
if (topicStr == TopicMS){ | |
if((char)payload[0] == '1'){ | |
Serial.println("Sending power on signal to Music System"); | |
irsend.sendRaw(rawDataMS_ON1, 37, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_ON2, 37, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_ON3, 37, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_ON4, 37, 38); // Send a raw data capture at 38kHz. | |
} | |
else if ((char)payload[0] == '0'){ | |
Serial.println("Sending power off signal to Music System"); | |
irsend.sendRaw(rawDataMS_OFF1, 35, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_OFF2, 35, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_OFF3, 35, 38); // Send a raw data capture at 38kHz. | |
irsend.sendRaw(rawDataMS_OFF4, 35, 38); // Send a raw data capture at 38kHz. | |
} | |
}else if (topicStr == TopicAC){ | |
if((char)payload[0] == '1'){ | |
Serial.println("Sending power on signal to AC"); | |
irsend.sendRaw(rawDataAC_ON, 199, 38); // Send a raw data capture at 38kHz. | |
}else if ((char)payload[0] == '0'){ | |
Serial.println("Sending power off signal to AC"); | |
irsend.sendRaw(rawDataAC_OFF, 199, 38); // Send a raw data capture at 38kHz. | |
} | |
} | |
} | |
void reconnect() { | |
if(WiFi.status() != WL_CONNECTED){ | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
if(WiFi.status() == WL_CONNECTED){ | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
String clientName; | |
clientName += "esp8266-"; | |
uint8_t mac[6]; | |
WiFi.macAddress(mac); | |
clientName += macToStr(mac); | |
if (client.connect((char*) clientName.c_str())) { | |
Serial.print("\tMTQQ Connected"); | |
client.subscribe(TopicAC); | |
client.subscribe(TopicMS); | |
} | |
else{Serial.println("\tFailed."); abort();} | |
} | |
} | |
} | |
String macToStr(const uint8_t* mac){ | |
String result; | |
for (int i = 0; i < 6; ++i) { | |
result += String(mac[i], 16); | |
if (i < 5){ | |
result += ':'; | |
} | |
} | |
return result; | |
} | |
void setup() { | |
client.setServer(MQTT_SERVER, 1883); | |
client.setCallback(callback); | |
delay(100); | |
irsend.begin(); | |
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY); | |
WiFi.begin(ssid, password); | |
reconnect(); | |
delay(2000); | |
} | |
void loop(){ | |
if (!client.connected() && WiFi.status() == 3) {reconnect();} | |
client.loop(); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment