# Temperature sensor project # Nilas Sparrström - ns222ty **Project overview** This project connects a temperature sensor to a LoPy4 and plots the temperature with a specific interval **Time approximation** 8 hours **Objective** The aim of this project is for the user to learn more about the principle of IOT devices, sensors and protocols. It measures the temperature in one place and lets the user read it somewhere else. All done with limited resources and knowledge. | Material | Purpose | | -------- | -------- | |Temperature sensor - MCP9700 TO-92|Sends a voltage that corresponds to the temperature where it is located | |Development board - PyCom LoPy 4 | Reads the voltage, computes and sends the temperature data to the database | |Expansion board - PyCom |Gives the LoPy a USB connector and easier access to the pins| |Connection wires|Connects the development board to the sensor | | Breadboard | Makes the connection easier and the project modular for temporary use| All material was part of a kit from electrokit.com and cost 995 SEK **Computer setup** The IDE chosen was Visual Studio which can be found for free at visualstudio.microsoft.com The plugin pymakr needs to be installed and is used to send code to PyCom Node.js also needs to be downloaded and installed. This can can be downloaded from nodejs.org The device also needs to be updated and registered at pybytes.pycom.io/ **Setup** - Cicuit diagram ![](https://i.imgur.com/ePBehwH.jpg) - Setup ![](https://i.imgur.com/SPNZF6T.jpg) **Code** ``` import time import pycom import _thread from mqtt import MQTTClient import ubinascii import hashlib import machine pycom.heartbeat(False) with open('config.json') as f: config = json.load(f) 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(): try: adc = machine.ADC() # creating ADC temp = adc.channel(pin='P20') # reading data temp=temp()*1.1/4096 # computing to voltage temp=temp-0.5 # calibrating temp=temp/0.01 # computing to temperature c.publish(topic_pub,'{"nilastempsensor": {"temp":' + str(temp) + '}}') # publishing the data print('The temperature is:' + str(temp)) # printing the data blink_led() except (NameError): print('Failed to send due to NameError!') except (ValueError): print('Failed to send due to ValueError!') except (TypeError): print('Failed to send due to TypeError!') topic_pub = 'devices/nilastempsensor/' topic_sub = 'devices/nilastempsensor/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 c = MQTTClient(client_name,broker_url,user=config['user_mqtt'],password=config['pass_mqtt']) c.set_callback(sub_cb) c.connect() c.subscribe(topic_sub) _thread.start_new_thread(interval_send,[10]) # sending the data with a 10 second interval ``` **Transmitting the DATA** The Data is transmitted over wifi with the transport protocol 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/dInboTu.png) **Final result** ![](https://i.imgur.com/9u96DbC.png) This is how the temperature is visualized with MQTT. The fluctuation of the temperature could be due to the temperature changing or the temperature sensor having a low accuracy.