Skip to content

Instantly share code, notes, and snippets.

@User65k
Created September 16, 2018 20:16
Show Gist options
  • Save User65k/0caf3febfbf7f8e071a15e84399dbfe5 to your computer and use it in GitHub Desktop.
Save User65k/0caf3febfbf7f8e071a15e84399dbfe5 to your computer and use it in GitHub Desktop.
/*
* Listener for 433MHz "HomeWizard" signals (on a Raspi).
* Created as https://github.com/1technophile/NewRemoteSwitch did not work for me.
* License: GPLv3.
*
* _ _
* '0': | |_| |_____ (T,T,T,5T)
* _ _
* '1': | |_____| |_ (T,5T,T,T)
*
* - start pulse: 1T high, 10T low
* - 26 bit: Address
* - 1 bit: group bit
* - 1 bit: on/off/[dim]
* - 4 bit: unit
* - stop pulse: 1T high, 40T low
*
* /
#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
const int PIN = 2;
static unsigned int nReceivedValue; // decoded val
/* //debug:
const int DUR_BUF_SIZE = 64;
static unsigned int DUR_BUF[DUR_BUF_SIZE];
static unsigned int DUR_BUF_POINT = 0;*/
void handleInterrupt() {
static unsigned long lastTime = 0;
static unsigned int recvBuff = 0;
static unsigned char state = 0;
static unsigned int pulse_len_short_max;
static unsigned int pulse_len_long_max;
static unsigned int pulse_len_sync_max;
const long time = micros();
const unsigned int duration = time - lastTime;
if (duration < 100) return; //jitter
if (1==digitalRead(PIN))
{
//rising -> delay is signal
if (duration < pulse_len_short_max) {
if (state==1)
{
state='0';//this was the fist pause of '0'
}else if (state=='1')
{
recvBuff = (recvBuff << 1) | 1;
state=1;//this was the second pause of '1'
}else state=0;
}else if (duration < pulse_len_long_max) {
if (state==1)
{
state='1';//this was the fist pause of '1'
}else if (state=='0')
{
recvBuff = recvBuff << 1;
state=1;//this was the second pause of '0'
}else state=0;
}else if (duration < pulse_len_sync_max) {
//sync
state=1;
//DUR_BUF_POINT = 0;
recvBuff = 0;
}else {
//longer than everything
if (state==1)
{
//also we had something in the past -> this must be the end
nReceivedValue = recvBuff;
}
state = 0;
}
/*if (state==1 || DUR_BUF_POINT > 0)
{
DUR_BUF[DUR_BUF_POINT++] = state;
if (DUR_BUF_POINT >= DUR_BUF_SIZE) {
DUR_BUF_POINT = 0;
}
}*/
}else{
//falling -> delay is sync (HIGH is alway of duration T)
if (240 < duration && duration < 280)
{
unsigned int pulseD = duration;
pulse_len_short_max = pulseD * 3;
pulse_len_long_max = pulseD * 7.5;
pulse_len_sync_max = pulseD * 12;
}
}
lastTime = time;
}
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.
if(wiringPiSetup() == -1) {
printf("wiringPiSetup failed, exiting...");
return 0;
}
// Receiver on interrupt 0 => that is pin #2
nReceivedValue = 0;
wiringPiISR(PIN, INT_EDGE_BOTH, &handleInterrupt);
while(1) {
if (nReceivedValue != 0) {
printf("%u\n", nReceivedValue );
/*
for(int i=0;i<DUR_BUF_SIZE;i++)
{
printf("%i\t", DUR_BUF[i] );
}
printf("\n");
nReceivedValue = 0;
for(int i=1;i<DUR_BUF_SIZE;i+=2)
{
nReceivedValue = nReceivedValue << 1;
if (DUR_BUF[i]==49)
nReceivedValue |= 1;
printf("%i", DUR_BUF[i]-48 );
}
printf("\n%u\n", nReceivedValue );*/
fflush(stdout);
nReceivedValue = 0;
}
usleep(100);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment