Raspberry pi & Raspberry Pico 433MHZ: transmit, receive, relay
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Example for receiving
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
|
||||
If you want to visualize a telegram copy the raw data and
|
||||
paste it into http://test.sui.li/oszi/
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (mySwitch.available()) {
|
||||
output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
|
||||
mySwitch.resetAvailable();
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
static const char* bin2tristate(const char* bin);
|
||||
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);
|
||||
|
||||
void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
|
||||
|
||||
const char* b = dec2binWzerofill(decimal, length);
|
||||
Serial.print("Decimal: ");
|
||||
Serial.print(decimal);
|
||||
Serial.print(" (");
|
||||
Serial.print( length );
|
||||
Serial.print("Bit) Binary: ");
|
||||
Serial.print( b );
|
||||
Serial.print(" Tri-State: ");
|
||||
Serial.print( bin2tristate( b) );
|
||||
Serial.print(" PulseLength: ");
|
||||
Serial.print(delay);
|
||||
Serial.print(" microseconds");
|
||||
Serial.print(" Protocol: ");
|
||||
Serial.println(protocol);
|
||||
|
||||
Serial.print("Raw data: ");
|
||||
for (unsigned int i=0; i<= length*2; i++) {
|
||||
Serial.print(raw[i]);
|
||||
Serial.print(",");
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
static const char* bin2tristate(const char* bin) {
|
||||
static char returnValue[50];
|
||||
int pos = 0;
|
||||
int pos2 = 0;
|
||||
while (bin[pos]!='\0' && bin[pos+1]!='\0') {
|
||||
if (bin[pos]=='0' && bin[pos+1]=='0') {
|
||||
returnValue[pos2] = '0';
|
||||
} else if (bin[pos]=='1' && bin[pos+1]=='1') {
|
||||
returnValue[pos2] = '1';
|
||||
} else if (bin[pos]=='0' && bin[pos+1]=='1') {
|
||||
returnValue[pos2] = 'F';
|
||||
} else {
|
||||
return "not applicable";
|
||||
}
|
||||
pos = pos+2;
|
||||
pos2++;
|
||||
}
|
||||
returnValue[pos2] = '\0';
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
|
||||
static char bin[64];
|
||||
unsigned int i=0;
|
||||
|
||||
while (Dec > 0) {
|
||||
bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
|
||||
Dec = Dec >> 1;
|
||||
}
|
||||
|
||||
for (unsigned int j = 0; j< bitLength; j++) {
|
||||
if (j >= bitLength - i) {
|
||||
bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
|
||||
} else {
|
||||
bin[j] = '0';
|
||||
}
|
||||
}
|
||||
bin[bitLength] = '\0';
|
||||
|
||||
return bin;
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
Simple example for receiving
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (mySwitch.available()) {
|
||||
|
||||
Serial.print("Received ");
|
||||
Serial.print( mySwitch.getReceivedValue() );
|
||||
Serial.print(" / ");
|
||||
Serial.print( mySwitch.getReceivedBitlength() );
|
||||
Serial.print("bit ");
|
||||
Serial.print("Protocol: ");
|
||||
Serial.println( mySwitch.getReceivedProtocol() );
|
||||
|
||||
mySwitch.resetAvailable();
|
||||
}
|
||||
}
|
57
433/433Utils/rc-switch/examples/SendDemo/SendDemo.ino
Normal file
57
433/433Utils/rc-switch/examples/SendDemo/SendDemo.ino
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
Example for different sending methods
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
// Transmitter is connected to Arduino Pin #10
|
||||
mySwitch.enableTransmit(10);
|
||||
|
||||
// Optional set protocol (default is 1, will work for most outlets)
|
||||
// mySwitch.setProtocol(2);
|
||||
|
||||
// Optional set pulse length.
|
||||
// mySwitch.setPulseLength(320);
|
||||
|
||||
// Optional set number of transmission repetitions.
|
||||
// mySwitch.setRepeatTransmit(15);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
/* See Example: TypeA_WithDIPSwitches */
|
||||
mySwitch.switchOn("11111", "00010");
|
||||
delay(1000);
|
||||
mySwitch.switchOff("11111", "00010");
|
||||
delay(1000);
|
||||
|
||||
/* Same switch as above, but using decimal code */
|
||||
mySwitch.send(5393, 24);
|
||||
delay(1000);
|
||||
mySwitch.send(5396, 24);
|
||||
delay(1000);
|
||||
|
||||
/* Same switch as above, but using binary code */
|
||||
mySwitch.send("000000000001010100010001");
|
||||
delay(1000);
|
||||
mySwitch.send("000000000001010100010100");
|
||||
delay(1000);
|
||||
|
||||
/* Same switch as above, but tri-state code */
|
||||
mySwitch.sendTriState("00000FFF0F0F");
|
||||
delay(1000);
|
||||
mySwitch.sendTriState("00000FFF0FF0");
|
||||
delay(1000);
|
||||
|
||||
delay(20000);
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Example for outlets which are configured with a 10 pole DIP switch.
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
|
||||
// Transmitter is connected to Arduino Pin #10
|
||||
mySwitch.enableTransmit(10);
|
||||
|
||||
// Optional set pulse length.
|
||||
// mySwitch.setPulseLength(320);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Switch on:
|
||||
// The first parameter represents the setting of the first 5 DIP switches.
|
||||
// In this example it's ON-ON-OFF-OFF-ON.
|
||||
//
|
||||
// The second parameter represents the setting of the last 5 DIP switches.
|
||||
// In this example the last 5 DIP switches are OFF-ON-OFF-ON-OFF.
|
||||
mySwitch.switchOn("11001", "01010");
|
||||
|
||||
// Wait a second
|
||||
delay(1000);
|
||||
|
||||
// Switch off
|
||||
mySwitch.switchOff("11001", "01010");
|
||||
|
||||
// Wait another second
|
||||
delay(1000);
|
||||
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
This is a minimal sketch without using the library at all but only works for
|
||||
the 10 pole dip switch sockets. It saves a lot of memory and thus might be
|
||||
very useful to use with ATTinys :)
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
int RCLpin = 7;
|
||||
|
||||
void setup() {
|
||||
pinMode(RCLpin, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
RCLswitch(0b010001000001); // DIPs an Steckdose: 0100010000 An:01
|
||||
delay(2000);
|
||||
|
||||
RCLswitch(0b010001000010); // DIPs an Steckdose: 0100010000 Aus:10
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
void RCLswitch(uint16_t code) {
|
||||
for (int nRepeat=0; nRepeat<6; nRepeat++) {
|
||||
for (int i=4; i<16; i++) {
|
||||
RCLtransmit(1,3);
|
||||
if (((code << (i-4)) & 2048) > 0) {
|
||||
RCLtransmit(1,3);
|
||||
} else {
|
||||
RCLtransmit(3,1);
|
||||
}
|
||||
}
|
||||
RCLtransmit(1,31);
|
||||
}
|
||||
}
|
||||
|
||||
void RCLtransmit(int nHighPulses, int nLowPulses) {
|
||||
digitalWrite(RCLpin, HIGH);
|
||||
delayMicroseconds( 350 * nHighPulses);
|
||||
digitalWrite(RCLpin, LOW);
|
||||
delayMicroseconds( 350 * nLowPulses);
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Example for outlets which are configured with two rotary/sliding switches.
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
|
||||
// Transmitter is connected to Arduino Pin #10
|
||||
mySwitch.enableTransmit(10);
|
||||
|
||||
// Optional set pulse length.
|
||||
// mySwitch.setPulseLength(320);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Switch on:
|
||||
// The first parameter represents the setting of the first rotary switch.
|
||||
// In this example it's switched to "1" or "A" or "I".
|
||||
//
|
||||
// The second parameter represents the setting of the second rotary switch.
|
||||
// In this example it's switched to "4" or "D" or "IV".
|
||||
mySwitch.switchOn(1, 4);
|
||||
|
||||
// Wait a second
|
||||
delay(1000);
|
||||
|
||||
// Switch off
|
||||
mySwitch.switchOff(1, 4);
|
||||
|
||||
// Wait another second
|
||||
delay(1000);
|
||||
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
Example for Intertechno outlets
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
|
||||
// Transmitter is connected to Arduino Pin #10
|
||||
mySwitch.enableTransmit(10);
|
||||
|
||||
// Optional set pulse length.
|
||||
// mySwitch.setPulseLength(320);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Switch on:
|
||||
// The first parameter represents the familycode (a, b, c, ... f)
|
||||
// The second parameter represents the group number
|
||||
// The third parameter represents the device number
|
||||
//
|
||||
// In this example it's family 'b', group #3, device #2
|
||||
mySwitch.switchOn('b', 3, 2);
|
||||
|
||||
// Wait a second
|
||||
delay(1000);
|
||||
|
||||
// Switch off
|
||||
mySwitch.switchOff('b', 3, 2);
|
||||
|
||||
// Wait another second
|
||||
delay(1000);
|
||||
|
||||
}
|
41
433/433Utils/rc-switch/examples/TypeD_REV/TypeD_REV.ino
Normal file
41
433/433Utils/rc-switch/examples/TypeD_REV/TypeD_REV.ino
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
Example for REV outlets (e.g. 8342L)
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
|
||||
Need help? http://forum.ardumote.com
|
||||
*/
|
||||
|
||||
#include <RCSwitch.h>
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
void setup() {
|
||||
|
||||
// Transmitter is connected to Arduino Pin #10
|
||||
mySwitch.enableTransmit(10);
|
||||
|
||||
// set pulse length.
|
||||
mySwitch.setPulseLength(360);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Switch on:
|
||||
// The first parameter represents the channel (a, b, c, d)
|
||||
// The second parameter represents the device number
|
||||
//
|
||||
// In this example it's family 'd', device #2
|
||||
mySwitch.switchOn('d', 2);
|
||||
|
||||
// Wait a second
|
||||
delay(1000);
|
||||
|
||||
// Switch off
|
||||
mySwitch.switchOff('d', 2);
|
||||
|
||||
// Wait another second
|
||||
delay(1000);
|
||||
|
||||
}
|
154
433/433Utils/rc-switch/examples/Webserver/Webserver.ino
Normal file
154
433/433Utils/rc-switch/examples/Webserver/Webserver.ino
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
A simple RCSwitch/Ethernet/Webserver demo
|
||||
|
||||
https://github.com/sui77/rc-switch/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <RCSwitch.h>
|
||||
|
||||
// Ethernet configuration
|
||||
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
|
||||
uint8_t ip[] = { 192,168,0, 2 }; // IP Address
|
||||
EthernetServer server(80); // Server Port 80
|
||||
|
||||
// RCSwitch configuration
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
int RCTransmissionPin = 7;
|
||||
|
||||
// More to do...
|
||||
// You should also modify the processCommand() and
|
||||
// httpResponseHome() functions to fit your needs.
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Setup
|
||||
*/
|
||||
void setup() {
|
||||
Ethernet.begin(mac, ip);
|
||||
server.begin();
|
||||
mySwitch.enableTransmit( RCTransmissionPin );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop
|
||||
*/
|
||||
void loop() {
|
||||
char* command = httpServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Command dispatcher
|
||||
*/
|
||||
void processCommand(char* command) {
|
||||
if (strcmp(command, "1-on") == 0) {
|
||||
mySwitch.switchOn(1,1);
|
||||
} else if (strcmp(command, "1-off") == 0) {
|
||||
mySwitch.switchOff(1,1);
|
||||
} else if (strcmp(command, "2-on") == 0) {
|
||||
mySwitch.switchOn(1,2);
|
||||
} else if (strcmp(command, "2-off") == 0) {
|
||||
mySwitch.switchOff(1,2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Response with homepage
|
||||
*/
|
||||
void httpResponseHome(EthernetClient c) {
|
||||
c.println("HTTP/1.1 200 OK");
|
||||
c.println("Content-Type: text/html");
|
||||
c.println();
|
||||
c.println("<html>");
|
||||
c.println("<head>");
|
||||
c.println( "<title>RCSwitch Webserver Demo</title>");
|
||||
c.println( "<style>");
|
||||
c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
|
||||
c.println( "</style>");
|
||||
c.println("</head>");
|
||||
c.println("<body>");
|
||||
c.println( "<h1>RCSwitch Webserver Demo</h1>");
|
||||
c.println( "<ul>");
|
||||
c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
|
||||
c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
|
||||
c.println( "</ul>");
|
||||
c.println( "<ul>");
|
||||
c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
|
||||
c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
|
||||
c.println( "</ul>");
|
||||
c.println( "<hr>");
|
||||
c.println( "<a href=\"https://github.com/sui77/rc-switch/\">https://github.com/sui77/rc-switch/</a>");
|
||||
c.println("</body>");
|
||||
c.println("</html>");
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Redirect to homepage
|
||||
*/
|
||||
void httpResponseRedirect(EthernetClient c) {
|
||||
c.println("HTTP/1.1 301 Found");
|
||||
c.println("Location: /");
|
||||
c.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Response 414 error
|
||||
* Command must not be longer than 30 characters
|
||||
**/
|
||||
void httpResponse414(EthernetClient c) {
|
||||
c.println("HTTP/1.1 414 Request URI too long");
|
||||
c.println("Content-Type: text/plain");
|
||||
c.println();
|
||||
c.println("414 Request URI too long");
|
||||
}
|
||||
|
||||
/**
|
||||
* Process HTTP requests, parse first request header line and
|
||||
* call processCommand with GET query string (everything after
|
||||
* the ? question mark in the URL).
|
||||
*/
|
||||
char* httpServer() {
|
||||
EthernetClient client = server.available();
|
||||
if (client) {
|
||||
char sReturnCommand[32];
|
||||
int nCommandPos=-1;
|
||||
sReturnCommand[0] = '\0';
|
||||
while (client.connected()) {
|
||||
if (client.available()) {
|
||||
char c = client.read();
|
||||
if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
|
||||
sReturnCommand[nCommandPos] = '\0';
|
||||
if (strcmp(sReturnCommand, "\0") == 0) {
|
||||
httpResponseHome(client);
|
||||
} else {
|
||||
processCommand(sReturnCommand);
|
||||
httpResponseRedirect(client);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (nCommandPos>-1) {
|
||||
sReturnCommand[nCommandPos++] = c;
|
||||
}
|
||||
if (c == '?' && nCommandPos == -1) {
|
||||
nCommandPos = 0;
|
||||
}
|
||||
}
|
||||
if (nCommandPos > 30) {
|
||||
httpResponse414(client);
|
||||
sReturnCommand[0] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nCommandPos!=-1) {
|
||||
sReturnCommand[nCommandPos] = '\0';
|
||||
}
|
||||
// give the web browser time to receive the data
|
||||
delay(1);
|
||||
client.stop();
|
||||
|
||||
return sReturnCommand;
|
||||
}
|
||||
return '\0';
|
||||
}
|
Reference in New Issue
Block a user