Advertisement
Luigi998

16x2 LCD Raspi-Server

Oct 29th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. import smbus
  3. import time
  4. import psutil
  5. import socket
  6. import subprocess
  7. import os
  8. import RPi.GPIO as GPIO
  9.  
  10. GPIO.setmode(GPIO.BOARD)
  11. GPIO.setwarnings(False)
  12.  
  13. GPIO.setup(8, GPIO.IN)
  14. GPIO.setup(10, GPIO.IN)
  15. GPIO.setup(26, GPIO.OUT)
  16. GPIO.setup(24, GPIO.OUT)
  17. GPIO.setup(22, GPIO.OUT)
  18.  
  19. GPIO.output(26, 0)
  20. GPIO.output(24, 0)
  21. GPIO.output(22, 0)
  22.  
  23. ########################################################
  24.  
  25. I2C_ADDR = 0x27
  26. LCD_WIDTH = 16
  27.  
  28. LCD_CHR = 1
  29. LCD_CMD = 0
  30.  
  31. LCD_LINE_1 = 0x80
  32. LCD_LINE_2 = 0xC0
  33. LCD_LINE_3 = 0x94
  34. LCD_LINE_4 = 0xD4
  35.  
  36. LCD_BACKLIGHT = 0x08 # On
  37. #LCD_BACKLIGHT = 0x00 # Off
  38.  
  39. ENABLE = 0b00000100
  40.  
  41. E_PULSE = 0.0005
  42. E_DELAY = 0.0005
  43.  
  44. bus = smbus.SMBus(1)
  45.  
  46. def red():
  47. GPIO.output(26, 1)
  48. GPIO.output(24, 0)
  49. GPIO.output(22, 0)
  50.  
  51. def green():
  52. GPIO.output(26, 0)
  53. GPIO.output(24, 1)
  54. GPIO.output(22, 0)
  55.  
  56. def blue():
  57. GPIO.output(26, 0)
  58. GPIO.output(24, 0)
  59. GPIO.output(22, 1)
  60.  
  61. def black():
  62. GPIO.output(26, 0)
  63. GPIO.output(24, 0)
  64. GPIO.output(22, 0)
  65.  
  66. def lcd_init():
  67. lcd_byte(0x33,LCD_CMD)
  68. lcd_byte(0x32,LCD_CMD)
  69. lcd_byte(0x06,LCD_CMD)
  70. lcd_byte(0x0C,LCD_CMD)
  71. lcd_byte(0x28,LCD_CMD)
  72. lcd_byte(0x01,LCD_CMD)
  73. time.sleep(E_DELAY)
  74.  
  75. def lcd_byte(bits, mode):
  76. bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  77. bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
  78.  
  79. bus.write_byte(I2C_ADDR, bits_high)
  80. lcd_toggle_enable(bits_high)
  81.  
  82. bus.write_byte(I2C_ADDR, bits_low)
  83. lcd_toggle_enable(bits_low)
  84.  
  85. def lcd_toggle_enable(bits):
  86. time.sleep(E_DELAY)
  87. bus.write_byte(I2C_ADDR, (bits | ENABLE))
  88. time.sleep(E_PULSE)
  89. bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  90. time.sleep(E_DELAY)
  91.  
  92. def lcd_string(message,line):
  93. message = message.ljust(LCD_WIDTH," ")
  94. lcd_byte(line, LCD_CMD)
  95. for i in range(LCD_WIDTH):
  96. lcd_byte(ord(message[i]),LCD_CHR)
  97.  
  98. def getCpuTemperature():
  99. tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
  100. cpu_temp = tempFile.read()
  101. tempFile.close()
  102. return float(cpu_temp)/1000
  103.  
  104. def getRAMinfo():
  105. p = os.popen('free')
  106. i = 0
  107. while 1:
  108. i = i + 1
  109. line = p.readline()
  110. if i==2:
  111. return(line.split()[1:4])
  112.  
  113. ########################################################
  114.  
  115. host = socket.gethostname()
  116. ipnum = subprocess.check_output(["hostname", "-I"])
  117.  
  118. ########################################################
  119.  
  120. def main():
  121. # Main program block
  122. blue()
  123. # Initialise display
  124. lcd_init()
  125.  
  126. ## state - decides what LED should be on and off
  127. ## initialize to 0 or all off
  128. state = 1
  129.  
  130. ## increment - the direction of states
  131. ## initialize to 1 or increasing
  132. inc = 1
  133.  
  134. ## Set prev_input's initial value to 2
  135. prev_input=2
  136.  
  137. while True:
  138.  
  139. ## When state toggle button is pressed
  140. if ( GPIO.input(8) == True ):
  141. green()
  142. time.sleep(1)
  143. state = state + 1
  144. else:
  145. time.sleep(0.2)
  146.  
  147. if (state == 1): #CPU
  148. # print("CPU")
  149. blue()
  150. CPU_usage = psutil.cpu_percent(interval = .5)
  151. lcd_string("CPU Load %s%%" % (CPU_usage),LCD_LINE_1)
  152. lcd_string("CPU Temp %s" % (getCpuTemperature()),LCD_LINE_2)
  153.  
  154. elif (state == 2): #RAM
  155. # print("RAM")
  156. blue()
  157. RAM_stats = getRAMinfo()
  158. RAM_used = round(int(RAM_stats[1]) / 1000,1)
  159. RAM_free = round(int(RAM_stats[2]) / 1000,1)
  160. lcd_string("RAM Used %sMB " % (RAM_used),LCD_LINE_1)
  161. lcd_string("RAM Free %sMB " % (RAM_free),LCD_LINE_2)
  162.  
  163. elif (state == 3): #IP
  164. # print("IP")
  165. blue()
  166. lcd_string("%s" % (host),LCD_LINE_1)
  167. lcd_string("%s" % (ipnum)[0:14],LCD_LINE_2)
  168. # print(host)
  169. # print(ipnum)[0:14]
  170.  
  171. elif (state == 4): #SHUTDOWN
  172. if ( state == 4 and GPIO.input(10) == True ):
  173. # print("SHUTDOWN BUTTON")
  174. red()
  175. time.sleep(2)
  176. os.system('sudo shutdown -h now')
  177. exit()
  178. else:
  179. # print("SHUTDOWN")
  180. blue()
  181. lcd_string("SHUTDOWN",LCD_LINE_1)
  182. lcd_string(" ",LCD_LINE_2)
  183.  
  184. elif (state == 5): #REBOOT
  185. if ( state == 5 and GPIO.input(10) == True ):
  186. # print("REBOOT BUTTON")
  187. red()
  188. time.sleep(2)
  189. os.system('sudo shutdown -r now')
  190. exit()
  191. else:
  192. # print("REBOOT")
  193. blue()
  194. lcd_string("REBOOT",LCD_LINE_1)
  195. lcd_string(" ",LCD_LINE_2)
  196.  
  197. elif (state == 6): #OFF
  198. # print("OFF")
  199. LCD_BACKLIGHT = 0x00
  200. black()
  201. state = state - 6
  202.  
  203. else:
  204. time.sleep(0.2)
  205.  
  206. if __name__ == '__main__':
  207. try:
  208. main()
  209. except (KeyboardInterrupt, SystemExit):
  210. LCD_BACKLIGHT = 0x00
  211. black()
  212. pass
  213. finally:
  214. lcd_byte(0x01, LCD_CMD)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement