Need Help with Adding Sinric Pro To send ir Codes

As you have read the title, my problem is that I cannot add Sinric pro to the below code. Im trying to create a AC remote which can be controlled via Blynk and Alexa. I’m Trying to access the blynk using alexa, Most of might probably recommend IFTTT but due to corporate greed IFTTT is reducing the free plan’s functionality to 2 applets which is not enough for my project.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>

const int ledPin = 12;
const int ledPin2 = 13;
const uint16_t kIrLed = 4;  // ESP8266 GPIO pin to use. Recommended: 4 (D2).

IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.

#define auth ""
char ssid[] = "";
char pass[] = "!";

// AC control pin



void setup() {
  irsend.begin();
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  
}

void loop() {
  Blynk.run();
}

BLYNK_WRITE(V1) {
  int powerState = param.asInt();
  
  if (powerState == 1) {
    irsend.sendLG(0x8800C40, 28);
    digitalWrite(ledPin, HIGH);
    delay(500); // Adjust the delay if necessary
  } else {
    irsend.sendLG(0x88C0051, 28);
    delay(500); // Adjust the delay if necessary
    digitalWrite(ledPin, LOW);
  }
}

BLYNK_WRITE(V2) {
  int temperature = param.asInt();
  
  // Convert the desired temperature to the corresponding IR command
  unsigned long irCommand = 0;

  if (temperature == 18) {
    irCommand = 0x880834F;
  } else if (temperature == 19) {
    irCommand = 0x8808440;
  } else if (temperature == 20) {
    irCommand = 0x8808541;
  } else if (temperature == 21) {
    irCommand = 0x8808642;
  } else if (temperature == 22) {
    irCommand = 0x8808743;
  } else if (temperature == 23) {
    irCommand = 0x8808844;
  } else if (temperature == 24) {
    irCommand = 0x8808945;
  } else if (temperature == 25) {
    irCommand = 0x8808A46;
  } else if (temperature == 26) {
    irCommand = 0x88C0051;
  } else if (temperature == 27) {
    irCommand = 0x8808C48;
  } else if (temperature == 28) {
    irCommand = 0x8808D49;
  } else if (temperature == 29) {
    irCommand = 0x8808E4A;
  } else if (temperature == 30) {
    irCommand = 0x8808F4B;
  }

  irsend.sendLG(irCommand, 28);
}

BLYNK_WRITE(V3) {
  int timer = param.asInt();
  
  // Convert the desired timer value to the corresponding IR command
  unsigned long irCommand = 0;

  if (timer == 1) {
    irCommand = 0x88A03C9;
  } else if (timer == 2) {
    irCommand = 0x88A0789;
  } else if (timer == 3) {
    irCommand = 0x88A0B49;
  } else if (timer == 4) {
    irCommand = 0x88A0F09;
  } else if (timer == 5) {
    irCommand = 0x88A12C9;
  } else if (timer == 6) {
    irCommand = 0x88A1689;
  } else if (timer == 7) {
    irCommand = 0x88A1A49;
  } else if (timer == 8) {
    irCommand = 0x88A000A;
  }

  irsend.sendLG(irCommand, 28);
}
BLYNK_WRITE(V4) {
  int energysaverstate = param.asInt();
  
  if (energysaverstate == 1) {
    irsend.sendLG(0x88C0095, 28);
    digitalWrite(ledPin2, HIGH);
    delay(500); // Adjust the delay if necessary
  } else {
    irsend.sendLG(0x88C0095, 28);
    digitalWrite(ledPin2, LOW);
    delay(500); // Adjust the delay if necessary
  }
}

@nyobnobi when you post code to the forum the triple backticks need to be on a separate line for them to work correctly.
I’ve edited your post to fix this, but please do this yourself in future.

I’m guessing you’re a fan of Paul Hibbert’s Youtube channel? :crazy_face:

I’ve not seen Sinric Pro mentioned on the forum for a while, so i doubt that there are many people using it with Blynk.
Looking at the code examples on the Synric Pro Github site, it seems that there are some lines of code that do the initialisation in void setup, and these would need to be after the Blynk.begin() line in your sketch, as this created the WiFi connection.

Then, there are Sinric related callbacks, which are trigged by the Sinric library when an Alexa command is received. These are similar to the BLYNK_WRITE(vPin) callbacks in your existing sketch.
To avoid code duplication it would make sense to move most of the code from your BLYNK_WRITE(vPin) callbacks into a function, and call that function from whatever device (Blynk or Alexa/Sinric) that triggers the change, something like this…

int powerState; // make this global by declaring it outside of a function

BLYNK_WRITE(V1)
{
  powerState = param.asInt();
  toggle_AC_power(); // call the new function
}
  

bool onPowerState(const String &deviceId, bool &state) 
{
  Serial.printf("Thermostat %s turned %s\r\n", deviceId.c_str(), state?"on":"off");
  powerState  = state; // update YOUR global variable
  toggle_AC_power(); // call the new function
  // you will probably want to do a Blynk.virtualWrite(V2,powerState) here, to update the V2 widget  
  return true; // request handled properly
}


  
void toggle_AC_power()
{
  if (powerState == 1)
  {
    irsend.sendLG(0x8800C40, 28);
    digitalWrite(ledPin, HIGH);
    delay(500); // Adjust the delay if necessary
  } 
 else
 {
    irsend.sendLG(0x88C0051, 28);
    delay(500); // Adjust the delay if necessary
    digitalWrite(ledPin, LOW);
  }
}

you would obviously need to have defined the Suinric callbacks, secret keys etc elsewhere in the sketch, and add SinricPro.handle(); to your void loop.
Does this make sense?

There are other alternatives to IFTTT such as voiceflow and Node-Red with the “node-red-contrib-alexa-remote2-applestrudel” and Blynk plug-ins - which is the approach I use.

Pete.

Hey Pete thanks for the reply and advice. I didnt find any alternatives for ifttt until you replied.I’m gonna try and implement voiceflow.
cheers,
nyobnobi

1 Like

Make sure that you use the region suffix for the Blynk server that hosts your account in the API calls, as described in the Troubleshooting part of the REST API section of the Blynk documentation.

Pete.