MakeCode code - Temperature Alarm

micro:bit code

In this tutorial, we'll create the code for a micro:bit which will monitor the temperature and if it rises above or falls below a given limit, the temperature alarm application in the micro:bit blue application will be notified over Bluetooth.


step 1 - MakeCode

Go to the MakeCode web site in your web browser and start a new project by clicking Projects and then selecting New Project.


step 2 - make sure your project includes the Bluetooth package

Extensions

Add the Bluetooth extension (previously known as a package) to your project using the Extensions button which you will find in the Advanced list. When prompted to, choose to remove the radio package.


step 3 - create some variables

We're going to use a number of different variables for the following purposes:

  1. to hold the upper temperature limit
  2. to hold the lower temperature limit

Create those variables and set them to suitable initial temperature values such as 20 degrees Celsius for the lower temperature and 24 degrees Celsius for the upper limit. Feel free to display a message of your choice as your microbit starts up, as well.

Your code should now look something like this:

step 4 - measure the temperature every 2 seconds

Create another variable called 'temp'. In your 'forever' block, assign the new variable to the temperature as measured by the 'temperature' block, which you will find in the 'input' collection of blocks. Display the value of this variable using the 'show number' block from the 'basic' collection and then pause for 2 seconds using the 'pause' block.

Your code should now look something like this:

step 5 - Indicate whenever a Bluetooth connection is established or lost

When a device like a smartphone connects to your micro:bit, we want to display a letter 'C'. If a connection is lost, we want to display a letter 'D'. This will help you keep track of what's going on.

From the Bluetooth block collection, add 'on bluetooth connected' and 'on bluetooth disconnected' blocks. In each, add a 'show string' block to display C or D accordingly.

Your code should now look something like this:

step 6 - Transmit events over Bluetooth to indicate whether the the temperature is too high, too low or just right

Go into your MakeCode project settings and select No Pairing Required. This will make testing easier. You can enable pairing later if you think you need it.

Our micro:bit code will communicate with the connected micro:bit Blue application by sending events. You can read all about events here.

The Temperature Alarm micro:bit Blue app expects alarms to be indicated by an event with ID 9000 and a value of 0 meaning 'just right', 1 meaning 'too cold' or 2 meaning 'too hot'

Update your code to raise events using the block from the Advanced, Control collection so that it looks like the following. You'll need to create several new variables and initialise them in your 'on start' block as well.

step 7 - Allow micro:bit Blue to change the temperature limits

The Settings screen of the micro:bit Blue Temperature Alarm app, lets you set the upper and lower limits. Once again, Bluetooth events are used for communication but this time they are sent by the app to the micro:bit rather than in the other direction.

An event with ID 9001 is used to set the lower temperature limit and 9002 for the upper limit. The limit value is in the value part of the event.

Add two 'on event' blocks from the Advanced/Control collection. Set the first to handle events with the 'set lower'event type of 9001 and the other to handle the 'set upper' event type of 9002. Handle these events by setting the appropriate temperature limit variable to the event value.

Your event handler blocks should look like this:

That's it!

The MakeCode project appears below, followed by the JavaScript.

let too_hot = 0
let just_right = 0
let too_cold = 0
let alarm_event = 0
let temp = 0
let lower = 0
let upper = 0
bluetooth.onBluetoothConnected(function () {
    basic.showString("C")
})
control.onEvent(9001, EventBusValue.MICROBIT_EVT_ANY, function () {
    lower = control.eventValue()
})
bluetooth.onBluetoothDisconnected(function () {
    basic.showString("D")
})
control.onEvent(9002, EventBusValue.MICROBIT_EVT_ANY, function () {
    upper = control.eventValue()
})
basic.showString("Monitor")
lower = 20
upper = 27
alarm_event = 9000
just_right = 0
too_cold = 1
too_hot = 2
basic.forever(function () {
    temp = input.temperature()
    basic.showNumber(temp)
    // basic.showNumber(temp)
    if (temp < lower) {
        control.raiseEvent(
        alarm_event,
        too_cold
        )
    } else {
        if (temp > upper) {
            control.raiseEvent(
            alarm_event,
            too_hot
            )
        } else {
            control.raiseEvent(
            alarm_event,
            just_right
            )
        }
    }
    basic.pause(2000)
})