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 121 forks source link

Sending RF Signals #290

Closed VelvetThunder1 closed 1 year ago

VelvetThunder1 commented 1 year ago

I have a RF controlled outlet and would like to be able to turn it on and off using my echo dot. I've tried using the Switch example script but im a beginner at arduino programming and am not sure how to change the code to send the signals. Also not familiar with IR so I wasnt able to work with the TV example script either. Here is the script:

/*
 * 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

#include <RCSwitch.h>
#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 "SinricPro.h"
#include "src/SinricProSwitch.h"

#define WIFI_SSID         ""    
#define WIFI_PASS         ""
#define APP_KEY           ""      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        ""   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

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

bool myPowerState = false;
RCSwitch mySwitch = RCSwitch();
/* 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");
  myPowerState = state;
  mySwitch.send(5314307, 24);
  return true; // request handled properly
}
/*
void handleButtonPress() {
  unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
  if (digitalRead(BUTTON_PIN) == LOW && actualMillis - lastBtnPress > 1000)  { // is button pressed (inverted logic! button pressed = LOW) and debounced?
    if (myPowerState) {     // flip myPowerState: if it was true, set it to false, vice versa
      myPowerState = false;
    } else {
      myPowerState = true;
    }
    digitalWrite(LED_PIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)

    // get Switch device back
    SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
    // send powerstate event
    mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
    Serial.printf("Device %s turned %s (manually via flashbutton)\r\n", mySwitch.getDeviceId().c_str(), myPowerState?"on":"off");

    lastBtnPress = actualMillis;  // update last button press variable
  } 
}
*/
// 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.restoreDeviceStates(true); // Uncomment to restore the last known state from the server.
  SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function
void setup() {
// Transmitter is connected to Arduino Pin D4
  mySwitch.enableTransmit(5);
  mySwitch.setPulseLength(183);

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

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

Edit by sivar2311: Removed credentials & added code formatting

sivar2311 commented 1 year ago

Hi @VelvetThunder1 What issue do you encounter exactly?

To make your code compile I only had to change one line: #include "src/SinricProSwitch.h" to #include "SinricProSwitch.h"

Note: You're using the same name "mySwitch" for the global RCSwitch instance and for SinricProSwitch! Therefor I recommend to rename the RCSwitch instance to rcSwitch:

old

RCSwitch mySwitch = RCSwitch();

new:

RCSwitch rcSwitch;

Thus needs a few more renamings in

onPowerState()

old:

  mySwitch.send(5314307, 24);

new:

  rcSwitch.send(5314307, 24);

setup()

old:

  mySwitch.enableTransmit(5);
  mySwitch.setPulseLength(183);

new:

  rcSwitch.enableTransmit(5);
  rcSwitch.setPulseLength(183);
VelvetThunder1 commented 1 year ago

The issue is that the script works fine, however I am not sure where to add the rcSwitch code to send the RF signals. I am able to discover the device and turn it on manually but unable to transmit the signals.

VelvetThunder1 commented 1 year ago

Hi @VelvetThunder1 What issue do you encounter exactly?

To make your code compile I only had to change one line: #include "src/SinricProSwitch.h" to #include "SinricProSwitch.h"

Note: You're using the same name "mySwitch" for the global RCSwitch instance and for SinricProSwitch! Therefor I recommend to rename the RCSwitch instance to rcSwitch:

old

RCSwitch mySwitch = RCSwitch();

new:

RCSwitch rcSwitch;

Thus needs a few more renamings in

onPowerState()

old:

  mySwitch.send(5314307, 24);

new:

  rcSwitch.send(5314307, 24);

setup()

old:

  mySwitch.enableTransmit(5);
  mySwitch.setPulseLength(183);

new:

  rcSwitch.enableTransmit(5);
  rcSwitch.setPulseLength(183);

Thanks, will try this and see what results I get.

sivar2311 commented 1 year ago

The issue is that the script works fine, however I am not sure where to add the rcSwitch code to send the RF signals. I am able to discover the device and turn it on manually but unable to transmit the signals.

Can you post a working (non-SinricPro) sketch that shows how to control your RF device?

VelvetThunder1 commented 1 year ago

Sure, here it is:

/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const char* ssid = "";
const char* password = "";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  mySwitch.enableTransmit(5);
  mySwitch.setPulseLength(183);
  Serial.begin(9600);
  delay(10);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  // Match the request
  int val;
  if (req.indexOf("/3off") != -1) {
    Serial.println("Sending OFF signal");
    //send RF Off singal
    mySwitch.send(5314316, 24);
  } else if (req.indexOf("/3on") != -1) {
    Serial.println("Sending ON signal");
    //send RF On singal
    mySwitch.send(5314307, 24);
  } else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
//  digitalWrite(2, val);

  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nswitch is now ";
  s += (val)?"off":"on";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}

Edit (sivar2311): Code formatting

sivar2311 commented 1 year ago
bool onPowerState(const String &deviceId, bool &state) {
  if (state == true) {
    Serial.println("Sending ON signal");
    rcSwitch.send(5314307, 24);
  } else {
    Serial.println("Sending OFF signal");
    rcSwitch.send(5314316, 24);
  }
  return true;
}

The state variable is true when the device is requested to turn on, and false if the device is requested to turn off.

VelvetThunder1 commented 1 year ago

Ohh, I got it now, thanks for explaining. I'll try this immediately.

VelvetThunder1 commented 1 year ago

@sivar2311 Thank you so much for all your help, its finally working now!