QuantumRevenger
Posts: 5
Joined: Tue Apr 11, 2017 5:32 pm

Is it possible to record GPIO input and replay it as output?

Thu Apr 13, 2017 5:11 pm

I'm developing an automated gate opener for my disabled partner and am having issues working out the binary code for the keyfob. I know it's not using any special encryption or techniques due to the age of the hardware and manufacture advice - a general universal keyfob.

I've got a transmitter and receiver from https://www.amazon.co.uk/433MHz-Transce ... ransceiver

And have tried several solutions from GitHub ranging from Python to C++ without success, and am thinking is it possible to record the GPIO input for 3 seconds from the receiver, store it, and replay it on the transmitter.

I'm not fluent in either Python or C++ so any advice or sample scripts to achieve this would be gratefully appreciated.

Goals:

1. Read GPIO Input for 3 seconds, store in file.
2. Write GPIO Output for 3 seconds, read from file.

User avatar
joan
Posts: 13806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 5:25 pm

It's certainly possible.

May I suggest you have a look at my piscope and see what the raw data looks like.

http://abyz.co.uk/rpi/pigpio/piscope.html

If the data looks clean it will be reasonably straightforward to process.

QuantumRevenger
Posts: 5
Joined: Tue Apr 11, 2017 5:32 pm

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 6:07 pm

Hi Joan,

I've got Piscope / Pigpio installed and the signal is clean e.g nothing shows when the button isn't pressed when I press the button it displays the signal. It's very long and seems to have an opening block, repeated pattern, then a closing block. There are hundreds of (high / low) points. Some like (Big Big) (Little Little Little) (Big Little) and (Little Little)

I believe the (Big Little) represents a binary zero (0) and the (Little Little) represents a one (1) but the others I cannot understand or find reference to.

Kind regards,

Jordan

User avatar
joan
Posts: 13806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 6:57 pm

See if you can capture and save a clean sample.
Samples can be saved with File Save All Samples or File Save Selected Samples.

To select samples enter pause mode. Press 1 to specify the start of the samples (green marker) and 2 to specify the end of the samples (red marker).

The samples may be saved in the native piscope format or in VCD format.

Data saved in VCD format may be viewed and further processed with GTKWave.

Data saved in the native piscope format may be restored later with File Restore Saved Data.
If you save it in piscope format you should be able to paste the text into a code block so we could have a look.

QuantumRevenger
Posts: 5
Joined: Tue Apr 11, 2017 5:32 pm

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 7:43 pm

Hi Joan,

Thank you for being so helpful in this project it's much appreciated, here is a copy of the data collected.

I've uploaded to Pastebin as it's too large for a code block - it contains approx a five-second window of the button being pressed.

Attachment: https://pastebin.com/EYF7MDZS

I can verify that transmitting the information works, I tested it by repeating the GPIO input to the GPIO output with a delay - the gates opened straight away :) It's a matter of storing it on the RPi and transmitting it again. [Tested in a room out of range of the gates, and a very long cable!]

Kind regards,

Jordan

User avatar
joan
Posts: 13806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 8:06 pm

I don't recognise the signal. This seems to be one message.
rf.png
rf.png (46.93 KiB) Viewed 4038 times
j2.piscope

Code: Select all

#piscope
#date 2017-04-11 22:57:33
6892787 118181DF
6893152 100181DF
6893552 118181DF
6893918 100181DF
6894332 118181DF
6894687 100181DF
6895098 118181DF
6895457 100181DF
6895884 118181DF
6896232 100181DF
6896642 118181DF
6897002 100181DF
6897417 118181DF
6897773 100181DF
6898187 118181DF
6898543 100181DF
6898967 118181DF
6899312 100181DF
6899738 118181DF
6900082 100181DF
6900508 118181DF
6900854 100181DF
6901282 118181DF
6901628 100181DF
6905512 118181DF
6905900 100181DF
6906667 118181DF
6907407 100181DF
6907822 118181DF
6908207 100181DF
6908977 118181DF
6909372 100181DF
6910137 118181DF
6910882 100181DF
6911312 118181DF
6911678 100181DF
6912462 118181DF
6912834 100181DF
6913602 118181DF
6914353 100181DF
6914774 118181DF
6915142 100181DF
6915918 118181DF
6916662 100181DF
6917083 118181DF
6917462 100181DF
6918237 118181DF
6918622 100181DF
6919397 118181DF
6920137 100181DF
6920557 118181DF
6920922 100181DF
6921708 118181DF
6922452 100181DF
6922869 118181DF
6923618 100181DF
6924023 118181DF
6924788 100181DF
6925177 118181DF
6925543 100181DF
6926338 118181DF
6927068 100181DF
6927487 118181DF
6927843 100181DF
6928642 118181DF
6929417 100181DF
6929803 118181DF
6930538 100181DF
6930958 118181DF
6931718 100181DF
6932113 118181DF
6932507 100181DF
6933278 118181DF
6934028 100181DF
6934422 118181DF
6934834 100181DF
6935588 118181DF
6935972 100181DF
6936742 118181DF
6937147 100181DF
6937894 118181DF
6938657 100181DF
6939062 118181DF
6939808 100181DF
6940213 118181DF
6941003 100181DF
6941363 118181DF
6941777 100181DF
6942518 118181DF
6942932 100181DF
6943678 118181DF
6944062 100181DF
6944834 118181DF
6945232 100181DF
6945998 118181DF
6946387 100181DF
6947142 118181DF
6947543 100181DF
6948303 118181DF
6949093 100181DF
6949473 118181DF
6949858 100181DF
6950613 118181DF
6951038 100181DF
6951772 118181DF
6952173 100181DF
6952922 118181DF
6953703 100181DF
6954083 118181DF
6954859 100181DF
6955243 118181DF
6956013 100181DF
6956398 118181DF
6957172 100181DF
6957548 118181DF
6958372 100181DF
6958707 118181DF
6959468 100181DF
6959875 118181DF
6960258 100181DF
6961023 118181DF
6961427 100181DF
6962178 118181DF
6962968 100181DF
6963333 118181DF
6964093 100181DF
6964493 118181DF
6964865 100181DF
6965638 118181DF
6966467 100181DF
6966799 118181DF
6967578 100181DF
6967950 118181DF
6968677 100181DF
6969127 118181DF
6969848 100181DF
6970283 118181DF
6970978 100181DF
6971438 118181DF
6972212 100181DF
6972588 118181DF
6973358 100181DF
6973743 118181DF
6974543 100181DF
6974893 118181DF
6975668 100181DF
6976063 118181DF
6976835 100181DF
6977207 118181DF
6977629 100181DF
6978373 118181DF
6979173 100181DF
6979528 118181DF
6979965 100181DF
6980683 118181DF
6981113 100181DF
You can use the following code to play the GPIO.

Code: Select all

#!/usr/bin/env python

# playback.py
# 2016-12-23
# Public Domain

import os
import sys
import time

import pigpio

mapping = [None] * 32

def usage():
   print(
"""
Usage: {} file GPIO<=GPIO>*

E.g. {} rec.txt 5 7=10
means play 5 (source 5) and 7 (source 10) from samples in rec.txt
""".format(sys.argv[0], sys.argv[0]))

def validate_gpioarg(arg):

   # 5   => g_to = 5, g_from = 5
   # 3=9 => g_to = 3, g_from = 9

   gpio = arg.split("=")

   if len(gpio) > 2:
      return (-1, -1)

   try:
      g_to = int(gpio[0])
   except:
     return (-1, -1)

   g_from = g_to

   if len(gpio) > 1:
      try:
         g_from = int(gpio[1])
      except:
        return (-1, -1)

   if g_to < 0 or g_to > 31 or g_from < 0 or g_from > 31:
      return (-1, -1)

   return (g_to, g_from)

def validate_args():

   ok = True

   args = len(sys.argv)

   if args < 2:
      usage()
      return False

   filename = sys.argv[1]

   if not os.path.isfile(filename):
      print("Bad file: {}".format(filename))
      ok = False

   if args < 3:
      usage()
      return False

   for i in range(args-2):

      arg = sys.argv[i+2]

      (g_to, g_from) = validate_gpioarg(arg)

      if g_to == -1:
         print("Bad gpio: {}".format(arg))
         ok = False
      else:
         mapping[g_to] = g_from

   return ok

if not validate_args():
   exit(1)

# condense gpio assignments to a list

GPIO = []

for i in range(32):
   if mapping[i] is not None:
      GPIO.append((i, 1<<i, 1<<mapping[i]))

wf = []

with open(sys.argv[1], 'r') as f:
   first = True
   for line in f:

      if line[0] != '#':
         fields = line.split()
         stamp = int(fields[0])
         levels = int(fields[1],16)

         if first:
            first = False

            on = 0
            off = 0

            for g in GPIO:
               if g[2] & levels:
                  on = on | g[1]
               else:
                  off = off | g[1]

            last_on = off
            last_off = on

         else:

            if on == last_on and off == last_off:
               p = wf.pop()
               delay = p.delay
            else:
               delay = 0

            wf.append(pigpio.pulse(on, off, (stamp-last_stamp)+delay))

            last_on = on
            last_off = off

            on = 0
            off = 0

            for g in GPIO:
                if g[2] & levels:
                   on = on | g[1]
                else:
                   off = off | g[1]

         last_stamp = stamp

   if on != last_on or off != last_off:
      wf.append(pigpio.pulse(on, off, 0))
   else:
      p = wf.pop()
      p.delay = 0
      wf.append(p)

   f.close()

pi = pigpio.pi() # Connect to local Pi.

if not pi.connected:
   exit()

for g in GPIO:
   pi.set_mode(g[0], pigpio.OUTPUT) # Need to be outputs for wave to work

pi.wave_add_new()

pi.wave_add_generic(wf)

wid = pi.wave_create()

if wid >= 0:
   pi.wave_send_once(wid)

   while pi.wave_tx_busy():
      time.sleep(0.01)

   pi.wave_delete(wid)

pi.stop()
./playback.py j2.piscope 23 # playback to GPIO 23 (from GPIO 23) or
./playback.py j2.piscope 5=23 # playback to GPIO 5 (from GPIO 23)

If someone recognises the code there should be a simpler method.

QuantumRevenger
Posts: 5
Joined: Tue Apr 11, 2017 5:32 pm

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 8:27 pm

Hi Joan,

Thank you ever so much! I will test the code first thing in the morning :)

Kind regards,

Jordan

User avatar
joan
Posts: 13806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Is it possible to record GPIO input and replay it as out

Thu Apr 13, 2017 8:34 pm

QuantumRevenger wrote:Hi Joan,

Thank you ever so much! I will test the code first thing in the morning :)

Kind regards,

Jordan
Your large sample is probably to big to playback using pigpio waves. If you do need such a large sample to drive your hardware it should be possible to come up with a much simpler playback method (it won't be as faithful to the original timings but hopefully it would still work).

QuantumRevenger
Posts: 5
Joined: Tue Apr 11, 2017 5:32 pm

Re: Is it possible to record GPIO input and replay it as out

Fri Apr 14, 2017 2:07 pm

Good Afternoon,

I tried the code this morning and took several samples to replay which transmitted without issue (detected with a monitor at the gate) however, it does not accept the same code twice and seems to be using rolling code.

I have come up with a working solution which utilises the existing key fob, by bridging the switch button it triggers the code to be sent from the remote. Which relay controller would be recommended for up to a 12V Switch? Preferably via Amazon in the UK.

Kind regards,

Jordan

User avatar
joan
Posts: 13806
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Is it possible to record GPIO input and replay it as out

Fri Apr 14, 2017 4:29 pm

QuantumRevenger wrote:Good Afternoon,

I tried the code this morning and took several samples to replay which transmitted without issue (detected with a monitor at the gate) however, it does not accept the same code twice and seems to be using rolling code.

I have come up with a working solution which utilises the existing key fob, by bridging the switch button it triggers the code to be sent from the remote. Which relay controller would be recommended for up to a 12V Switch? Preferably via Amazon in the UK.

Kind regards,

Jordan
I just use the cheapest I can find which isn't really very useful as a suggestion.

I'd start a new post to get relay suggestions.

luberth
Posts: 5
Joined: Sat Nov 24, 2018 10:44 am

Re: Is it possible to record GPIO input and replay it as output?

Wed Nov 28, 2018 7:35 pm

hello
File Save Selected Samples.
To select samples enter pause mode. Press 1 to specify the start of the samples (green marker) and 2 to specify the end of the samples (red marker).
strange, but i do not understand above
ok pause i get
nr 1 and 2 key on keyboard? does nothing see no red or green markers
can only save complete file
g does the golden line ok stepping and seeing u m seconds of a puls

also i do not understand how to save gpio selected viewing pin number preferences
i do not want to view them all, say only 22 to 30
but every time almost all gpio option boxes are selected
gpio selection pins is not saved on exit and reloaded on start
config file?

and another thing ;-)
i do not like the green seperator lines between channels almost the same as puls green line
in your avator image on this forum you have bleu seperator line
also cannot figure out how to set this seperator line to another color darkgrey
config file?

luberth
http://luberth.com/Bangert_30_1619GJ_An ... _Pi_433Mhz

Return to “General programming discussion”