1 Replies - 251 Views - Last Post: 10 November 2017 - 10:16 AM Rate Topic: -----

#1 AShahwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-August 17

Incorrect reading from 4x4 matrix keypad

Posted 10 November 2017 - 09:35 AM

Hey friends, can someone help me rectify an error in my code.
I have a simple 4x4 matrix keypad attached to my Raspberry Pi 2 model B

I am running the following code and whenever I press a certain key, it prints some other key on screen. For instance, I pressed '4' and '5' appeared and then I pressed '4' again and this time 'B' appeared and so on. What am I doing wrong?

import RPi.GPIO as GPIO
#import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

MATRIX=[[1,2,3,'A'],
    [4,5,6,'B'],
    [7,8,9,'C'],
    ['+',0,'#','D']]

ROW= [7,11,13,15]
COL= [12,16,18,22]

for j in range(4):
    GPIO.setup(COL[j], GPIO.OUT)
    GPIO.output(COL[j], 1)

for i in range(4):
    GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)

try:
    while(True):
        for j in range(4):
            GPIO.output(COL[j], 0)

            for i in range(4):
                    if GPIO.input(ROW[i]) == 0:
                            print (MATRIX[i][j])
                           # time.sleep(0.2)
                            while(GPIO.input(ROW[i]) == 0):
                                    pass

    GPIO.output(COL[j], 1)

except KeyboardInterrupt:
    GPIO.cleanup()


Is This A Good Question/Topic? 0
  • +

Replies To: Incorrect reading from 4x4 matrix keypad

#2 AShahwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-August 17

Re: Incorrect reading from 4x4 matrix keypad

Posted 10 November 2017 - 10:16 AM

[NOTE] There was some error in posting but I can neither EDIT nor DELETE it so here is the actual question I wanted to post:

Hey friends, can someone help me out here....

I have a simple client and server scenario with two Raspberry Pis (RPi 2 model B). One is server, another one is client.

I take user input at client (with a 4x4 matrix keypad). It takes the user input and sends it to the other side on the server. The program is running fine on both sides, but there seems to be some issue with data conversion (data types).

For instance, I press '3' on the keypad; on the server it says b'temp'. Note that when I remove this b'' from the code, it gives following error:

TypeError: a bytes-like object is required, not 'str' 


Secondly, once I press the second number, the program gets stuck and nothing further appears on-screen on either side.

Following is the client code:

import socket
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

host= '192.168.2.20'
port= 12345

MATRIX=[[1,2,3,'A'],
        [4,5,6,'B'],
        [7,8,9,'C'],
        ['+',0,'#','D']]

ROW= [7,11,13,15]
COL= [12,16,18,22]

for j in range(4):
    GPIO.setup(COL[j], GPIO.OUT)
    GPIO.output(COL[j], 1)
    #print ("working!")

for i in range(4):
    GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
    #print ("Also working!")

mySocket= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket successfully created')
mySocket= connect((host, port))
 
while True:
    try:
        for j in range(4):
            GPIO.output(COL[j], 0)

            for i in range(4):
                if GPIO.input(ROW[i]) == 0:
                    print (MATRIX[i][j])
                    mySocket.send(b'MATRIX[i][j]')
                    print (mySocket.recv(1024))
                    while(GPIO.input(ROW[i]) == 0):
                        print ('Inside the loop')
                        pass
            GPIO.output(COL[j], 1)
            #mySocket.close()

    except KeyboardInterrupt:
        GPIO.cleanup()


Now here is the server side:

import socket
import io

try:
    s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print ('Socket successfully created')

except:
    print ('Socket creation failed')

host= '192.168.2.20'
port= 12345
s.bind((host, port))

s.listen(5)
print ('Listening...')
while True:
    c, addr = s.accept()
    print ('Got connection from',addr)
    byt = io.BytesIO(c.recv(1024))
    print (byt.read())
    c.sendall(b'Thank you for connecting')
    #c.close()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1