Closed VelvetThunder1 closed 2 years 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
old:
mySwitch.send(5314307, 24);
new:
rcSwitch.send(5314307, 24);
old:
mySwitch.enableTransmit(5);
mySwitch.setPulseLength(183);
new:
rcSwitch.enableTransmit(5);
rcSwitch.setPulseLength(183);
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.
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 torcSwitch
: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.
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?
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
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.
Ohh, I got it now, thanks for explaining. I'll try this immediately.
@sivar2311 Thank you so much for all your help, its finally working now!
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:
Edit by sivar2311: Removed credentials & added code formatting