Last active
May 31, 2024 06:12
-
-
Save kakopappa/7fa62119ea71ce381fde92711dc82bc3 to your computer and use it in GitHub Desktop.
sinricpro-tv
This file contains hidden or 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
// #define ENABLE_DEBUG | |
#define SINRICPRO_NOSSL | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
#include "SinricPro.h" | |
#include "SinricProTV.h" | |
#define WIFI_SSID "YOUR-WIFI-SSID" | |
#define WIFI_PASS "YOUR-WIFI-PASSWORD" | |
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" | |
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
#define TV_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx" | |
#define BAUD_RATE 115200 | |
const uint16_t kIrLed = 4; | |
IRsend irsend(kIrLed); | |
bool tvPowerState; | |
bool onPowerState(const String &deviceId, bool &state) { | |
Serial.printf("TV turned %s\r\n", state?"on":"off"); | |
tvPowerState = state; // set powerState | |
/* | |
Protocol : NEC | |
Code : 0x20DF10EF (32 Bits) | |
uint16_t rawData[71] = {9032, 4476, 582, 544, 582, 546, 582, 1652, 582, 546, 582, 544, 582, 546, 582, 546, 582, 546, 582, 1654, 582, 1654, 582, 544, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 546, 582, 544, 584, 544, 582, 1654, 582, 546, 582, 546, 582, 546, 582, 546, 582, 1654, 582, 1654, 582, 1652, 582, 546, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 41158, 9004, 2222, 582}; // NEC 20DF10EF | |
uint32_t address = 0x4; | |
uint32_t command = 0x8; | |
uint64_t data = 0x20DF10EF; | |
*/ | |
uint16_t rawData[71] = {9032, 4476, 582, 544, 582, 546, 582, 1652, 582, 546, 582, 544, 582, 546, 582, 546, 582, 546, 582, 1654, 582, 1654, 582, 544, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 546, 582, 544, 584, 544, 582, 1654, 582, 546, 582, 546, 582, 546, 582, 546, 582, 1654, 582, 1654, 582, 1652, 582, 546, 582, 1654, 582, 1654, 582, 1654, 582, 1654, 582, 41158, 9004, 2222, 582}; // NEC 20DF10EF | |
irsend.sendRaw(rawData, 71 /* IR len */, 38 /* kHz */); | |
return true; | |
} | |
// setup function for WiFi connection | |
void setupWiFi() { | |
Serial.printf("\r\n[Wifi]: Connecting"); | |
#if defined(ESP8266) | |
WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
WiFi.setAutoReconnect(true); | |
#elif defined(ESP32) | |
WiFi.setSleep(false); | |
WiFi.setAutoReconnect(true); | |
#endif | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
IPAddress localIP = WiFi.localIP(); | |
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
} | |
// setup function for SinricPro | |
void setupSinricPro() { | |
// add device to SinricPro | |
SinricProTV& myTV = SinricPro[TV_ID]; | |
// set callback functions to device | |
myTV.onPowerState(onPowerState); | |
// For other https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/TV/TV.ino | |
// myTV.onAdjustVolume(onAdjustVolume); | |
// myTV.onChangeChannel(onChangeChannel); | |
// myTV.onChangeChannelNumber(onChangeChannelNumber); | |
// myTV.onMediaControl(onMediaControl); | |
// myTV.onMute(onMute); | |
// myTV.onSelectInput(onSelectInput); | |
// myTV.onSetVolume(onSetVolume); | |
// myTV.onSkipChannels(onSkipChannels); | |
// setup SinricPro | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
void setupIR() { | |
irsend.begin(); | |
} | |
// main setup function | |
void setup() { | |
setupIR(); | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment