---
# System prepended metadata

title: Remote monitoring of whether window is closed or not - reduces burglary risk

---

# Remote monitoring of whether window is closed or not - reduces burglary risk
Author: Malva von Schenck (mv222py)

In this tutorial an Arduino Nano RP2040 microboard together with an hall-effect sensor will be connected to the WiFi to gather and send data to an vizualisation analytics web application for monitoring. The application will be able to be used remote.

By having basic knowledge in microPython, of microcontrollers and sensors - this tutorial should be possible to finish within approximately 15 hours.

# Objective
This project aims to use a hall-effect sensor connected to the WiFi to enable remote monitoring of the bottom floor window facing the street. It is very easy to forget to close this window - which increases the risk of having burglary. The purpose of this project is to improve security. Improving security is also why I choose this proejct.

I want to use the data to be able to check one extra time - while standing by the bus station, or simply just leving home, to see whether the window is closed or not. The data will give insights to how good we are at closing the window - and if it is even neccessary. Maybe we forget the window all the time - without any bulgary. Then it is not a neccessary concern in the future.


# Material
List of material:


| Hardware | Price |
| -------- | -------- |
| Arduino Nano RP2040 Connect (with headers)     | 239 SEK     |
| LNU – 1DT305 Tillämpad IoT – Sensors only bundle     | 103 SEK     |

The list of material shows specifically what was bought from Electrokit Sweden AB. However, the specific sensor used from the sensors only bundle is:
| Sensor | 
| -------- |
| TLV49645 SIP-3 Hall-effektsensor digital     |

The hall-effect sensor could be used for positioning of objects. Through detecting magnetic field, the sensor can recognize if a object is close to the sensor or not. The circuit allows to operate the voltages between 3 V up to 26 V DC. See figure 1.

![](https://i.imgur.com/6V4ewSK.png) 
Fig 1: Hall-effect sensor TLV49645 SIP-3

For more information, see the datasheet to the hall-effect sensor:
https://www.electrokit.com/uploads/productfile/41015/2343219.pdf

The Ardino Nano RP2040 microboard can operate the voltages between 4.5 up to 21 V. It also has a Flash memory of 16MB. Wireless communication over WiFi is  possible. See figure 2.
![Uploading file..._uqhknns2e]() 
Fig 2: Arduino Nano RP2040 with headers

For more infomration, see the datasheet to the Ardion Nano RP2040: https://www.electrokit.com/uploads/productfile/41018/ABX00053-Datasheet-2.pdf

A breadboard and jumper wire was also used, these were included in the LNU – 1DT305 Tillämpad IoT – Sensors only bundle boguth from Electrokit. 

# Computer setup

Connect your microboard to your computer using an USB. Thonny is the chosen Integrated Development Environment (IDE) in this tutorial. First install the Thonny editor. For Windors, Mac or Linux - install from this link: https://thonny.org/. When you have managed to download and opened Thonny, navigate to "View" and choose both "Files" and "Shell" for a more practical view to work in. Then, download the .uf2 file from: https://micropython.org/download/ARDUINO_NANO_RP2040_CONNECT/. Copy this file, open your "File explorer", go into "This PC", press the RESET button (white button ontop of the microboard) twice to see your device, and finally - drop the downloaded and copied file into the device file. MicroPython should now be installed on the microboard. Now, open Thonny and have a look in the Thonny Editor. Navigate to "Run" and click on "Select Interpreter". Choose MicroPython(generic) for the interpreter or device. Then choose your device for the port, e.g., COM4.

To see if your microboard is able to run PythonCode, try to run the script:

print("Hello world")

Did you get Hello world? Then it works!

# Putting everything together

Before connecting the microboard and the sensors, it is good to read the dara sheets (provided in the Material chapter) to see what supply voltages the products can accept. After looking through the data sheet for the Arduino Nano RP2040 microboard - we can see that its voltage regulator can accept supply voltage between 4.5 V to 21 V. While the hall-effect sensor can operate the voltages between 3 V up to 26 V DC. This means that the sensor can be connected to both the +5 V pin and the +3V3 pin. I connected the sensor supply voltage to the +5 V pin at the board. See figure 2 to understand what pin to connect the jumper wire to the board for the supply voltage +5V. It is in red on the left side.
![](https://i.imgur.com/ZcSrrlU.png)
Figure 2: Hardware overview of the Arduino Nano RP2040

To understand what pin to connect to the hall-effect sensor - to get the supply voltage pin - I had a look in the data-sheet for the hall-effect sensor (could also be found in the Material chapter). Vdd is the Supply voltage pin, which can be seen in figure 3.
![](https://i.imgur.com/1q3TuFw.png)
Figure 3: Pin configuration of the hall-effect sensor

Two other important pins to connect where the GND and the Output. For the right pins for the sensor, see figure 3. TRhe GND and the Output on the microboard was simply chosen from figure 2. To know what number the output pin is, choose the number straight after GPIO. For example, my chosen pin is GPIO26, therefore I write 26 in the code.

In the circuit diagram, red is the supply voltage, black is the GND and yellow is the Output.

![](https://i.imgur.com/BdNN6b0.png)


# Platform
Datacake will be used. The free version. It is a cloud solution which requires no programming skills and is very easy to use. The datacake platforms suites both small and large projects, with few or many sensors.

# The code

The boot.py script is used to connect to the WiFi.
```
import network, socket
import config

# AP info
SSID=config.WIFI_SSID # Network SSID
KEY=config.WIFI_PASS  # Network key

PORT = 80
HOST = "www.google.com"

# Init wlan module and connect to network
print("Trying to connect. Note this may take a while...")

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)

# We should have a valid IP now via DHCP
print("Wi-Fi Connected ", wlan.ifconfig())
```
Figure 4: Boot script



The config.py script is used to store WiFi credentials and Datacake credentials. This script is called in other scripts, such as the boot.py.

```
# WiFi credentials
WIFI_SSID = '<SSID for my WiFi>'
WIFI_PASS = '<Password>'

# Datacake credentials
SERIAL_NUMBER = '1998malva0130'
MQTT_BROKER = 'mqtt.datacake.co'
TOKEN = '0dc1114eb8551c1f206b75bd75b328c2cb4ea301'
PORT = 1883
TOPIC = 'dtck-pub/halleffect/3299a288-0fb4-4ac1-b4b5-20cb367c7fc9/HALLEFFECT1'

```
Figure 5: Config script


To be able to publish sensor data to my platform, I need a MQTT library. Therefore I imported an MQTT script.
The mqtt script used can be found: https://raw.githubusercontent.com/RuiSantosdotme/ESP-MicroPython/master/code/MQTT/umqttsimple.py


The main.py scirpt is used to get data from the microboard and to send data to the MQTT broker.

```
from mqtt import MQTTClient
import time
import ujson
import machine
import config
from machine import Pin



#def halleff():
halleffect = Pin(26, mode=Pin.IN)


def sub_cb(topic, msg):
   print(msg)

# MQTT Setup
client = MQTTClient(config.SERIAL_NUMBER,
                    config.MQTT_BROKER,
                    user=config.TOKEN,
                    password=config.TOKEN,
                    port=config.PORT)
client.set_callback(sub_cb)
client.connect()
print('connected to MQTT broker')

# The MQTT topic that we publish data to
my_topic = config.TOPIC

while True:

    value = halleffect()
    print(value)
    time.sleep_ms(100)
    # Here, we publish the dummy data to the broker, under topic
    client.publish(topic=my_topic, msg=str(value))
    #client.check_msg()
    print("Send data to MQTT broker, sleeping for 10 seconds...")
    time.sleep(10) # Wait 10 seconds

```
Figure 6: Main script

# Transmitting the data/connectivity
I plan to use Wifi since I want to be able to monitor my sensor from a distance, but only from a close distance (the bus stop). Then the WiFi range is good enough. The Arduino RP2040 Connect only works with 2.4 GHz Wifi. I have chosen to use my mobile hotspot since our WiFi only have 5 GHz and not the 2.4 GHz. I´ll connect to WiFi and then send the sensor data by using MQTT. This is why I also chose Datacake - because it has the connectivity type MQTT available.

As seen in the main.py script, my data is sent every 10 second.
```
    print("Send data to MQTT broker, sleeping for 10 seconds...")
    time.sleep(10) # Wait 10 seconds
```
![](https://i.imgur.com/RO31aTn.png)

# Presenting the data

Data is saved every 10 second in the database. The dashboard looks as the following in Datacake:

![](https://i.imgur.com/WlIeU8h.png)

Since it is a Bolean dashboard, it should show red when the magnet is not close by, and green if the magnet is there (open or closed window).

# Finalizing the design
I think I learned a lot through this project. I really appriciate building something real and not only reading about Internet of Things - actually producing something. I am completely new working with IoT so I think it went over my expectations good.

See figure 7 for the final design.
![](https://i.imgur.com/mEJNyGR.jpg)
![](https://i.imgur.com/u7um0Qo.jpg)
Figure 7: Final design
