Temperature and Atmospheric Pressure Monitoring in Blynk using Raspberry Pi Zero W

Overview

Atmospheric pressure is an indicator of weather. When a low-pressure system moves into an area, it usually leads to cloudiness, wind, and precipitation. High-pressure systems usually lead to fair, calm weather. Temperature plays an important role and it is important to know what temperature is, how it is measured, and what implications it may have for society as a whole.

In this tutorial we will monitor the temperature and the atmospheric pressure in Blynk mobile app using Raspberry Pi Zero W.

 

Here’s the sample output from the Blynk app:

Hardware Components

Software Components

Application Discussion

DS18B20 Digital Temperature Sensor

The DS18B20 is a 1-wire programmable Temperature sensor from Maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil etc. The construction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy. It can measure a wide range of temperature from -55°C to +125° with a decent accuracy of ±5°C. Each sensor has a unique address and requires only one pin of the MCU to transfer data so it is a very good choice for measuring temperature at multiple points without compromising much of your digital pins on the microcontroller.

How does it work:

The sensor works with the method of 1-Wire communication. It requires only the data pin connected to the microcontroller with a pull up resistor and the other two pins are used for power. The pull-up resistor is used to keep the line in high state when the bus is not in use. The temperature value measured by the sensor will be stored in a 2-byte register inside the sensor. This data can be read by using the 1- wire communication protocol.

BMP280 Barometric Pressure Altitude Sensor

BMP280 is an environmental sensor which reads temperature and barometric pressure that is the next generation upgrade to the BMP085/BMP180/BMP183. This sensor is great for all sorts of weather sensing and can even be used in both I2C and SPI modes. This sensor has high accuracy and low cost making it an ideal solution for precision pressure measurements of up to ± 1 hPa and a temperature of up to ± 1.0 ° C. Because the pressure changes with altitude and pressure measurements are very accurate, you can use this sensor and the altimeter ± 1 meter accuracy.

VNC Viewer

Virtual network computing (VNC) is a type of remote-control software that makes it possible to control another computer over a network connection. Keystrokes and mouse clicks are transmitted from one computer to another, allowing technical support staff to manage a desktop, server, or other networked device without being in the same physical location. We will use this software to access our Raspberry Pi Zero W via Wi-fi network. This tutorial is based on using VNC Viewer to be able to work on the Raspberry Pi Zero W desktop.

Software Setup

Install the Blynk app from Playstore or App Store in your phone.

Open the app and select New Project.

Enter the Project Name, then press Choose Device and select Raspberry Pi 3 B and then press Connection Type and select WiFi. Then press Create Project.

Press Ok, your Auth Token was sent to your email but you can also find it in the Project Settings.

Go to widgets menu and select Value display, then press the Value Display widget and press the pin and select V2 and also enter the title.

Go to widgets menu again and select Value Display, then press the Value Display widget and press the pin and select V4 and enter the title.

Select the Value Display in widgets menu again, then press the Value Display and press the pin and select V3 and enter the title.

If you’re already connected to the Blynk app, just press the Play icon on the top left corner.

NOTE: you can make your own design and format in your Blynk app.

Hardware Setup

DS18B20 Setup

Connect the GND to the GND of the Raspberry Pi Zero W and the VCC to 3V3 pin. Connect the DQ to the GPIO 4. And also place a pull up 4.7k ohms resistor in the DQ of the DS18B20.

BMP280 Setup

Connect the SCK to GPIO 3 of the Raspberry Pi Zero, next is connect the SDI to GPIO 2, then connect the VCC to 3v3 and the GND to the GND of Raspberry Pi Zero W.

Code

Libraries Included

Raspberry Pi Code

import os
import glob
import time
import BlynkLib

import Adafruit_BMP.BMP280 as BMP280

sensor = BMP280.BMP280()

BLYNK_AUTH = '805fa4c5382046cd868b9f1b4857a7ac'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-03079779b9e3/w1_slave'

def read_temp_raw():
    f = open(temp_sensor, 'r')
    lines = f.readlines()
    f.close()
    return lines
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        print(temp_c,'Degrees Celcius')
	print(temp_f,'Degrees Farenheit')
        blynk.virtual_write(2, '{:.2f}'.format(temp_c))
        blynk.virtual_write(4, '{:.2f}'.format(temp_f))
        	
while True:    
        read_temp()
        blynk.virtual_write(3, '{:.2f}'.format(sensor.read_pressure()/1000))
        print('Pressure = {0:0.2f} KPa'.format(sensor.read_pressure()/1000))
	time.sleep(3)

Code Breakdown

BLYNK_AUTH = '805fa4c5382046cd868b9f1b4857a7ac'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-03079779b9e3/w1_slave'

This code initializes the Blynk and also the GPIO pins, by turning the GPIO module and the temperature module. And then enters the correct device file that holds the temperature data.

def read_temp_raw():
    f = open(temp_sensor, 'r')
    lines = f.readlines()
    f.close()
    return lines

This is the read_temp_raw() function. This is the code that reads the temperature data, it opens the temperature device file and then returns the text.

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        print(temp_c,'Degrees Celcius')
	print(temp_f,'Degrees Farenheit')
        blynk.virtual_write(2, '{:.2f}'.format(temp_c))
        blynk.virtual_write(4, '{:.2f}'.format(temp_f))

This is the read_temp() function. This code converts the value or data of the sensor into temperature in celsius and farenheit. And sends the data or value of the temperature in celsius and farenheit in the Blynk app.

while True:    
        read_temp()
        blynk.virtual_write(3, '{:.2f}'.format(sensor.read_pressure()/1000))
        print('Pressure = {0:0.2f} KPa'.format(sensor.read_pressure()/1000))
	time.sleep(3)

This code prints the temperature value in celsius and farenheit and the atmospheric pressure value, then sends the atmospheric pressure value to the blynk app.

Conclusion

With the recent rise of technology, it allows us to to create new ideas that can help us in our daily life. Like this project, it helps us in monitoring the weather since the atmospheric pressure and temperature are one of the parameter in weather and it helps us in making decisions and save us time throughout the day.

Reference

[1] https://enviroliteracy.org/air-climate-weather/weather/temperature/

[2] https://www.nationalgeographic.org/encyclopedia/atmospheric-pressure/

[3] https://tutorial.cytron.io/2019/01/02/displaying-sensors-data-on-blynk-app-using-raspberry-pi/

The post Temperature and Atmospheric Pressure Monitoring in Blynk using Raspberry Pi Zero W appeared first on CreateLabz.

Atmospheric pressureBlynkBmp280Ds18b20KnowledgebaseRaspberry piRaspberry pi zeroRpiTemperature and atmospheric pressure monitoringTemperature sensor

Leave a comment

All comments are moderated before being published