sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
228 stars 122 forks source link

Working on IR Blaster for AC using Device type as switch #183

Closed amanjn16 closed 3 years ago

amanjn16 commented 3 years ago

I am trying to make a IR blaster using switch as device type. It is working great except that I am only able to transmit Switch on code irrespective of whether I turn the device on or off. Please help me correct my code so that IR sends right raw data based on whether I turn device on or off.

/*
 * Example for how to use SinricPro Switch device:
 * - setup a switch device
 * - handle request using callback (turn on/off builtin led indicating device power state)
 * - send event to sinricPro server (flash button is used to turn on/off device manually)
 * 
 * If you encounter any issues:
 * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
 * - ensure all dependent libraries are installed
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
 *   - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
 * - open serial monitor and check whats happening
 * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
 * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
 */

// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
       #define DEBUG_ESP_PORT Serial
       #define NODEBUG_WEBSOCKETS
       #define NDEBUG
#endif 

#include <Arduino.h>
#ifdef ESP8266 
       #include <ESP8266WiFi.h>
#endif 
#ifdef ESP32   
       #include <WiFi.h>
#endif
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRutils.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID ""
#define WIFI_PASS ""
#define APP_KEY ""
#define APP_SECRET ""
#define SWITCH_ID         ""    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         9600                // Change baudrate to your need

#define IR_LED 4
IRsend irsend(IR_LED);
uint16_t AC_POWER_ON [243] = {3254, 1662,  380, 476,  356, 476,  354, 1264,  382, 476,  354, 1294,  354, 474,  356, 476,  354, 450,  382, 1266,  380, 1264,  384, 450,  380, 476,  356, 474,  356, 1266,  382, 1266,  382, 450,  382, 476,  354, 450,  382, 474,  356, 476,  356, 476,  354, 476,  354, 476,  356, 476,  354, 476,  356, 450,  380, 476,  356, 446,  382, 1266,  382, 476,  354, 450,  380, 474,  356, 476,  356, 450,  382, 448,  382, 476,  356, 1290,  356, 476,  356, 446,  384, 474,  356, 474,  356, 448,  382, 1266,  382, 1290,  356, 1292,  356, 1266,  380, 1264,  384, 1266,  380, 476,  356, 448,  382, 474,  356, 1266,  382, 448,  382, 450,  382, 448,  382, 474,  356, 448,  384, 448,  382, 474,  356, 474,  356, 1292,  356, 1292,  354, 474,  356, 474,  356, 1292,  354, 476,  356, 448,  382, 474,  356, 474,  382, 1266,  382, 448,  356, 448,  406, 1266,  382, 448,  382, 450,  356, 450,  406, 450,  358, 474,  382, 450,  382, 422,  408, 1238,  408, 450,  356, 448,  408, 422,  408, 450,  380, 448,  382, 448,  382, 450,  380, 450,  382, 450,  382, 448,  382, 450,  382, 448,  382, 450,  380, 424,  408, 450,  380, 448,  382, 424,  408, 448,  382, 448,  382, 448,  382, 448,  382, 450,  380, 450,  380, 448,  384, 448,  380, 448,  382, 448,  382, 450,  382, 448,  384, 448,  382, 450,  380, 1266,  382, 448,  382, 1240,  408, 1240,  406, 448,  384, 1264,  382, 448,  382, 1264,  384};
uint16_t AC_POWER_OFF [115] = {3354, 1636,  406, 424,  406, 424,  406, 1216,  430, 398,  432, 1214,  408, 424,  432, 398,  430, 398,  432, 1216,  408, 1266,  406, 398,  406, 424,  432, 400,  406, 1268,  404, 1240,  380, 450,  380, 450,  382, 448,  382, 424,  432, 424,  380, 450,  380, 398,  432, 424,  408, 450,  380, 426,  406, 424,  406, 422,  408, 450,  380, 1268,  380, 450,  380, 398,  432, 426,  406, 394,  436, 424,  406, 424,  406, 424,  406, 1268,  380, 424,  406, 424,  406, 450,  380, 424,  406, 1242,  406, 398,  432, 424,  406, 450,  380, 426,  380, 450,  406, 450,  356, 1266,  406, 424,  404, 1246,  402, 1268,  380, 1266,  378, 1270,  354, 1266,  404, 1246,  378};
int i=0;
#define BUTTON_PIN 0   // GPIO for BUTTON (inverted: LOW = pressed, HIGH = released)
#define LED_PIN   2   // GPIO for LED (inverted)

bool myPowerState = false;
unsigned long lastBtnPress = 0;

/* bool onPowerState(String deviceId, bool &state) 
 *
 * Callback for setPowerState request
 * parameters
 *  String deviceId (r)
 *    contains deviceId (useful if this callback used by multiple devices)
 *  bool &state (r/w)
 *    contains the requested state (true:on / false:off)
 *    must return the new state
 * 
 * return
 *  true if request should be marked as handled correctly / false if not
 */

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");

   if (state=true){
    Serial.printf("state=true");
  irsend.sendRaw(AC_POWER_OFF, 115, 38);

  }
   if (state=false) {
  Serial.printf("state=false");
  irsend.sendRaw(AC_POWER_ON, 243, 38);
 }

 myPowerState = state;
    return true; // request handled properly
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];

  // set callback function to device
  mySwitch.onPowerState(onPowerState);

  // 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);
}

// main setup function
void setup() {
  // pinMode(BUTTON_PIN, INPUT_PULLUP); // GPIO 0 as input, pulled high
 //  pinMode(LED_PIN, OUTPUT); // define LED GPIO as output
 //  digitalWrite(LED_PIN, HIGH); // turn off LED on bootup

  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  irsend.begin();
  setupWiFi();
  setupSinricPro();
}

void loop() {
  SinricPro.handle();
}

(Edit by sivar2311 for better code readability)

sivar2311 commented 3 years ago

Your comparison is wrong... to compare values you need to use "==" not "=":

if (state==true) ...

Use the short way:

if (state) {
  irsend.sendRaw(AC_POWER_OFF, 115, 38);
} else {
  irsend.sendRaw(AC_POWER_ON, 243, 38);
}

= is the assignment operator. See c++ comparison operators