# Babymonitor
David Granath / dg222px
In this project I created a babymonitor which is able to display brightness, temperature and if it's noisy. The system uses a microcontroller, sensors, MQTT-broker and lastly Adafruit Dashboard for visualisation of the data. Adafruit collects data from the broker which collects data from the microcontroller via Wifi.
It should take about 4 hours to perform this tutorial.
## Objective
I chose to create a babymonitor because it's something that is very useful for anyone that has a baby. With the help of a babymonitor you can monitor the baby and their environment even while they are asleep. The purpose for it is mainly to prevent babies from crying for their parents for a longer period of time but also to check the temperature and brightness of the environment. The data from the system will bring the user some insight to how their baby is doing.
## Material
|Component|Purpose|Provider|Cost|
| ----- | ----- | ----- | ----- |
|Raspberry Pi Pico WH|Main processing unit|[Elektrokit](https://www.electrokit.com/raspberry-pi-pico-wh)|99 kr|
|DHT11|Temperature data|[Elektrokit](https://www.electrokit.com/digital-temperatur-och-fuktsensor-dht11)|49 kr|
|Photoresistor|Brightness data|[Elektrokit](https://www.electrokit.com/fotomotstand-cds-4-7-kohm)|9.5 kr|
|Sound sensor|Sound data|[Elektrokit](https://www.electrokit.com/ljudsensor-analogdigital)|36 kr|
|Breadboard|840-point breadboard|[Elektrokit](https://www.electrokit.com/kopplingsdack-840-anslutningar)|69 kr|
|1 x 10kΩ resistor|Resistor for photoresistor|[Elektrokit](https://www.electrokit.com/motstand-kolfilm-0.25w-10kohm-10k)|1 kr|
|Jumper wires (Male-Male)|Connect components|[Elektrokit](https://www.electrokit.com/labbsladd-20-pin-15cm-hane/hane)|52 kr|
|USB Cable (USB-A to Micro-USB)|Connect to Raspberry Pi|[Elektrokit](https://www.electrokit.com/usb-kabel-a-hane-micro-b-5p-hane-1.8m)|39 kr|
||||354,5 kr|
## Computer setup
To create the babymonitor I chose the IDE [Thonny](https://thonny.org) because it was very easy use. To be able to start coding you need to install MicroPython which can be done [here](https://www.raspberrypi.com/documentation/microcontrollers/micropython.html). After downloading the file hold the boot button on the Pico while plugging it in to a the computer containing the MicroPython file. It should now be accessable on the devices page. Just put the MicroPython file in it and it will be accessable inside of Thonny by selecting it down to the right where it probablys says "Local Python 3". Click that button and select MicroPython (Raspberry Pi Pico). Now you're able to code and upload it to the Pico by right-clicking a file and select "Upload to /".
## Putting everything together
To understand the setup it's necessary to know what the different pins on the Pico does. Please look at this [datasheet](https://datasheets.raspberrypi.com/picow/pico-w-datasheet.pdf).
The setup is quite simple due to all the sensors work great with the 3.3 volt that the Pico is able to provide from its 3V3 PIN (PIN 36). But you need to be sure to connect the photoresistor and the DHT11 to an ADC PIN such as PIN 31 and 32.

This in an image of how my breadboard looks.
## Platform
The platform I chose was [Adafruit I/O](https://io.adafruit.com). I chose Adafruit I/O because it provides a cloud-based service with both a MQTT Broker and a Dashboard. The MQTT Broker is a publish/subscribe system which provides an easy way to collect the data and then send it elsewhere. The Adafruit Dashboard provides an easy and nice way to visualize the data provided by the broker.
## The code
The code starts off with trying to connect to the wifi which is done with the help of the network library provided by MicroPython. You can use this library to connect to the wifi like this (the keys library is my folder with sensitive information):
```
import network, machine, keys
wlan = network.WLAN()
ssid = keys.WIFI_SSID
key = keys.WIFI_PASS
def connect_wlan():
wlan.active(True)
wlan.connect(ssid, key)
```
After connecting to the wifi you can connect to the Adafruit MQTT Broker. I used the [umqtt library](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py) for this due to it providing an easy way to connect to the broker. With this library you can use the `MQTTClient()` function which creates a client object that can call the `connect()` function which connects it to the MQTT Broker. It's necessary to make an I/O Adafruit account for this to work:
```
client = MQTTClient(keys.AIO_CLIENT_ID, keys.AIO_SERVER, keys.AIO_PORT, keys.AIO_USER, keys.AIO_KEY)
client.connect()
```
The sensors are coded a bit different from one another. But what they do have in common is that's important that you provide the correct Pin when using the Pin library. This is an example of using the Pin library from the machine library:
`digitalPin = Pin(22, Pin.IN)`
The difference between the sensors is that the photoresistor uses the ADC library, the DHT11 uses the dht library and the sound sensor only uses a digital pin.
Due to the sound sensor needing to have some kind of way to prevent false positives (like a quick noise that isn't necessarily a baby crying) I made this code:
```
def is_noisy():
noise_counter = 0
for x in range(20):
if(__measure() == 1):
noise_counter += 1
time.sleep_ms(50)
if(noise_counter >= 10):
return 1
else:
return 0
```
This code only returns 1 (True) if at least half of the measurements during a second are returned as true by the digital output of the sound senor.
The main loop is a bit tricky to understand due to the previous code that takes a second:
```
#Collect and send data forever (until stopped)
while True:
#Brightness (Every 37.5 sec, 1.6/min)
if(timer%15 == 0):
brightness = photoresistor.measure()
broker.publish(client, str(brightness), keys.AIO_FEED_BRIGHT)
#Temperature (Every 37.5 sec, 1.6/min)
if(timer%15 == 0):
temp = temperature.measure()
broker.publish(client, str(temp), keys.AIO_FEED_TEMP)
#Check Microphone (Every 2.5 sec, 24/min)
#(Measurement takes 1 second)
is_noisy = sounddetector.is_noisy()
broker.publish(client, str(is_noisy), keys.AIO_FEED_SOUND)
#Limit to 37.5 loops per min (Note that mic takes a sec)
time.sleep(1.5)
#Add to timer
timer = timer + 1
```
It's coded this way because the sensors data is published to different feeds and have different frequencies.
## Transmitting the data / connectivity
Data is sent about 27.2 times per minute, 24/min for the sound sensor and 1.6/min each for the photoresistor and DHT11. I/O Adafruit has a limit of 30 messages/min which is why it's lower than 30. To send this data I used wifi. The transport protcols I used was Adafruit MQTT Broker which I used to provide data for my Adafruit Dashboard.
## Presenting the data
The dashboard is built with the help of Adafruit Dashboard. It's able to present the data that is stored up to 30 days in different forms, such as graph and line gauge. This is how the dashboard looks:

The amount of data that is saved inside of the broker depends on how often the system is on. If it was on all the time it would save be able to save 27.2 messages per minute. I/O Adafruit clears the feed data 30 days after last entry or if it goes past 2.5 GB storage.
## Finalizing the design
This is a picture of the babymonitor:

The project went good but there is a lot of room for improvement in my opinion. Some improvements that I would like to add in the future are a better sound sensor, notification capabilites, 3D-printed case, a battery and communication via LoRaWAN. Right now the babymonitor is both unsafe for children and not very portable which both are very important aspects of a babymonitor.
Here is a [link](https://github.com/Devve2000/babymonitor) to the repository containing all the code that I used for the project.