# LED indicating temperature
**Intruduction**
Name: Linnéa Höst
Student credentials: lh223nz
For my first project I have done a themometer which trigger the LED to light up when needed.
The reason is to alert that temperature is higher then we want and a fan should be started manually. The purpose of this is to notifying when the temperature has higher value than wanted by lighting up LED dioder. It will make the device to light either a green diod led (if temperature is okay) or a red light (when the temperature is too high). This project will not send a message in that way only in the REPL interface. The time to make this is hard to say and depend on how much you know from before. For me this took about 15 h included videos and slaming my head against the wall when the code not working as I wanted it to.. But It was worth it!
**Material**
For this project we need some hardware. Down you see which I have used, where you can buy it and the cost for it.
| Hardware | Cost |
| ----------------- | ---------------------- |
| PyCom makr | €16 at Pycom |
| PyCom lopy4 | €34.95 at Pycom |
| PyCom antennae | €9.00 at Pycom |
| temperature sensor MCP9700 | 6 SEK at Electrokit.com|
| breadboard | 47 SEK at Electrokit.com|
| LED diod | ca: 5 SEK at Electrokit.com|
| Some wire | ca: 28 SEK at Electrokit.com|
**Computer Setup**
The flashing of the firmware is done on Pybytes due to the connection to the platform wich is very neat(more information about the platform below). The Flashing means that the old code will be deleted on the device and will be updated to the newst update if you make that choice.
The IDE (Integrated development environment) I'm using for the project is Atom. I choose Atom beacuse I have used it before and think it is easy.
In Atom there is a pymakr plugin which you download. When you have the Pymakr plugin, a window in the bottom of atom will show. This is the REPL interface, the directly connection to the device. On the side of this window there are some bottons that you will use. Upload project to the device is one of them and that is how the code will be uploaded to the device. You can run your code on the "run this file"-button without uploading the code directly.
**Circuit diagram**

Temp = Temperature sensor
EB = Expansionboard
GND = Ground
ADC = Analog to digital conversion
DAC = Digital to analog conversion
Temp
Put the red wire in 5v5 on the EB and the other end to Temp+
Black cable from Temp- to GND
And the yellow wire from pin 16 in EB, wich is a ADC.
Red diode. 1,65v
Take another yellow wire and put it in pin 21 on the EB.
Pin 21 is a Dac, wich is used to output analog values like a specific voltage. In default 3,3v
Take the other end of the yellow wire you put in pin 21 and attach an 330ohm resistance.
Connect the free end of the resistance to the + of your diode and the - to GND.
Green diode. 2v
Take a gray/white wire and put it in pin 22 on the EB. Pin 22 is also a Dac. In default 3,3v output.
Take the other end of the gray/white wire you put in pin 22 and attach an 330ohm resistance.
Connect the free end of the resistance to the + of your diode and the - to GND.
You're all set up!
**Platform***
The platform I have choosen to work with is Pybytes since it is a very neat way to connect your Pycom device since it is already integrated. The Platform is cloudbased and it is free of cost, you just have to create a account on the webbsite. It is very user-friendly!
It is a great platform that is recommended to use for your first project due to the simplicity. The platform makes it easy to use when collecting data and showing the result on a dashboard.
To conncet your device on Pybytes, you go to device and add a device via USB. Enable network and prioritize them. I choose to work with Wifi since I have trouble connecting to LoRa.
Then you activate the device with firmware updater which mean that you open firmware updater and use the generated token from Pybytes to conncet to the platform.
**The Code**
This is the code I have been using for this project.
import machine
import time
adc = machine.ADC()
apin = adc.channel(pin='P16')
def send_temp_data ():
while True:
millivolts = apin.voltage()
degC = (millivolts - 500.0) / 10.0
print('Millivolts '+ str(millivolts))
print('Degree in celcius ' + str(degC))
if degC <= 25:
print('Temperatur is okay!')
dacpin = machine.DAC('P22') #green diod
dacpin.write(1) #set output voltage to 100% of 3.3V.
time.sleep(5)
else:
print('It is too hot, please start fan.')
dacpin = machine.DAC('P21') #red diod
dacpin.write(0.7) #set output voltage to 70% of 3.3V.
time.sleep(5)
pybytes.send_signal(2, degC)
dacpin.deinit()
time.sleep(10)
send_temp_data()
**Transmitting the data / connectivity**
In this case the data is sent every 10 second and I am using wifi as my wireless prtocol and the transport protocol are MQTT.
MQTT (Message Queuing Telemetry Transport) makes it possible to send data from a sensor to a platform like Pybytes in this case. The MQTT is good for IoT project since it is an easy way of sending data and do not need lots of bandwidth.
In the code I built a function that will go on forever if I don't stop it when I call for it.
It include letting the machine know which pin we are using and the how to calculate the value to celcius from the thermometer.
The function include an "if-else"-statement. If the value is above 25, the temperature is to high and the red diod should light up to notify that a fan should be turned on. If temperature is 25 or below the green diod should light up.
The row below is the part in the code that will send the value from the thermometer and send it to Pybytes if you go to tab "Signals".
pybytes.send_signal(2, degC)
This mean that the temperature in celcius will be sent to signal 2 in pybytes and can be presented there.
**Presenting the data**
On the Pybytes platform you have a tab called dashboard where you can see that data have been received.

The data is saved as often as the data is sent to the Pycom service.
You are able to create display of your data on the Pybytes platform which can be shown in barchart, table or like in this case linechart.

This can also be added on the dashboard and the dashboard can be organized after your personal preference.
**The project is done!**
My first project is done! It is not an advanced project but for me it was important to get a grip of what you can do, how to do it and experiment!


The original idea was to connect a fan which should turn on when the thermometer measured a higher degree than we want. Then we could exchange one of the LED to a fan but then we also need to change some code. But this is a easy and fun way to start understanding IoT and grow some interest.
Have fun and be creative!