davidjmorin

Working Python Raspberry Pi Status Update

Nov 13th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. from __future__ import print_function
  2. from urllib.request import urlopen
  3. import json
  4. import subprocess
  5. import requests
  6. import random
  7. import sys
  8. import os
  9. import time
  10. import datetime
  11. import MySQLdb as my
  12. import psutil
  13.  
  14. db = my.connect(host=".com",
  15. user="",
  16. passwd="",
  17. db="test"
  18. )
  19.  
  20. cursor = db.cursor()
  21.  
  22. def getCPUtemperature():
  23. res = os.popen('vcgencmd measure_temp').readline()
  24. return(res.replace("temp=","").replace("'C\n",""))
  25.  
  26. r = requests.get('http://ipv4.icanhazip.com/')
  27. p = r.text
  28.  
  29.  
  30. name = "Rockland, ME"
  31. now = datetime.datetime.now()
  32. currentTime = datetime.datetime.now()
  33. temp = float(getCPUtemperature())
  34. temp = (temp * 1.8) + 32
  35. boot = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
  36. id = 10008
  37.  
  38. sql = "insert into test VALUES(NULL, '%s', '%s', '%s', '%s', '%s')" % \
  39. (name, temp , boot, currentTime, p)
  40.  
  41. number_of_rows = cursor.execute(sql)
  42. db.commit()
  43.  
  44. db.close()
  45.  
  46.  
  47.  
  48.  
  49. #### Edit crontab -e #### / Dont use sudo crontabe
  50.  
  51. */1 * * * * sh /home/pi/python/status.sh >/home/pi/logs/cronlog1 2>&1
  52.  
  53. ######################
  54.  
  55. ###### status.sh ######
  56.  
  57. #!/bin/sh
  58. # launcher.sh
  59. # navigate to home directory, then to this directory, then execute python scrip$
  60.  
  61.  
  62. sudo python3 /home/pi/python/sensors.py
  63.  
  64. ############################
  65.  
  66. Required Installs
  67. sudo pip3 install mysqlclient
  68. sudo pip3 install psutil
  69. sudo apt-get update
  70. sudo apt install mariadb-server
  71. mkdir python
  72. mkdir logs
  73. sudo nano python/sensors.py
  74. sudo nano python/status.sh
  75.  
  76. sudo nano /etc/motd
Add Comment
Please, Sign In to add comment