# How to set up your first IoT project.
Alexandra Jernberg aj224gj
Let´s learn the basics on how to get your first IoT project up and running with pycom´s hardware and the temperature sensor DS18B20.
Level: ★ Beginner
Time needed: Can not estimate because of problems that can occure. Between some hours up to a week, depending of your computer skills and luck :)
Cost: ≈67€

---
**Objective**
By doing this project I wanted to learn about the basics with setting up an IoT project in a simple way to further on dive deeper in to this world of creativity. But let´s start small. Hopefully you as a reader will get inspired to set up your first project too!
My tutorial is a step-by-step guide for this perticular project where we will use a temperature sensor and in the end recive the sensor data in a platform. I have also shared some thoughts and tips how I manage to set up my project. The steps are more or less from pycom´s own website and will link you there in the steps for you to read more if it´s something you want to read more about.
Because I where totally new in the field of IoT and because I ran in to some problems with firmware updates, I have done some check boxes for you to have an overview where you are in your project if you come off track.
The pycom documentations is a very helpful tool for you so save the link in your web browser and don´t forget to use it!
# https://docs.pycom.io/

---
# Things you need in this project
You will need the six explained things below to follow the steps in this tutorial. The links provided are just examples from where I bought the parts. The cost of this project is approximately 67€.
I also made you a list with check boxes so you can make sure you have ordered every thing you need.
- [ ] Pycom Expansion board 3.0
- [ ] Micro-USB cable
- [ ] Pycom LoPy4
- [ ] Temperature Sensor DS18B20
- [ ] Breadboard
- [ ] Hook-up Wires

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
| Expansion board 3.0 | Expansion board | 16€ | https://pycom.io/product/expansion-board-3-0/ |

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
| Micro-USB cable | To connect the expansion board to your computer | 5€ | Probably you have one at home|

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
| LoPy4 | A development board with wi-fi that will be connected on the expansion board | 35€ | https://pycom.io/product/lopy4/ |

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
| Temperature Sensor DS18B20 | Singel-bus digital teperature sensor, a LED and a resistor | 3€ | https://www.electrokit.com/en/product/temperature-sensor-ds18b20/ |

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
|
Solderless Breadboard 400 | Used for an easier way to connect sensors with wires to Pycom device | 5€ | https://www.electrokit.com/en/product/solderless-breadboard-400-tie-points/ |

| IoT Thing | Specification | Cost | Link |
| ------------------------------------ | --------------------------------------------------------------------- | ------------------------------- | --- |
|
Hook-up wires w. pins for breadboard | Wires to connect the sensor on breadboard to Pycom device | 3€ | https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/ |
It´s possible to do this project without the breadboard and hook-up wires but it makes it easier to use these parts, so I have included them i my project. The parts does not costs alot and you will anyway need them if you gonna connect more sensores in future projects.
LoPy4 has the advantages to be connected to LoRa network but where I live I have no connection to a LoRa gateway I learned later in my project, so I have only worked with wifi in this project. If you want to use LoRa instead of wifi you also need to buy an antennea. But this tutorial will only cover the use of a wifi connection. If your aim to just use just wifi there is a cheaper choice of developing board called [WiPy](https://pycom.io/product/wipy-3-0/).
---
# **Hardware and computer setup**
**STEP 1 Update the Expansion board 3.0**
Before connecting your module (LoPy4) to your Expansion Board 3.0, it´s recommended to update (flash) the firmware on the Expansion Board. You find the instructions on how to do this [in this link](https://docs.pycom.io/pytrackpysense/installation/firmware/)
For some reason my expansion board could not compleat the update. I could continue this project without an updated firmware anyway. Hopefully you will manage to complete the update, or you deside to skip that step.
- [ ] Pycom Firmwared installed
- [ ] Expansion board update compleated
**STEP 2 Connect the LoPy4 to your expansionboard**
Unplug the expansion board from your computer.
Make sure that you connect the LoPy4 in the right direction on the expansion board. The text "Pycom LoPy4" on the Lopy module should aling the text "Pycom" on the expanison board.

**STEP 3 Create an account in Pybytes plattform.**
* Create you acount [here](https:pybytes.pycom.io/)
* Add your device [follow this guide](https://docs.pycom.io/pybytes/connect/)
* Provisioning [follow this guide](https://docs.pycom.io/pybytes/connect/quick/)
It is here in [pybytes platform](https://pybytes.pycom.io/) that you later on will recive your sensor data and get the data visualised in a dashboard.
- [ ] I have created an acoount
- [ ] Provisioning completed

**STEP 4 Download ATOM**
ATOM is an IDE (Integrated development environment) which is your text editor where you write your code and upload your code to your pycom device. I have choosen ATOM because of it´s easy to use.
Download Atom [here](https://atom.io/) and install.
- [ ] Atom is installed

**STEP 5 Install Pymaker plug-in in ATOM**
When you have installed ATOM on your computer you need to install the Pymakr plug-in i ATOM. Pymakr enables you to communicate to your Pycom board using the build in command line REPL (the REPL interface appears as a new window in the bottom of the ATOM window.) In REPL interface you can run a single file to your board, upload your entire project or directly type and execute commands.
* To get Pymaker work you first need to [install Node.js](https://nodejs.org/en/)
After Node.js is installed you can go back to Atom and follow this steps to install Pymakr.
* Settings -> Install Packages-> search for Pymakr -> install Pymaker.
- [ ] Node.js installed on your computer
- [ ] Pymaker package is installed in Atom


*REPL interface window*
**STEP 6 Test your device and make sure all works fine**
To make sure your device are connected to wifi and that everything works correctly you are now ready to do your first little project in Atom by follow this [guide](https://docs.pycom.io/gettingstarted/programming/first-project/) on how to make your pycom device blink as a traffic light.
---
# **Putting everything together**
In step 2 you connected your LoPy4 module to the expansion board. Now it´s time to connect the sensor to the breadboard and connect the wires between the breadbord and the Pycom device.
As a told you earlier, when you are using just one sensor a breadboard is not necessary, but easier to work with. Learn more about how a solderless bredboard works [in this link](https://protosupplies.com/guide-to-solderless-breadboards/)
#### **Temperature sensor Module**
I have used the temperature sensor module DS18B20 which allows ambient temperature measurment using digita serial bus.
This sensor is easy to use and comes with a [user guide](https://www.electrokit.com/produkt/temperatursensor-ds18b20/) which is very handy for a beginner. I noticed that not all sensors provide a user guide so be aware for that when you buy your next sensor, I find it very useful to not be needed to google for all information when your head is already full of new things to learn.


**Step 7 Connect the sensor**
* You connect the ground pin (yellow left line) to the GND on the expansion board
* Followed by the power line (green middel line) to 3V3
* And then the signal pin (right red line) to P10.

Now it should look like this!
- [ ] Yeay, it does!
---
# **Platform**
You can choose between different online platforms to have your sensor data preseted, like the free option [Unidots STEM](https://ubidots.com/stem/), and a pay option [google cloud IoT Core](https://cloud.google.com/iot-core/?utm_source=google&utm_medium=cpc&utm_campaign=emea-emea-all-en-dr-bkws-all-all-trial-e-gcp-1009139&utm_content=text-ad-none-any-DEV_c-CRE_253515259856-ADGP_Hybrid+%7C+AW+SEM+%7C+BKWS+~+EXA_M:1_EMEA_EN_IoT_IoT+Core_Keyword+Launch-KWID_43700053286084085-aud-606988878374:kwd-323286818831-userloc_9062428&utm_term=KW_google%20cloud%20iot-NET_g-PLAC_&&gclid=Cj0KCQjw9IX4BRCcARIsAOD2OB1LW7SI7vdTyMWVvxNFZVTHUJhYVOlvcQNi5XVbPezRFVZEqvzACX8aAiUbEALw_wcB).
I started to use Unidots because of it´s ´should be easy to use, but I failed to syncronice with Atoms. One explanation for this could be that I could´n update the firmware on my expansion board, but I really don´t know and my dead line for the project was close so I decided to sort that out after I have finished this project.
In the end I choosed [Pybytes platform](https://pybytes.pycom.io/) where we made our account so you will have your data presented in the same place, and it´s easy to use and it worked for me. To learn more about Pybytes platform here is a link to [Pybytes forum](https://forum.pycom.io/category/43/pybytes-iot-platform).

---
# Upoad your code
Now it´s time to go back to Atom and upload your code to your device.
**Step 7 Open Atom**
In the Project window (on the left side in Atom) follow this steps.
1. Create a **New Project Folder** and name this **"Temp Sensor DS18B20"**
2. Within **"Temp Sensor DS18B20" folder** create a **New Folder** and name it **"lib"**.
This is your library folder where we need to paste a code that can "speak" with the sensor.
3. Within **"lib folder"** create a **New File** and name it **["onewire.py"](https://gist.github.com/alexandrajernberg/3dbba65e277a1b29fbbe5ff3a8e1c119)**
Copy the code from this [**Github link**](https://gist.github.com/alexandrajernberg/3dbba65e277a1b29fbbe5ff3a8e1c119) and paste the code in your onewire.py file
4. Click on your **"Temp Sensor DS18B20"** and create a **New File** and name it **["main.py"](https://gist.github.com/alexandrajernberg/2713b794919a14a5fe218eb6429289d3)**
Copy the code from this [**Github link**](https://gist.github.com/alexandrajernberg/2713b794919a14a5fe218eb6429289d3) and paste the code in your main.py file.
This is how your stucture should look like this.

Now you will upload the project to your LoPy. Mark your **Project file Temp Sensor DS18B20** in the project window and press the **upload button** (red mark in the picture below) in the **REPL window**.
If you’ve done everything correctly, you should start seeing your data printed in the REPL window.

- [ ] I have manage to upload the code
**The code**
This is the [main.py](https://gist.github.com/alexandrajernberg/3dbba65e277a1b29fbbe5ff3a8e1c119)
```
import pycom
import time
import socket
import time
import ubinascii
from machine import Pin
from machine import Timer
from onewire import DS18X20 #Library for dealing with DS18X20 sensors
from onewire import OneWire #Library for Onewire
ow = OneWire(Pin('P10')) #Create an object for interfacing through onewire on P10
timebetweentransmissions = 1800 #the time in seconds between every transmissions
temp = DS18X20(ow) #Create on object for temperature from DS18B20 with onewire
owtemp = temp.read_temp_async() #Read the temperature value from DS18B20
temp.start_conversion() #This is called after reading the temperature
roundedtemp = ("%.2f" % round(owtemp, 2))
print("my rounded time", roundedtemp)
print("aj224gj: DS18B20 - Temperature at boot-up: %d C" % owtemp) #Output the DS18B20 temperature at boot-up
#send signals with values to pybytes, TODO: use a better encoding to reduce size of payload
pybytes.send_signal(1,owtemp)
chrono = Timer.Chrono() #Create a Chrono object from Timer to measure time
chrono.start() #Start measuring time
#Main loop for sending measurements, time based
while True:
elapsed_seconds = chrono.read() #Get the elapsed time since start of chrono (or reset)
if elapsed_seconds < timebetweentransmissions: #Check that the elapsed time is below threshold
time.sleep(1) #Sleep before next loop
elif elapsed_seconds > timebetweentransmissions: #Timer threshold has been reached, send measurements to PyBytes and output to terminal
print("Idle time out limit reached after %f seconds, sending values to PyBytes" % elapsed_seconds)
owtemp = temp.read_temp_async()
pybytes.send_signal(1,owtemp)
temp.start_conversion()
time.sleep(1)
chrono.reset()
```
* A measurment is taken every 30 minutes
* The data is sent over Wifi using the MQTT protocol.
* The data is sent to the Pybytes platform
---
# **Presenting the data**
Now you can go to [Pybytes platform](https://https://pybytes.pycom.io/) and see if your data is being transmitted in the dashbord.

Here you can see your dashboard and your sensor data that is transmitted from your device. It is not much more then the temperature values on a time line. But that is what we wanted with the project.
Learn more about Pybytes platform [here](https://docs.pycom.io/pybytes/).
---
# **Finalizing the design**

So, now we have worked us through our first IoT experience!
The goal was to understand all the steps, the devices, firmwares and softwares needed and put everything together and get out some sensor data in Pybytes platform. I hope this guide helped you manage to set up your project.
**Final thoughts**
The project have given me an insight on how the world of IoT works. I was a compete beginner and it was not an easy road I will adimt. But I manage to complete the project thanks to very kind people and I really appreciate their help.
Pybytes platform works just fine for a simple project like this. But I learned a lot of other choices of platforms and I will have an advantages of that knowledge in my future projects.
I now looking forward to learn more about coding and to develope a new fun IoT project.
---
# 3D printed case

I designed a neat case for the expansion board to protect the pins that sticks out in the bottom.
---
---