Skip to content

Instantly share code, notes, and snippets.

@abstractvector
Created April 30, 2020 20:00
Show Gist options
  • Save abstractvector/3d4100dc0e2934f6210d7c8eefea7874 to your computer and use it in GitHub Desktop.
Save abstractvector/3d4100dc0e2934f6210d7c8eefea7874 to your computer and use it in GitHub Desktop.
ESP32 433MHz receiver
#include <Arduino.h>
#define RF_DATA_PIN 32
#define TIMING_SYNC_SHORT_HIGH 190
#define TIMING_SYNC_SHORT_LOW 300
#define TIMING_SYNC_LONG_HIGH 1580
#define TIMING_SYNC_LONG_LOW 1520
#define TIMING_DATA_HIGH 210
#define TIMING_DATA_LOW_1 920
#define TIMING_DATA_LOW_0 560
#define TIMING_NOISE_LONG 2000
#define TIMING_NOISE_SHORT 140
#define TIMING_TOLERANCE 120
#define PACKET_LENGTH 40
unsigned long previousTime = 0;
int duration = 0;
int countShortSyncs = 0;
int countLongSyncs = 0;
bool timeWithinTolerance(int baseline, int measurement)
{
return measurement >= baseline - TIMING_TOLERANCE && measurement <= baseline + TIMING_TOLERANCE;
}
void resetCounters() {
countShortSyncs = 0;
countLongSyncs = 0;
}
void ICACHE_RAM_ATTR handleInterrupt()
{
unsigned long time = micros();
duration = time - previousTime;
previousTime = time;
if (duration > TIMING_NOISE_LONG || duration < TIMING_NOISE_SHORT)
{
return;
}
if (countShortSyncs < 4 && timeWithinTolerance(TIMING_SYNC_SHORT_LOW, duration)) {
Serial.print("Short sync: ");
Serial.println(countShortSyncs + 1);
countShortSyncs++;
return;
}
if (countShortSyncs != 4) {
resetCounters();
return;
}
if (timeWithinTolerance(TIMING_SYNC_LONG_LOW, duration)) {
Serial.print("Long sync: ");
Serial.println(countLongSyncs + 1);
countLongSyncs++;
return;
}
if (countLongSyncs != 4) {
resetCounters();
return;
}
}
void setup()
{
Serial.begin(115200);
pinMode(digitalPinToInterrupt(RF_DATA_PIN), INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RF_DATA_PIN), handleInterrupt, RISING);
}
void loop()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment