Reading a module GPIO pin status in python and performing a task

8 thoughts on “Reading a module GPIO pin status in python and performing a task

  1. Hi Guys,


    I’ve been struggling to implement this
    (without complicated software or hardware solution) but to no avail.
    Basically i can play around with the GPIO pin states but i can’t do
    something useful as interrupt or event monitoring applications.


    is, I can read and print a pin state: e.g at#gpio: 1,0 but how do i use
    that data to perform interrupt or compare that read or received data
    (string) with a prestored one such that it does something if the read/received and stored
    happens to be the same.


    In other words,  are there any
    code snipets in python or a simple software idea, that monitors the
    state of a module GPIO pin in python and branches elsewhere (e.g to a
    subroutine within the program and does something clever) just like in microcontrollers programming.


    Any heads-up will be appreciatead.



    1. Hi Paul,


      Interrupts are of course impossible, however branching at detection of a long and steady enough signal to do whatever job is trivial, maybe with a simple if block or procedure/function call. Are you looking for something special?


      1. Thanks for the reply Cosmin.


        Well, if that is the case, then I’ll have to accomplish a continous interupt monitoring using an external microcontroller which is fine with me.


        Basically, what i need doesn’t have to be something too special; a simple if or procedure call contsruct as indicated by you will suffice (i can always take it from there).


        At this pointponit in time i’m still teaching myself python and esp with those Telit modules, so a python code snippest (comments is trivial) will be much more appreciated as i can always develop the code once i get the knitty gritty of things.


        I’m familiar with programming in BASIC, C, LabVIEW, MATlab, AHDL but still a novice with python and i’m hoping by this year end to be an intermediate python programmer esp on Telit modules . Tongue out











        1. Here is an example,  the prints can be replaced with function calls:


          Small script to monitor GPIO
          Device Telit GM862
          Monitor GPIO3 which has 47k pull up resistor (HIGH)
          If GPIO is found LOW, the module proceeds to shutdown
          or sleep

          import GPIO
          import MOD
          import MDM

          print ‘Simple GPIO monitor exampler’

          # not really needed, is INPUT by default
          GPIO.setIOdir(3, 1, 0)

          # Enter monitor loop
          while (1):
              if ( GPIO.getIOvalue(3) == 1 ):
                  print ‘GPIO3 is HIGHr’
              elif ( GPIO.getIOvalue(3) == 0 ):
                  print ‘GPIO3 is LOWr’
                  print ‘Error occuredr’
              # Optional, with slow input signals – can sleep 1 second
              # print ‘Going to sleepr’
              # MOD.sleep(10)       

          # GPIO3 was found LOW, so SHUTDOWN the module
          print ‘Shutdown nowr’
          MDM.send(‘AT#SHDNr’, 5)

          # Or enter sleep mode for 100 seconds
          # print ‘Enter sleep moder’
          # MOD.powerSaving(100)


          1. Thanks Cosmin,


            That was quite straightforward than i expected becuase I’ve been doing something very similar to yours but using the AT commands approach and it isn’t working.

            Maybe i ‘ve been missing something.


            Nevertheless, I’ll give yours a try this weekend and see where i was going wrong with mine (just for interest sake) and if i can’t still get mine to work, i’ll post you the code for diagnosis.



          2. Right Paul, using GPIO dedicated Python module is much simpler then MDM and AT commands, nevertheless this latter method should work as well, we can take a look just for the sake of having things correctly experimented and solved.


  2. Hi Cosmin,


    Just to update u, your code snippet works fine although i’ve to add some delays to prevent it from running too fast and crashing my PC everynow and then.Smile


    Can you enlightten me on the following:


    1) Is there a limit (monitoring time, buffer size) on how long a GPIO monitoring must

    happen.  Because after a while, when doing the monitoring using pythonwin, i always receive a "string index out of range" error.


    2) When using the module and cmux; my PC crashes after a while (60mins or so).


    3) A while ago, i read somewhere of bugs in the python "GPIO.setIOdir" command.

         Is this problem fully solve, as the above command when use in a code happens to

         work without error only the very first time and gives error message if the same code

         is run again.




    Regarding the GPIO AT command approach; I’ve been trying for days now using various approches (disabled using #). As u can see, I’m not so sure of what parameter to check and how to check it. Only the first approach seems to work but not correctly.


    Also how and when is  the "find or index" method used to look for a ack string (e.g OK, ERROR, +CREG: 0,1 etc) in a variable after issuing a command. Pease, any working examples?



    Included belew is my python script for monitoring a GPIO pin using the AT command approach.  Please tell me what i’m doing wrongly.


    import GPIO
    import MDM
    import MOD

    print ‘GPIO3 Monitor r’
    MDM.send(‘AT#GPIO=3,1,0 r’,0)
    while Counter<5:
          S=MDM.send(‘AT#GPIO=3,2 r’,0)
          print S
          print R
          print Counter
          if S==1:
          #if R==1:
          #if R==’0,1′:
          #if R==’GPIO: 0,1′:
          #if R.find(‘GPIO: 0,1 r’) == 1:
          #if R.find(‘OK r’) == 1:
             print ‘GPIO3 is high r’
             MOD.sleep (10)
          elif S==0:
          #elif R==0:
          #elif R==’0,0′:
          #elif R==’GPIO: 0,0′:
          #elif R.find(‘GPIO: 0,0 r’) == 1:
          #if R.find(‘OK r’) == -1:
             print ‘GPIO3 is low r’
             MOD.sleep (10)
             print ‘Error Encountered r’
             print ‘Rebooting r’
             MDM.send(‘AT#REBOOT r’,0)
             MOD.sleep (10)

    I’ll be greatful if you can please clarify me on the above.







    1. Well, take a look in in Lib folder of your Python PC installation, you’ll see this library is implemented on PC using MDM and AT commands, of course being impossible to use the module’s native library. I think this explains some of your questions and offers an usage example.

      If you want to  test the native library you must run the code on the module itself.

      Regarding PC/CMUX crash, I suppose it might be a particular problem, maybe a driver or something like this. However I suggest again to run lengthy tests on module, being the real thing.