def _login(self): user = self.user.value; passwd = self.passwd.value; if not network.isConnected(): network.connect() # plain silly, we need to remove this if network.isConnected(): ans = protocol.sendMessage(TYPE = protocol.LOGIN, USER = user, PASS = passwd) if ans == True: state.logged_in = True state.in_game_select = True return else: title = "Error" err_msg = "Invalid username or password" else: title, err_msg = network.getError() # If the login was incorrect prompt the user CustomDialog(title, err_msg).open()
def talk_to_home(): print 'Awaiting network connection' onboard.waitalert1() self.data_loop = False network.wait(whenHearCall=heard) print 'Network connection made' onboard.waitalert2() onboard.connalert1() while True: if network.isConnected()==False: onboard.connalert2() print 'Disconnected. Waiting for connection' cut = False while cut==False: #print 'attempt hang up' try: network.hangUp() onboard.waitalert1() network.wait(whenHearCall=heard) cut = True #print 'hang up successful' except: pass #print 'cannot hang up' print "Connection made" onboard.waitalert2() onboard.connalert1()
def heard(phrase): onboard.inalert1() #remove with eh print "INCOMING:" + phrase words = str(phrase) self.homemsg = eval(words) onboard.inalert2() if network.isConnected(): if self.homemsg['MSG'] == 'DATA': if self.data_loop == False: self.data_loop = True def data_loop(): while True: if network.isConnected(): onboard.outalert1() response = self.state #print"RESPONSE:" + str(response) try: network.say(str(response)) #print response except: pass #print 'could not send message' onboard.outalert2() sleep(0.1) else: pass #print 'no home connection' x = threading.Thread(target=data_loop) x.start() elif self.homemsg['MSG'] == 'SER': arduino.write(self.homemsg['CMD']) else: print"NO CONNECTION TO RESPOND"
def recvMessage(self, world, lobby): if not network.isConnected(): network.connect() if not network.isConnected(): return "" message = network.mySelect() if message == "": return msg_type = message[0] if msg_type == MAP: self._handleMapRecv(message[1:], world) elif msg_type == START: state.in_game = True state.in_game_lobby = False elif msg_type == GAMES_LIST: self._handleGamesList(message[1:], lobby)
def heard(phrase): global score print("them:" + phrase) guessed_number = int(phrase) #time.sleep(1) #pause here to make the Pi seem human if network.isConnected(): if imagined_number > guessed_number: network.say("higher") elif imagined_number == guessed_number: network.say("CORRECT!!!") score = 1 else: network.say("lower")
def heard(phrase): global score global guessed_number print("them:" + phrase) if phrase == "higher": guessed_number=guessed_number*2 #double it! elif phrase == "lower": guessed_number=guessed_number-1 #decrement it. elif phrase == "CORRECT!!!": score = 1 if score == 0 and network.isConnected(): print("me:" + str(guessed_number)) network.say(str(guessed_number))
def data_loop(): while True: if network.isConnected(): onboard.outalert1() response = self.state #print"RESPONSE:" + str(response) try: network.say(str(response)) #print response except: pass #print 'could not send message' onboard.outalert2() sleep(0.1) else: pass
def heard(phrase): onboard.inalert1() print "INCOMING:" + phrase onboard.inalert2() if network.isConnected(): if phrase == 'DATA': response = onboard.sense_DATA() onboard.outalert1() print "RESPONSE:" + str(response) try: network.say(str(response)) except: print 'could not send message' onboard.outalert2() else: print "NO CONNECTION TO RESPOND"
def run_server(): trace("run_server") start_ticker() while True: try: trace("waiting for connection...") network.wait(whenHearCall=incoming, port=PORT) trace("connected!") while network.isConnected(): time.sleep(1) print("wasting time...") print("connection lost") finally: stop_status_reports() network.hangUp()
def heard(phrase): onboard.inalert1() #remove with eh print "INCOMING:" + phrase words = str(phrase) self.homemsg = eval(words) onboard.inalert2() if network.isConnected(): if self.homemsg['MSG'] == 'DATA':: onboard.outalert1() response = onboard.sense_DATA() print"RESPONSE:" + str(response) try: network.say(str(response)) except: print 'could not send message' onboard.outalert2() else: print"NO CONNECTION TO RESPOND"
if a == "\r" or a == "\n": pass # skip it elif a == "0": GPIO.output(LED, False) else: GPIO.output(LED, True) gotResponse = True while True: while True: try: print "connecting to switch server" network.call(SERVER_IP, whenHearCall=heard) break except: print "refused" time.sleep(1) print "connected" while network.isConnected(): gotResponse = False print "polling" network.say("?") while network.isConnected() and not gotResponse: print "waiting" time.sleep(1) print "connection closed"
# net_led.py (c) 2013 @whaleygeek # # A network LED client - showing a LED that reflects remote switch status # # Connects to, and polls, a switch server once per second, # and updates the LED status to reflect that of the switch. import RPi.GPIO as GPIO import time import sys import network SERVER_IP = sys.argv[1] LED = 11 GPIO.setmode(GPIO.BCM) GPIO.setup(LED, GPIO.OUT) def heard(phrase): a = phrase[0] if a == "0": GPIO.output(LED, False) else: GPIO.output(LED, True) network.call(SERVER_IP, whenHearCall=heard) while network.isConnected(): network.say("?") time.sleep(1)
#Github: raspberrypilearning/networking-lessons import network import sys def heard(phrase): print('them:' + phrase) if (len(sys.argv) >= 2): network.call(sys.argv[1], whenHearCall=heard) else: network.wait(whenHearCall=heard) while network.isConnected(): phrase = input() print("me:" + phrase) network.say(phrase)
# End Credit # calls the function that will display the GUI from the ClientModule program ClientGUI = ClientModule.run() # Start Credit: The code to start the connection of the client came from Client example from the following site: # http://blog.whaleygeek.co.uk/raspberry-pi-internet-of-things-demonstrator/ # connect to the server and call the heard function once connected while (True): try: print "connecting to switch server" network.call(SERVER_IP, whenHearCall=heard) break except: print "refused" time.sleep(1) print "connected" while network.isConnected(): gotResponse = False network.say("?") while network.isConnected() and not gotResponse: time.sleep(1) # when connection ends print message print "connection closed" # End Credit
def run(self): while network.isConnected(): if not self.sender.empty(): data = self.empty.get() print('Sending:', data) network.say(json.dumps(data))
def __init__(self): def heard(phrase): onboard.inalert1() #remove with eh print "INCOMING:" + phrase words = str(phrase) self.homemsg = eval(words) onboard.inalert2() if network.isConnected(): if self.homemsg['MSG'] == 'DATA':: onboard.outalert1() response = onboard.sense_DATA() print"RESPONSE:" + str(response) try: network.say(str(response)) except: print 'could not send message' onboard.outalert2() else: print"NO CONNECTION TO RESPOND" def scale(val, src, dst): """ Scale the given value from the scale of src to the scale of dst. val: float or int src: tuple dst: tuple example: print(scale(99, (0.0, 99.0), (-1.0, +1.0))) """ return (float(val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0] def scale_stick(value): return scale(value,(0,255),(-1,1)) def scale_trigger(value): return int(scale(value,(0,255),(0,100))) def control_wait(): print 'Awaiting controller connection' self.gamepad = None while self.gamepad == None: devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()] for device in devices: if device.name == 'PLAYSTATION(R)3 Controller': ps3dev = device.fn self.gamepad = evdev.InputDevice(ps3dev) print 'Controller connection made' def define_allkeys(): self.allkeys = { #control name: [event type, event code] 'CROSS_btn': [1, 302], #cross button 'TRIAN_btn': [1, 300], #triangle button 'CIRCL_btn': [1, 301], #circle button 'SQUAR_btn': [1, 303], #square button 'LEFTA_btn': [1, 295], #left arrow button 'RIGHT_btn': [1, 293], #right arrow button 'UPARR_btn': [1, 292], #up arrow button 'DOWNA_btn': [1, 294], #down arrow button 'SELCT_btn': [1, 288], #select button 'START_btn': [1, 291], #start button 'LEFT1_btn': [1, 298], #L1 button 'LEFT2_btn': [1, 296], #L2 button 'RIHT1_btn': [1, 299], #R1 button 'RIHT2_btn': [1, 297], #R2 button 'LSTCK_btn': [1, 289], #left stick button 'RSTCK_btn': [1, 290], #right stick button 'LEFTY_stk': [3, 1], #left joystick y-axis 'RIHTY_stk': [3, 5], #right joystick y-axis 'LEFTX_stk': [3, 0], #left joystick x-axis 'RIHTX_stk': [3, 2], #right joystick x-axis 'LEFT1_trg': [3, 50], #L1 trigger 'LEFT2_trg': [3, 48], #L2 trigger 'RIHT1_trg': [3, 51], #R1 trigger 'RIHT2_trg': [3, 49] #R2 trigger #'XAXIS_acc': [3, 59], #x-axis accelerometer #'YAXIS_acc': [3, 60] #y-axis accelerometer } self.command = 'CMO:0||LMO:0||RMO:0||DOOR:0' def pass_command(): acc = scale_trigger(self.keyval['RIHT2_trg'])-scale_trigger(self.keyval['LEFT2_trg']) turn = scale_stick(self.keyval['LEFTX_stk']) pivot = self.keyval['SQUAR_btn'] #sensitivity threshold of 15% kept both motors turning okay if (abs(turn)>0.15) and (pivot==0): #no pivot turn - 1 wheel stationary if turn < 0: #left turn - right motor fwd/rev, left motor stationary l_acc = 0 r_acc = int(acc*-1*turn) elif turn > 0: #right turn - right motor fwd/rev, right motor stationary r_acc = 0 l_acc = int(acc*turn) elif (abs(turn)>0.15) and (pivot!=0): if turn < 0: #left turn - right motor fwd/rev, left motor oppose l_acc = int(turn*acc) r_acc = int(-1*turn*acc) elif turn > 0: #right turn - left motor fwd/rev, right motor oppose r_acc = int(turn*-1*acc) l_acc = int(turn*acc) else: #no turn l_acc = acc r_acc = acc string = 'CMO:0||'+'LMO:'+str(l_acc)+'||RMO:'+str(r_acc)+'||DOOR:0' if string != self.command: self.command = string print self.command def control(): control_wait() define_allkeys() self.keyval = {} #key values for key in self.allkeys: self.keyval[key] = 0 #initialize all values to zero try: for event in self.gamepad.read_loop(): for key in self.allkeys: [etype, ecode] = self.allkeys[key] if event.type == etype and event.code == ecode: self.keyval[key] = event.value pass_command() except: pass ############ this is where the program starts its loop t = threading.Thread(target=control)# need to work on this some more t.start() print 'Awaiting network connection' onboard.waitalert1() network.wait(whenHearCall=heard) print 'Network connection made' onboard.waitalert2() onboard.connalert1() while True: while network.isConnected(): talk = True if network.isConnected()==False: onboard.connalert2() print 'Disconnected. Waiting for connection' network.hangUp() onboard.waitalert1() network.wait(whenHearCall=heard) print "Connection made" onboard.waitalert2() onboard.connalert1()
# A simple network chat program between to Raspberry Pi's import network import sys def heard(phrase): print phrase print "Galaxy Communication 1.0" network.wait(whenHearCall=heard) while True: if network.isConnected(): phrase = raw_input() network.say(phrase) else: network.wait(whenHearCall=heard)
if (len(sys.argv) < 2): sys.exit() score=0 imagined_number=int(sys.argv[1]) def heard(phrase): global score print("them:" + phrase) guessed_number = int(phrase) #time.sleep(1) #pause here to make the Pi seem human if network.isConnected(): if imagined_number > guessed_number: network.say("higher") elif imagined_number == guessed_number: network.say("CORRECT!!!") score = 1 else: network.say("lower") network.wait(whenHearCall=heard) #wait for quiz taker to connect if network.isConnected(): network.say("Guess a number!") while score == 0 and network.isConnected(): time.sleep(3)
def send(msg): trace("outgoing:%s" % msg) if network.isConnected(): network.say(msg) else: stop_status_reports()