首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在自定义打印中将两个Python代码组合在一起

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

Stack Overflow用户
提问于 2017-02-05 07:27:29
回答 1查看 108关注 0票数 0

我一直在使用Direwolf和我的RPi 3上的一些python脚本为APRS开发一个小型气象站。

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

我有我的BMP180传感器的主要代码,它以特定的格式输出数据,用于温度和压力。然而,我增加了一个湿度传感器,但我一直无法组合代码,以获得我需要的输出格式。

下面是我的主要代码,代码位于底部,我需要添加这些代码才能获得所需的输出(也显示在打印作业的底部)。

@050501z000/000g000t042r000p000P000h42b9999 RPI

哪里

h42=h+humidity百分比

代码语言:javascript
复制
#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)
EN

回答 1

Stack Overflow用户

发布于 2017-02-05 08:12:21

import放在脚本的开头,rest放在main()

你可以用{:02.0f}来得到两位数--即。h09

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42049565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档