Python:将两个Codepieces放在一起

时间:2013-12-30 23:50:31

标签: python mysql

你好,晚上好!

我有一个python代码,它从raspberry_pi中读取模拟传感器,并在我的mysqldb中插入数据的seconde代码。每个代码都是单独工作的。我想把这两段代码放在一起。我的目标是将temp和timestammp插入mysqldb。 python对我来说真的很新。对不起我的坏事:-) best regars,martin

第一个代码:

#!/usr/bin/python

import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / 1023
  volts = round(volts,places)
  return volts

# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):

  # ADC Value
  # (approx)  Temp  Volts
  #    0      -50    0.00
  #   78      -25    0.25
  #  155        0    0.50
  #  233       25    0.75
  #  310       50    1.00
  #  465      100    1.50
  #  775      200    2.50
  # 1023      280    3.30

  temp = ((data * 330)/1023)-50
  temp = round(temp,places)
  return temp

# Define sensor channels
light_channel = 0
temp_channel  = 1

# Define delay between readings
delay = 5

while True:

  # Read the light sensor data
  light_level = ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)

  # Read the temperature sensor data
  temp_level = ReadChannel(temp_channel)
  temp_volts = ConvertVolts(temp_level,2)
  temp       = ConvertTemp(temp_level,2)

  # Print out results
  print "--------------------------------------------"
  print("Light: {} ({}V)".format(light_level,light_volts))
  print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))

  # Wait before repeating loop
  time.sleep(delay)

和第二个代码:

db = MySQLdb.connect(host="10.1.1.100", port=3306, user="schnickschnack", passwd="schnackschnick", db="visu")
    cursor = db.cursor()
    cursor.execute("INSERT INTO temp (value) VALUES(%s)", (temp))

2 个答案:

答案 0 :(得分:0)

只是......把它放在一起:

# ...

# Print out results
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))

# Your extra code here to add temp to the database
db = MySQLdb.connect(host="10.1.1.100", port=3306, user="schnickschnack", passwd="schnackschnick", db="visu")
cursor = db.cursor()
cursor.execute("INSERT INTO temp (value) VALUES(%s)", (temp))

# Wait before repeating loop
time.sleep(delay)

您可能希望将db =行移出循环,因此您只需连接一次数据库,而不是一遍又一遍。然后,特别是如果delay很长,你可能不会。假设你确实想要那样,那就很简单:拿走那条线,将它移到while True之上,然后将它移到第一列。

答案 1 :(得分:0)

感谢您的帮助,现在它有效!我对python使用的缩进“功能”感到困惑。 这里是代码:

#!/usr/bin/python

import spidev
import time
import os
import MySQLdb

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / 1023
  volts = round(volts,places)
  return volts

# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):

  # ADC Value
  # (approx)  Temp  Volts
  #    0      -50    0.00
  #   78      -25    0.25
  #  155        0    0.50
  #  233       25    0.75
  #  310       50    1.00
  #  465      100    1.50
  #  775      200    2.50
  # 1023      280    3.30

  temp = ((data * 330)/1023)-50
  temp = round(temp,places)
  return temp


# Define sensor channels
light_channel = 0
temp_channel  = 1
 # Define delay between readings
delay = 5


while True:

  # Read the light sensor data
  light_level = ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)

  # Read the temperature sensor data
  temp_level = ReadChannel(temp_channel)
  temp_volts = ConvertVolts(temp_level,2)
  temp       = ConvertTemp(temp_level,2)

  # Print out results
  print "--------------------------------------------"
  print("Light: {} ({}V)".format(light_level,light_volts))
  print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))

  # Your extra code here to add temp to the database
  db = MySQLdb.connect(host="10.0.1.100", port=3306, user="12233", passwd="1212", db="visu")
  cursor = db.cursor()
  cursor.execute("INSERT INTO temp (value) VALUES(%s)", (temp))

  # Wait before repeating loop
  time.sleep(delay)