1

Can you give me an idea on how to check the statuses of specific GPIO pins if it is HIGH or LOW? My project is all about checking the statuses of GPIO pins (6 GPIO pins). I want to check all of the pins at once and send a SMS message the statuses of the pins if it is HIGH or LOW. This the head start of my program.

If I send a message powerrefreshall it will trigger this:

SendSMS(ser,14)
print "Refresh ALL Relays"

this is my SendSMS method:

def SendSMS (ser,value):
  ser.write("AT\r")
  line = ser.read(size = 64)
  print line
  ser.write('AT+CMGS = "09073096***"\r')
  if (value == 14):
     if (GPIO.input(3) == 0):
        ser.write('lightbulbon')
     elif (GPIO.input(3) == 1):
        ser.write('lightbulboff')

I don't know how to check the 5 other pins all at once. I am lost.

3 Answers3

2

To check the state of each gpio on the extension header you could use read_bank_1 from Python or gpioRead_Bits_0_31 from C.

To check the state of all the gpios you are interested in you could use a mask.

Mask off all but the gpios you want and then compare against the states in which you are interested.

e.g. suppose the gpio state is read by one of the above into variable gpio_state.

If you are interested in gpios 4 , 7, 9, 10 then construct a variable mask of (1<<4) | (1<<7) | (1<<9) | (1<<10). Then setting variable now_state to gpio_state & mask would isolate the gpios of interest.

If you wanted to check that

  • all the gpios are high check that now_state equals mask.
  • all the gpios are low check that now_state equals 0.
  • that gpios 4 and 10 are high the others low check that now_state equals (1<<4) | (1<<10).

etc. etc.

joan
  • 71,852
  • 5
  • 76
  • 108
0

Yes the code might be wierd but with simple solution. You can "mask the lag" by declarating the functions before the main function. Declarating it seperatly and calling them in main function. It might speed up the code cycle rather then writing the code "all in one go". I hope it helps... Test this with arduino, you will understand what i mean. The difference might be more then noticable.

0

I have been able to check all the gpio pins individually..

if (value == 14):
   if (GPIO.input(5) == 0):
      ser.write('1')
   else:
      ser.write('0')
   if (GPIO.input(11) == 0):
      ser.write('1')
   else:
      ser.write('0')
   if (GPIO.input(7) == 0):
      ser.write('1')
   else:
      ser.write('0')
   if (GPIO.input(13) == 0):
      ser.write('1')
   else:
      ser.write('0')
   if (GPIO.input(3) == 0):
      ser.write('1')
   else:
      ser.write('0')
   if (GPIO.input(15) == 0):
      ser.write('1')
   else:
      ser.write('0')
ser.write(chr(26))
return

if the gpio pins states are LOW, the message that will be sent is 111111 if the gpio pins states are HIGH, the message that will be sent is 000000 if gpio pin 5 is LOW, the message that will be sent is 100000 and so on with 64 combinations... I am controlling an active-low relay