433 MHz revisited

Posted on Sun 14 September 2014 in embedded

Well, after this long time you may expect that i cracked the code. Sorry, but I didn’t. I just wanted to update you with the current version of my sketch to decode the weather sensor.

Status

The current status of the project is somehow revisited. I had some spar time and wanted to test the sensor again. This time i referenced to the following resource, because i’d like the idea to use interrupts to read the correct data.

Code

After i took a look into my old code and almost after the moment i had to admit that it isn’t that good, i rewrote the whole thing from scratch. The main difference is that i measure the time by calling “micros()” instead of using the raw value read by “pulseIn()”. With this the values get more stable than in the last try to decode the signal.

unsigned long ntime=0;
unsigned long otime=0;
unsigned long signal=0;
//unsigned long garbage=0;

unsigned int init_seq=0;
unsigned int init_count=0;

unsigned int data_seq=0;
unsigned int data_count=0;

unsigned int stop_seq=0;

unsigned int finished=0;

unsigned int buf[50];

void setup() {
  Serial.begin(115200);
  Serial.println("go!");
  pinMode(2, INPUT);
}


void loop() {
  int i;

  unsigned long LowVal=pulseIn(2,LOW);

  ntime = micros();
  if (LowVal<300) return;
  signal = ntime-otime;
  otime=ntime;

  if (finished == 1) {
    for (i=0; i<50; i++) {
      if ( buf[i] > 1 ) break;
      Serial.print(buf[i]);
      //Serial.print("\t");
    }
    Serial.println();
    finished=0;
  }

  if (signal > 50000 && signal < 60000) {
    //Serial.println("Start 50000 found");
    init_seq=1;
    init_count=0;
    data_seq=0;
    data_count=0;
    stop_seq=0;
    finished=0;
    //Serial.print("Garbage  50k:");
    //Serial.println(garbage);
    //garbage=0;
    return;
  }
  if (signal > 200000 ) {
    // Last signal after sequence
    //Serial.print("Garbage 200k:");
    //Serial.println(garbage);
    //garbage=0;
    return;
  }

  if (signal > 1800 && signal < 1899 && stop_seq == 1) {
    // Last stop bit
    finished=1;
    stop_seq=0;
  }

  if (init_seq == 0 && data_seq == 0 ) return;

  //garbage++;

  //Serial.print("RAW: ");
  //Serial.print(signal);
  //Serial.println();

  if (init_seq == 1) {
    if ( signal > 1800 && signal < 1899 ) {
      init_count++;
      return;
    }
    if ( signal > 9100 && signal < 9199 && init_count == 7 ) {
      //Serial.println("Start seq complete. Data seq start.");
      data_seq=1;
      data_count=0;
      for (i=0; i<50; i++) {
        buf[i]=2;
      }
    }
    init_seq=0;
    init_count=0;
    return;
  }
  if (data_seq == 1) {
    if ( data_count > 49 ) {
      // buffer protect
      data_seq=0;
      data_count=0;
      return;
    }
    if ( signal > 1800 && signal < 1899 ) {
      // let's assume that this is 0
      buf[data_count]=0;
    }
    if ( signal > 3600 && signal < 3699 ) {
      // let's assume that this is 1
      buf[data_count]=1;
    }
    if ( signal > 3400 && signal < 3499 ) {
      // let's think, this is a stop bit mark - dunno why
      data_seq = 0;
      stop_seq = 1;
      return;
    }
    data_count++;
  }
}

As you see, this is the current development state. Perhaps i start using git or something, that you can follow my changes other the time.

Setup

Currently i have the arduino running at an raspberry pi in order to get a long term evaluation and much data. This will lead to a better understanding of how this signal is generated.

Strange output

I found out that this shitty sensor does not send every time with the same bit count. Somehow some bits get lost on the way. I have no real idea how this happens, but i will try to build an antenna to optimize the receiving circuit a bit.

What’s next?

Next update will “hopefully” include a working sketch and i will write down the workflow again, that you can follow it to be more successful with decoding your personal signals ;)

Comments