· 4 years ago · Jun 20, 2021, 04:36 AM
1# MADE BY NOAH
2
3# This is a somewhat straightforward program that relies on my raspberry pi
4# It sends a request like an api request to the raspberry pi in the form of a 1-time socket (the socket is disposed of after every request), then the raspberry pi returns what keys on my 16-key keypad/key matrix are pressed
5# The server will be up a lot more now that I have a blower fan blowing on it that is held by legos, with a 9V battery hooked up to it
6
7# Sending requests with a delay faster than .1s will just make the returned value innacurate
8# Port may change from time to time
9
10# IMPORTANT:
11
12# WILL NOT RUN CORRECTLY IN SOLOLEARN, VISUAL STUDIO OR VISUAL STUDIO CODE IS REQUIRED
13
14# ALL REQUESTS ARE LOGGED, REQUESTER'S IP IS ALSO LOGGED
15# IPS ARE NOT LOGGED OR PUBLICISED IN ANY WAY
16# IP LOGGING IS ONLY FOR SECURITY PURPOSES, WIHOUT MALICIOUS INTENT
17# YOU MAY ASK TO HAVE YOUR IP REMOVED FROM THE LOGS
18
19
20
21
22
23
24
25
26
27
28
29
30
31import socket # Import socket module
32from time import sleep
33import os
34s = socket.socket() # Create a socket object
35host = "192.168.11.239" # Get local machine name
36port = 2 # Reserve a port for your service.
37
38letters = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(" ")
39alts = "~ ` ! # $ % ^ & * ( ) _ - { } [ ] : ; \" ' / . > ? \\".split(" ")
40
41def newkey(key):
42 #handle all the key presses (add chars to string)
43 # this is useable if you wanna see when I type something with the keypad, which I wont
44 global string
45 global caps
46 localerrors = 0
47 if not key == "*":
48 string += key
49 else:
50 index = len(letters) - 1
51 for i in letters:
52 try:
53 string = string.replace("#" + str(index + 1), letters[index])
54 string = string.replace("#C" + str(index + 1), letters[index].upper())
55 string = string.replace("#A" + str(index + 1), alts[index])
56 except:
57 localerrors += 1
58 index -= 1
59
60 print(string)
61 #string is now what the user typed after processing
62 #--RUN CODE HERE--
63 string = ""
64caps = False
65s.connect((host, port))
66errors = 0
67end = False
68lastkeys = []
69string = ""
70while end == False: #replace with "if True:" for sololearn compiling
71 try:
72 s = socket.socket()
73 s.connect((host, port))
74 s.send(bytes("text","UTF-8"))
75 rec = s.recv(1024).decode("UTF-8")
76 #print(rec + " : keys pressed")
77 keys = rec.split(",")
78
79 # run your code here
80 # the keys var is all the pressed keys
81 print(keys)
82 if len(keys) > 1:
83 keys = rec.replace("X,","").split(",") # my server returns an X as a pressed key so its not returning an empty string
84 for i in keys:
85 pressed = False
86 for q in lastkeys:
87 if q == i:
88 pressed = True
89 if not pressed:
90 nothing = None
91 # newkey(i) # un-comment this line out if you wanna see if I type something using the keypad
92
93 lastkeys = keys
94 except Exception as e:
95 print("An error has occured. This may be due to the server being offline.")
96 sleep(0.1)