Source Code

What is this code for?

This code is used to power the BMP280 Pressure and Tempature Sensor as well as the SG90 Servo that operates the autromatic inflation system on the camper.

We use a Raspberry Pi 3 as our main computer, it is old but mighty and able to carry out all of our project needs. To make sure that our Raspberry Pi is not only able to run on the camper, but with the camper seperately from a power source- we connected a UPS (Unlimited Power Supply) Battery. So once it is unplugged from the wall, the code is still able to keep running. 
 

CODE FOR BMP SENSOR


import time
from ctypes import c_short

DEVICE = 0x77 

bus = smbus.SMBus(1) 


 # Binary to String Data
def convertToString(data):
 return str((data[1] + (256 * data[0])) / 1.2)

def getShort(data, index):
 return c_short((data[index] << 8) + data[index + 1]).value

def getUshort(data, index):
 return (data[index] << 8) + data[index + 1]

#Registering Chip Address

def readBmp180Id(addr=DEVICE):
 REG_ID     = 0xD0
 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
 return (chip_id, chip_version)
 

#Register Addy
def readBmp180(addr=0x77):
 REG_CALIB  = 0xAA
 REG_MEAS   = 0xF4
 REG_MSB    = 0xF6
 REG_LSB    = 0xF7
 # Control Register Address
 CRV_TEMP   = 0x2E
 CRV_PRES   = 0x34 
 # Oversample 
 OVERSAMPLE = 3    # 0 - 3
 
 # calibration

 cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)

 # byte data to word 
 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)

 # more temperature
 X1 = ((UT - AC6) * AC5) >> 15
 X2 = (MC << 11) / (X1 + MD)
 B5 = X1 + X2
 temperature = int(B5 + 8) >> 4
 temperature = temperature / 10.0

 # more 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,pressure)
 

 

CODE FOR TEMPATURE AND PRESSURE OUTPUT 

import bmpsensor
import time
while True:
   temp, pressure = bmpsensor.readBmp180()
   print("Tempature is: ", temp)
   print("Pressure is: ",pressure)
   print("\n")
   time.sleep(2)

 

CODE FOR PRESSURE OUTPUT ONTO SG90 SERVO

import bmpsensor
import time
import RPi.GPIO as GPIO


while True:
   time.sleep(10)
   temp, pressure = bmpsensor.readBmp180()
   print("Pressure is: ",pressure)


   if pressure < 103440:
       GPIO.setmode(GPIO.BOARD)
       GPIO.setup(11,GPIO.OUT)
       servo1 = GPIO.PWM(11,50)
       
       servo1.start(0)
       
       servo1.ChangeDutyCycle(6)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(8.8)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(6)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(8.8)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(6)

       servo1.stop()
       
   else:
       GPIO.setmode(GPIO.BOARD)
       GPIO.setup(11,GPIO.OUT)
       servo1 = GPIO.PWM(11,50)
       time.sleep(5)
       print("Pressure is: ",pressure)
       servo1.start(0)
       servo1.ChangeDutyCycle(6)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(9)
       time.sleep(0.5)
       servo1.ChangeDutyCycle(6)
       servo1.stop()
       

 

©Copyright. All rights reserved.

We need your consent to load the translations

We use a third-party service to translate the website content that may collect data about your activity. Please review the details in the privacy policy and accept the service to view the translations.