Now broadcasting in python.
This commit is contained in:
@@ -3,11 +3,12 @@
|
|||||||
#
|
#
|
||||||
# bmp180.py
|
# bmp180.py
|
||||||
# Read data from a digital pressure sensor.
|
# Read data from a digital pressure sensor.
|
||||||
# And broadcast to network.
|
# And broadcast to local network.
|
||||||
# Author : Jony Silva
|
# Author : Jony Silva
|
||||||
# Date : Wed 12 Jul 21:03:26 UTC 2017
|
# Date : Wed 12 Jul 21:03:26 UTC 2017
|
||||||
#
|
#
|
||||||
# Reading the chip code taken from :
|
# All the credit for the code to read
|
||||||
|
# the chip BMP180 goes to:
|
||||||
# http://www.raspberrypi-spy.co.uk/
|
# http://www.raspberrypi-spy.co.uk/
|
||||||
#
|
#
|
||||||
#--------------------------------------
|
#--------------------------------------
|
||||||
@@ -18,6 +19,9 @@ from ctypes import c_short
|
|||||||
|
|
||||||
DEVICE = 0x77 # Default device I2C address
|
DEVICE = 0x77 # Default device I2C address
|
||||||
|
|
||||||
|
# Interval sending in seconds between broadcasts
|
||||||
|
SEND_TIMING = 1
|
||||||
|
|
||||||
# addressing information of target
|
# addressing information of target
|
||||||
IPADDR = '<broadcast>'
|
IPADDR = '<broadcast>'
|
||||||
PORTNUM = 10000
|
PORTNUM = 10000
|
||||||
@@ -25,11 +29,6 @@ PORTNUM = 10000
|
|||||||
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
||||||
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
||||||
|
|
||||||
def convertToString(data):
|
|
||||||
# Simple function to convert binary data into
|
|
||||||
# a string
|
|
||||||
return str((data[1] + (256 * data[0])) / 1.2)
|
|
||||||
|
|
||||||
def getShort(data, index):
|
def getShort(data, index):
|
||||||
# return two bytes from data as a signed 16-bit value
|
# return two bytes from data as a signed 16-bit value
|
||||||
return c_short((data[index] << 8) + data[index + 1]).value
|
return c_short((data[index] << 8) + data[index + 1]).value
|
||||||
@@ -38,12 +37,6 @@ def getUshort(data, index):
|
|||||||
# return two bytes from data as an unsigned 16-bit value
|
# return two bytes from data as an unsigned 16-bit value
|
||||||
return (data[index] << 8) + data[index + 1]
|
return (data[index] << 8) + data[index + 1]
|
||||||
|
|
||||||
def readBmp180Id(addr=DEVICE):
|
|
||||||
# Chip ID Register Address
|
|
||||||
REG_ID = 0xD0
|
|
||||||
(chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
|
|
||||||
return (chip_id, chip_version)
|
|
||||||
|
|
||||||
def readBmp180(addr=DEVICE):
|
def readBmp180(addr=DEVICE):
|
||||||
# Register Addresses
|
# Register Addresses
|
||||||
REG_CALIB = 0xAA
|
REG_CALIB = 0xAA
|
||||||
@@ -56,7 +49,6 @@ def readBmp180(addr=DEVICE):
|
|||||||
# Oversample setting
|
# Oversample setting
|
||||||
OVERSAMPLE = 3 # 0 - 3
|
OVERSAMPLE = 3 # 0 - 3
|
||||||
|
|
||||||
# Read calibration data
|
|
||||||
# Read calibration data from EEPROM
|
# Read calibration data from EEPROM
|
||||||
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
|
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
|
||||||
|
|
||||||
@@ -116,29 +108,18 @@ def readBmp180(addr=DEVICE):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
(chip_id, chip_version) = readBmp180Id()
|
|
||||||
print("Chip ID : {0}".format(chip_id))
|
|
||||||
print("Version : {0}".format(chip_version))
|
|
||||||
|
|
||||||
print
|
|
||||||
|
|
||||||
(temperature,pressure) = readBmp180()
|
|
||||||
temp = "Temperature : {0} C".format(temperature)
|
|
||||||
press = "Pressure : {0} mbar".format(pressure)
|
|
||||||
|
|
||||||
print temp
|
|
||||||
print press
|
|
||||||
|
|
||||||
# initialize a socket, think of it as a cable
|
|
||||||
# SOCK_DGRAM specifies that this is UDP
|
|
||||||
s = socket(AF_INET, SOCK_DGRAM)
|
s = socket(AF_INET, SOCK_DGRAM)
|
||||||
|
|
||||||
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
|
||||||
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
||||||
s.sendto(temp, (IPADDR, PORTNUM))
|
|
||||||
|
|
||||||
# close the socket
|
print "Broadcasting Temperature and Pressure on Port:", (PORTNUM)
|
||||||
s.close()
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
main()
|
main()
|
||||||
|
Reference in New Issue
Block a user