... | ... | @@ -13,7 +13,7 @@ You will have the opportunity to mimic data interception from a fitness tracker |
|
|
|
|
|
## Features
|
|
|
* **Estimated Time: 1 Hour** (base lab) - **4 Hours** (Further Exploration)
|
|
|
* **Appropriate Grades:** 6-12
|
|
|
* **Appropriate Grades:** 9-12
|
|
|
* **Topic Alignment:** ( **L**imited, **M**edium, **H**igh )
|
|
|
* GenCyber Cybersecurity First Principles:
|
|
|
|
... | ... | @@ -84,7 +84,7 @@ def capture_data(): |
|
|
|
|
|
|
|
|
## Step 5: Flash the base code
|
|
|
If you don't remember how to flash code to your Micro::Bit, see the instructions [here](how-to-flash). The base code is also given below.
|
|
|
If you don't remember how to flash code to your Micro::Bit, see the instructions [here](how-to-flash). The base code is also given below. Note that comments are given in the base code to help complete the code.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Base Code (Click to Expand)
|
... | ... | @@ -215,12 +215,241 @@ while True: |
|
|
* What method do you use to run a block of code every time data is received?
|
|
|
* How do you find the total acceleration from the x-acceleration, y-acceleration, and z-acceleration?
|
|
|
|
|
|
2. Complete the base code. You will program the LED's to provide feedback as to what is happening with the fitTracker. You will also grab the acceleration from the Micro::Bit to send over the radio.
|
|
|
* Get the Micro::Bit's x, y, and z acceleration values
|
|
|
* Send the acceleration values over the radio to the receiver
|
|
|
* Display a happy face when ready to start measuring data
|
|
|
* Display a heart when measuring data
|
|
|
* Dispaly a check mark when finished measuring data
|
|
|
2. Complete the code. Specifically:
|
|
|
|
|
|
* In the data packet received block/function (capture_data in Python), get the Micro::Bit's x, y, and z acceleration values. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
radio.onDataPacketReceived( ({ receivedString: name, receivedNumber: value }) => {
|
|
|
if (name == "x") {
|
|
|
X_Accel = value
|
|
|
} else if (name == "y") {
|
|
|
Y_Accel = value
|
|
|
} else if (name == "z") {
|
|
|
Z_Accel = value
|
|
|
write_data()
|
|
|
} else if (name == "done") {
|
|
|
serial.writeLine("done")
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
}
|
|
|
})
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Python Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
def capture_data():
|
|
|
vals = {'x':0, 'y':0, 'z':0}
|
|
|
vals['x'] = accelerometer.get_x()
|
|
|
vals['y'] = accelerometer.get_y()
|
|
|
vals['z'] = accelerometer.get_z()
|
|
|
radio.send("x"+str(x_Accel))
|
|
|
radio.send("y"+str(y_Accel))
|
|
|
radio.send("z"+str(z_Accel))
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
* When the A button is pressed, add code to set measuring to true and show a heart on the display. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
measuring = true
|
|
|
basic.showIcon(IconNames.Heart)
|
|
|
})
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Python Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
elif button_a.is_pressed():
|
|
|
measuring = True
|
|
|
display.show(Image.HEART)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
* When the B button is pressed, add code to set measuring to false and show a check mark on the display. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.B, () => {
|
|
|
measuring = false
|
|
|
basic.showIcon(IconNames.Yes)
|
|
|
})
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Python Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
elif button_b.is_pressed():
|
|
|
measuring = False
|
|
|
display.show(Image.YES)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
* When A and B are pressed together, add code to set measuring to false and display a happy face. One potential solution to this code section can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<pre>
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
measuring = false
|
|
|
radio.sendValue("done", 0)
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
})
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Python Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
if button_a.is_pressed() and button_b.is_pressed():
|
|
|
measuring = False
|
|
|
radio.send("done")
|
|
|
display.show(Image.HAPPY)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
* Finally, send the values over the radio. The final completed code can be seen below.
|
|
|
<details>
|
|
|
<summary>
|
|
|
JavaScript Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
|
|
|
<pre>
|
|
|
let Z_Accel = 0
|
|
|
let Y_Accel = 0
|
|
|
let X_Accel = 0
|
|
|
let measuring = false
|
|
|
radio.onDataPacketReceived( ({ receivedString: name, receivedNumber: value }) => {
|
|
|
if (name == "x") {
|
|
|
X_Accel = value
|
|
|
} else if (name == "y") {
|
|
|
Y_Accel = value
|
|
|
} else if (name == "z") {
|
|
|
Z_Accel = value
|
|
|
write_data()
|
|
|
} else if (name == "done") {
|
|
|
serial.writeLine("done")
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
}
|
|
|
})
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
measuring = false
|
|
|
radio.sendValue("done", 0)
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
})
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
measuring = true
|
|
|
basic.showIcon(IconNames.Heart)
|
|
|
})
|
|
|
input.onButtonPressed(Button.B, () => {
|
|
|
measuring = false
|
|
|
basic.showIcon(IconNames.Yes)
|
|
|
})
|
|
|
function capture_data() {
|
|
|
X_Accel = input.acceleration(Dimension.X)
|
|
|
Y_Accel = input.acceleration(Dimension.Y)
|
|
|
Z_Accel = input.acceleration(Dimension.Z)
|
|
|
radio.sendValue("x", X_Accel)
|
|
|
radio.sendValue("y", Y_Accel)
|
|
|
radio.sendValue("z", Z_Accel)
|
|
|
}
|
|
|
function write_data() {
|
|
|
serial.writeLine("" + X_Accel + "," + Y_Accel + "," + Z_Accel)
|
|
|
}
|
|
|
radio.setGroup(1)
|
|
|
radio.setTransmitPower(7)
|
|
|
basic.showIcon(IconNames.Yes)
|
|
|
basic.forever(() => {
|
|
|
if (measuring) {
|
|
|
capture_data()
|
|
|
basic.pause(200)
|
|
|
}
|
|
|
})
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Python Potential Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
from microbit import *
|
|
|
import radio
|
|
|
|
|
|
radio.on()
|
|
|
radio.config(group=1)
|
|
|
radio.config(power=7)
|
|
|
z_Accel = 0
|
|
|
y_Accel = 0
|
|
|
x_Accel = 0
|
|
|
measuring = False
|
|
|
display.show(Image.YES)
|
|
|
|
|
|
def capture_data():
|
|
|
vals = {'x':0, 'y':0, 'z':0}
|
|
|
vals['x'] = accelerometer.get_x()
|
|
|
vals['y'] = accelerometer.get_y()
|
|
|
vals['z'] = accelerometer.get_z()
|
|
|
radio.send("x"+str(x_Accel))
|
|
|
radio.send("y"+str(y_Accel))
|
|
|
radio.send("z"+str(z_Accel))
|
|
|
|
|
|
|
|
|
def write_data():
|
|
|
print(x_Accel, y_Accel, z_Accel, sep=',', end='\n')
|
|
|
|
|
|
while True:
|
|
|
if button_a.is_pressed() and button_b.is_pressed():
|
|
|
measuring = False
|
|
|
radio.send("done")
|
|
|
display.show(Image.HAPPY)
|
|
|
elif button_a.is_pressed():
|
|
|
measuring = True
|
|
|
display.show(Image.HEART)
|
|
|
elif button_b.is_pressed():
|
|
|
measuring = False
|
|
|
display.show(Image.YES)
|
|
|
incoming = radio.receive()
|
|
|
if incoming is not None:
|
|
|
if incoming.startswith('done'):
|
|
|
display.show(Image.HAPPY)
|
|
|
print("done")
|
|
|
elif incoming.startswith('x'):
|
|
|
elif incoming.startswith('y'):
|
|
|
elif incoming.startswith('z'):
|
|
|
|
|
|
write_data()
|
|
|
else:
|
|
|
#x_Accel, y_Accel, z_Accel = incoming.split(",")
|
|
|
#write_data()
|
|
|
if measuring:
|
|
|
capture_data()
|
|
|
sleep(200)
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
|
|
|
3. Find a partner that programmed their Micro::Bit in the same language as you (so if you used JavaScript, find someone who also used JavaScript).
|
... | ... | |