Buffering a bitstream and save it to array

Hello,

i have a question,
i want to decode information from a 433mhz weather station. i know the coding protocol from this weather station. the weather station sends each 5 minutes a bitstream.
How can i buffer this stream and save it to a array? the data is coming in on gpio 2 on interrupt base.
i only need code to save it in a array so that i can loop with a for loop through the bits, to get the neccesary information

below the protocol, i have the ws1200.

* Technische informatie:
 * Decodes signals from Alecto Weatherstation outdoor unit, type 3 (94/126 pulses, 47/63 bits, 433 MHz).
 * WS1100 Message Format: (7 bits preamble, 5 Bytes, 40 bits):
 * AAAAAAA AAAABBBB BBBB__CC CCCCCCCC DDDDDDDD EEEEEEEE
 *                        Temperature Humidity Checksum
 * A = start/unknown, first 8 bits are always 11111111
 * B = Rolling code
 * C = Temperature (10 bit value with -400 base)
 * D = Checksum
 * E = Humidity
 *
 * WS1200 Message Format: (7 bits preamble, 7 Bytes, 56 bits):
 * AAAAAAA AAAABBBB BBBB__CC CCCCCCCC DDDDDDDD DDDDDDDD EEEEEEEE FFFFFFFF 
 *                        Temperature Rain LSB Rain MSB ???????? Checksum
 * A = start/unknown, first 8 bits are always 11111111
 * B = Rolling code
 * C = Temperature (10 bit value with -400 base)
 * D = Rain ( * 0.3 mm)
 * E = ?
 * F = Checksum
 \*********************************************************************************************/

I do not know what the bit timings are for your sensor but below is code I wrote 8 years ago to decode a no-brand weather sensor I used to use.

// My 433Mhz weather sensor decoder.
//             |   | = 480us
// __           ___       ___    ___
//   |         |  |      |  |   |  |
//   |_________|  |______|  |___|  |
//
//   |  Sync      |    1    |  0   |
//   |  9760us    | 4400us  | 2440us

/*
 0101 1010 0001 1101 0001 0001 0100 0101 1100 27.6C 92%
                BBCC TTTT TTTT TTTT HHHH HHHH
T = Temperature
H = Humidity
C = Channel
B = Battery?
*/

// Defines
#define allDataBits 36                                    // Number of data bits to expect
// isrFlags bit numbers
#define F_HAVE_DATA 1                                     // 0=Nothing in read buffer, 1=Data in read buffer
#define F_GOOD_DATA 2                                     // 0=Unverified data, 1=Verified (2 consecutive matching reads)
#define F_CARRY_BIT 3                                     // Bit used to carry over bit shift from one long to the other
#define F_STATE 7                                         // 0=Sync mode, 1=Data mode

// Constants
const unsigned long sync_MIN = 9700;                      // Minimum Sync time in micro seconds
const unsigned long sync_MAX = 9800;
const unsigned long bit1_MIN = 4300;
const unsigned long bit1_MAX = 4500;
const unsigned long bit0_MIN = 2400;
const unsigned long bit0_MAX = 2500;
const unsigned long glitch_Length = 400;                  // Anything below this value is a glitch and will be ignored.

// Interrupt variables
unsigned long fall_Time = 0;                              // Placeholder for microsecond time when last falling edge occured.
unsigned long rise_Time = 0;                              // Placeholder for microsecond time when last rising edge occured.
byte bit_Count = 0;                                       // Bit counter for received bits.
unsigned long build_Buffer[] = {0,0};                     // Placeholder last data packet being received.
volatile unsigned long read_Buffer[] = {0,0};             // Placeholder last full data packet read.
volatile byte isrFlags = 0;                               // Various flag bits

unsigned long myData0 = 0;
unsigned long myData1 = 0;

void setup() {
  pinMode(13,OUTPUT); // Used for debugging
  Serial.begin(115200);
  pinMode(2,INPUT);
  Serial.println(F("ISR Pin 2 Configured For Input."));
  attachInterrupt(0,PinChangeISR0,CHANGE);
  Serial.println(F("Pin 2 ISR Function Attached. Here we go."));
}

void loop() {
  if (bitRead(isrFlags,F_GOOD_DATA) == 1) {
    if (myData1 != read_Buffer[1]){
      // We have at least 2 consecutive matching reads
      myData0 = read_Buffer[0]; // Read the data spread over 2x 32 variables
      myData1 = read_Buffer[1];
      bitClear(isrFlags,F_HAVE_DATA); // Flag we have read the data
      dec2binLong(myData0,4);
      dec2binLong(myData1,32);

      Serial.print(" Channel=");
      byte y = (myData1 >> 20) & 0x3;
      Serial.print(y);

      Serial.print(", Temperature=");
      int x = myData1 >> 4;
      x = x >> 4;
      Serial.print(x/10.0,1);
      
      Serial.print("C, Humidity=");
      y = myData1;
      Serial.print(y);
      Serial.print("%");
      Serial.println();
    }
  }
  delay(10);
}

void dec2binLong(unsigned long myNum, byte NumberOfBits) {
  if (NumberOfBits <= 32){
    myNum = myNum << (32 - NumberOfBits);
    for (int i=0; i<NumberOfBits; i++) {
      if ((i % 4) == 0)
      Serial.print(" ");
      if (bitRead(myNum,31) == 1) 
    Serial.print("1");
    else 
    Serial.print("0");
    myNum = myNum << 1;
    }
  }
}

void PinChangeISR0(){                                     // Pin 2 (Interrupt 0) service routine
  unsigned long Time = micros();                          // Get current time
  if (digitalRead(2) == LOW) {
    // Falling edge
    if (Time > (rise_Time + glitch_Length)) {
      // Not a glitch
      Time = micros() - fall_Time;                        // Subtract last falling edge to get pulse time.
      if (bitRead(build_Buffer[1],31) == 1)
      bitSet(isrFlags, F_CARRY_BIT);
      else
      bitClear(isrFlags, F_CARRY_BIT);
      
      if (bitRead(isrFlags, F_STATE) == 1) {
        // Looking for Data
        if ((Time > bit0_MIN) && (Time < bit0_MAX)) {
          // 0 bit
          build_Buffer[1] = build_Buffer[1] << 1;
          build_Buffer[0] = build_Buffer[0] << 1;
          if (bitRead(isrFlags,F_CARRY_BIT) == 1)
          bitSet(build_Buffer[0],0);
          bit_Count++;
        }
        else if ((Time > bit1_MIN) && (Time < bit1_MAX)) {
          // 1 bit
          build_Buffer[1] = build_Buffer[1] << 1;
          bitSet(build_Buffer[1],0);
          build_Buffer[0] = build_Buffer[0] << 1;
          if (bitRead(isrFlags,F_CARRY_BIT) == 1)
          bitSet(build_Buffer[0],0);
          bit_Count++;
        }
        else {
          // Not a 0 or 1 bit so restart data build and check if it's a sync?
          bit_Count = 0;
          build_Buffer[0] = 0;
          build_Buffer[1] = 0;
          bitClear(isrFlags, F_GOOD_DATA);                // Signal data reads dont' match
          bitClear(isrFlags, F_STATE);                    // Set looking for Sync mode
          if ((Time > sync_MIN) && (Time < sync_MAX)) {
            // Sync length okay
            bitSet(isrFlags, F_STATE);                    // Set data mode
          }
        }
        if (bit_Count >= allDataBits) {
          // All bits arrived
          bitClear(isrFlags, F_GOOD_DATA);                // Assume data reads don't match
          if (build_Buffer[0] == read_Buffer[0]) {
            if (build_Buffer[1] == read_Buffer[1]) 
            bitSet(isrFlags, F_GOOD_DATA);              // Set data reads match
          }
          read_Buffer[0] = build_Buffer[0];
          read_Buffer[1] = build_Buffer[1];
          bitSet(isrFlags, F_HAVE_DATA);                  // Set data available
          bitClear(isrFlags, F_STATE);                    // Set looking for Sync mode
          digitalWrite(13,HIGH); // Used for debugging
          build_Buffer[0] = 0;
          build_Buffer[1] = 0;
          bit_Count = 0;
        }
      }
      else {
        // Looking for sync
        if ((Time > sync_MIN) && (Time < sync_MAX)) {
          // Sync length okay
          build_Buffer[0] = 0;
          build_Buffer[1] = 0;
          bit_Count = 0;
          bitSet(isrFlags, F_STATE);                      // Set data mode
          digitalWrite(13,LOW); // Used for debugging
        }
      }
      fall_Time = micros();                               // Store fall time
    }
  }
  else {
    // Rising edge
    if (Time > (fall_Time + glitch_Length)) {
      // Not a glitch
      rise_Time = Time;                                   // Store rise time
    }
  }
}



I have the bit timings:
The pulselength is 40us
The sample rate of the raw signal is 30uS (sample width / resolution)
the weather station has a pulsecount of 126 pulses.
the signal repeat interval is 250 mS, in that time the same signal should not accepted again.

i want some function that fetch the signal and save it in this variable.
The original code is for a arduino mega, but i want to have it on a esp32. but the esp32 has another cpu freq. so the original code did not work on the esp32. it doesnt fetch the signal. i saw the raw bits in serial monitor when the weather station sends a signal.

struct RawSignalStruct                                                          // Raw signal variabelen places in a struct
  {
  int  Number;                                                                  // Number of pulses, times two as every pulse has a mark and a space.
  byte Repeats;                                                                 // Number of re-transmits on transmit actions.
  byte Delay;                                                                   // Delay in ms. after transmit of a single RF pulse packet
  byte Multiply;                                                                // Pulses[] * Multiply is the real pulse time in microseconds 
  unsigned long Time;                                                           // Timestamp indicating when the signal was received (millis())
  byte Pulses[RAW_BUFFER_SIZE+2];                                               // Table with the measured pulses in microseconds divided by RawSignal.Multiply. (halves RAM usage)
                                                                                // First pulse is located in element 1. Element 0 is used for special purposes, like signalling the use of a specific plugin
} RawSignal={0,0,0,0,0,0L};

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.