2017-07-12 21:32:04 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#--------------------------------------
|
|
|
|
#
|
|
|
|
# bmp180.py
|
|
|
|
# Read data from a digital pressure sensor.
|
2017-07-13 00:25:52 +00:00
|
|
|
# And broadcast to local network.
|
2017-07-12 21:32:04 +00:00
|
|
|
# Author : Jony Silva
|
|
|
|
# Date : Wed 12 Jul 21:03:26 UTC 2017
|
|
|
|
#
|
2017-07-13 00:25:52 +00:00
|
|
|
# All the credit for the code to read
|
|
|
|
# the chip BMP180 goes to:
|
2017-07-12 21:32:04 +00:00
|
|
|
# http://www.raspberrypi-spy.co.uk/
|
|
|
|
#
|
|
|
|
#--------------------------------------
|
|
|
|
import smbus
|
|
|
|
import time
|
|
|
|
from socket import *
|
|
|
|
from ctypes import c_short
|
|
|
|
|
|
|
|
DEVICE = 0x77 # Default device I2C address
|
2017-07-13 00:25:52 +00:00
|
|
|
|
|
|
|
# Interval sending in seconds between broadcasts
|
|
|
|
SEND_TIMING = 1
|
2017-07-12 21:32:04 +00:00
|
|
|
|
|
|
|
# addressing information of target
|
|
|
|
IPADDR = '<broadcast>'
|
2017-07-13 00:25:52 +00:00
|
|
|
PORTNUM = 10000
|
2017-07-12 21:32:04 +00:00
|
|
|
|
|
|
|
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
|
|
|
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
|
|
|
|
|
|
|
def getShort(data, index):
|
|
|
|
# return two bytes from data as a signed 16-bit value
|
|
|
|
return c_short((data[index] << 8) + data[index + 1]).value
|
|
|
|
|
|
|
|
def getUshort(data, index):
|
|
|
|
# return two bytes from data as an unsigned 16-bit value
|
|
|
|
return (data[index] << 8) + data[index + 1]
|
|
|
|
|
|
|
|
def readBmp180(addr=DEVICE):
|
|
|
|
# Register Addresses
|
|
|
|
REG_CALIB = 0xAA
|
|
|
|
REG_MEAS = 0xF4
|
|
|
|
REG_MSB = 0xF6
|
|
|
|
REG_LSB = 0xF7
|
|
|
|
# Control Register Address
|
|
|
|
CRV_TEMP = 0x2E
|
|
|
|
CRV_PRES = 0x34
|
|
|
|
# Oversample setting
|
|
|
|
OVERSAMPLE = 3 # 0 - 3
|
|
|
|
|
|
|
|
# Read calibration data from EEPROM
|
|
|
|
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
|
|
|
|
|
|
|
|
# Convert byte data to word values
|
|
|
|
AC1 = getShort(cal, 0)
|
|
|
|
AC2 = getShort(cal, 2)
|
|
|
|
AC3 = getShort(cal, 4)
|
|
|
|
AC4 = getUshort(cal, 6)
|
|
|
|
AC5 = getUshort(cal, 8)
|
|
|
|
AC6 = getUshort(cal, 10)
|
|
|
|
B1 = getShort(cal, 12)
|
|
|
|
B2 = getShort(cal, 14)
|
|
|
|
MB = getShort(cal, 16)
|
|
|
|
MC = getShort(cal, 18)
|
|
|
|
MD = getShort(cal, 20)
|
|
|
|
|
|
|
|
# Read temperature
|
|
|
|
bus.write_byte_data(addr, REG_MEAS, CRV_TEMP)
|
|
|
|
time.sleep(0.005)
|
|
|
|
(msb, lsb) = bus.read_i2c_block_data(addr, REG_MSB, 2)
|
|
|
|
UT = (msb << 8) + lsb
|
|
|
|
|
|
|
|
# Read pressure
|
|
|
|
bus.write_byte_data(addr, REG_MEAS, CRV_PRES + (OVERSAMPLE << 6))
|
|
|
|
time.sleep(0.04)
|
|
|
|
(msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)
|
|
|
|
UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)
|
|
|
|
|
|
|
|
# Refine temperature
|
|
|
|
X1 = ((UT - AC6) * AC5) >> 15
|
|
|
|
X2 = (MC << 11) / (X1 + MD)
|
|
|
|
B5 = X1 + X2
|
|
|
|
temperature = int(B5 + 8) >> 4
|
|
|
|
|
|
|
|
# Refine pressure
|
|
|
|
B6 = B5 - 4000
|
|
|
|
B62 = int(B6 * B6) >> 12
|
|
|
|
X1 = (B2 * B62) >> 11
|
|
|
|
X2 = int(AC2 * B6) >> 11
|
|
|
|
X3 = X1 + X2
|
|
|
|
B3 = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2
|
|
|
|
|
|
|
|
X1 = int(AC3 * B6) >> 13
|
|
|
|
X2 = (B1 * B62) >> 16
|
|
|
|
X3 = ((X1 + X2) + 2) >> 2
|
|
|
|
B4 = (AC4 * (X3 + 32768)) >> 15
|
|
|
|
B7 = (UP - B3) * (50000 >> OVERSAMPLE)
|
|
|
|
|
|
|
|
P = (B7 * 2) / B4
|
|
|
|
|
|
|
|
X1 = (int(P) >> 8) * (int(P) >> 8)
|
|
|
|
X1 = (X1 * 3038) >> 16
|
|
|
|
X2 = int(-7357 * P) >> 16
|
|
|
|
pressure = int(P + ((X1 + X2 + 3791) >> 4))
|
|
|
|
|
|
|
|
return (temperature/10.0,pressure/100.0)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2017-07-13 00:25:52 +00:00
|
|
|
s = socket(AF_INET, SOCK_DGRAM)
|
|
|
|
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
2017-07-12 21:32:04 +00:00
|
|
|
|
2017-07-13 00:25:52 +00:00
|
|
|
print "Broadcasting Temperature and Pressure on Port:", (PORTNUM)
|
2017-07-12 21:32:04 +00:00
|
|
|
|
2017-07-13 00:25:52 +00:00
|
|
|
while 1:
|
|
|
|
(temperature,pressure) = readBmp180()
|
|
|
|
temp = "Temperature : {0} C".format(temperature)
|
|
|
|
press = "Pressure : {0} mbar".format(pressure)
|
|
|
|
s.sendto(temp+'\n'+press+'\n', (IPADDR, PORTNUM))
|
|
|
|
time.sleep(SEND_TIMING)
|
2017-07-12 21:32:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
main()
|
|
|
|
|