Wireless Remote Weather Station with micro:bit

Pages
Contributors: bboyho
Favorited Favorite 6

Experiment 5: Real Time Wireless Data Logging

Introduction

At the time of release, we did not have a real time clock (RTC) available for the micro:climate kit. We used the running time to record when a reading was obtained. In this experiment, we will be adding the gator:RTC to help record the day and time for each reading before saving it with the OpenLog.

Wireless Remote Weather Station with micro:bit Demo

Parts Needed

You will need the following parts:

  • 2x micro:bit Boards
  • 2x Micro-B USB Cables
  • 1x Weather:bit
  • 1x Weather Meter
  • 7x 12" M/M Jumper Wires
  • 1x SparkFun Soil Moisture Sensor
  • 1x Waterproof Temperature Sensor
  • 1x OpenLog with Headers
  • 1x microSD Card
  • 1x microSD Card Reader
  • 1x gator:RTC
  • 4x Alligator to Pigtails
  • 1x micro:bit Breakout with Headers
  • 1x Breadboard

For a few items listed, you will need more than one unit (i.e. micro:bits, micro-B USB cables, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.

Weather Meter Kit

Weather Meter Kit

SEN-15901
$79.95
11
Temperature Sensor - Waterproof (DS18B20)

Temperature Sensor - Waterproof (DS18B20)

SEN-11050
$10.95
29
SparkFun micro:climate kit for micro:bit - v3.0

SparkFun micro:climate kit for micro:bit - v3.0

KIT-16274
$119.95
1
Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

PRT-12002
$5.50
48
SparkFun weather:bit - micro:bit Carrier Board (Qwiic)

SparkFun weather:bit - micro:bit Carrier Board (Qwiic)

DEV-15837
$17.95
SparkFun Soil Moisture Sensor (with Screw Terminals)

SparkFun Soil Moisture Sensor (with Screw Terminals)

SEN-13637
$7.50
4
USB Micro-B Cable - 6 Foot

USB Micro-B Cable - 6 Foot

CAB-10215
$5.50
15
Alligator Clip with Pigtail (10 Pack)

Alligator Clip with Pigtail (10 Pack)

CAB-14303
$7.50
4
Jumper Wires Premium 12" M/M  Pack of 10

Jumper Wires Premium 12" M/M Pack of 10

PRT-09387
$4.95

SparkFun gator:RTC - micro:bit Accessory Board

COM-15486
Retired

micro:bit Board

DEV-14208
10 Retired

SparkFun micro:bit Breakout (with Headers)

BOB-13989
4 Retired

5.1 Requesting Weather Station Data

We could add the RTC to the weather station and just have the first micro:bit (let's call this micro:bit A) send the day and time. However, let's make it interesting and have the second micro:bit (we will call this micro:bit B) request this data. In this part of the experiment, we will be listening for a string/number pair before sending out the data.

Hardware Hookup

We recommend having just the weather station's micro:bit connected for this section to avoid confusion when uploading code.

Inserting a USB Cable into the Micro:bit on the Weather Station's Side

Nothing is new here - the setup is the same as the previous experiment. We recommend having just the weather station's micro:bit connected for this section to avoid confusion when uploading code.

Weather Station Setup on a Table

Running Your Script

We are going to use Microsoft MakeCode to program the micro:bit. You can download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode.

Code to Note

This is pretty much the same code that was used in 2.1 and 4.1 except for one addition.

Screenshot of Code

Click on image for a closer view.

The only difference is that we are now requesting the weather station's data after each interval. If we receive the request from the micro:bit B in the form of a string/value pair, we will begin sending the readings as we do in the previous examples.

Closeup of Code

5.2 Requesting and Recording Readings in Intervals

In this section, we will request data from the weather station after a certain period of time has passed. If we receive everything, we will output the readings to either the serial terminal or OpenLog.

Hardware Hookup

To avoid confusion when uploading code, unplug the first micro:bit from your computer. Then connect the second micro:bit to your computer via USB cable.

Insert Second micro:bit

If you have not already, insert the microSD card into the OpenLog.

microSD Card inserted into OpenLog's microSD Card Socket

Then make the following connection using the circuit diagram. You'll need to connect a few parts together using a combination of M/M jumper wires on a breadboard. Then using the alligator with pigtails, you will need to connect the gator:RTC to the rest of the circuit.

Circuit Diagram

Your setup should look similar to the image below.

micro:bit connected to OpenLog and gator:RTC

Running Your Script

We are going to use Microsoft MakeCode to program the micro:bit. You can download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode.


Note: You may need to disable your ad/pop blocker to interact with the MakeCode programming environment and simulated circuit!

Code to Note

This is quite a lot of code but don't worry, we did most of the work in previous examples! We are also repeating most of the steps for each sensor reading so don't feel overwhelmed. Let's inspect the changes.

Screenshot of Code

The code is pretty big. You can click this image for a closer view. However, it would be better to just view the entire code in the MakeCode editor.

In the on start block, we want to be able to easily send serial to either the serial terminal or OpenLog. We create a variable called Debug and set it equal to 0. We also set up a variable to be able to set the RTC's day and time. We will call this Set_RTC and set it equal to 0. Write_Data is also created to output the comma delimited data and is initially set to 0.

Depending on the value of Debug, we direct which of the pins to send the serial data to and adjust the baud rate as necessary using condition statements. We will also have have a condition statement for setting the day and time. If the Set_RTC is set to 1, we will set the day and time from the gator:RTC extension blocks. For simplicity and avoiding confusion, we will just use military time.

Closeup of Code

Click image for a closer view

We set the radio, header to help read the data, arrays, and a variable to keep track of the total number of readings received.

Closeup of Code

Click image for a closer view

The variables for Past_DD_MM_YYYY and Past_HH_MM_SS will be useful when requesting data over a certain period of time. This period of time will be our Interval in seconds and we set it equal to 30 seconds. The time it takes for all 10 string/number pairs is about ~15 seconds so we will want to give it some breathing room for micro:bit B to finish what it is doing before requesting the next set of readings. We then set up condition statements to convert the interval to either the Past_Hour, Past_Minute, or Past_Second.

Closeup of Code

Click image for a closer view

In the forever block, we will keep checking the time. Each time we jump into the if statement, we will convert the time into seconds. If the time is greater than or equal than the last time that we recorded data, we will save the date and time. We will then send a request to micro:bit A to begin sending data back.

Closeup of Code

Click image for a closer view

We will not send comma delimited readings through the serial terminal or OpenLog until we press button A. If we press this button, we will begin writing serial data to either port just like we did in the micro:climate kit experiment guide.

Closeup of Code

You will notice in parts of the on radio received name value block that it is not exactly the same as the previous experiment. Since we are recording data, we do not want to also record any output that was used to debug the code. Therefore, we add an if statement to only send these messages to the serial terminal when we are in debug mode.

Closeup of Code

Click image for a closer view

We will also add a small 100ms pause to give the micro:bit some time to write data to either serial port.

Closeup of Code

Click image for a closer view


What You Should See

The code is set currently to output sensor data to the OpenLog. If you have not set up your RTC, you will need to adjust a few settings. Just before uploading the code to micro:bit B, make sure to adjust these settings:

  • Debug to 1
  • Set_RTC to 1
  • set date to _________ , _____ __ ____ to the current date
  • set time to __ : __ : __ AM to the current time

When set, pair micro:bit B to your computer to use the one-click download feature for MakeCode. Upload code and click on the "Show console Device" button begin viewing the serial output in the MakeCode console. Then power up micro:bit A that is attached to the weather station. micro:bit B will begin requesting data from micro:bit A as soon as we are within our specified interval. Press button A on micro:bit B to write data to the serial terminal.

Wireless Remote Weather Station with micro:bit Demo

If we received all 10 readings, we will output the date, time, and 9 readings that were received. Try changing the sensor readings on the weather station every 30 seconds to see if the values are responding to your input. If it is, we are good to start logging data!

Serial Output of Data Received Checked, and Printed in a Comma Delimited Row

Unplug the weather station from your computer if you are using it to power micro:bit A. Then adjust the settings to direct serial data to the Openlog and turn off any comment that was used for debugging.

  • Set_RTC to 0
  • Debug to 0

We will assume that micro:bit B is still paired to your computer. Click the Download button to upload code. Power up the weather station again and hit button A to start sending serial data to the OpenLog. Try changing the sensor readings on the weather station every 30 seconds and waiting a bit to obtain some sensor readings. Hit button A again to stop recording. Remove power from micro:bit B.

Insert the microSD card into a microSD card reader. Open up the saved file in your favorite text editor or spreadsheet to inspect the values. If the readings make sense, our weather station is ready! Depending on your application, try adjusting the interval to record data to 5 minutes, 15 minutes, 30 minutes, or an hour!

MicroSD card in Card Readers

If you are a user of Google Sheets, try importing this data on a spreadsheet like we did at the end of the micro:climiate kit experiment guide. You can import this text file as a CSV and then graph these values pretty easily. We recommend this quick guide from Google on how to import a .txt file as a CSV into sheets.