# Temperature sensor project # Adam Khalif - ak224md **Project overview** This project uses a LoPy4-device togehter with a temperature sensor to monitor the temperature. The data is sent to the cloud using wifi. **Time approximation** 10 hours **Objective** This project is a good way to start experimenting with IoT and learn more about sensors and protocolls. The purpose of the project is to meassure the temperature of for example a room and be able to read the data from somewhere else. The project will give insights of how a simple IoT device can be set up with a small budget and limited knowlege and give inspiration to other similar solutions and projects. **Material** | Material | Function | | -------------------------------- | -------------------------------------------------------- | | Temperature sensor MCP9700 TO-92 | Sends a voltage to the LoPy depending on the temperature | | USB to micro USB cable | Supplies the LopY with voltage and upload code | | 5 Connections wires | Connects the temperature sensor with the LopY | | PyCom - LoPy4 | Reads sensor, computing and transmitts data | | Pycom - Expansion board | USB and wire connections | | Breadboard | Makes it easier to connect sensor | All the material was bougth from elektrokit.com and the total cost was 995 kr. **Computer setup** The chosen IDE for the project was ATOM. It can be downloaded from atom.io. Pymkr needs to be installed via atom. This plugin is used to upload code to the PyCom. Node.js also needs to be downloaded and installed. This can can be downloaded from nodejs.org The device also need to be registered have a firmware update. This is done by regestering an account on pybytes.pycom.io/ and following the guidlines on how to get started. **Circuit diagram** ![](https://i.imgur.com/gvFYV0a.jpg) **Platform** The choosen platform that I recommend using is MQTT-explorer, which is cloud based and very easy to get startet with. Another possible platform that is easy to use is the Pybytes platform. I also tried to use TIG-stack which also is a neat way to present the data but a little more complicated. **Code** ``` import time import pycom import _thread from mqtt import MQTTClient import ubinascii import hashlib import machine pycom.heartbeat(False) def sub_cb(topic, msg): print((topic, msg)) def interval_send(t_): while True: send_value() time.sleep(t_) def blink_led(): for n in range(1): pycom.rgbled(0xfcfc03) time.sleep(0.5) pycom.rgbled(0x000000) time.sleep(0.2) def send_value(): adc = machine.ADC() # create an ADC object apin_temp = adc.channel(pin='P20') # reads data from pin temp = apin_temp()*1.1/4096 # compute to voltagee temp = temp - 0.5 # calibration temp = temp / 0.01 # compute to temperature try: c.publish(topic_pub,'{"adams_sensor": {"temp":' + str(temp) + '}}') # publish data on topic print('Sensor data sent ... Temp: ' + str(temp)) # prints the data blink_led() except (NameError, ValueError, TypeError): print('Failed to send!') topic_pub = 'devices/adams_sensor/' topic_sub = 'devices/office-sens/control' broker_url = 'sjolab.lnu.se' client_name = ubinascii.hexlify(hashlib.md5(machine.unique_id()).digest()) # create a md5 hash of the pycom WLAN mac username = "iotlnu" # for mqtt password = "micropython" c = MQTTClient(client_name,broker_url,user=username,password=password) # creat mqtt client c.set_callback(sub_cb) c.connect() c.subscribe(topic_sub) _thread.start_new_thread(interval_send,[10]) # send the data with a 10 seconds interval ``` This is the code that needs to be uploaded to the PyCom device via Atom. The important stuff happens in the function called "send value". The signal is read from pin 20 on the Lopy. The resolotion is 12 bits which gives a digital value between 0 and 4095 and the pin can read voltage between 0 and 1.1 V. So by multiplying with 1.1 and dividing by 4096 the voltage output from the sensor is yielded. The sensor will give 0.5 volts output when the temperature is 0 C. This means that 0.5 has to be subtracted for the sensor to be calibrated to the celsius scale. The sensor will then have a linear behavior and give 0.01 volt for every degree celsius. This is why the value is divided by 0.01. **Transmitting the data** The data is sent every 10 seconds. Wifi is used as the wireless protocol and the transport protocol used is MQTT. **Presenting the data** The data can be visualized using the tool MQTT explorer. It can be downloaded from mqtt-explorer.com. Use the following information to connect. ![](https://i.imgur.com/R3jkayi.png) **Final result** ![](https://i.imgur.com/QmNBPlV.png) This is how the temperature data will look like when it is uploaded. The data is uploaded every 10 seconds. As seen in the picture it looks like the temperature is changing a lot. This can be considered as noise due to low precision in the sensor readings.