MakeCode code - BittyWeb Event Monitoring - Temperature

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 Bitty Event Monitoring application in BittyWeb 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


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 BittyWeb application by sending events. You can read all about events here.

The Bitty Event Monitoring application in BittyWeb expects alarms to be indicated by an event with ID 9100 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.

That's it!

The MakeCode project appears below.