Monday, December 17, 2012

A state machine for decoding 433 MHz signals

The figure above are a plot of the response at 433 MHz versus time. If one would decode this signal there are three different states one should consider:


  • 0 - Wait for signal. In this state we have not seen any signals yet.
  • 1 - Peak detected. We want to determine if this is a valid peak for us.
  • 2 - Delay detected. Here we want to determine if the delay is valid. It should not be to short, not to long and it could also be a "0" or a "1". If we have recieved all our expected bits we should store the packet and look for more packets.



Instead of hacking with some microcontroller, that eventually will do this decoding, I wrote up a python program to do this offline. Files from the logic analyser running at 1 MHz sampling rate are sendt through the python program. The program calls a function everytime the input changes, simulating a interrupt pin on a MCU. It also increases a count for each entry in the file, simulating a 1MHz timer.

 #return the "port" state, 1 if transmission otherwise 0  
 def readport(line):  
      return int(line.split('\t')[1])  
 # a pulse is ca 387  
 # init ca 9617  
 # 1 is ca 4600  
 # 0 is ca 2060  
 #state  
 # 0 - wait for pulse  
 # 1 - make sure the pulse is of correct length, 200 to 500  
 # 2 - measure delay, "init", "0","1" or "illegal" delay  
 # simulate interrupt due to pin state change  
 def statechange():  
      global state, counter, res, timer  
      if nextportstate == 1: # we are going from 0 -> 1  
           transition = 1  
      else:  
           transition = 2 #from 1 -> 0  
      #print debugtime, state, res  
      if state == 0 and transition == 1: # possible transmission/pulse detected  
           state = 1  
           timer = 500 #time out after 500  
           counter = 0 # reset timer  
           return  
      if state == 0 and transition == 2: # disregard  
           state = 0  
           timer = 0  
           counter = 0  
           return  
      if state == 1: #(and transition == 2):  
           if transition ==1 or counter < 200: #200  
                #print "counter " , counter  
                #print "ERROR in state==1"  
                state = 0  
                timer = 0  
                counter = 0 # reset timer  
           else: # we detected a legal pulse, determine delay length  
                # 0 < delay 1500 illegal, goto state 0  
                # 1500 < delay < 2500 legal, "0"  
                # 2500 < delay <5000 legal "1"  
                # 5000 < delay < 8000 illegal, goto state 0  
                # 9000 < delay 11000 legal, init delay  
                # 11000 < delay illegal, state 0  
                state = 2  
                timer = 10000  
                counter = 0 # reset timer  
           return  
      if state == 2: #(and transition == 1)  
           if transition == 2: #and counter < 8000:  
                print "ERROR in state==2"  
                state = 0  
                timer = 0  
                counter = 0  
           else: #legal init delay  
                #print "counter state==2 ",counter  
                if counter > 9000: # init pulse, reset data  
                     print "RES", res  
                     res = ''  
                     state = 1  
                     timer = 500  
                     counter = 0  
                elif counter > 4000: #got a "1"  
                     res = res + "1"  
                     #print res  
                     state = 1  
                     timer = 500  
                     counter = 0  
                elif counter > 1700: #got a "0"  
                     res = res + "0"  
                     #print res  
                     state = 1  
                     timer = 500  
                     counter = 0  
                else: # illegal, to short  
                     res = ""  
                     state = 0  
                     timer = 0  
                     counter = 0  
                if len(res) == 28:  
                     print "RES", res  
                     res = ''  
                     state = 0  
                     timer = 0  
                     counter = 0  
           return  
 #simulate interrupt due to timer  
 #if something times out it is du to a to long pulse or delay  
 # we will return to state==0  
 def timerint():  
      global state, counter, timer  
      state = 0  
      timer = 0  
      counter = 0  
 #fp = open("untitled.tsv", "r") #noisy data  
 fp = open("untitled13.tsv", "r") 
 data = fp.readlines()  
 fp.close()  
 # timer  
 # 0 do not time out  
 #   
 timer = 0   
 print "file read"  
 transition = 0  
 # 0 no change  
 # 1 low to high  
 # 2 high to low  
 counter = 0 # simulate an interrupt counting  
 portstate = 0  
 nextportstate = 0  
 debugtime = -1  
 res = "XXX"  
 timer = 0  
 state = 0  
 for l in data:  
      #print debugtime, state, counter,timer  
      if timer > 0:  
           if counter>= timer:  
                timerint()  
                #counter = 0  
      counter = counter + 1  
      debugtime = debugtime + 1  
      #print debugtime, portstate       
      if portstate != nextportstate:  
           callint = 1  
      else:  
           callint = 0  
      portstate = nextportstate  
      nextportstate = readport(l)  
      if callint==1:  
           statechange()  
 #res 0100010100011111100100110100  
 #    0100010100011111100100110100  

This python program works as expected, at least as long as the signal quality is good.

Making the signal weaker tend to split the peaks into several, shorter peaks, sending the routine back to state "0" without recording any transmission. It is not clear if this should be adressed.

Only a real test on an MCU could provide that answer.

No comments:

Post a Comment