Microbot Lab
Description
This lab enables students to learn about communication interception through a hands on approach with Micro:Bots. We will demonstrate the use of radio technology as a means for remote control and the potential for radio wave hijacking. Students will learn the fundamentals of Bluetooth and radio waves through implementation by using one micro:bit as a controller for another micro:bit embedded in a robot.
Features
-
Estimated Time: 1 Hour (base lab), 3 Hours (Hacking, Further Exploration)
-
Appropriate Grades: 6-12
-
Topic Alignment ( Limited, Medium, High )
-
GenCyber Cybersecurity First Principles:
GenCyber CFPs Alignment Data Hiding M Least Privilege L Abstraction L Domain Separation H Resource Encapsulation M Simplicity M Modularity H Process Isolation L Layering L Minimization M -
GenCyber Cybersecurity Concepts:
GenCyber CCs Alignment Defense in Depth M Confidentiality H Integrity H Availability H Think Like an Adversary H Keep It Simple L
Step 0: The Basics
- Micro::Bit Coding Options
- [Click here to code with JavaScript/Blocks] (https://makecode.microbit.org)
- Click here to code with MicroPython
- Microbot Basics
Step 1: Gather Materials
- Micro::Bit (1 per person / 2 per team)
- Micro-USB Cable
- Battery Pack (Equivalent to two AA batteries)
- Microbot
Step 2: Get Familiar With Microbots
Radios work by sending and receiving messages over different channels. This allows you to use on Micro:Bit to send messages and another to receive them. In this lab, you will use a Micro:bit as a controller to get user input and then send messages based on those inputs. Then, you will use a second Micro:bit to receive messages from the controller and then control the robot based on those received messages.
Start this lab by going over this basic programming example.
The first set of code is for the Micro:Bit controller. This controller waits for the user to press the A button or the B button. When the user presses the A button, the controller sends the message "left" to the robot. When the user presses the B button, the controller sends the message "right" to the robot.
The second set of code is for the Micro:Bot robot. This Micro:Bit waits to receive a message. If they receive a "left" message, it tells the robot to go left. If they receive a "right" message, it tells the robot to go right.
Step 3: Download The Files
- Download (click on) the following file to get started: MicroBotLab.zip
- Unzip MicroBotLab.zip to your desired folder. For instructions on how to unzip click here
- NOTE: There is an extra folder for this lab, Attackers
- Attackers: This directory holds code to simulate an attack that jams the radio channel and makes the robots behave chaotically.
Step 4: Open your editor
- This base code is also available in the directory that you downloaded as well as this repository. If you don't remember how to open code in your editor, see the instructions here.
Step 5: Get Moto-Bit library
- For JavaScript/Blocks, start by selecting the "Advanced" drop down box in the libraries toolbar.
Then, scroll down to the bottom of the advanced packages. Select the "Add Package" button.
In the packages pop-up, search for "Motobit". Select the moto-bit library in the results section.
You can check to make sure that you have the library by searching for the library in your libraries toolbar.
Step 6: Programming!
Partner up first!
Complete the Robot Code
-
Open the robot base code
- If you forgot how to open code in an editor click here
-
Complete the robot code. You will add channel choosing functionality and give the motobit commands after receiving a radio transmission from the controller:
- When button a and button b are pressed, set the radio group
- When button a is pressed, if the radio group has not been set, decrement the radio group by 1
- When button b is pressed, if the radio group has not been set, increment the radio group by 1
- When the command forward is received over the radio, drive the mototbit Forward
- When the command stop is received over the radio, stop the motobit
- When the command left is received over the radio, drive the mototbit left
- When the command right is received over the radio, drive the mototbit right
-
Generate your robot .hex file and flash your robot Micro::Bit. If you don't remember how to flash code to your Micro::Bit, see the instructions here.
Compelete the Controller code
-
Open the controller base code
- Remember to stick with the same programming language as you chose for the robot
-
Complete the controller base code. You will add channel choosing functionality and send radio commands depending on the buttons pressed:
- When button a and button b are pressed, if the radio channel has not already been set, set it.
- If the radio channel has already been set, send a forward command over the radio
- When button a is pressed, if the radio group has not been set, decrement the radio group by 1.
- If the radio group has been set, send a left command over the radio
- When button b is pressed, if the radio group has not been set, increment the radio group by 1.
- If the radio group has been set, send a right command over the radio
- When button a and button b are pressed, if the radio channel has not already been set, set it.
-
Generate your controller .hex file and flash your controller Micro::Bit. If you don't remember how to flash code to your Micro::Bit, see the instructions here.
Potential JavaScript Robot Solution, robot.js (Click to Expand)
let radioGroup = 0 let groupSet = 0 input.onButtonPressed(Button.AB, () => { if (groupSet != 1) { groupSet = 1 radio.setGroup(radioGroup) } }) radio.onDataPacketReceived( ({ receivedString: remoteString }) => { if (remoteString == "forward") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 100) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 100) basic.showLeds(` . . # . . . # # # . # . # . # . . # . . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else if (remoteString == "left") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 50) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 50) basic.showLeds(` . . # . . . # . . . # # # # # . # . . . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else if (remoteString == "right") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50) motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50) basic.showLeds(` . . # . . . . . # . # # # # # . . . # . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else {
} }) input.onButtonPressed(Button.A, () => { if (groupSet != 1 && radioGroup > 0) { radioGroup = radioGroup - 1 } }) input.onButtonPressed(Button.B, () => { if (groupSet != 1 && radioGroup < 255) { radioGroup = radioGroup + 1 } }) while (groupSet != 1) { basic.showNumber(radioGroup) } basic.showIcon(IconNames.Happy) basic.pause(100) basic.showNumber(radioGroup) motobit.enable(MotorPower.On)
Potential JavaScript Controller Solution, controller.js (Click to Expand)
let groupSet = 0 let radioGroup = 0 input.onButtonPressed(Button.A, () => { if (groupSet == 0 && radioGroup > 0) { radioGroup = radioGroup - 1 } else { radio.sendString("left") basic.showLeds(` . . # . . . # . . . # # # # # . # . . . . . # . . `) } }) input.onButtonPressed(Button.AB, () => { if (groupSet == 0) { groupSet = 1 radio.setGroup(radioGroup) basic.showIcon(IconNames.Happy) } else { radio.sendString("forward") basic.showLeds(` . . # . . . # # # . # . # . # . . # . . . . # . . `) } }) input.onButtonPressed(Button.B, () => { if (groupSet == 0 && radioGroup < 255) { radioGroup = radioGroup + 1 } else { radio.sendString("right") basic.showLeds(` . . # . . . . . # . # # # # # . . . # . . . # . . `) } }) radioGroup = 0 while (groupSet != 1) { basic.showNumber(radioGroup) } basic.showIcon(IconNames.Happy) basic.pause(100) basic.showNumber(radioGroup)
Potential Python Robot Solution, excerpt from robot.py (Click to Expand)
from microbit import * import radio
############################ #NEW CODE
from microbit import * import radio
class MotoBit: moto_l = 0x21 moto_r = 0x20 moto_on = 0x70
def init(self, address = 0x59): self.ADDR = address
def write16(self,a,b): i2c.write(self.ADDR, bytes([a,b]), repeat=False)
def enable(self, pwr): if pwr: self.write16(0x70,1) else: self.write16(0x70,0)
def set_speed(self, motor, speed): motor = motor + 32 if speed>=0: self.write16(motor,128 + speed) else: speed = speed + 127 self.write16(motor, speed)
def drive(self,left,right): self.set_speed(0,right) self.set_speed(1,left)
radio_group = 0 #number of radio frequency to use group_set = False #has the radio group been set? motobit = MotoBit() motobit.enable(True) # Enable motor driver radio.on() radio.config(power=7)
stop_sign = Image( "09990:" "90099:" "90909:" "99009:" "09990")
while True:# do forever if not group_set: display.show(str(radio_group)) if button_a.is_pressed() and button_b.is_pressed(): group_set = True radio.config(channel = radio_group) display.show(Image.HAPPY)
elif button_a.is_pressed(): if radio_group > 0: radio_group = radio_group - 1 elif button_b.is_pressed(): if radio_group < 255: radio_group = radio_group + 1 sleep(150)
else:# radio group has been set incoming = radio.receive()
if incoming is "forward": motobit.drive(127, 127) display.show(Image.ARROW_N) sleep(200) motobit.drive(0,0) elif incoming is "right": motobit.drive(60,-60) display.show(Image.ARROW_E) sleep(200) motobit.drive(0,0) elif incoming is "left": motobit.drive(-60, 60) display.show(Image.ARROW_W) sleep(200) motobit.drive(0,0) sleep(100)
Potential Python Controller Solution, controller.py (Click to Expand)
from microbit import * import radio
radio_group = 0 #number of radio frequency to use forward = False group_set = False #has the radio group been set? radio.on() radio.config(power=7)
stop_sign = Image( "09990:" "90099:" "90909:" "99009:" "09990")
while True:# do forever if group_set is False: display.show(str(radio_group))
if button_a.is_pressed() and button_b.is_pressed(): if group_set is False: group_set = True radio.config(channel = radio_group) display.show(Image.HAPPY) forward = False else: if forward == False: radio.send("forward") forward = True display.show(Image.ARROW_N) else: radio.send("stop") forward = False display.show(stop_sign) elif button_a.is_pressed(): if group_set == False and radio_group > 0 : radio_group = radio_group - 1 else: radio.send("left") display.show(Image.ARROW_W) elif button_b.is_pressed(): if group_set is False and radio_group < 255: radio_group = radio_group + 1 else: radio.send("right") display.show(Image.ARROW_E) sleep(200)
Test your code
- Your robot should:
- Allow radio group selection on startup and allow locking that radio group in
- wait for commands from the controller
- move forward, right, left, and stop when instructed to do so by controller
- Your Controller should:
- Allow radio group selection on startup and allow locking that radio group in
- send commands to the robot upon various button presses: a+b is "start" and "stop", a is "left", b is "right"
Hack
- Now try to intercept other robots. Are you able to control another group's robot simply by changing what radio group you set your controller to?
- Are you able to control another group's robot?
- Does your control work with their robot?
Step 7: Further Exploration
- Spend some time exploring with your Micro::Bit and see what kind of improvements you can make.
- Can you think of a way to incorporate a reverse function for the microbot?
- What if you needed to push and hold a+b to make it move in a direction?
- How would you select between forward and reverse?
- Are you still able to control other robots after editing your code?
Potential Javascript Edit, Add Reverse functionality, excerpt from controller.js(Click to Expand)
input.onButtonPressed(Button.AB, () => { if (groupSet == 0) { groupSet = 1 radio.setGroup(radioGroup) basic.showIcon(IconNames.Happy) } else { radio.sendString("reverse") //change forward to reverse basic.showLeds(` //change LED pattern to down arrow . . # . . . . # . . # . # . # . # # # . . . # . . `) } })
Potential Javascript Edit, Add Reverse functionality, excerpt from robot.js(Click to Expand)
radio.onDataPacketReceived( ({ receivedString: remoteString }) => { if (remoteString == "reverse") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 100) //set motor to reverse motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 100) //set motor to reverse basic.showLeds(` //change LED to down arrow patters . . # . . . . # . . # . # . # . # # # . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else if (remoteString == "left") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 50) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 50) basic.showLeds(` . . # . . . # . . . # # # # # . # . . . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else if (remoteString == "right") { motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50) motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50) basic.showLeds(` . . # . . . . . # . # # # # # . . . # . . . # . . `) basic.pause(200) motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0) motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0) } else {}
})
Potential Python Edit, Add Reverse functionality, Excerpt from controller.py(Click to Expand)
#Remember to change forward to reverse at top of file!
if button_a.is_pressed() and button_b.is_pressed(): if group_set is False: group_set = True radio.config(channel = radio_group) display.show(Image.HAPPY) reverse = False # change forward to reverse else: if reverse == False: #changed forward to reverse radio.send("reverse") #changed forward to reverse reverse = True #changed forward to reverse display.show(Image.ARROW_S) #Display a down arrow instead of up else: radio.send("stop") forward = False display.show(stop_sign)
Potential Python Edit, Add Reverse functionality, Excerpt from robot.py (Click to Expand)
while True:# do forever if not group_set: display.show(str(radio_group)) if button_a.is_pressed() and button_b.is_pressed(): group_set = True radio.config(channel = radio_group) display.show(Image.HAPPY)elif button_a.is_pressed(): if radio_group > 0: radio_group = radio_group - 1 elif button_b.is_pressed(): if radio_group < 255: radio_group = radio_group + 1 sleep(150) else:# radio group has been set incoming = radio.receive() if incoming is "reverse": #changed forward to reverse motobit.drive(-127, -127) #drive the motors backward instead of forward display.show(Image.ARROW_S) #Change arrow to down facing sleep(200) motobit.drive(0,0) elif incoming is "right": motobit.drive(60,-60) display.show(Image.ARROW_E) sleep(200) motobit.drive(0,0) elif incoming is "left": motobit.drive(-60, 60) display.show(Image.ARROW_W) sleep(200) motobit.drive(0,0) sleep(100)
The GenCyber Wyoming COWPOKES program is supported by the National Security Agency and the National Science Foundation through Award #H98230-18-1-0095. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation, the National Security Agency, or the U.S. government.