def get_sensors(): a = button_a.is_pressed() b = button_b.is_pressed() x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() pin_one_analog_input = pin1.read_analog() pin_two_analog_input = pin2.read_analog() gesture = accelerometer.current_gesture() details = radio.receive_full() # msg, rssi, timestamp = details dic = { # "topic": TOPIC, "payload": { "button_a": a, "button_b": b, "x": x, "y": y, "z": z, "pin_one_analog_input": pin_one_analog_input, "pin_two_analog_input": pin_two_analog_input, "gesture": gesture, "radio_data": details } } return dic
def forever(): """ Code to run again and again """ details = radio.receive_full() if details is not None: message, strength, timestamp = details update_display(strength)
def update_speeds(speed1, speed2): while True: message = radio.receive_full() if message is None: break motor, speed = interpret_packet_value_pair(message[0]) if motor is None: return speed1, speed2 if motor == 'left': speed2 = int(speed) elif motor == 'right': speed1 = int(speed) return speed1, speed2
sleep(1000) if button_b.was_pressed(): my_id = my_id - 1 if (MIN_ID > my_id): my_id = MIN_ID display.show(my_id) sleep(1000) # If the dice is held face down it behaves as a receiver # TODO - split this off as a seperate version of code... if accelerometer.is_gesture('face down'): # Read any incoming messages. dice_value = 0 display.clear() display.show(rx) details = radio.receive_full() if details: msg, rssi, timestamp = details uart.write(msg) #rx_id = msg[0] #rx_seq = msg[1] #rx_dice = msg[2] # Shake the Dice to start rolling... if accelerometer.is_gesture('shake'): display.clear() while accelerometer.is_gesture('shake'): sleep(10) sleep(300) # Now that shaking has stopped we are rolling... turns = random.randint(4,8) # number of sides to roll over
f = open(filename, "rb") print("-----START:" + filename) print( "Receiver,Sender,First Contact Time (min),Last Contact Time (min)" ) while True: sleep(50) l = f.readline() if not l: break print("x" + hex2str(ID) + ",x" + hex2str(l[0:4]) + "," + str(int.from_bytes(l[4:6], "big")) + "," + str(int.from_bytes(l[6:8], "big")) + (",!" if (len(l) >= 9 and l[8] == "!") else "")) display.clear() d = radio.receive_full() while d: received_infected = False received_id, rssi, timestamp = d timestamp = int(timestamp / 1000) if len(received_id) == 5 and received_id[4] == '!': received_infected = True received_id = received_id[:-1] if rssi > RSSI_THRESHOLD: if received_id in contacts: if contacts[received_id][1] + TIMEOUT < timestamp: contacts[received_id] = (timestamp, timestamp) else: contacts[received_id] = (contacts[received_id][0], timestamp) else:
cur_num_tx = cur_num_tx + 1 ackd = False if lu > 0: cidx += 1 if uart.any(): rl = uart.readline() if rl: b1 = str(rl, 'UTF-8') ur += b1.strip() pos = ur.find("=") if pos >= 0: ro.send(ur[:pos]) ur = "" rns = ro.receive_full() if rns: rxc += 1 msg = rns[0] rssi = rns[1] line = str(msg, 'UTF-8')[3:] items = line.split(":") if len(items) == 3: sid, did, value = items sid = int(sid) did = int(did) if not sid in uids: uids.append(sid) if sid == lid: # sid collision
return mac_addr this_machine = getMachine() print(getMachine() + "." + ".") display.show(Image.SQUARE) start_time = running_time() signals = {} while True: # we only receive a radio broadcast from others incoming = radio.receive_full() while incoming is not None: msg, rssi, timestamp = incoming signalStrength = rssi if msg is not None: R = " " if rssi > -70: R = "A" if rssi > -65: R = "S" if rssi > -55: R = "D" if rssi > -40: R = "W"
# Code for the "detector" microbits. Make sure # a speaker is attached in the usual way. a from microbit import * import radio import music radio.config(channel=10) radio.on() while True: message = radio.receive_full() if message: strength = message[1] + 100 displaystrength = int((strength / 10) + 1) display.show(str(displaystrength)) music.pitch(strength * 50, 100) else: display.show(Image.NO) # Code for the "transmitter" microbits. a from microbit imort * import radio id = "10" display.show(id) radio.config(power=0) radio.config(channel=10) radio.on() while True: radio.send(id)
# Add your Python code here. E.g. from microbit import * import radio radio.on() radio.config(queue=1) while True: mesg = radio.receive_full() if mesg: _, rssi, _ = mesg distance = 10**(rssi / 20) display.scroll('{:5.2f}'.format(distance)) sleep(1000)
while True: sleep(50) # prevent serial overruns l = f.readline() if not l: break print("x" + hex2str( ID ) + # adding x to every ID number so spreadsheets don't try to interpret as number/scientific notation ",x" + hex2str(l[0:4]) + "," + str(int.from_bytes(l[4:6], "big")) + "," + str(int.from_bytes(l[6:8], "big")) + (",!" if (len(l) >= 9 and l[8] == "!") else "")) display.clear() #check for received messages and process d = radio.receive_full() while d: received_infected = False received_id, rssi, timestamp = d timestamp = int( timestamp / 1000 ) # timestamp from radio is in microseconds; convert to milliseconds if len(received_id) == 5 and received_id[ 4] == '!': # infected contacts are marked with a ! after the ID received_infected = True received_id = received_id[:-1] # trim infected marker if rssi > RSSI_THRESHOLD: if received_id in contacts: if contacts[received_id][ 1] + TIMEOUT < timestamp: # previous contact timed out; start again contacts[received_id] = (
#PLAYER from microbit import * import radio import music #importing everything we will need. radio.on() #turning on the radio. while True: #establishing loop. msg = radio.receive_full( ) #receiving radio messages and signal strength (Booth, 2018). if msg: #checking if a radio message is recived. if 'marco' in msg[0]: #checking if correct message is received. signal_s = int(msg[1]) #gives signal strength. print(signal_s) display.clear() sleep(100) if signal_s <= -100: #checking what signal strength is received. img = Image('11111:11111:11111:11111:11111') display.show( img ) #displaying lights, depending on the signal strength the light will be dimmer of brighter. music.play( 'B9:3' ) #playing music, depending on the signal strength the pitch will be higher or lower. sleep(10) elif signal_s <= -90: img2 = Image('33333:33333:33333:33333:33333') display.show(img2) music.play('B9:3') sleep(10)
from microbit import * import radio radio.on() radio.config(queue=1, channel=7, power=5) #Store 1 package, set channel and output power myName = "ADA" # use variable for name while True: # display own name display.scroll("MY NAME IS %s" % (myName), delay=80) # used delay to control speed # Broadcast name to other Microbits radio.send_bytes(myName) sleep(500) # add a short (500 ms) pause before starting over # check if a name has been received received = radio.receive_full() # returns None if nothing is received if received: # A packet has been received msg, rssi, timeStamp = received received = str(msg, 'utf-8') #convert to string if (rssi > -60): # only greet if person is close display.scroll("HELLO %s" % (received), delay=80) sleep(500) # short pause
def signaller(): global colour global wasTogether global lastID global isAUp global isADown global isBUp global isBDown global isTogether global wasTogether while True: # always receive the radio broadcasts incoming = radio.receive_full() while incoming is not None: msgbytes, rssi, timestamp = incoming msg = str(msgbytes) start = msg.find(getHeaderBytes()) if (start > 0): finalmsg = msg[start + len(getHeaderBytes()):len(msg) - 1] # print("recv:" + finalmsg) debugRSSI = "{}".format(rssi) recvMachineID, toMachine, b = finalmsg.split(',') # print("Machine ID: " + a ) receiveColour = int(b) # get the date time now = utime.ticks_ms() nowStr = str(now) # construct the serial message serialMsg = recvMachineID + "," + nowStr + "," + debugRSSI + ",{:d}".format( receiveColour) print(serialMsg) # We are forcing the colour to set as an ACK if toMachine == "x": # Otherwise we need to make a choice to whether we set our colour if (lastID != recvMachineID): lastID = recvMachineID # db -47 is close # db -100 is 12m if rssi > SignalStrength: colour = receiveColour updateState(recvMachineID, colour) # Send the ACK if colour == 0: music.play('A') if colour == 1: pin2.write_digital(1) sleep(1000) pin2.write_digital(0) if colour == 2: music.play('AG') else: if toMachine == getMachine(): colour = receiveColour incoming = radio.receive_full() isA = button_a.is_pressed() isB = button_b.is_pressed() # ------------------------------------------ # New algorithmn for detecting button presses isAUp = 0 isBUp = 0 # Are either A or B held? if isA: isAUp = 0 isADown = 1 if isB: isBUp = 0 isBDown = 1 # Were they released? if ((not isA) and isADown): isAUp = 1 isADown = 0 if ((not isB) and isBDown): isBUp = 1 isBDown = 0 if isA and isB: isTogether = 1 if not isA and not isB: if isTogether: wasTogether = 1 isTogether = 0 else: wasTogether = 0 # ------------------------------------------ # Send a new colour according to whether A or B released, A was released or B was released if wasTogether: updateState("x", 2) elif isAUp and (isTogether == 0): updateState("x", 0) elif isBUp and (isTogether == 0): updateState("x", 1) # Show a pattern if colour == 0: display.show(Image.SQUARE) if colour == 1: display.show(Image.CHESSBOARD) if colour == 2: display.show(Image.ARROW_N)
def contagion(): global start_time global colour while True: # always receive the radio broadcasts incoming = radio.receive_full() while incoming is not None: msgbytes, rssi, timestamp = incoming msg = str(msgbytes) start = msg.find(getHeaderBytes()) if (start > 0): finalmsg = msg[start + len(getHeaderBytes()):len(msg) - 1] debugRSSI = "{}".format(rssi) recvMachineID, toMachine, b = finalmsg.split(',') # print("Machine ID: " + a ) receiveColour = int(b) # get the date time now = utime.ticks_ms() nowStr = str(now) # construct the serial message serialMsg = recvMachineID + "," + nowStr + "," + debugRSSI + ",{:d}".format( colour) print(serialMsg) # if we want to collect what is being sent # db -47 is close # db -100 is 12m if rssi > SignalStrength: # if we receive a happy face treat it as a reset if (receiveColour == 2): receiveColour = 0 # two's are effectively immune if colour != receiveColour and colour != 2: # you've been saved if receiveColour == 0: colour = receiveColour music.play('C') music.play('B') vibrate() # you've been infected if receiveColour == 1: colour = receiveColour music.play('A') vibrate() # you can't be infected if your colour is 2. You are immune incoming = radio.receive_full() elapsed_time = running_time() - start_time if (elapsed_time > 3000) and (colour > 0): start_time = running_time() updateState("x", colour) dot = Image("00000:" "00000:" "00800:" "00000:" "00000") if colour == 0: display.show(dot) if colour == 1: display.show(Image.SQUARE) if colour == 2: display.show(Image.CHESSBOARD)
import microbit import radio import neopixel radio.config(power=1, channel=33, queue=1) radio.on() last_receive = 0 print("Starting") NP = neopixel.NeoPixel(microbit.pin2, 1) # one neopixel on pin 2 while True: incoming = radio.receive_full() # get everything if incoming: msg, strength, time = incoming msg = str(msg[3:], 'utf-8') # lop off prepending characters, convert to string strength = strength + 255 # make strength run from 0 to 255 NP[0] = ((strength), ) * 3 # signal strength as brightness NP.show() # refresh microbit.display.show(msg) # show device ID last_receive = microbit.running_time( ) # get time of last receive for comparison if microbit.running_time( ) - last_receive > 3000: # no messages for three seconds NP.clear() NP.show() microbit.display.clear()
from microbit import * import radio def draw_counter(counter): for x in range(5): for y in range(5): display.set_pixel(x, y, 9 if counter > 5 * x + y else 0) radio.on() strength = 0 while True: if button_a.was_pressed(): strength = 0 response = radio.receive_full() if response: strength = (response[1] / -4) - 10 draw_counter(strength) sleep(10)