Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding=utf-8
- import os, sys, time, subprocess, calendar
- from datetime import datetime
- import mysql.connector
- from mysql.connector import Error
- OverlayString = ""
- current_time = datetime.now().strftime('%H:%M:%S')
- current_date = datetime.now().strftime('%d-%m-%Y')
- def cpu_temp():
- temp = os.popen("vcgencmd measure_temp").readline()
- return (temp.replace("temp=",""))
- tfile = open("/sys/bus/w1/devices/w1_bus_master1/28-0417837f42ff/w1_slave")
- text1 = tfile.read()
- tfile.close()
- tempdata1 = text1.split()[-1]
- temp1 = float(tempdata1[2:])
- temp1 = temp1 / 1000
- temp1 = '%6.1f'%temp1
- tfile2 = open("/sys/bus/w1/devices/w1_bus_master1/28-0517908cbdff/w1_slave")
- text2 = tfile2.read()
- tfile2.close()
- tempdata2 = text2.split()[-1]
- temp2 = float(tempdata2[2:])
- temp2 = temp2 / 1000
- temp2 = '%6.1f'%temp2
- tfile3 = open("/sys/bus/w1/devices/w1_bus_master1/28-051790b51aff/w1_slave")
- text3 = tfile3.read()
- tfile3.close()
- tempdata3 = text3.split()[-1]
- temp3 = float(tempdata3[2:])
- temp3 = temp3 / 1000
- temp3 = '%6.1f'%temp3
- print temp1
- print temp2
- print temp3
- try:
- connection = mysql.connector.connect(host='localhost',
- database='Temps',
- user='Grafana',
- password='test')
- sql_insert_query = """ INSERT INTO `Temp1`
- (`Temp`, `Time`, `Date`) VALUES ('temp1', current_time, current_date)"""
- cursor = connection.cursor()
- result = cursor.execute(sql_insert_query)
- connection.commit()
- print ("Record inserted successfully into python_users table")
- except mysql.connector.Error as error :
- connection.rollback() #rollback if any exception occured
- print("Failed inserting record into Temps table {}".format(error))
- finally:
- #closing database connection.
- if(connection.is_connected()):
- cursor.close()
- connection.close()
- print("MySQL connection is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement