# Project report - Push button sensor
### Author: Simon Gebrenegus (sg223ce)
## Introduction
This project report will be a tutorial on how send data from a push-button-sensor to an internet application with the help of IoT devices.
The amount of time it will take for a beginner to do this example project is approximately 60 to 90 minutes.
## Objective
This specific project was chosen because a simple project that was created by myself was the aim. I'm a beginner and wanted to do something basic to build on in the future.
The purpose this project serves for a beginner in the IoT-field is to get started and get a basic understanding of building your own IoT-project.
The push-button-example can be used for example if you want to documentate when and how often you take breaks when studying/working. If you remember to push the button everytime you take a break, you can get a good overview of how many times and when you take breaks.
The insights the project will give is basic knowledge in electronics, wiring, microcontrollers, expansion boards, programming, and IoT aplications.
## Material
**Pycom - LoPy4 with headers (microcontroller) from Electrokit**

The LoPy4 is a microcontroller which works as a computer in a single integrated circuit. It is dedicated to perform one single task and cointains memory, programmable input/output peripherals, and a processor. This specific microcontroller is programmed by MicroPython.
**Pycom - Expansion board from Electrokit**

The expansion board is used for adding or expanding some sort of functionality to the computer (the microcontroller in this case). You can see it as upgrading a computer.
**Breadboard from Electrokit**

The holes in the breadboard let you insert electronic components to prototype an electronic circuit in a easy way.
**MicroConnect USB A - USB Micro-B from Electrokit**

For connecting the LoPy4 to the computer or something else for powering the device.
**Push Button (41015723) from Electrokit**

The actual push button which will be where the actual data will come from and be sent to to the internet application.
**3pcs Jumper Cables M/F from Electrokit**

Three cables that will be used for voltage, transferring the data, and the ground (GND)
**Computer**
The computer will be used for programming the device and creating the dashboard which will visualise the data.
**Wi-Fi connection**
Wi-Fi will be used for communicating between the Pycom devices and the computer.
Everything (without Computer and Wi-Fi connection ofcourse) is bought from Electrokit. The push button is from a sensor kit that has 25 different sensors in it and costs 239 SE. The rest of the material was bought from a special package for the course called "LNU - 1DT305 Tillämpad IoT - LoPy4 and sensors bundle" and the cost was 796 SE.
## Computer setup
The IDE (integrated development environment) chosen for this project was Atom.
So the first thing you should do is to install Atom at https://atom.io. Another IDE that can be used is Pymakr. NodeJS should be installed in order to run Pymakr, at https://nodejs.org/en/
Create a project in Atom by creating a folder with the project name "Button". In the folder you create a file named "main.py" where the code will be written. The programming language is MicroPython and the code will be uploaded to the device by clicking on "Upload project to device". Be sure that the device is connected and that the right project is chosen. Check the red circles in the picture below.

## Putting everything together
Three wires are used to connect the sensor (via the breadboard). One wire is for power (the middle wire from the sensor) which should be connected to 3V3. One wire is signal which is for transmitting the data and should be conencted from the side where it says "S" to P16 on the pycom device. The third wire should be connected from the minus side of the sensor to GND.
Connect the power line (middle) and ground to +5V and GND respectively. Connect signal (S) to pin 3 on the arduino.
 

## Platform
The platform that will be used is called Pybytes. It is based on a cloud and is free. You need to create an account at https://pybytes.pycom.io, which will be where the dashboard for this project will be found.
This platform can be used for updating the firmware for the pycom device, visualising the data etc.
## The code
```
from machine import Pin #IMPORTING LIBRARY CALLED PIN WHICH IS STORED IN THE PYCOM DEVICE
import time #import library called time
import pycom #import library called pycom
pycom.pybytes_on_boot(True) #Making sure the "connection" to pybytes is activated
p_in = Pin('P16', mode = Pin.IN) #We are telling the push button is connected to P16, and naming the variable "p_in". p_in can get value 1 (when the button is not pressed) and 0 (when the button is pressed). The data will be transmitted from P16.
while(1): #While true
pycom.heartbeat(False) #Turning the heartbeat LED off.
val = p_in() # We are renaming p_in() to val
if val == 1: #If the push button is not pressed.
pycom.rgbled(0x7f0000) # LED should glow Red
print("NOT PRESSED") #Print "NOT PRESSED"
pybytes.send_signal(2, "NOT PRESSED") #Send a signal to pybytes that says "NOT PRESSED"
else: #Else if val is not == 1 (which means it's 0)
pycom.rgbled(0x007f00) #LED should glow green
print("PRESSED") #Print "PRESSED"
pybytes.send_signal(3, "PRESSED!") ##Send a signal to pybytes that says "PRESSED"
time.sleep(1) #Pauses for one second before next loop starts
```
## Transmitting the data
As the code implies it pauses one second before it sends the data again, so the data is sent every two seconds.
The wireless protocol used for transmitting the data is Wi-Fi.
MQTT is the transport protocol that is used.
## Dashboard

This is where the data will be visualised. As you can see on this screenshot the data is saved there for 24 hours. It will be visualised by time series and bar plots.
## Finalizing the design

Above on the dashboard you can see when it recieved the data and how much data it is based on which time of the day.

The signals is the data and is saved in the database forever. When I pressed on the Push Button you can see that data named "PRESSED" was sent, and when I didn't push the button, data "NOT PRESSED" is sent.


I learnt a lot during this project. I tried a couple of sensor examples before the project and as my project I wanted to do something on my own where I have to write or change the code by myself. Therefore I chose a very basic example. This example isn't so practical because just using a push button that sends "PRESSED" and "NOT PRESSED" is not so creative but the aim was to just create something as a beginner and send some data.