Now broadcasting in python.

This commit is contained in:
The Weather PI
2017-07-13 00:25:52 +00:00
parent 7300ad4ba2
commit beb9c1a72f

View File

@@ -3,11 +3,12 @@
#
# bmp180.py
# Read data from a digital pressure sensor.
# And broadcast to network.
# And broadcast to local network.
# Author : Jony Silva
# 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/
#
#--------------------------------------
@@ -17,19 +18,17 @@ from socket import *
from ctypes import c_short
DEVICE = 0x77 # Default device I2C address
# Interval sending in seconds between broadcasts
SEND_TIMING = 1
# addressing information of target
IPADDR = '<broadcast>'
PORTNUM = 10000
PORTNUM = 10000
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
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):
# return two bytes from data as a signed 16-bit 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 (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):
# Register Addresses
REG_CALIB = 0xAA
@@ -56,7 +49,6 @@ def readBmp180(addr=DEVICE):
# Oversample setting
OVERSAMPLE = 3 # 0 - 3
# Read calibration data
# Read calibration data from EEPROM
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
@@ -116,30 +108,19 @@ def readBmp180(addr=DEVICE):
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.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.sendto(temp, (IPADDR, PORTNUM))
print "Broadcasting Temperature and Pressure on Port:", (PORTNUM)
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)
# close the socket
s.close()
if __name__=="__main__":
main()