# FiPy - First Introduction Project Yellowstone
#### jj224dj Henrik Jonsson
#### Project description
> This project aim is to learn how to set up, configure and run a Pycom FiPy device from scratch with micropython. I will have tree sensors temperature, altitude and light.
#### Time
> I estimate the total time of 4 hours from start to the end. You need about 3 hours for installing, setting up, configuration, and connecting your pycom FiPy device, 1 hour for create account and configuration a cloud service account.
#### Why this project?
> I never worked with IoT before so I need to get a better understanding and knowledge of how it works and can be used. I want to learn how to read the data from sensors and how to present it.
#### My purpose
>This project is a start for understand the technology with IoT. My goal is more advanced projects in the future. Like build a small Bluetooth device that reads and shows the value of a glucose sensor. Maybe attached to a fridge with a magnet or like a clock on a table. This because both my 6 years old twin boys was diagnoses with diabetes type 1, 2021.
#### What insights will this give me?
>After this project I will have knowlage and understanding how IoT works, seen the diffrent teqnichs for sending small packed of data, like the LoRaWan. This will help me for further my project to come.
#### List of materials
>My materials I bought for this project comes from Electrokit.se. There was problem with hardware delivery and I got everything 2 weeks after the course had started.
FiPy and sensors bundle ,total price 1525 SEK
The bundle includes:
– FiPy with headers – Expansion board
– Antennae
– Micro USB cable
– Jumper wire
– Breadboard
– 10 x Resistor 1 kohm
– 10 x Resistor 10 kohm
– 10 x Resistor 330 ohm
– 10 x Resistor 560 ohm
– 5 x LED red
– 5 x LED orange
– 5 x LED green
– 2 x LDR (Light Dependent Resistor)
– Tilt switch
– Temperature sensor MCP9700
– Hall-effect sensor TLV49645
– Magnet
>This bundle was recommend for me to order from electrokit.se. A lot of thing I'm not using for this project but can be useful to play around with in other test project. I like to learn by doing.
## The setup
>The following step will guide you though how to start your device.


#### Step 1:
>Download the Firmware Update tool from this link https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true
#### Step 2:
> Install the downloaded file “pycom_firmware_updater_1.16.6.exe”
after the installation start the program.
#### Step 3:
> If you haven't already you need to connect your FiPy to your Pysense boards and then power everything up with the USB-cable
#### Step 4:
> Click continue on the first screen and the second screen. On the third screen, you need to select the right communication, the COM port number on your computer that connects to your device. If your not certain check your device manager in Windows. Looks something like this:

> choose “Seriell USB-enhet” (COM6 in this picture)
(you can take out the USB-cable and put it back in, then you see witch COM port that disper and show up again.
Check High speed transfer
Check Show Advanced Settings.
Set Type to development, then click Continue

>
> On next screen, set Device Type: FiPy
and Erase during update
Click continue

>
>
> Device is updating:
> 
>
> And then you are finnished, click Done.

#### ATOM IDE
> The FiPy i runs micorpython and I choose ATOM as my IDE (Integrated Development Environment). It’s an open source text and code editor for Windows, macOS and Linux developed by GitHub.
Atom is easy to start and work with.
Download from: https://atom.io/
Install Atom and follow the instruction.
Start ATOM (If you run Windows machine you may have to run ATOM as Administrator).
We need to install a plugin called pymakr. In ATOM go to Settings in the File menu and scroll down to install. Write pymakr and install.
If you are on a Windows machine you may need to install https://nodejs.org/en/ Node.js for the pymakr to run
>
>
> In ATOM you can now connect to your device with the right COM port. Try to write something in the terminal, for example 1+2 and you get 3. It’s working!
> 
>
>To start a new project you need to choose a folder in ATOM.
From the File menu, click Add new project folder and choose where to store your files.

>
>Now, you can create a new file, right click on you project folder and choos create file, name it main.py
Here you write down your code and then you upload it to your device for it to be execute. Upload project to device and it runs automatically.
>
#### Connection
>In this project we could have used only the internal sensors that came with the Pysense 2 board, but to learn how to connect external sensors and use a breadboard we need to separate the FiPy from the Pysense board.

>Pysense 2.0X
Lauout for pysnese 2.0x can be found at
https://docs.pycom.io/datasheets/expansionboards/pysense2/
Connect the cables as following:
>
>Yellow J29 to Pysense P1
Green J28 to Pysense P2
Red B30 to Pysense 5V
Black B29 to GND
Red A29 to red plus rail
Blue A28 to blue minus rail
Red cable from minus rail to Psense 3v3
Purple cable A26 to Psense P22 (for Pysense sensors)
Green cable A25 to Psense P21 (for Pysense sensors)
>
>Red + rail to B2 (breadboard)
Resistor 10kOhm D2 to G2
Yellow cable H2 to B20
Light Depedent Reistor I2 to I4
Blue cable H4 to B28
#### Platform
> I tried two different platform for communication. First I set up LoRaWAN on Helium and it worked fine, but where I was going to stay the coming weeks there is no LoRaWan so I needed to change and went for WIFI.
>
> I send all my data to the cloud and I choose Datacake to handle my data, it’s free for up to two devices. Then they have a paid subscription but for this project the free version will be enough.
>
>Datacake free version include
7 days data retention
500 datapoints / day
max. 2 per workspace
Cancel any time
>
#### The code
>from mqtt import MQTTClient
import time
import ujson
import machine
from machine import ADC
from machine import Pin
import pycom
from pycoproc_2 import Pycoproc
from SI7006A20 import SI7006A20
from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE
>
>pycom.heartbeat(False)
pycom.rgbled(0x0A0A08)
>
>LightSensorPin = 'P16'
lightPin = Pin(LightSensorPin, mode=Pin.IN)
adc = ADC(bits=10)
apin = adc.channel(attn=ADC.ATTN_11DB, pin=LightSensorPin)
>
>py = Pycoproc()
>
>if 'pybytes' in globals():
if(pybytes.isconnected()):
print('Pybytes is connected, sending signals to Pybytes')
pybytes_enabled = True
>
>mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals
si = SI7006A20(py)
print("\n")
topic_pub_temp = str(si.temperature())
topic_pub_altitude = str(mp.altitude())
topic_pub_light = val = apin() # read an analog value
>
>def sub_cb(topic, msg):
print(msg)
>
>client = MQTTClient('1234henrik5678',
'mqtt.datacake.co',
user='764fb5180928bf0bdecd8ad529625d256ab6cc30',
password='764fb5180928bf0bdecd8ad529625d256ab6cc30',
port=1883)
>
>client.set_callback(sub_cb)
>client.connect()
>print('connected')
>
>my_topic = "dtck-pub/mypycom01/d37cc1b9-1df5-41e6-a95e-fd3fdaa3a1bf/TEMPERATURE"
my_topic2 = "dtck-pub/mypycom01/d37cc1b9-1df5-41e6-a95e-fd3fdaa3a1bf/ALTITUDE"
my_topic3 = "dtck-pub/mypycom01/d37cc1b9-1df5-41e6-a95e-fd3fdaa3a1bf/LIGHTSENSOR"
>
>while True:
topic_pub_temp = str(si.temperature())
topic_pub_altitude = str(mp.altitude())
topic_pub_light = val = apin()
client.publish(topic=my_topic, msg=str(topic_pub_temp))
client.publish(topic=my_topic2, msg=str(topic_pub_altitude))
client.publish(topic=my_topic3, msg=str(topic_pub_light))
client.check_msg()
print(topic_pub_temp + " Celius ",topic_pub_altitude + " Meter ",topic_pub_light, "LUX ")
time.sleep(300)
#### Transmitting the data
>The data is sent every five minutes through WIFI.
>As mention earlier the lack of LoRaWAN made me go with WIFI.
#### Which transport protocols were used
>MQTT, it's "a lightweight messaging protocol that is ideal for sending small packets of data to and from IoT devices via WiFi."
To read more about MQTT visit https://docs.pycom.io/tutorials/networkprotocols/mqtt/
#### Presenting the data
>Screen shot from ATOM

>
>Screen shot from Datacake

#### How often is data saved in the database
>The data are being sent every 5 min and are stored for 7 day. There is no trigger for this, just a while loop.
## Finalizing the design
>Improve for the design: This is quite a simple design and the programming could be more advanced, is straight forward with no special code.
>To build a case around this I would remove the breadborad and solder everything together. I would say this projectis just something to get at start with with IoT.
This is how project Yellowstone looks like

>Notes: I have altered the breadboard picture with the wiring from https://hackmd.io/@lnu-iot/By-930cTu
and I have used code example from the lab moments to put everything togather