我需要通过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)
如果可能的话,我真的需要将传感器序列号转换为它的名称 任何帮助将不胜感激,花了我数周时间才能到达此阶段
答案 0 :(得分: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)