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);
// ---------------------------------------------------------------------------------------
// Acurite 00606TX Transmitter
// ---------------------------------------------------------------------------------------

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

#define MESSAGE_SIZE 32
#define LSFR_OFFSET 4

uint8_t LSFR_sequence[MESSAGE_SIZE] = {0};

const int TX_PIN = D2;
byte payload[4];

// ---------------------------------------------------------------------------------------
// setup
// ---------------------------------------------------------------------------------------
void setup ( void ) {
    pinMode(TX_PIN, OUTPUT);
    Serial.begin ( 115200 );
    Serial.println("Sending...");
    generatePayload();
    doTransmission();
}
// ---------------------------------------------------------------------------------------
// loop
// ---------------------------------------------------------------------------------------
void loop ( void ) {
  delay(20000);
}

// ---------------------------------------------------------------------------------------
// generatePayload
// ---------------------------------------------------------------------------------------
void generatePayload(){
      //define rolling code
    byte rollingCode = 0xAA;
    
    //generate random temperature
    int temperature = (int)random(0,400);
    Serial.printf("Temp: %.1f\n",temperature/10.0);

    int hightByteTemp = temperature >> 8;
    int lowByteTemp = temperature & 0xff;

    //add battery OK flag to 
    hightByteTemp = 0x80 | hightByteTemp;
    
    payload[0] = rollingCode;
    payload[1] = hightByteTemp;
    payload[2] = lowByteTemp;
    payload[3] = computeChecksum(3,payload);

    Serial.print("Payload: ");
    for(int i=0;i<4;i++){
      Serial.printf("%02x ",payload[i]);
    }
    Serial.println("");
}

// ---------------------------------------------------------------------------------------
// toggleLED
// ---------------------------------------------------------------------------------------
void toggleLED(){
    //flash led
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
}

// ---------------------------------------------------------------------------------------
// doTransmission
// ---------------------------------------------------------------------------------------
void doTransmission() {

    Serial.println("Doing transmission...");
    toggleLED();

    for (int burst = 1; burst <= 7; burst++) {
      Serial.print("Sending: ");
      sendPayload();
      sendSync();
      Serial.println("");
    }
}

// ---------------------------------------------------------------------------------------
// sendPayload
// ---------------------------------------------------------------------------------------
void sendPayload(){
  for(int i=0;i<4;i++){
    sendByte(payload[i]);
  }  
}

// ---------------------------------------------------------------------------------------
// sendByte
// ---------------------------------------------------------------------------------------
void sendByte(byte b){
  for(int i=7;i>=0;i--){
    int bitValue = readBit(b,i); 
    sendBit(bitValue);
    if(i%4==0){
      Serial.print(" ");
    }
  }  
}

// ---------------------------------------------------------------------------------------
// readBit
// ---------------------------------------------------------------------------------------
int readBit(byte b, int bitPos){
  int x = b & (1 << bitPos);
  return x == 0 ? 0 : 1;
}

// ---------------------------------------------------------------------------------------
// sendBit
// ---------------------------------------------------------------------------------------
void sendBit(int val){
  
  digitalWrite(TX_PIN, HIGH );    
  delayMicroseconds(SEP_LENGTH );  

  digitalWrite(TX_PIN, LOW );
  if(val==0){
    delayMicroseconds(BIT0_LENGTH );    
    Serial.print("0");
  }else{
    delayMicroseconds(BIT1_LENGTH );    
    Serial.print("1");
  }
}

// ---------------------------------------------------------------------------------------
// sendSync
// ---------------------------------------------------------------------------------------
void sendSync(){
  digitalWrite(TX_PIN, HIGH );
  delayMicroseconds(SEP_LENGTH);
  digitalWrite(TX_PIN, LOW );
  delayMicroseconds(SYNC_LENGTH );
  Serial.print(" -");
}

// ---------------------------------------------------------------------------------------
// calculateLSFR
// ---------------------------------------------------------------------------------------
void calculateLSFR() {
    int i;
    uint8_t reg = 0x7C;
    uint8_t temp_reg = 0;
 
    for (i = 0; i < MESSAGE_SIZE; i++) {
        temp_reg = reg & 0x01;
        reg >>= 1;
        reg |= (temp_reg << 7);
 
        if (temp_reg) {
            reg ^= 0x18;
        }
 
        LSFR_sequence[i] = reg;
        //printf("%02x\n", LSFR_sequence[i]);
    }
}
// ---------------------------------------------------------------------------------------
// combineLSFR
// --------------------------------------------------------------------------------------- 
uint8_t combineLSFR(uint8_t len, uint8_t *data) {
    uint8_t hash_reg = 0; // not 0x64
    int byte_idx, bit_idx;
    uint8_t byte, bit;
    //printf("***COMBINE\n");
 
    for (byte_idx = 0; byte_idx < len; byte_idx++) {
        for (bit_idx = 7; bit_idx >= 0; bit_idx--) {
            bit = (data[byte_idx] & (1 << bit_idx)) >> bit_idx;
            if (bit) {
                hash_reg ^= LSFR_sequence[byte_idx * 8 + (7 - bit_idx) + LSFR_OFFSET];
                //printf("[%d]: %02x\n", byte_idx * 8 + (7 - bit_idx), hash_reg);
            }
            bit = 0;
        }
    }
 
    return hash_reg;
}
 
// ---------------------------------------------------------------------------------------
// computeChecksum
// --------------------------------------------------------------------------------------- 
uint8_t computeChecksum(int length, uint8_t *buff) {
    calculateLSFR();
    return combineLSFR(length, buff);
}


Reading the values from the Transmitter

...