Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Mister
Published © Apache-2.0

MQTT Window Weather Station

Use a Raspberry Pi, a MPL3115A2 temp/pressure sensor, and 3D printed case to measure local weather outside and send data over MQTT.

BeginnerFull instructions provided2 hours10,934
MQTT Window Weather Station

Things used in this project

Hardware components

Raspberry Pi 1 Model B
Raspberry Pi 1 Model B
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Altitude/Pressure Sensor Breakout MPL3115A2
SparkFun Altitude/Pressure Sensor Breakout MPL3115A2
×1

Software apps and online services

Home Assistant
Home Assistant

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

STL for MPL3115A2 Enclosure

3D printable STL enclosure file for the MPL3115A2 sensor.

Schematics

Fritzing Diagram

Connecting MPL3115A2 to Raspberry Pi

Code

MPL3115A2 Python File

Python
This is the python file to communicate with the sensor and also to publish the sensor data to the MQTT broker. Be sure to change your MQTT broker information.
import smbus
import time
import datetime

# Import MQTT client.
import paho.mqtt.publish as publish
import paho.mqtt.client as paho

# MQTT credentials
mqtt_username  = 'homeassistant' # MQTT client username
mqtt_password = '3355' # MQTT client password
client_id = '1138' # unique client_id
mqtt_broker = "192.168.86.228" # broker address, usually your HASS IP address

# connection event
def on_connect(client, data, flags, rc):
    print('Connected: ' + str(rc))

# create the MQTT client
client = paho.Client(client_id=client_id, protocol=paho.MQTTv31)  # * set a random string (max 23 chars)

# Command topic
temperature_topic  = 'home/outside/temperature'
pressure_topic  = 'home/outside/pressure'

# client connection
client.username_pw_set(mqtt_username, mqtt_password)  # MQTT server credentials
client.connect(mqtt_broker)  # MQTT server address  

# Get I2C bus
bus = smbus.SMBus(1)

# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38)
#               0xB9(185)       Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)

# MPL3115A2 address, 0x60(96)
# Select data configuration register, 0x13(19) 0x07(07) 
# Data ready event enabled for altitude, pressure, temperature
bus.write_byte_data(0x60, 0x13, 0x07)

# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38) 0xB9(185)       
# Active mode, OSR = 128, Altimeter mode
bus.write_byte_data(0x60, 0x26, 0xB9)

time.sleep(1)

# MPL3115A2 address, 0x60(96) 
# Read data back from 0x00(00), 6 bytes
# status, tHeight MSB1, tHeight MSB, tHeight LSB, temp MSB, temp LSB
data = bus.read_i2c_block_data(0x60, 0x00, 6)

# Convert the data to 20-bits
tHeight = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16
altitude = tHeight / 16.0
cTemp = temp / 16.0
fTemp = cTemp * 1.8 + 32
date = str(datetime.datetime.now())

# MPL3115A2 address, 0x60(96)
# Select control register, 0x26(38) 0x39(57)        
# Active mode, OSR = 128, Barometer mode
bus.write_byte_data(0x60, 0x26, 0x39)

time.sleep(1)

# MPL3115A2 address, 0x60(96)
# Read data back from 0x00(00), 4 bytes
# status, pres MSB1, pres MSB, pres LSB
data = bus.read_i2c_block_data(0x60, 0x00, 4)

# Convert the data to 20-bits
pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
pressure = (pres / 4.0) / 1000.0

# Publish our data
client.publish(temperature_topic, "%.1f" %cTemp)
client.publish(pressure_topic, "%.1f" %pressure)

time.sleep(10)

# Output data to screen 
print "Date : %s" %date
print "Pressure : %.2f kPa" %pressure
print "Altitude : %.2f m" %altitude
print "Temperature in Celsius  : %.2f C" %cTemp
print "Temperature in Fahrenheit  : %.2f F" %fTemp

Credits

Mister
2 projects • 29 followers
Independent mobile developer specializing in the design and development of building great mobile experiences.

Comments