Raspberry pi & Raspberry Pico 433MHZ: transmit, receive, relay

This commit is contained in:
2023-04-12 14:56:55 +08:00
parent d3f887d658
commit 295f4e8a66
40 changed files with 3493 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
# Defines the RPI variable which is needed by rc-switch/RCSwitch.h
CXXFLAGS=-DRPI
CFLAGS += -Wall -Os -g
all: send codesend RFSniffer
send: ../rc-switch/RCSwitch.o send.o
$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $+ -o $@ -lwiringPi -lwiringPiDev -lcrypt
codesend: ../rc-switch/RCSwitch.o codesend.o
$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $+ -o $@ -lwiringPi -lwiringPiDev -lcrypt
RFSniffer: ../rc-switch/RCSwitch.o RFSniffer.o
$(CXX) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $+ -o $@ -lwiringPi -lwiringPiDev -lcrypt
clean:
$(RM) ../rc-switch/*.o *.o send codesend servo RFSniffer

View File

@@ -0,0 +1,17 @@
# About
rcswitch-pi is for controlling rc remote controlled power sockets
with the raspberry pi. Kudos to the projects [rc-switch](http://code.google.com/p/rc-switch)
and [wiringpi](https://projects.drogon.net/raspberry-pi/wiringpi).
I just adapted the rc-switch code to use the wiringpi library instead of
the library provided by the arduino.
## Usage
First you have to install the [wiringpi](https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/) library.
After that you can compile the example program *send* by executing *make*.
It uses wiringPi pin no 2 by default. You may want to change the used GPIO pin before compilation of the codesend.cpp source file. (Good Resource for Pin Details https://pinout.xyz/pinout/wiringpi)
## Note
The 'RF\_Sniffer' code is as yet untested. It _should_ work, but it is still being tested thoroughly. It's provided to allow you to start playing with it now.

View File

@@ -0,0 +1,102 @@
/*
RFSniffer
Usage: ./RFSniffer [<pulseLength>]
[] = optional
Hacked from http://code.google.com/p/rc-switch/
by @justy to provide a handy RF code sniffer
*/
#include "../rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
RCSwitch mySwitch;
void light()
{
digitalWrite(25, LOW);
usleep(200000);
digitalWrite(25, HIGH);
//printf("%d\n", digitalRead(25));
}
int main(int argc, char *argv[]) {
// This pin is not the first pin on the RPi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
int PIN = 7;
if(wiringPiSetup() == -1) {
printf("wiringPiSetup failed, exiting...");
return 0;
}
int pulseLength = 0;
if (argv[1] != NULL) pulseLength = atoi(argv[1]);
mySwitch = RCSwitch();
if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
pinMode(25, OUTPUT);
#define LIVE 1
static int i=0;
unsigned long value = 0;
static int values[LIVE] = {0};
while(1) {
while(1) {
if (mySwitch.available()) {
value = mySwitch.getReceivedValue();
values[i] = value;
if (value == 0)
{
printf("Unknown encoding\n");
}
else
{
light();
printf("i=%d, Received %lu\n", i, values[i] );
}
fflush(stdout);
mySwitch.resetAvailable();
if (i != 0) {
if (values[1] == values[0]+1)
printf("有我的按钮发来数据\n");
}
if (i != 1) {
if (values[0] == values[1]+1)
printf("有我的按钮发来数据\n");
}
if (i==LIVE) {
i=0;
break;
}
i=i+1;
}
}
usleep(100);
}
return 0;
}

View File

@@ -0,0 +1,67 @@
/*
Usage: ./codesend decimalcode [protocol] [pulselength]
decimalcode - As decoded by RFSniffer
protocol - According to rc-switch definitions
pulselength - pulselength in microseconds
'codesend' hacked from 'send' by @justy
- The provided rc_switch 'send' command uses the form systemCode, unitCode, command
which is not suitable for our purposes. Instead, we call
send(code, length); // where length is always 24 and code is simply the code
we find using the RF_sniffer.ino Arduino sketch.
(Use RF_Sniffer.ino to check that RF signals are being produced by the RPi's transmitter
or your remote control)
*/
#include "../rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
// This pin is not the first pin on the RPi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
int PIN = 7;
// Parse the first parameter to this command as an integer
int protocol = 0; // A value of 0 will use rc-switch's default value
int pulseLength = 0;
int bitLength = 24;
// If no command line argument is given, print the help text
if (argc == 1) {
printf("Usage: %s decimalcode [protocol] [pulselength] [bitlength]\n", argv[0]);
printf("decimalcode\t- As decoded by RFSniffer\n");
printf("protocol\t- According to rc-switch definitions\n");
printf("pulselength\t- pulselength in microseconds\n");
printf("bitlength\t- bit length\n");
return -1;
}
// Change protocol and pulse length accroding to parameters
char *eptr;
unsigned long code = strtoul(argv[1], &eptr, 10);
if (argc >= 3)
protocol = atoi(argv[2]);
if (argc >= 4)
pulseLength = atoi(argv[3]);
if (argc >= 5)
bitLength = atoi(argv[4]);
if (wiringPiSetup() == -1)
return 1;
printf("sending code[%lu]\n", code);
RCSwitch mySwitch = RCSwitch();
if (protocol != 0)
mySwitch.setProtocol(protocol);
if (pulseLength != 0)
mySwitch.setPulseLength(pulseLength);
mySwitch.enableTransmit(PIN);
mySwitch.send(code, bitLength);
return 0;
}

View File

@@ -0,0 +1,62 @@
/*
Usage: ./send <systemCode> <unitCode> <command>
Command is 0 for OFF and 1 for ON
*/
#include "../rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
/*
output PIN is hardcoded for testing purposes
see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
for pin mapping of the raspberry pi GPIO connector
*/
int PIN = 7;
const char *code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };
if (argc < 4) {
printf("Sending 433 MHz remote plug control codes, hardcoded on wiringpi pin %d.\n", PIN);
printf("Usage: %s <systemCode> <unitCode> <command> [pulseLength]\n", argv[0]);
printf("systemCode - First five settings of Type A 10 pole DIP switch, e.g. 11111\n");
printf("unitCode - Switch number [1 .. 5] or [10000 .. 00001]\n");
printf("command - 0 for OFF and 1 for ON\n");
printf("pulseLength - optional pulse length\n");
return -1;
}
char *systemCode = argv[1];
const char *unitCode;
if (strlen(argv[2]) == 5) {
unitCode = argv[2];
} else if (atoi(argv[2]) > 0 and atoi(argv[2]) < 6) {
unitCode = code[atoi(argv[2])];
} else {
return -1;
}
int command = atoi(argv[3]);
if (wiringPiSetup() == -1)
return 1;
printf("sending systemCode[%s] unitCode[%s] command[%i]\n", systemCode, unitCode, command);
RCSwitch mySwitch = RCSwitch();
if (argv[4] != NULL)
mySwitch.setPulseLength(atoi(argv[4]));
mySwitch.enableTransmit(PIN);
switch (command) {
case 1:
mySwitch.switchOn(systemCode, unitCode);
break;
case 0:
mySwitch.switchOff(systemCode, unitCode);
break;
default:
printf("command[%i] is unsupported\n", command);
return -1;
}
return 0;
}