# 1DT305 Tillämpad IoT: Project: Weather Station **Author: Tomas Nilsson, tn222hh** # Introduction In this tutorial, a simple weather station will be constructed using the Pycom LoPy4 microcomputer and a BMP180 temperature and barometric pressure sensor. The device will do the following: 1. Measure the temperature, unit degrees C. 2. Measure the barometric pressure, unit hPa. 3. Send the temperature and pressure data to Pybytes once every hour. 4. Calculate the minimum, maximum and average temperature ever 24th measure, send the data to Pybytes. 5. When data is sent, a blue led will blink for 0,5 seconds. 6. The RGB sensor will be used to show the indoor temperature. If the temperature is too low (below 20 degrees C) the RGB will be blue. If the temperature is too high (above 25 deg C) it will be red. Else, it will shine with a green light. 7. The data will be shown on a dashboard in the Pybytes site. 8. The device will use Wifi to communicate with the Internet. **Time to complete the project** The time it should take to follow this guide and build a similar device is approximately four hours. Some soldering needs to be done, and if you have no experience with soldering it may take a little longer time. **Objective** I chose this project because I am interested in measuring weather data. For example, barometric pressure data can be used to predict weather changes and the current temperature data is always important to know. The project serves two purpuses: To build a useful device and to learn the fundamentals of iOt, Python programming, and electronics. It will give the following insights: 1. How to configure and set up the Pybytes environment. 2. How to install the Atom IDE and flash the Pybytes firmware onto the LoPy4 device. 3. How to connect a LED, calculate the appropriate resistance value. 4. How to use the BMP180 sensor. The sensor uses the I2C protocol and a library is used to easily configure and read data from the sensor. 5. How to write a simple Python program to read data from the sensor, send data to Pybytes, flash the LED when data is sent and set the appropriate colour of the RGB LED according to temperature. 6. How to display the data on a simple dashboard on Pybytes. ## List of materials The following materials and devices are used in this project: | Item | Price | | -------- | -------- | | Pycom LoPy4 | 560 kr | | Pycom Expansion Board | 199 kr | | Blue LED | 99 kr for 25 leds | Resistor 100 Ohm | 89 kr (resistor kit) | | USB Cable type B micro | 29 kr | | Sensor BMP180 | 69 kr | | Some breadboard wires | 45 kr (wire kit)| | Breadboard half size | 49 kr | | Soldering iron 30W | 99 kr | | Solder 15g 1 mm | 39 kr | The items required can be bought from many online shops, for example m.nu, electrokit.com, kjell.com. ## Description of the devices and components ### The LoPy4 This is the main component that controls the weather station. It is a microcomputer running Python natively and it has built-in Wifi, LoRa, Sigfox and Bluetooth. This microcomputer is intended to use for IoT applications. ![](https://i.imgur.com/wSpYdcw.jpg) Picture from https://media.rs-online.com/t_large/F1628047-01.jpg ### The expansion board The expansion board makes the LoPy4 much easier to use. For example, it provides ports to easily connect wires from the LoPy4 to sensors and other devices, a USB connector that allows the LoPy4 to be connected to a computer and programs transferred to the LoPy via the USB. ![](https://i.imgur.com/co7C8sV.png) Picture from Pycom homepage, https://docs.pycom.io/gettingstarted/connection/lopy4/ ### LED and resistor The LED will be connected to a port on the expansion board. It must be connected to a resistor in series, because the LED itself has a very low resistance. ### The BMP180 sensor The BMP180 sensor is a cheap sensor that can measure temperature and barometric pressure. It uses the I2C protocol to communicate with other devices, which is a common serial protocol that is often used for communication between microcontrollers and peripheral units such as sensors or displays. ![](https://i.imgur.com/A8xJR1v.jpg) Picture from https://www.makerfabs.com/bmp180-barometric-pressure-temperature-altitude-sensor.html ### Breadboard A breadboard is useful to connect wires to the microcomputer and other devices such as sensors and LEDs. The breadboard eliminates the need to solder and is useful for experimenting, testing and education. ![](https://i.imgur.com/iF6TLCI.jpg) *Picture from Wikipedia* ### Soldering iron If a breadboard is used, the only soldering that has to be done is to solder the connection pins to the BMP180 sensor. It is important to have a small tip on the soldering iron because of the small size of the BMP180 sensor card. ![](https://i.imgur.com/pZvR3Yu.jpg) Picture from https://www.circuitspecialists.com/content/104613/sh-813a-220v.jpg ## Computer setup ### Choosing the IDE I chose the Atom IDE because it is a modern, open-source IDE that is easy to use and it is easy to install the Pybytes plugin. Go to https://atom.io/ and download the version that suits your operating system and install the program. ### Creating the device in Pybytes 1. Open a web browser, enter the address pybytes.pycom.io. If you have never logged in, you will start at the login page. 2. Click the button **Sign up**, enter namn, e-mail address, choose a password, check the box to confirm that you accept the conditions and click **Create account**. 3. Go to your e-mail, open the message from Pycom and click the link to activate your Pycom account. 4. Go to pypytes.pycom.io and log into your new account. 5. On the startpage click **Configure networks**. Click the button Add Wifi. Enter the SSID and password for the Wifi you intend to use, then click **Save**. 6. Now, you are logged in and ready to start! Connect the Pycom with expansion board using the USB cable. Click Add device via USB. Choose Lopy4. Check the box Wifi, leave the other boxes unchecked. Click the button **Next**. 7. Choose a name for your device, for example WeatherStation, choose your Wifi (that you configured earlier), click the button **Save**. 8. Find the section *Activate your device with pymakr plugin*. Copy the activation link that you find there, it should be in the form *pybytes.activate(“...”)*. ### Setting up the Atom environment Now, we need to install the Pymakr plugin in the Atom IDE to be able to communicate with the Pycom device. Follow these steps: 1. Start the Atom IDE. Connect the Pycom with expansion board to the USB. 2. You should now be presented with a start page *Get to know Atom*. From here, the Pymakr plugin can be installed. Click the button **Open installer**, then click the green button **Install**. ![](https://i.imgur.com/YQRY8Tq.jpg) 3. Now, a tab *Settings* should be opened. Enter pymakr in the text field *Search packages*. Now, a small box will be shown. Click the button **Install**. Wait for the installation to be completed. 4. After the installation is completed, a REPL interface is shown in the lower part of the IDE window. It should look something like this: ![](https://i.imgur.com/DTdMxOl.jpg) 5. In the REPL prompt, enter the activation string you copied earlier from the Pybytes web app. Wait for the activation to finish. ![](https://i.imgur.com/S77rFdZ.jpg) 6. Now, the environment is set up and we are ready to start coding! 7. To start a new project, create a new file (File -> new File). Create a new folder, save the file with the name *main.py*. Then, open the folder by selecting File -> Open Folder. Write the code in the file main.py. 8. When you want to test a program, push the Run button on the left side of the Repl interface. To upload the file to the Pycom and store it there, push the button Upload project to device (the button with the up-arrow). Now, the program is stored on the LoPy4 and will remain in its memory even if the microcontroller is disconnected. ## Connecting the electronics Now, it is time to assemble the electronics. The BMP180 sensor, the blue LED and the resistor are connected on the breadboard and connected to the Pycom. Pin P8 is used to control the LED and pins P9 and P10 are used to read data from the BMP180 via the I2C protocol, as shown in the picture below. ![](https://i.imgur.com/0I1EdqU.jpg) ### Choosing the resistance The value of the resistor must be chosen so that the current through the LED is approximately 20 mA. The voltage over the LED is approximately 2 Volts. The Pycom provides the voltage 3,3V. Because the voltage over the LED i 2V, the voltage over the resistor is U=3,3V - 2V = 1,3V. Now, Ohm's law can be used to calculate the appropriate resistance: R=Voltage/Current = 1,3/0,02 = 65 Ohm. The smallest resistor I have is 100 Ohm. That is good enough, it will make the current through the LED a little less than 20 mA which is quite enough to give a strong light. ### Power calculations With the 100 Ohm resistor, the current through the LED and resistor is, according to Ohm's law, I = Voltage/Resistance = 1,3/100 = 13 mA The power dissipated through the resistor is the product of the voltage and current, thus Power = 1,3*0,013 = 0,0169 W. This is a very low power, far below what the resistor and LED can handle. ## Choice of platform I chose to use the Pybytes web platform to store and manage my data on the Internet. The reason for this choice is that Pybytes is easy to use and manage, it is made especially for the Pycom devices and it is very easy to send data from a Pycom device to Pybytes. It is also very easy to create a dashboard to show the data in a table or a graph. ## The code The algorithm to implement is the following: ``` Repeat: Read pressure and temperature from BMP180 Store temperature in a list Send pressure and temperature to Pybytes Let the blue LED flash for 0,5 seconds Set the RGB led according to temperature If 23 measures have passed since last time: Send maximum temp to Pybytes Send minimum temp to Pybytes Send average temp to Pybytes ``` ### Implementation in Python To handle the BMP180 sensor, a helper library is used. The library consists of a class BMP180 in a file called bmp085.py. This file must be placed in a folder called *lib* directly under the project folder. The library can be downloaded from here: https://github.com/iot-lnu/applied-iot-20/tree/master/sensor-examples/bmp085_and_180-pressure-temp ### The code for the weather station ``` # Measuring barometric pressure and temperature using av BMP180 sensor import time import machine import pycom from machine import I2C from bmp085 import BMP180 from machine import Pin def calcAverage(values): return sum(values)/len(values); def blinkLED(myLED, blinktime): myLED.value(1) time.sleep(blinktime) myLED.value(0) time.sleep(blinktime) def RGBTemperature(temp): if temp<20: pycom.rgbled(0x0000FF) # Blue elif temp>25: pycom.rgbled(0xFF0000) # Red else: pycom.rgbled(0x00FF00) # Green temperatures=[] #Create an empty list for storing the temperatures #Create a Pin object for the blue LED blueLED = Pin('P8', mode = Pin.OUT) #Set up i2c protocol for the BMP180 sensor i2c = I2C(0) # create on bus 0 i2c = I2C(0, I2C.MASTER) # create and init as a master i2c = I2C(0, pins=('P9','P10')) # PIN assignments (P9=SDA, P10=SCL) i2c.init(I2C.MASTER, baudrate=115200) # init as a master bmp = BMP180(i2c) pycom.heartbeat(False) for i in range(1,1000): temp=bmp.temperature #Read temperature from BMP180 temperatures.append(temp) pressure=bmp.pressure #Read pressure from BMP180 RGBTemperature(temp) #Set color of RGB diode according to temperature print('Temperature = ', temp) print('Pressure = ', pressure) pybytes.send_signal(1,temp) pybytes.send_signal(2,round(pressure,2)) if(i%24==0): pybytes.send_signal(3,round(min(temperatures),2)) pybytes.send_signal(4,round(max(temperatures),2)) pybytes.send_signal(5,round(calcAverage(temperatures),2)) temperatures=[] #Clear the temperature list blinkLED(blueLED,0.5) #The time until next iteration in the loop. For example, time.sleep(3600) #makes the Pycom read from the BMP180 and send data once every hour. time.sleep(3600) pycom.rgbled(0x000000) #Set RGB LED to black ``` ### Some comments to the code The code is quite straight-forward. Everything happens in the for-loop. The values of temperature and pressure are read from the sensor and sent to Pybytes using the *pybytes.send_signal()* function. After the data is sent, the blue LED is set to blink for 0,5 seconds. The minimum, maximum and average data is only sent eftery 24th update. To realize this, the Modulus operator is used. The statement *if(i%24==0)* means that this code block will only be run every 24th iteration of the loop. This is done in the function *blinkLed()*. Also, the internal RGB LED is set to a colour that depends on the temperature value. A function was created to set the RGB LED, that takes the temperature as parameter. The main reason for doing this as a function is that the RGB LED function can be easily turned off by commenting out the line *RGBTemperature(temp)* in the loop if we don't want this function (the light can be annoying and it consumes energy so if the unit is powered by battery, the RGB led should probably not be lit). The source code is also available on Github: https://github.com/tomas1978/WeatherStation ## Transmitting the data In the code above, temperature and pressure data is sent once every hour. Once every 24 hours, the minimum, maximum and average values of the temperature are calculated and transmitted to the Pybytes website. The interval with which the data is calculated and sent can be easily altered by changing the constant within time.sleep() in the Python code. The wireless protocol used is Wifi. The reason for this choice is that I use my weather station inside my apartment or right outside my window, where I have a very strong signal from my personal Wifi. Of course, my choice of Wifi limits the unit's effective range and it will not work far from my configures Wifi. Also, the Wifi is not very power effective and consumes a lot of power compared to LoRa, but for my application, it does not matter that much, because I intend to use the weather station where I have access to a power outlet. If the weather station would be placed outside, far away from my Wifi, LoRa would probably have been a better choice. LoRa could make the useful area of the weather station much larger, as long as it is within reach of a LoRa gateway. The data is sent to Pybytes using the pybytes.send_signal(signalNumber, data) instruction. The signal number is then set up to display the data in a graph or a table on the Pybytes website. The MQTT protocol is used to send data to Pybytes. ## Presenting the data To present the data, a dashboard on the Pybytes website is used. The data is stored in the database on Pybytes when data is sent (once every hour for temperature and pressure data, once every 24 hours for maximum, minimum and average temperature). The internal RGB LED on the Pycom is used as a trigger to react on the data (the value of the temperature) and the colour is set to indicate if the temperature is low, high or "comfortable". ### How to create the dashboard Log into the Pybytes website, click on Devices (the microchip icon), select the device for the weather station. Click the word Signals. To the right on this screen, the signals 1, 2, 3, and 4 will be shown (the signals sent from the Python program). For example, signal number 1 was used to send the temperature data. ![](https://i.imgur.com/LvzTpEV.jpg) We will give the signals names that represent the data. For example, signal 1 should be given the name Temperature. Click the pen symbol on Signal 1, and enter a name and a unit in the dialog that appears. ![](https://i.imgur.com/y4nXi7D.jpg) Do the same thing to the other signals and give them proper names and units. ### Creating a dashboard From the start, the dashboard only contains graphs showing how much data is transmitted and your location. Here, it is possible to add graphs and tables to represent the data that your Pycom device sends. To add a representation for a signal on the dashboard, remain on Signals. On the middle of the screen, the signals are shown. Click the one you want to add to the dashboard, for example Temperature. Then, click the button **Create new display**. You can now choose between two types of graphs and a table. Choose the line chart, then click the button **Create**. The new graph is now shown on the top of the screen. Click **Edit** and check the box **Display on dashboard**. Then, click **Save**. ![](https://i.imgur.com/W2Q6For.jpg) The new graph is now added to the dashboard. Click on the name of the device to see the dashboard. Do similar things with the other signals. For example, add a line chart for the barometric pressure and tables for the minimum, maximum and average temperatures. ## Final results The picture below shows my Pycom connected to the BMP180 sensor and a blue LED in series with a 100 Ohm resistor. As one can see in the picture, a breadboard is used to connect the components. I usually use the USB to power the Pycom, either powered by a computer, a mobile charger or a USB powerbank. I also tried to power the device with a battery pack with two AAA batteries, which was enough to power the Pycom device. ![](https://i.imgur.com/h3mLDBr.png) The data sent to Pycom is presented on a dashboard, as shown in the picture below. The picture below shows the data sent to Pycom as graphs and tables. ![](https://i.imgur.com/C2a4OMe.jpg) ### Conclusion This tutorial shows how to build a simple IoT device that reads some data from a sensor and shows this data on a simple dashboard on the Internet. Hopefully, it can give the reader some basic skills to build upon and a foundation to do more advanced IoT projects in the future.