# Plans and Hardware Selection for a Hands-on Implementation of IoT with MCUs and MongoDB

Do you have a cool idea for a device that you may consider producing and selling? Would you want to have some new functionality implemented for your smart home? Do you want to understand how IoT works with a real example that we can work with from beginning to end? Are you a microcontroller aficionado and want to find some implementation alternatives? If the answer is yes to any of these questions, welcome to this series of articles and videos.

# Table of Contents

1.  The idea and the challenge
2.  The vision, the mission and the product
3.  Rules of engagement
4.  The plan
5.  Hardware selection
    1.  Raspberry Pi Pico W
    2.  Micro:bit
    3.  Adafruit Circuit Playground Bluefruit
    4.  Adafruit Feather nRF52840 Sense
    5.  Espressif ESP32-C6-DevKitC-1
6.  Recap and future content

# The idea and the challenge

For a while, I have been implementing many automations at home using third-party hardware and software products. This brings me a lot of joy and, in most cases, improves the environment my family and I live in.  In the past, this used to be harder, but nowadays, the tools and their compatibility have improved greatly.  You can start with something as trivial but useful as turning on the garden lights right after sunset and off at the time that you usually go to bed.  But you can easily go much further.

For example, I have a door sensor that is installed in my garage door that triggers a timer when the door is opened and turns a light red after six minutes.  This simple application of domotics has helped me to avoid leaving the door open countless times.

All the fun, and sometimes even frustration, that I have experienced implementing these functionalities, together with the crazy ideas that I sometimes have for creating and building things, have made me take a step forward and accept a new challenge in this area.  So I did some thinking and came up with a project that combined different requirements that made it suitable to be used as a proof of concept and something that I could share with you.

Let me describe the main characteristics of this project:

-   It should be something that a startup could do (or, at least, close enough.)  So, I will share the vision and the mission of that wannabe startup.  But most importantly, I will introduce the concept for our first product.  You don't have to *buy* the idea, nor will I spend time trying to demonstrate that there is a suitable business need for that, in other words, this is a BPNI (business plan not included) project.
-   The idea should involve something beyond just a plain microcontroller (MCU).  I would like to have some of those, maybe even in different rooms, and have their data collected in some way.
-   The data will be collected wirelessly. Having several sensors in different places, the wired option isn't very appealing.  I will opt for communications implemented over radio frequencies: Bluetooth and WiFi.  I might consider using ZigBee, Thread, or something similar in the future if there is enough interest.  Please be vocal in your comments on this article.
-   I will use a computer to collect all the sensor measurements locally and send them to the cloud.
-   The data is going to be ingested into MongoDB Atlas and we will use some of its IoT capabilities, such as time series collections and real-time analytics.
-   Finally, I'm going to use some programming languages that are on the edge or even out of my comfort zone, just to prove that they shouldn't be the limitation.

# The vision, the mission and the product

**Vision**: we need work environments that enhance our productivity.
Consider that technology, and IoT in particular, can be helpful for that.

**Mission**: We are going to create, sell, and support IoT products that will help our users to be more productive and feel more comfortable in their work environments.

The first product in the pipeline is going to help our customers to measure and control noise levels in the workspace.

Hopefully, by now you are relieved that this isn't going to be another temperature sensor tutorial. Yippee-ki-yay!

Let's use an implementation diagram that we will refine in the future.  In the diagram, I have included an *undetermined* number of sensors (actually, 5) to measure the noise levels in different places, hence the ear shape used for them.  In my initial implementation, I will only use a few (two or three) with the sole purpose of verifying that the collecting station can work with more than one at any given time.  My first choice for the collecting station, which is represented by the inbox icon, is to use a Raspberry Pi (RPi) that has built-in support for Bluetooth and WiFi. Finally, on the top of the diagram, we have a MongoDB Atlas cluster that we will use to store and use the sensor data.

 videos in the past. Please forget my mistakes when using it.

Finally, there are some things that I won't be covering in this project, both for the sake of brevity and for my lack of knowledge of them.  The most obvious ones are creating custom PCBs with the components and 3D printing a case for the resulting device.  But most importantly, I won't be implementing firmware for all of the devkits that I will select and even less in different languages.  Just some of the boards in some of the languages.  As we lazy people like to say, this is left as an exercise to the reader.

# The plan

Coming back to the goal of this project, it is to mimic what one would do when one wants to create a new device from scratch.  I will start, then, by selecting some microcontroller devkits that are available on the market.  That is the first step and it is included in this article.

One of the main features of the hardware that I plan to use is to have some way of working wirelessly.  I plan to have some sensors, and if they require a wired connection to the collecting station, it would be a very strong limitation. Thus, my next step is to implement this communication.  I have considered two alternatives for the communication.  The first one is Bluetooth Low Energy (BLE) and the second one is MQTT over WiFi.  I will give a more detailed explanation when we get to them.  From the perspective of power consumption, the first option seems to be better, and consuming less power means batteries that last longer and happier users.

But, there seems to be less (complete) documentation on how to implement it.  For example, I could find neither good documentation for the BLE library that comes with MicroPython nor anything on how to use BLE with Bluez and DBus.  Also, if I successfully implement both sides of the BLE communication, I need to confirm that I can make it work concurrently with more than one sensor.

My second and third steps will be to implement the peripheral role of the BLE communication on the microcontroller devkits and then the central role on the RPi.

I will continue with the implementation of the WiFi counterparts.  Step 4 is going to be making the sensors publish their measurements via MQTT over WiFi, and Step 5 will be to have the Raspberry Pi subscribe to the MQTT service and receive the data.

Eventually, in Step 6, I will use the MongoDB C++ driver to upload the data to a MongoDB Atlas cluster.  Once the data is ingested by the MongoDB Atlas cluster, we will be able to enjoy the advantages it offers in terms of storing and archiving the data, querying it, and using real-time analytics and visualization.

So, this is the list of steps of the plan:

1.  Project intro (you are here)
2.  BLE peripheral firmware
3.  BLE central for Raspberry Pi OS
4.  MQTT publisher firmware
5.  MQTT subscriber for Raspberry Pi OS
6.  Upload data from Raspberry Pi OS to MongoDB Atlas clusters
7.  Work with the data using MongoDB

I have a couple of ideas that I may add at the end of this series, but for now, this matches my goals and what I wanted to share with you.  Keep in mind that it is also possible that I will need to include intermediate steps to refine some code or include some required functionality.  I am open to suggestions for topics that can be added and enhancements to this content.  Send them my way while the project is still in progress.

# Hardware selection

I will start this hands-on part by defining the features that I will be using and then come up with some popular and affordable devkit boards that implement those features or, at least, can be made to do so.  I will end up with a list of devkit boards.  It will be nothing like the "top devkit boards" of this year, but rather a list of suggested boards that can be used for a project like this one.

Let's start with the features:

-   They have to implement at least one of the two radio-frequency communication standards: WiFi and/or Bluetooth.
-   They have to have a microphone or some pins that allow me to connect one.
-   Having another sensor on board is appreciated but not required.  Reading the temperature is extremely simple, so I will start by using that instead of getting audio.  I will focus on the audio part later when the communications are implemented and working.
-   I plan to have independent sensors, so it would be nice if I could plug a battery instead of using the USB power. Again, a nice feature, but not a must-have.
-   Last, but not least, having documentation available, examples, and a vibrant community will make our lives easier.

## Raspberry Pi Pico W

 is produced by the same company that sells the well-known Raspberry Pi single-board computers, but it is a microcontroller board with its own RP-2040 chip.  The RP-2040 is a dual-core Arm Cortex-M0+ processor.  The W model includes a fully certified module that provides 802.11n WiFi and Bluetooth 5.2.  It doesn't contain a microphone in the devkit board, but there are examples and code available for connecting an electret microphone.  It does have a temperature sensor, though. It also doesn't have a battery socket so we will have to use our spare USB chargers.

Finally, in terms of creating code for this board, we can use:

-   MicroPython, which is an implementation of Python3 for microcontrollers.  It is efficient and offers the niceties of the Python language: easy to learn, mature ecosystem with many libraries, and even REPL.
-   C/C++ that provide a lower-level interface to extract every bit of juice from the board.
-   JavaScript as I have learned very recently.  The concept is similar to the one in the MicroPython environment but less mature (for now).
-   There are some Rust crates for this processor and the board, but it may require extra effort to use BLE or WiFi using the embassy crate.

## Micro:bit

 is a board created for learning purposes.  It comes with several built-in sensors, including a microphone, and LEDs that we can use to get feedback on the noise levels.  It uses a Nordic nRF52833 that features an ARM Cortex-M4 processor with a full Bluetooth Low Energy stack, but no WiFi.  It has a battery socket and it can be bought with a case for AA batteries.

The educational goal is also present when we search for options to write code.  These are the main options:

-   Microsoft MakeCode which is a free platform to learn programming online using a graphical interface to operate with different blocks of code.
-   Python using MicroPython or its own web interface.
-   C/C++ with the Arduino IDE.
-   Rust, because the introductory guide for embedded Rust uses the microbit as the reference board. So, no better board to learn how to use Rust with embedded devices.  BLE is not in the guide, but we could also use the embassy nrf-softdevice crate to implement it.

## Adafruit Circuit Playground Bluefruit

 is also aimed at people who want to have their first contact with electronics.  It comes with a bunch of sensors, including temperature one and a microphone, and it also has some very nice RGB LEDs.  Its main chip is a Nordic nRF52840 Cortex M4 processor with Bluetooth Low Energy support.  As was the case with the micro:bit board, there's no WiFi support on this board.  It has a JST PH connector for a lipo battery or an AAA battery pack.

It can be used with Microsoft MakeCode, but its preferred programming environment is CircuitPython.  CircuitPython is a fork of MicroPython with some specific and slightly more refined libraries for Adafruit products, such as this board. If you want to use Rust, there is a crate for an older version of this board, without BLE support.  But then again, we could use the embassy crates for that purpose.

## Adafruit Feather nRF52840 Sense

 is also based on the Nordic nRF52840 Cortex M4 and offers Bluetooth Low Energy but no WiFi.  It comes with many on-board sensors, including microphone and temperature.  It also features an RGB LED and a JST PH connector for a battery that can be charged using the USB connector.

While this board can also be used to learn, I would argue that it's more aimed at prototyping and the programming options are:

-   CircuitPython as with all the Adafruit boards.
-   C/C++ with the Arduino IDE.
-   Rust, using the previously mentioned crates.

## Espressif ESP32-C6-DevKitC-1

 features a RISC-V single-core processor and a WROOM module that provides not only WiFi and Bluetooth connectivity but also Zigbee and Thread (both are network protocols specifically designed for IoT).  It has no sensors on-board, but it does have an LED and two USB-C ports, one for UART communications and the other one for USB Type-C serial communications.

Espressif boards have traditionally been programmed in C/C++, but during the last year, they have been promoting Rust as a supported environment.  It even has an introductory book that explains the basics for their boards.

# Recap and future content

:youtube]{vid=FW8n8IcEwTM}

In this article, we have introduced the project that I will be developing.  It will be a series of sensors that gather noise data that will be collected by a bespoke implementation of a collecting station.  I will explore two mechanisms for the communication between the sensors and the collecting station: BLE and MQTT over WiFi.  Once the data is in the collecting station, I will send it to a MongoDB Atlas cluster on the Cloud using the C++ driver and we will finish the project by showing some potential uses of the data in the Cloud.

I have presented you with a list of requirements for the development boards and some alternatives that match those requirements, and you can use it for this or similar projects.  In our next episode, I will try to implement the BLE peripheral role in one or more of the boards.

If you have any questions or feedback, head to the [MongoDB Developer Community forum.

  [1]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt28e47a9dd6c27329/65533c1b9f2b99ec15bc9579/Adventures_in_IoT.png
  [2]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltf77df78f4a2fdad2/65536b9e647c28790d4e8033/devices.jpeg
  [3]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt38747f873267cbc5/655365a46053f868fac92221/rp2.jpeg
  [4]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt0d38d6409869d9a1/655365b64d285956b1afabf2/microbit.jpeg
  [5]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltfa0bf312ed01d222/655365cc2e0ea10531178104/circuit-playground.jpeg
  [6]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt5ed758c5d26c0382/655365da9984b880675a9ace/feather.jpeg
  [7]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt1ad337c149332b61/655365e9e9c23ce2e927441b/esp32-c6.jpeg