# Backpack temperature notification * ## Name: Kanayo Yamashita (ky222br) * ## Project overview: This project is to make a device that notify me high temperature in the backpack so that I can protect my laptop from heat. * ## Approximate time required : 5 hours * ## Objective I chose this project because I wanted to protect my laptop from heat while I was travelling with it in my backcpack during the summer. The device I made notify me on Dicsord when it detects certain temperature so that I can know if I should change place to cool down the temperature in my backpack to protect the laptop. * ## Material * ### List of material 1. Pycom Lopy4 ... is a development board containing microptocessor. 2. Expansion board ... With this, Lopy4 can expand its function. 3. USB cable ... connects Pycom Lopy 4 to a computer or a powerbank. 4. MCP9700 analog temperature sensor ... measures temperature. 5. Breadboard ... is a board to hold electronic components wired together. 6. Jumper wire (3pcs) ... is a wire used for making connection between components. 7. Powerbank ... is a mobile battery. * ### Place of purchase These materials listed above are all purchased on the internet (elektrokit.com) except powerbank. The price except the powerbank is 949 kr. including other components such as an antenna, LEDs and a hall-effect sensor etc. Powerbank can be purchased at a electronics store. * ## Computer setup * ### Step1: Connect Lopy4 to the computer Insert Lopy4 into the expansion board. Connect them to the computer with USB cable. * ### Step2: Install IDE on the computer After the device (Lopy4 with expansion board) is connected to the computer, install **[Node.js](https://nodejs.org/en/)** on the computer. Choose the LTS version. Next, install an IDE on the computer to interface with the device. This project uses Atom. Open Atom, Go to `Setting > Install`, Search for `Pymakr`, select the official Pycom Pymakr Plugin and install it. Then the device should automatically show up in the terminal of Atom. The connection is succeeded if you can see three arrows (>>>) in the terminal. You may need a few seconds to configure it for first time use. In case the connection is not done, try to disconnect your device and then connect again. On IDE you can write codes to talk to your device. You can write codes in the editor and hit "uploade project to device" so that codes are uploaded to the device. ![](https://i.imgur.com/EDtAvSy.png) * ### Step3: Create a new project folder and files. 1. Make a new folder On Atom, open a new folder by clicking `File > Open Folder`. Create a new folder and name the project. Select this folder and open it. 2. Add a new file called `lib` 3. Add a new file called `main.py` and `boot.py`. Place these two files outside of `lib`. ![](https://i.imgur.com/l0LOfsF.png) * ## Connect devices together 1. Prepare the Lopy4 device, a MCP9700 analog temperature sensor, a breadboad and 3 Jumper wires. It is recommended to use different colors of jumper wires to avoid connection failure. 2. Connect these items together as follows. ![](https://i.imgur.com/mXpEtKa.png) ![](https://i.imgur.com/YNzhDqC.png) * ## Connect to adafruit This project uses platform called adafruit. adafruit is a cloud platform which we can use for free with limited function which is enough for this project. The platform is where the Pycom device sends data from the sensor and where you can decide what you let the platform to do according to the data received from the sensor. I used this [tutorial](https://core-electronics.com.au/tutorials/internet-of-things-with-pycom-and-adafruit-io.html) as a reference. * ### Step1: Configuration First you need to configure the platform. 1. Make an account at adafruit. Go to [adafruit](https://io.adafruit.com/). Click "Get Started for Free" at the top of the page. Fill in the form and get an account. Go back to the top page and sign in. 2. Prepare "Feeds". (1) In "Feeds" you can discribe data. (2) Click "Feeds" in the naviation at adafruit IO. (3) From the Actions menu, select Create a New Feed. (4) Name this feed randoms. Click Create. 3. Prepare "Dashboard " with a line chart. (1) Click "Dashboard" in the naviation at adafruit IO. (2) Select Create a New Dashboard from the Action menu. (3) Name your dashboard and click "Create". (4) Get back to the dashboard list, click the name of the dashboard you just created. (5) Click the setting button on the right side to Create a New Block. Pick Line Chart. (6) Now link it to a feed so there’s data for the chart. Select "randoms" then click Next step. (7) You can just use the default values for the chart. Click Create Block. Now you have a internet platform ready to talk to your Lopy device. * ### Step2: Atom project 1. On Atom, in `lib` folder created at Computer setup, add a new file called `umqtt.py`. Copy the codes in the [link](https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.simple/umqtt/simple.py) and paste them in `umqtt.py` file. The reason you need to make this file is because `umqtt.py` is required to send data to adafruit. 2. In `boot.py`, write the following codes to connect to wifi. Please change wifi name and wifi password. ```python= from network import WLAN import machine import pycom pycom.rgbled(0xFF0000) # Red pycom.heartbeat_on_boot(False) wlan = WLAN(mode=WLAN.STA) nets = wlan.scan() for net in nets: if net.ssid == "CHANGE ME!!": # Change this to wifi name print('Network found!') wlan.connect(net.ssid, auth=(net.sec, "CHANGE ME!!"), timeout=5000) # Change this to wifi password pycom.rgbled(0xFF3300) # Orange while not wlan.isconnected(): machine.idle() # save power while waiting print('WLAN connection succeeded!') print(wlan.ifconfig()) pycom.rgbled(0x00FF00) # Green break ``` 3. In `main.py`, write the following codes. Do not forget to change several values (shown as CHANGE ME!!) By this code, the pycom device can send temperature data from the sensor when the temperature gets more than 28 degree. The device measures the temperature every 15 minutes (900 seconds). If an error occurs, wifi is disconnected so that you can reconnect wifi quicker. ```python= from network import WLAN import pycom import machine import time from lib.umqtt import MQTTClient import ubinascii # Adafruit IO (AIO) configuration AIO_SERVER = "io.adafruit.com" AIO_PORT = 1883 AIO_USER = "CHANGE ME!!" # Your user name at adafruit IO AIO_KEY = "CHANGE ME!!" # Find this in "My Key" at adafruit IO AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id()) # Can be anything AIO_RANDOMS_FEED = "CHANGE ME!!/feeds/randoms" #Your user name at adafruit IO # Use the MQTT protocol to connect to Adafruit IO client = MQTTClient(AIO_CLIENT_ID, AIO_SERVER, AIO_PORT, AIO_USER, AIO_KEY) # Connects the client client.connect() adc = machine.ADC() apin = adc.channel(pin='P16') wlan = WLAN(mode=WLAN.STA) shouldRun = True try: while shouldRun: # Repeat this loop forever millivolts = apin.voltage() celsius = (millivolts - 500.0) / 10.0 print("sending: {}".format(celsius)) if celsius > 28: client.publish(topic=AIO_RANDOMS_FEED, msg=str(celsius)) # Send data to adafruit time.sleep(900) #sleep for 900 second finally: wlan.disconnect() wlan = None ``` * ## Transmitting data Now the data from the sensor is sent to the platform. Since this project is aiming that I can notice high temperature in my backpack while I am travelling, I need to set a trigger which will send me a notification when the temperature reaches at a certain level. In this project I use Discord as a tool of notification. * ### Step1: Discord setup Setup dicord to handle the webhook sent - using the [MQTT library](https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.simple/umqtt/simple.py) - from adafruit IO by the [tutorial](https://learn.adafruit.com/discord-and-slack-connected-smart-plant-with-adafruit-io-triggers/discord-setup). * ### Step2: Set a trigger When it comes to Adafruit IO Setup in the [tutorial](https://learn.adafruit.com/discord-and-slack-connected-smart-plant-with-adafruit-io-triggers/discord-setup), select Reactive Trigger when you make a new trigger. Then, fill in blanks like this: ![](https://i.imgur.com/4PyBecH.png) With this setting, data is transmitting via wifi every 15 minutes. (The reason I set 15 minutes is because it is the minimum duration of Trigger I can use by free of charge at adafruit.) The data sent from the sensor will be preserved for 30 days. The dashboard shows like this: ![](https://i.imgur.com/k6MVkJo.png) * ### Step3: Go out with the device! Now all the setting is done except make the device portable. This project uses a powerbank. The powerbank I used is the one which is often sold as a mobile battery for your smartphone. The connection is easy. (1) Pull out USB cable from the computer. (2) Insert the USB cable to the powerbank. Since I used my smartphone's mobile data as the device's wireless protocol, Now the device is ready to go out together! * ## Result of the project I put the pycom device in my backpack and walked around outside. It surely gave me notification when the temperature reached at the setting level. * ### Good point I can know how the inside of backpack become hot. I can take a measure before my laptop get damaged by hot temperature. (I can for example try to go under shadow or inside of a building etc.) * ### Need to be improved The data of the temperature should be taken by the average of longer duration, not the direct data of every 15 minutes so that it can cover the imprecision of the sensor. (The sensor tends to show a bit higher than the actual.) ![](https://i.imgur.com/u1vPEIh.jpg) I put the pycom device in a bag when I take it out with me. ![](https://i.imgur.com/23mWLw7.jpg)