Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding=utf-8
  3. import os, sys, time, subprocess, calendar
  4. from datetime import datetime
  5. import mysql.connector
  6. from mysql.connector import Error
  7.  
  8. OverlayString = ""
  9.  
  10. current_time = datetime.now().strftime('%H:%M:%S')
  11. current_date = datetime.now().strftime('%d-%m-%Y')
  12.  
  13. def cpu_temp():
  14. temp = os.popen("vcgencmd measure_temp").readline()
  15. return (temp.replace("temp=",""))
  16.  
  17. tfile = open("/sys/bus/w1/devices/w1_bus_master1/28-0417837f42ff/w1_slave")
  18. text1 = tfile.read()
  19. tfile.close()
  20. tempdata1 = text1.split()[-1]
  21. temp1 = float(tempdata1[2:])
  22. temp1 = temp1 / 1000
  23. temp1 = '%6.1f'%temp1
  24.  
  25. tfile2 = open("/sys/bus/w1/devices/w1_bus_master1/28-0517908cbdff/w1_slave")
  26. text2 = tfile2.read()
  27. tfile2.close()
  28. tempdata2 = text2.split()[-1]
  29. temp2 = float(tempdata2[2:])
  30. temp2 = temp2 / 1000
  31. temp2 = '%6.1f'%temp2
  32.  
  33. tfile3 = open("/sys/bus/w1/devices/w1_bus_master1/28-051790b51aff/w1_slave")
  34. text3 = tfile3.read()
  35. tfile3.close()
  36. tempdata3 = text3.split()[-1]
  37. temp3 = float(tempdata3[2:])
  38. temp3 = temp3 / 1000
  39. temp3 = '%6.1f'%temp3
  40.  
  41. print temp1
  42. print temp2
  43. print temp3
  44. try:
  45. connection = mysql.connector.connect(host='localhost',
  46. database='Temps',
  47. user='Grafana',
  48. password='test')
  49. sql_insert_query = """ INSERT INTO `Temp1`
  50. (`Temp`, `Time`, `Date`) VALUES ('temp1', current_time, current_date)"""
  51. cursor = connection.cursor()
  52. result = cursor.execute(sql_insert_query)
  53. connection.commit()
  54. print ("Record inserted successfully into python_users table")
  55. except mysql.connector.Error as error :
  56. connection.rollback() #rollback if any exception occured
  57. print("Failed inserting record into Temps table {}".format(error))
  58. finally:
  59. #closing database connection.
  60. if(connection.is_connected()):
  61. cursor.close()
  62. connection.close()
  63. print("MySQL connection is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement