# Readings from reed switch sensor
By Carl Frick (cf222pe at Linneaus University)
### Project overview
This simple project is built around a Fipy microcontroller and collects sensor readings from a reed switch, a type of digital magnet sensor, and send a prompt to the Pybytes platform. For anyone wishing to replicate this project I estimate that it sould take a maximum of an afternoon of actual work time if you follow the steps in this tutorial.
### Project objective
Originally I had a different and much more complex project in mind. This resulting project was chosen after realizing that I needed to keep things simple in order to finish on time and pass the course. The reed sensor was the first of my sensors for my original project that I managed to get to work, and so it was simply put the path of least resistance to decide to stick with it. In my original project, the reed sensor was to be used for detecting when a hatch was opened. So lets say that it represents that.
On a more fundamental level, the objective for me to sign up to this course at all (and hence it also applies to this specific project) was to get started with IoT applications. Indeed this project, even though it is very barebones, has given me a first foray into the world of IoT.
I think this project is great for anyone looking for a simple and really approachable firt foray into IoT, and things you will learn include:
- connecting and taking readings from a simple digital sensor.
- Using your hardware to send data via wifi.
- Writing a simple script in the MicroPython programming language that could be deployed on a wide variety of different microprocessors.
If you are curious about IoT but think it seems complicated and a bit intimidating, this project might be perfect for you!
### Material
All of the material used for this project is presented in the table below:
#### Bill of material:
| Component | Function | Provider | Cost |
|:-------- |:--------:| --------:|--------:|
| Fipy | Microcontroller | https://pycom.io/shop/ | ca 1200 SEK |
| PyScan | Shield / expansion board with onboard sensors | https://pycom.io/shop/ |ca 800 SEK |
| Reed switch sensor | Senses magnets in its proximity | (https://sizable.se/P.138CC/Magnetsensor) | 21 SEK |
| Breadboard | Provides means for assembly | https://sizable.se/P.TVY7M/Kopplingsdack-med-830-punkter | 53 SEK |
| Connection wires | Connects all components | [Provider](https://sizable.se/P.K2HUW/Dupont-kopplingskablar-Hane-Hane-40-pin) | 24 SEK |
|micro usb cable |connect shield to computer| I had one laying around|-|
:::info
**One important thing to consider** is that if you are purchasing directly from Pycom as I did, is that they are based in the UK, and thus you face import and duty changes if you live in the European union. That, together with rather costly shipping, meant that I ended up spending almost double the amount I would have if I would have perchased from a reseller in Sweden where I live.
:::
:::info
**Another important thing to keep in mind** is that the micro usb cable must be capable of data transfer!
:::
The fipy was one of the microcontrollers recommended by the course staff. It was presented as a solid and convenient choice that might be easier work with than e.g the comparable Helltec device (https://heltec.org/project/wifi-lora-32/) so I decided to go for it.
The PyScan was selected mainly on the basis that it was one of few shields currentlöy in stock at the time of placing my order, but also because it has various onboard sensors. Particularly the light sensor and Rfid seemed interesting to me (even though I ended up not using them in this project in the end).
The pinout diagram of the Fipy microcontroller can be seen below:

The pinout diagram of the PyScan shield can be seen below:

### Computer setup
This project was completed using Atom as the IDE (integrated development environment). There are plenty of other solid choices and really it comes down to personal preference, but this tutorial will go through how to proceed seting up and programming the device using Atom.
The Atom IDE can be downloaded at Atom's website: https://atom.io/
In order to make everything work, one must install node.js as well as the PyMakr plugin. A good guide for how to get set up using Atom can be found here: https://docs.pycom.io/gettingstarted/software/atom/#app.
Install the fipy onto the PyScan board as per Pycom's instructions (see PyScan pinout diagram in above section of this tutorial for that).
When the above steps are completed, i.e the development environment is setup correctly and the microcontroller is connected with the shield, we can plug in the PyScan to the computer by means of the micro usb cable. Atom should now connect to your device automatically.
Now you can create a new project. Usually a correct folder structure should feature a project folder containg a main.py file, a boot.py file and a lib folder which in turn contain any library files that might be referenced from your main file. However, for this project only a main.py file is needed. When you are ready to do so, the project can easily be uploaded onto the device by either cliking the "upload project to device" button on the left hand side in the termainal, or by typing "ctrl + alt + s".
:::info
**Note** I did actually not need to update any firmware on either the fipy or pyscan, everything worked for me without having to do that.
:::
### Putting everything together
Seen here below is a handdrawn sketch of the very simple circuit diagram of this project.

The fipy is mounted on the Pyscan shield, which is plugged into a computer via micro usb. The reed switch sensor is plugged into pin 14, 3V3 and GND on the Pyscan.
It's really as simple as that!
### IoT platform
I selected Pybytes as my IoT platform as it is the very simplest option. It is a cloud based platform, so no installation is necessary. It is also free of charge.
You can connect multiple devices to your account, and for each device data can easily be received and stored and visualized. The interface is very intuitive and it is very easy to get started using pybytes.
A good guide for getting started can be found here: https://docs.pycom.io/pybytes/gettingstarted/.
### The code
For this project neither a boot file or any library files are needed, i.e the code consists of only a main.py file.
Below is the entire code contained in the main.py file.
```python=
import pycom
import machine
from time import sleep
pycom.heartbeat(False)
reed_switch = machine.Pin('P14', machine.Pin.IN)
string = "magnet detected"
while (True):
if (reed_switch.value() == 0):
pybytes.send_signal(1, string)
print("magnet detected")
sleep(1)
```
Lets break the code down:
- Lines 1 to 3 import various methods that are needed for the script.
- Line 5 deactivates the heartbeat function (which has to do with the integrated led).
- Line 9 defines a vaiable string which is used further down.
- Lines 11 to 16 contain a loop to continuously check for sensor data. If the sensor is activated, i.e if a magnet is close by, the system sends a signal to pybytes with the message "magnet detected" as well as prints a message in the terminal.
### Transmitting the data
I chose to send data using Wifi. As with many other choices during this project the main reason for that was that it is the most straight forward way to send data wirelessly.
Data is sent each time the reed switch detects a magnet. The protocol used is MQTT.
Easy to follow instruction to help you get up and running can be found here https://docs.pycom.io/pybytes/gettingstarted/.
I went the route of offline provisioning my device (see step 3 in the above linked guide). This was very straight forward so I recommend doing that!
:::info
**Note:** when provisioning my device I hade to check the boxes "Erase flash file system", and " Erase NVS"! The code then needs to be reinstalled onto the device.
**Also note:** only one software can connect to the device at a time. I.e you can not have both your IDE and Pybytes open at the same time.
:::
### Presenting the data
Data is saved in the database each time a signal from my device is received in Pybytes.
Below is a picture of the Pybytes dashboard. This section of the platform gives an overview of your devices data traffic, as well as location (which I have not activated for this project).

And here below is a picture showing the signals tab. Each time your device sends a signal to Pybytes, the signal type, time, and any message contained are presented here.

### Finalizing the design
As previously mentioned, this project is very simple compared to my original ambitions, but it is fully functional. It, and the course as a whole, did give me a very nice introduction to IoT and ultimately, that was what i was hoping for. In other words, it provided a great starting point for me to continue exploring IoT and to continue develop my project further from this very basic functionality.
I could of course have done something more advanced, as was my inital ambition. A few things that I will want to experiment with are:
- Lora
- Servo motor
- PIR sensor
- MP3 player (I have this one at home: https://sizable.se/P.H6TXU/DFPlayer-Mp3-spelare-med-serieport)
None of the above have anything inherently to do with the project the way it turned out, but they are allcomponents of the project I originally wanted to do.
finally, here below is a photo of the final result.
