【问题标题】:Python to populate sql with data based on Serial number on web pagePython根据网页上的序列号用数据填充sql
【发布时间】:2019-08-23 14:23:15
【问题描述】:

我需要从 Python 脚本生成以下格式的数据库,我已经生成了数据但格式错误,所以我需要修改代码和 sql 以使用我的本地主机网页

数据来自 Raspberry PI 和 1 线温度传感器设置

我目前有3个传感器,每个都有一个唯一的序列号,序列号显示在数据库下方,3个传感器分别是POND、FILTER、AMBIENT

    var myData = "date  Pond    Filter  Ambient\n\
2019-04-01 01:29:04 13.400  22.700  32.200\n\
2019-04-01 02:29:04 18.000  29.900  37.700\n\
2019-04-01 03:29:04 13.300  29.100  39.400\n\
2019-04-01 04:29:04 15.700  28.800  38.000\n\
2019-04-01 05:29:04 14.200  28.700  32.400\n\
2019-04-01 06:29:04 18.800  27.000  37.000\n\
2019-04-01 07:29:04 17.900  26.700  32.300\n\
2019-04-01 08:29:04 11.800  26.800  38.900\n\
2019-04-01 09:29:04 19.300  26.700  38.800\n\
2019-04-01 10:29:04 11.200  20.100  38.700\n\
2019-04-01 11:29:04 18.700  21.100  30.300\n\
2019-04-01 12:29:04 11.800  21.500  35.300\n\
2019-04-01 13:29:04 13.000  24.300  36.600\n\
2019-04-01 14:29:04 16.900  27.100  36.600\n\
2019-04-01 15:29:04 11.700  24.600  38.000\n";

每个传感器都有一个唯一的 ID,我需要给它一个名称以便于理解

28-0417c45ae5ff   =   Pond
28-0417c459f5ff   =   Filter
28-0517c48e7cff   =   Ambient

当前的 Python 脚本正在将数据发送到 sql 数据库,但我的新网页的格式错误,所以我需要更改 Python 和 sql 以正确记录数据

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import fnmatch
import time
import MySQLdb as mdb
import logging

logging.basicConfig(filename='/home/pi/Sensor_error.log',
  level=logging.DEBUG,
  format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)

# Load the modules (not required if they are loaded at boot) 
# os.system('modprobe w1-gpio')
# os.system('modprobe w1-therm')

# Function for storing readings into MySQL
def insertDB(IDs, temperature):

  try:

    con = mdb.connect('localhost',
                      'temp_insert',
                      'Insert',
                      'measurements');
    cursor = con.curssql = "INSERT INTO temperature(temperature, sensor_id)\
      VALUES ('%s', '%s')" % \
      ( temperature[i], IDs[i])
      cursor.execute(sql)
      sql = []
      con.commit()

    con.close()

  except mdb.Error, e:
    logger.error(e)

# Get readings from sensors and store them in MySQL

temperature = []
IDs = []

for filename in os.listdir("/sys/bus/w1/devices"):
  if fnmatch.fnmatch(filename, '28-*'):
    with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as f_obj:
      lines = f_obj.readlines()
      if lines[0].find("YES"):
        pok = lines[1].find('=')
        temperature.append(float(lines[1][pok+1:pok+6])/1000)
        IDs.append(filename)
      else:
        logger.error("Error reading sensor with ID: %s" % (filename))

if (len(temperature)>0):
  insertDB(IDs, temperature)

如果可能的话,我真的需要将传感器序列号翻译成它的名称 任何帮助将不胜感激,我花了几个星期才到这个阶段

【问题讨论】:

    标签: python mysql phpmyadmin mariadb


    【解决方案1】:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import os
    import fnmatch
    import time
    import MySQLdb as mdb
    import logging
    
    logging.basicConfig(filename='/home/pi/Sensor_error.log',level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(name)s %(message)s')
    logger=logging.getLogger(__name__)
    
    # Load the modules (not required if they are loaded at boot) 
    # os.system('modprobe w1-gpio')
    # os.system('modprobe w1-therm')
    
    # Function for storing readings into MySQL
    def insertDB(IDs, temperature):
    
    try:
    
        con = mdb.connect('localhost',
                      'temp_insert',
                      'Insert',
                      'measurements');
        cursor = con.curssql = "INSERT INTO temperature(temperature, sensor_id)\
               VALUES ('%s', '%s')" % \
                ( temperature[i], IDs[i])
        cursor.execute(sql)
        sql = []
        con.commit()
    
        con.close()
    
    except mdb.Error, e:
        logger.error(e)
    
    # Get readings from sensors and store them in MySQL
    
    temperature = []
    IDs = []
    sensor_switch = {'28-0417c45ae5ff':'Pond', '28-0417c459f5ff':'Filter',
                   '28-0517c48e7cff':'Ambient'} # a dictionary of ids
    
    for filename in os.listdir("/sys/bus/w1/devices"):
        if fnmatch.fnmatch(filename, '28-*'):
            with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as f_obj:
                lines = f_obj.readlines()
                if lines[0].find("YES"):
                    pok = lines[1].find('=')
                    temperature.append(float(lines[1][pok+1:pok+6])/1000)
                    IDs.append(sensor_switch.get(str(filename),'key_mismatch')) 
                    # use a dictionary's get method to switch content
                    # filename = '28-0417c45ae5ff' is switched to 'pond'
                else:
                    logger.error("Error reading sensor with ID: %s" % (filename))
    
    if (len(temperature)>0):
        insertDB(IDs, temperature)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2020-11-26
      相关资源
      最近更新 更多