Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Transmitter Code

Code Block
languagecpp
const int TX_PIN = 4;   // ESP8266 pin D2

typedef struct  {
    boolean power;
    int duration;   // micro seconds
} signal;

const signal SHORT_ON { true, 500 };
const signal SHORT_OFF { false, 2000 };
const signal LONG_ON { true, 850 }; // not used
const signal LONG_OFF { false, 4000 };
const signal STOP_OFF { false, 500 };
const int REST = 9000; // 9 ms gap


// Here's a temperature of 24.5 forever...
//
signal sig_1 [] {
    SHORT_ON,             // start bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    
    LONG_OFF, SHORT_ON,   // a one (1) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    
    LONG_OFF, SHORT_ON,   // a one (1) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit

    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
//    LONG_OFF, SHORT_ON,   // a one (1) bit

    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit

    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit

    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit

    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    LONG_OFF, SHORT_ON,   // a one (1) bit
    SHORT_OFF, SHORT_ON,  // a zero (0) bit

    STOP_OFF, SHORT_ON    // stop bit
};


void doTransmission(signal array[67]) {

    Serial.println("Doing transmission...");
    for (int burst = 1; burst <= 7; burst++) {

        for (int idx = 0; idx < 66; ++idx ) {   // payload
            digitalWrite(TX_PIN, array[idx].power);
            delayMicroseconds( array[idx].duration );
        }
        delayMicroseconds(REST);                // rest between bursts
    }
}

void setup ( void ) {
    pinMode(TX_PIN, OUTPUT);
    Serial.begin ( 115200 );
}


void loop ( void ) {

  Serial.println("Sending...");
  doTransmission(sig_1);
  delay(10000);

}


Reading the values from the Transmitter

Code Block
languagecpp
/* Convert RF signal into bits (temperature sensor version) 
 * Written by : Ray Wang (Rayshobby LLC)
 * http://rayshobby.net/?p=8827
 */

// ring buffer size has to be large enough to fit
// data between two successive sync signals
#define RING_BUFFER_SIZE  256

#define SYNC_LENGTH  9000
#define SEP_LENGTH   500
#define BIT1_LENGTH  4000
#define BIT0_LENGTH  2000

#define DATAPIN  D2  


unsigned long timings[RING_BUFFER_SIZE];
unsigned int syncIndex1 = 0;  // index of the first sync signal
unsigned int syncIndex2 = 0;  // index of the second sync signal
bool received = false;

void ICACHE_RAM_ATTR handler();

// detect if a sync signal is present
bool isSync(unsigned int idx) {
  unsigned long t0 = timings[(idx+RING_BUFFER_SIZE-1) % RING_BUFFER_SIZE];
  unsigned long t1 = timings[idx];

  // on the temperature sensor, the sync signal
  // is roughtly 9.0ms. Accounting for error
  // it should be within 8.0ms and 10.0ms
  if (t0>(SEP_LENGTH-100) && t0<(SEP_LENGTH+100) ){
    if (t1>(SYNC_LENGTH-1000) && t1<(SYNC_LENGTH+1000)){
      if (digitalRead(DATAPIN) == HIGH) {
         return true;         
      }
    }
  }
  return false;
}

/* Interrupt handler */
void ICACHE_RAM_ATTR handler() {
  static unsigned long duration = 0;
  static unsigned long lastTime = 0;
  static unsigned int ringIndex = 0;
  static unsigned int syncCount = 0;

  // ignore if we haven't processed the previous received signal
  if (received == true) {
    Serial.println("received==true");
    return;
  }

  // calculating timing since last change
  long time = micros();
  duration = time - lastTime;
  lastTime = time;

  // store data in ring buffer
  ringIndex = (ringIndex + 1) % RING_BUFFER_SIZE;
  timings[ringIndex] = duration;

  // detect sync signal
  if (isSync(ringIndex)) {
    syncCount ++;
    // first time sync is seen, record buffer index
    if (syncCount == 1) {
      syncIndex1 = (ringIndex+1) % RING_BUFFER_SIZE;
    } 
    else if (syncCount == 2) {
      // second time sync is seen, start bit conversion
      syncCount = 0;
      syncIndex2 = (ringIndex+1) % RING_BUFFER_SIZE;
      unsigned int changeCount = (syncIndex2 < syncIndex1) ? (syncIndex2+RING_BUFFER_SIZE - syncIndex1) : (syncIndex2 - syncIndex1);
      // changeCount must be 66 -- 32 bits x 2 + 2 for sync
//      Serial.print("ChangeCount:");
//      Serial.println(changeCount);
      
      if (changeCount < 66 || changeCount > 68) {
        received = false;
        syncIndex1 = 0;
        syncIndex2 = 0;
      } 
      else {
        received = true;
      }
    }

  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Started.");
  pinMode(DATAPIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(DATAPIN), handler, CHANGE);
  Serial.println("\n\nStarted");
}

void loop() {
  if (received == true) {
     Serial.println("Received...");
     
    // disable interrupt to avoid new data corrupting the buffer
    detachInterrupt(digitalPinToInterrupt(DATAPIN));

    // loop over the lowest 12 bits of the middle 2 bytes
    unsigned long temp = 0;
    bool negative = false;
    bool fail = false;
    for(unsigned int i=(syncIndex1+24)%RING_BUFFER_SIZE; i!=(syncIndex1+48)%RING_BUFFER_SIZE; i=(i+2)%RING_BUFFER_SIZE) {
      unsigned long t0 = timings[i], t1 = timings[(i+1)%RING_BUFFER_SIZE];
      if (t0>(SEP_LENGTH-100) && t0<(SEP_LENGTH+100)) {
        if (t1>(BIT1_LENGTH-1000) && t1<(BIT1_LENGTH+1000)) {
          if(i == (syncIndex1+24)%RING_BUFFER_SIZE) negative = true;
          temp = (temp << 1) + 1;
        } 
        else if (t1>(BIT0_LENGTH-1000) && t1<(BIT0_LENGTH+1000)) {
          temp = (temp << 1) + 0;
        } 
        else {
          fail = true;
        }
      } 
      else {
        fail = true;
      }
    }


    if (!fail) {
      if (negative) {
        temp = 4096 - temp; 
        Serial.print("-");
      }
      Serial.print(temp/10.0);  //temp in C
      
      Serial.write(176);    // degree symbol
      Serial.print("C/");
      Serial.print((temp)*9/50.0+32);  // convert to F
      Serial.write(176);    // degree symbol
      Serial.println("F");
    } else {
      Serial.println("Decoding error.");
    } 
    // delay for 1 second to avoid repetitions
    delay(1000);
    received = false;
    syncIndex1 = 0;
    syncIndex2 = 0;

    // re-enable interrupt
    attachInterrupt(digitalPinToInterrupt(DATAPIN), handler, CHANGE);
  }

}


Presentation

View file
nameRF Hacking -- Irdeto Hackday Oct-3-2019.pdf
height250

...