在自定义打印中将两个Python代码组合在一起

时间:2017-02-05 07:27:30

标签: python

我使用Direwolf和我的RPi 3上的一些python脚本在APRS小型气象站工作。

我的困境似乎很简单,但我缺乏蟒蛇知识。

我有我的BMP180传感器的主代码,它以特定的温度和压力格式输出数据。但是,我添加了一个湿度传感器,但是我无法将代码组合起来以获得我需要的输出格式。

这是我的主要代码,我需要添加底部的代码以获得所需的输出(也显示在打印作业的底部) 即。

  

@ 050501z000 / 000g000t042r000p000P000h42b9999 RPI

哪里

  

h42 = h +湿度百分比。

#Main BMP180 code

-----------------------------------------------------------------------------
import smbus
import time
import sys
from ctypes import c_short

DEVICE = 0x77 # Default device I2C address


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

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
  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
  # 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 = (B5 + 8) >> 4

  # Refine pressure
  B6  = B5 - 4000
  B62 = B6 * B6 >> 12
  X1  = (B2 * B62) >> 11
  X2  = AC2 * B6 >> 11
  X3  = X1 + X2
  B3  = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2

  X1 = 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 = (P >> 8) * (P >> 8)
  X1 = (X1 * 3038) >> 16
  X2 = (-7357 * P) >> 16
  pressure = P + ((X1 + X2 + 3791) >> 4)

  return (temperature/10.0 * 9/5 +32,pressure/10)

def main():

  (chip_id, chip_version) = readBmp180Id()
#  print "Chip ID     :", chip_id
 # print "Version     :", chip_version


  (temperature,pressure)=readBmp180()
  print time.strftime("@%d%H%Mz")+('000g000t0{0:0.0f}r000p000P000h00b{1:0.0f} RPI'.format(temperature, pressure))



if __name__=="__main__":
   main()


#Code that needs to be added

import Adafruit_DHT
humidity = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)

if humidity <= 99:
   print('{0:0.0f}'.format(humidity))
else:
   print('00')

#Desired output of print
#print time.strftime("@%d%H%Mz")+('000g000t0{0:0.0f}r000p000P000h00b{1:0.0f} RPI'.format(temperature, pressure))
#Where h00 = h(humidity output of the sensor)

1 个答案:

答案 0 :(得分:0)

import放在脚本的开头并在main()

中休息

您可以使用{:02.0f}始终获得两位数 - 即。 h09

import Adafruit_DHT

def main():

  chip_id, chip_version = readBmp180Id()

  temperature, pressure = readBmp180()

  humidity = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)

  if humidity > 99:
     huminidy = 0

  print time.strftime("@%d%H%Mz") + '000g000t0{:0.0f}r000p000P000h{:02.0f}b{:0.0f} RPI'.format(temperature, huminidy, pressure)


if __name__=="__main__":
   main()