D4E1: a No-code IoT Weather Station With Micro:bit

by FrederikDC in Circuits > Microcontrollers

312 Views, 1 Favorites, 0 Comments

D4E1: a No-code IoT Weather Station With Micro:bit

scheme.jpg

This project sends the micro:bit sensor data for the temperature and light intensity to a public MQTT broker so that it can be used in any kind of web or mobile application.

Schematically, the project looks like this:

  • micro:bit connects via USB to the Raspberry Pi
  • Raspberry Pi connects via Wifi with Hive MQ
  • Mobile application (MQTT Dashboard) connects with Hive MQ

Little to no code was written to realise this project as MakeCode for micro:bit and the Node-RED environment are both no-code / low-code environments.

Supplies

supplies.jpg

The following materials were used during this project:

  • Raspberry Pi model 3 B+ including
  • A power adapter
  • A 16GB SD card for storage (you can also use a 8GB card)
  • micro:bit including
  • A USB cable to connect to the Raspberry Pi
  • Some elastics to attach the micro:bit to the Pi

Programming the Micro:bit

microbit-code.jpg

If you have little or no experience with the micro:bit, first read the getting started tutorial on how to connect the micro:bit to your computer and transfer a program from your machine to the micro:bit.

The code on the micro:bit itself is simple to reproduce. You can also download the hex file.

  • Runs forever and repeats every time after a pause of 5 seconds (or 5000 milliseconds).
  • Shows the light level on a scale from 0 until 255 followed by the temperature on the leds of the micro:bit.
  • Sends the data l:000 followed by t:99 over the serial connection where 000 represents the value of the light and 99 the temperature.


Installing and Configuring the Raspberry Pi

pi.jpeg

In this step the operating system Raspberry Pi OS Lite (32 bit) is installed on the Pi along with the further configuration of the Pi such as setting up SSH and the Wifi.

Installation

  1. Download the Raspberry Pi Imager tool and install it.
  2. Remove the SD Card from the Pi and put it in your laptop
  3. Launch the Raspberry Pi Imager
  4. Click CHOOSE OS
  5. Choose Raspberry Pi OS Lite (32 bit) as we don't need a full Desktop version.
  6. Click CHOOSE STORAGE and choose your SD Card
  7. Click on the gear icon for the following settings:
  8. Give your Pi a name. For example: homeserver.local
  9. Make sure SSH access is enabled. This way you don't need an external screen or keyboard for further configuration
  10. Set a username (for example: pi) and a password (for example: apple).
  11. Set your WiFi settings
  12. Optionally set the region settings

Finally, click WRITE and choose Yes at the prompt that all files will be deleted. After a few minutes, the OS is installed and the SD Card can be reinserted into the Pi.

Boot the Pi by plugging in the power adapter. After a few seconds it will already be booted as the lite version of the operating system has been installed.

Installing Node-red

node-red.jpeg

This step installs Node-Red which is a visual programming language that allows you to develop a complete application by linking different blocks (nodes) together. All installation steps can be found in the Node-Red documentation.

Connect to your Pi using SSH and login with your credentials (password is in my case apple)

ssh pi@homeserver.local

Enter this command and confirm with y. Answer on the question to install Pi-specific libraries since we do not need them for this project.

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Wait for a couple of minutes until the installation completes. You can check the log file for error if you want to.

Make sure that Node-Red will start immediately after the Pi has been booted by entering

sudo systemctl enable nodered.service

Finally reboot the Pi

sudo reboot


Test if the installation was successful by opening your browser and navigate to http://homeserver.local:1880/. If you see the Node-Red welcome screen, you are good to go. Otherwise you might need to check for the IP address of your Pi and use http://[ip-of-you-pi]:1880 instead.

Setting Up Your MQTT Broker

hive-mq.jpg

MQTT is the standard for sending messages in an IoT environment. It is a simple protocol that uses a publish / subscribe architecture.

  • A client (Raspberry Pi) publishes a message to a broker (HiveMQ). The message contains a topic and a value for that topic.
  • The broker spreads the message to all subscribers (or MQTT Dashboard app)

Be sure to check https://mqtt.org/ for more information about the MQTT protocol.

I use HiveMQ as a broker for this project, because it is a free Cloud MQTT Broker that enables to connect up to 100 devices for free and 10GB data traffic a month.

  1. Go to https://console.hivemq.cloud/ and sign up for a new account
  2. Follow the steps in the wizard to complete your account. This includes creating a user with a password for your clients
  3. Check the details of your cluster in the overview panel. You will need the URL and user credentials later in this project.

"Writing Code" in Node-red

flow.jpg

This step develops our application in Node-red that will run on the Pi. The application will

  • connect with the micro:bit using USB
  • check if the incoming data is for light or temperature
  • get the value for light and temperature
  • public it to the public MQTT Broker
  • mb-to-app/light is the topic for the light
  • mb-to-app/temperature is the topic for the temperature

Take the following steps to reproduce the code:

  1. Open the menu --> Manage pallete and install the node node-red-node-serialport. This is required to establish serial communication with the micro:bit
  2. Open the menu --> Import and copy / paste the code in json format
  3. Check the micro:bit node. It is possible that you need to adjust the serial port name (in my flow /dev/ttyACM0)
  4. Check the mqqt out node
  5. Click the pencil next to the Server with the name Hive MQ
  6. Replace xxx.s1.eu.hivemq.cloud with the name of your Server (see Step 4)
  7. Go the the tab Security and replace the username and password with the ones you created in step 4
  8. Deploy the code

Testing Your Application

mqqttest.jpg

You can test if everything works by checking the green boxes. It should say connected on the serial node and both mqtt-out nodes.

You can also use a MQTT client like MQTTX to test if the data is published. Install the desktop client, connect to your public broker and subscribe to mb-to-app/light and mb-to-app/temperature. There should be a new value every 5 seconds.

Creating the Dashboard

screenshot.jpg

There are several apps that allow you to create a dashboard based on subscription to certain MQTT topics. My personal favourite is MQTT Dashboard that you can download from the play store.

  1. Install and open the app
  2. Click on the menu and select MQTT connections. Add your HiveMQ connection
  3. Click on the menu and select Dashboards. You can have a look at the sample dashboards or immediately create your own one.
  4. Create a new dashboard and add components. I have used a gauge and line chart for the temperature and a slider for the light.
  5. Configure the components. You can set several properties to adjust their look and feel. The most important section is the MQTT section where you select the connection and topic to subscribe.

Testing the Application

D4E1: a No-code IoT weather station