# Dog activity tracker An IoT project by Viktoria Björkman (vb222ph) ## Project overview This report presents the process of a project made during the Introduction to IoT summercourse at Linnaeus University in 2022. The report will include information such as the purpose of the device, materials used, steps taken to finalize the project and considered methods of data presentation. Being a beginner, the project took several hours to complete but, with the right knowledge and materials, it may be able to complete in a couple of hours. ## Objective The purpose of the device is to monitor the daily activity of a dog, meaning mostly tracking sleep vs time spent active. The reason for choosing this project was that i, as a dog owner myself, wish to give my dog the best life possible. A very important part of an individuals well-being is having a balanced sleep to active ratio. Sleeping too little may cause behavioural problems, while sleeping too much may indicate that the individual is suffering from depression. I think this project will help dog owners make sure that their dogs are getting enough sleep, by monitoring their time spent resting during the day. ## Materials To create this project the Arduino Nano RP2040 Connect microcontroller was used (see fig. 1). ![](https://i.imgur.com/CElu7B1.jpg) Fig. 1 - The Arduino Nano RP2040 Connect microcontroller with headers. The microcontroller is used to collect and send data over the internet, by being programmed to collaborate with sensors and connect to different networks. In this case, a breadboard (see fig. 2) was used to connect sensors to the microcontroller. The purpose of a breadboard is simply to provide an easy method to connect sensors to a microcontroller, without having to solder for example, and creates an opportuinity to experiment with different sensors. ![](https://i.imgur.com/XrgpTpu.jpg) Fig. 2 - Breadboard with 840 connections. To collect the preferred data, two different sensors were used. The first one is the MCP9700 temperature sensor (see fig. 3) and the second one is the SW-200D tilt switch (see fig. 4). ![](https://i.imgur.com/z0fO4Ns.png) Fig. 3 - The MCP9700 temperature sensor. ![](https://i.imgur.com/X7ic6cc.jpg) Fig. 4 - The SW-200D tilt switch. The MCP9700 temperature sensor is an analogue sensor, used to collect data about the surrounding temperature and send it to a microcontroller when connected to it. It was used in this project as an additional way to check that the dogs resting environment is optimal regarding to their needs. The SW-200D tilt switch collects information regarding whether or not an object is tilted, it does so through a metal bead and thus sends different data depending on which side of the sensor that the bead is located at, wich would indicate the dogs body position. To connect the different units a set of jumper wires was used. ### List of materials | IoT Device | Link | Cost | | -------- | -------- | -------- | | Arduino Nano RP2040 Connect | https://www.electrokit.com/produkt/arduino-nano-rp2040-connect-with-headers/ | 299.00 sek | | Temperature sensor MCP9700 | https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/ | 8.00 sek | | Tilt switch SW-200D | https://www.electrokit.com/produkt/tilt-switch/ | 12.00 sek | ## Computer setup The first things that needed to be done was to update the Arduino firmware and teach the device how to process MicroPython code, the programming language that was supposed to be used according to instructions. The firmware was updated through Arduinos own IoT Cloud according to this guide: https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-upgrading-nina-firmware. MicroPython was then "taught" to the device by following this online tutorial: https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api. Thonny was the recommended IDE for beginners and thus the one to be used for all of the programming and uploading the code to the device. As MicroPython was already integrated, the editor was ready for use after the installation. Before beginning the programming for the project, the setup was tested using a "blink" example. This was done through the following steps: 1. Connect the device via USB-cable to your computer. 2. Open up Thonny and create a new file. 3. Press save and then choose "This computer". 4. Create a new map and name the file "main.py", press save. 5. Use following code to use the devices internal LED: ```python= from machine import Pin import time led = Pin(6, mode=Pin.OUT) while True: led.value(1) time.sleep(0.5) led.value(0) time.sleep(0.5) ``` 5. Now press "save" and run the script. 6. Done! ## Putting everything together The following chapter will provide instructions as how to build the device, as well as a short concept description regarding a possible finalisation of the product. ### The device The device was put together using the Arduino Nano RP2040 Connect datasheet (see fig. 5). In the final setup (see fig. 6), we find the microcontroller, sensors and jumpercables all attached to the breadboard according to the datasheet specifications. ![](https://i.imgur.com/85RMYYT.png) Fig. 5 - The Arduino Nano RP2040 Connect datasheet. ![](https://i.imgur.com/nYYsOW7.jpg) Fig. 6 - Final setup of the device. Firstly, we can see the tilt switch (see fig. 6 (orange box) and fig. 7) that is connected to the microcontroller via two different jumper cables. The red cable is connected to the power output (3v3) and is meant to power the sensor, while the second cable (black) is connected to a digital input (D2), that will help send the data to the microcontroller for storage. ![](https://i.imgur.com/Qbf442q.png) Fig. 7 - Tilt switch connections. Further, we have the temperature sensor (see fig. 6 (green box) and fig. 8). The temperature sensor have three pins with different functions. Firstly, the VDD pin is supposed to be connected to the power source and have, like the tilt switch, been connected to the 3v3 power output using a red cable. Secondly, the VOUT pin is the one that will send the data to the device. As it is an analogue sensor, it has been connected to an analogue input (A0) by using a black jumper cable. Lastly, the third pin (GND) is connected, using blue jumper cables, to the GND input as to complete the circuit. ![](https://i.imgur.com/cYgrIzt.png) Fig. 8 - Temperature sensor connection sheet. Initially, the 5v power output was supposed to be used to power the sensors. However, as it provided uneven readings, the 3v3 power output was used in the end. ## Platform The platform used for connection was Datacake (https://datacake.co/) with their free plan as a choice of usage. The platform provided an easy way of connecting the device, configure sensors and present the data in a preferred way. An alternative platform, Helium (https://www.helium.com/), was considered, but seemed to lack some kind of compatability with the Arduino Nano RP2040 Connect. ## The code This chapter will explain the different documents and the code, accoring to the functions, created in this project. The functions considered were sensor configuration and network connection. ### Document purposes Four different documents (see fig. 9) were created in this projects, all with different purposes. ![](https://i.imgur.com/0S7WzwN.png) Fig. 9 - The documents used in the project. The "boot.py" file was created for the functions that would preferrably go active as soon as the device is powered up. The "config.py" and "mqtt.py" files was mainly used for connectivity functions. Lastly, the "main.py" contained the code for the data upload and sensor functions. ### The sensors The two sensors needed to be programmed quite differently, as the collected data and functionality differed significantly. #### Tilt switch To program the tilt switch, the following code was used: ```python= from machine import Pin import time tilt = Pin(25, mode=Pin.IN, pull=Pin.PULL_DOWN) while True: value = tilt() if value == 1: value = tilt() print(value) else: value = tilt() print(value) time.sleep_ms(100) ``` Line 1 and 2 contains the code that makes the microcontroller able to perform the sensor functionalities. The 4th line defines which pin input number that the sensor output is connested to. Line 8 to 15 defines how the sensor is going to perform. In this case, the sensor gives two different values (1 and 0) depending on wich side of the sensor that the bead is located at. "Print" simply tells the microcontroller what message to send depending on what value that was registered. #### Temperature sensor The temperature sensor was programmed using this code: ```python= from machine import time adc_pin = machine.Pin(26) adc = machine.ADC(adc_pin) while True: reading = adc.read_u16() conversion_factor = 3.3/(65536) millivolts = reading * conversion_factor * 1000 degC = (millivolts - 500.0) / 10.0 print("Temp:", degC) ``` The first two lines works the same way as in the tilt switch example. As the MCP9700 temperature sensor is an analogue sensor, it needed coding for converting the analogue input to a digital reading. Line 4 and 5 is therefore the ones that firstly defines what pin input to read and how to then convert it to digital readings. Line 9 to 13 describes how the sensor is to collect the surrounding temperature. The collected data needed to be converted as to know what number of celsius that was registered. The "print" function then tells the microcontroller to send the message "Temp:" followed by the degree celsius (degC) that was registered. ### Connecting to wi-fi For the microcontroller to be able to connect to the wi-fi and Datacake, the MQTT client had to be used. MQTT is a client that helps the device send data through a network connection. Credentials for connecting to the wi-fi and Datacake then had to be used. #### The MQTT The MQTT needed to have it's own document that contained a certain code (https://raw.githubusercontent.com/pycom/pycom-libraries/master/lib/mqtt/mqtt.py). The following code was then used in "main.py": ```python= from mqtt import MQTTClient import time import ujson import machine import config def sub_cb(topic, msg): print(msg) 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') while True: client.publish(topic=my_temp, msg=str(degC)) client.publish(topic=my_act, msg=str(value)) print('Send data to MQTT broker, sleeping for 1 minute...') time.sleep(60) ``` Line 1 to 17 makes sure that the microcontroller reads the "mqtt.py" document and performs accordingly. Line 21 to 24 are the ones telling the broker where to get the data from and at what interval. According to this code it sends data every minute. #### Wi-fi connection and credentials The "boot.py" document was used to configure the wi-fi connection, as it is usually preferred that the device connects to the network as soon as it's powered up. The following code was added: ```python= import network, socket import config SSID=config.WIFI_SSID KEY=config.WIFI_PASS PORT = 80 HOST = "www.google.com" print("Trying to connect. Note this may take a while...") wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(SSID, KEY) print("Wi-Fi Connected ", wlan.ifconfig()) ``` The first two lines tells the microcontroller to connect to a network and how. Here it's also supposed to import some credentials stored in the "config.py" document and use them to connect to the preferred wi-fi. #### Datacake The Datacake website was the one that was going to be used to store and present the collected data. Sending the data was done using the MQTT client by using certain code in the "config.py" document: ```python= SERIAL_NUMBER = '1234vikbj5678' MQTT_BROKER = 'mqtt.datacake.co' TOKEN = '1f51b7f1818ab95a3d16df1e1788f5e27faec5a0' PORT = 1883 TEMP = 'dtck-pub/arduino-rp2040/ee1bce71-ad09-4563-9577-d99d123128c8/TEMPERATURE' ACT = 'dtck-pub/arduino-rp2040/ee1bce71-ad09-4563-9577-d99d123128c8/ACTIVITY' ``` In Datacake, the connected device is given a serial number, that had to be specified, as well as the token, wich is the password that gives the device access to the account. The last two lines specify wich configured devices in datacake that data is supposed to be sent to. These are connected to the sensors using this code in the "main.py" document: ```python= client.publish(topic=my_temp, msg=str(degC)) client.publish(topic=my_act, msg=str(value)) ``` ## Transmitting the data In this project, wi-fi was used for connecting the device to the internet. The reason for this was that it was simple, with the Arduino Nano RP2040 Connects built-in wi-fi functionality, and a good solution in regard to the purpose of the device. As previously mentioned, the data was sent using the MQTT client, as it is easily connected to Datacake. The dog could have short moments of awakening, by being disturbed for example, data was therefore sent every minute. This was considered possible to give important insights, as the environment where the dog rest might not be optimal if it is often disturbed. ## Presenting the data The data was visualised using Datacakes widget function. This can be used by navigating to the dashboard and unlocking it for editing using the switch to the far right. The sensors first had to be added to Datacake, using the "field" function (see fig. 10) under the "configuration" tab. ![](https://i.imgur.com/l6RuqR0.png) Fig. 10 - Fields with the configured sensors. The temperature sensor was set as a float type, as it transmits a numeric value, and the tilt switch was set as a boolean type, as it only can provide two different values. Different widgets were then added, connected to the sensors and thus created a nice dashboard (see fig. 11). ![](https://i.imgur.com/8XxdN6h.png) Fig. 11 - Completed Datacake dashboard with collected data presented. The upper widgets presents the current status transmitted from the device, for the user to be able to check the dogs current situation. Using Datacakes "rules" function, the service notifies the user though e-mail whenever the activity status is changed. Some neat, representative symbols was also added so that many different kinds of users would easily understand the content. At the bottom there are two different charts that is presenting data registered over time. This was deemed necessary for the user to be able to monitor the dogs sleeping pattern and if there might be certain temperatures that it could be affected by. ## Finalizing the design As the needed materials didn't arrive, due to shortages, before several weeks of the course had already passed, there was little to no time for exploration. Due to this the device could not be tested in the field. Instead, usage was simulated by moving the device in a way that would seem natural, according to observation, and a concept idea was documented. ### Concept idea The device would be used by attaching it vertically to the dogs chest (see fig. 12). ![](https://i.imgur.com/KJJV4MH.jpg) Fig. 12 - A concept picture of Ayla, the Golden Retriever, with the current prototype device on her chest. To do this some kind of harness, similar to the one used in fig. 13, would probably provide the best solution. A lot of dogs lay down on their side while sleeping. Therefore, the device would be attached vertically, so that the metal bead would always rest on one side when the dog is positioned upward. Then, as the dog lays down on its side, the device would be slightly tilted the other way providing different data. ![](https://i.imgur.com/PcBHKal.jpg) Fig. 13 - Dog wearing a harness with the optimal shape to use with the device. ## Final words Being a beginner, when it comes to both IoT and Python programming, this project became quite a stressful and frustrating experience. For starters, the Arduino Nano RP2040 Connect microcontroller was not considered in the course material, which i assume is because it usually isn't used. Hours where spent to figure out how to use it properly, even in collaboration with TA's, which added to the stressful aspect a lot. In the end most things were figured out, atleast to the extent of what could be expected to produce in the course. Further, the programming became quite difficult to learn, although most of it seems clear enough in the end. The Python lecture was quite heavy, but the quiz and TA guidance somewhat made up for it. As a beginner there were a lot of work to put in to understand some basics, such that seemed to be assumed the students should know. But luckily the TA's were happy to explain. In the end, the outcome of this project is satisfactory, the device created is thought to be something worthy to be proud of. The final week even sparked some joy when the hard work put into the project finally produced some desired outcomes. As for the future of this device, the process of creating an actual usable prototype may happen if time and energy allows. Some neat features could probably be added to it, for example monitoring active dreaming through the Arduino Nano RP2040 Connects internal microphone or so, but only time will tell.