• 树莓派4B(Ubuntu20.04)使用LCD1602液晶屏开机自动显示IP及其他信息


    树莓派4B装了Ubuntu20.04当做便携服务器在使用,为了便携,自然是不接网线使用WiFi,不接显示屏使用SSH远程连接工具,那就面临一个问题,在没有固定分配IP的情况下,怎么不通过接显示器获取IP用来远程连接呢?搜索看了下,最方便的应该就是开机自启自动显示IP等信息了。

    在这里插入图片描述

    • Ubuntu20.04 Python环境下,获取IP、磁盘存储占用等配置信息
      • 获取IP
      import socket
      def getIP():
      	ip = socket.gethostbyname(socket.gethostname())
      	return 'IP:'+ str(ip)
      
      • 1
      • 2
      • 3
      • 4
      • 获取MAC
      import uuid
      def getMAC():
      	node = uuid.getnode()
      	macHex = uuid.UUID(int=node).hex[-12:]
      	MAC = []
      	for i in range(len(macHex))[::2]:
      		MAC.append(macHex[i:i+2])
      	MAC =':'.join(MAC)
      	return 'MAC:' + str(MAC)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 获取磁盘占用
      import os
      def getDisk():
      	 disk = os.statvfs("/")
      	 disk_size = disk.f_bsize * disk.f_blocks / (1024 ** 3)  # 1G = 1024M  1M = 1024KB 1KB = 1024bytes
      	 sizeStr = str("%s" % format(disk_size, '.2f'))
      	 disk_used = disk.f_bsize * (disk.f_blocks - disk.f_bfree) / (1024 ** 3)
      	 usedStr = str("%s" % format(disk_used, '.2f'))
      	 return usedStr +  "/" + sizeStr
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • 接入OLED屏幕,点亮屏幕显示
      import time
      import smbus
      BUS = smbus.SMBus(1)
      LCD_ADDR = 0x27
      BLEN = 1 #turn on/off background light
      def turn_light(key):
          global BLEN
          BLEN = key
          if key ==1 :
              BUS.write_byte(LCD_ADDR ,0x08)
          else:
              BUS.write_byte(LCD_ADDR ,0x00)
      def write_word(addr, data):
          global BLEN
          temp = data
          if BLEN == 1:
              temp |= 0x08
          else:
              temp &= 0xF7
          BUS.write_byte(addr ,temp)
      def send_command(comm):
          # Send bit7-4 firstly
          buf = comm & 0xF0
          buf |= 0x04               # RS = 0, RW = 0, EN = 1
          write_word(LCD_ADDR ,buf)
          time.sleep(0.002)
          buf &= 0xFB               # Make EN = 0
          write_word(LCD_ADDR ,buf)
          # Send bit3-0 secondly
          buf = (comm & 0x0F) << 4
          buf |= 0x04               # RS = 0, RW = 0, EN = 1
          write_word(LCD_ADDR ,buf)
          time.sleep(0.002)
          buf &= 0xFB               # Make EN = 0
          write_word(LCD_ADDR ,buf)
      def send_data(data):
          # Send bit7-4 firstly
          buf = data & 0xF0
          buf |= 0x05               # RS = 1, RW = 0, EN = 1
          write_word(LCD_ADDR ,buf)
          time.sleep(0.002)
          buf &= 0xFB               # Make EN = 0
          write_word(LCD_ADDR ,buf)
          # Send bit3-0 secondly
          buf = (data & 0x0F) << 4
          buf |= 0x05               # RS = 1, RW = 0, EN = 1
          write_word(LCD_ADDR ,buf)
          time.sleep(0.002)
          buf &= 0xFB               # Make EN = 0
          write_word(LCD_ADDR ,buf)
      def init_lcd():
          try:
              send_command(0x33) # Must initialize to 8-line mode at first
              time.sleep(0.005)
              send_command(0x32) # Then initialize to 4-line mode
              time.sleep(0.005)
              send_command(0x28) # 2 Lines & 5*7 dots
              time.sleep(0.005)
              send_command(0x0C) # Enable display without cursor
              time.sleep(0.005)
              send_command(0x01) # Clear Screen
              BUS.write_byte(LCD_ADDR ,0x08)
          except:
              return False
          else:
              return True
      def clear_lcd():
          send_command(0x01) # Clear Screen
      def print_lcd(x, y, str):
          if x < 0:
              x = 0
          if x > 15:
              x = 15
          if y <0:
              y = 0
          if y > 1:
              y = 1
          # Move cursor
          addr = 0x80 + 0x40 * y + x
          send_command(addr)
          for chr in str:
              send_data(ord(chr))
      if __name__ == '__main__':
          init_lcd()
          print_lcd(0, 0, 'Hello, world!')
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 点亮显示
      #!/user/bin/env python 
      import smbus
      import datetime
      import time
      import pytz as pytz
      import sys
      import LCD1602 as LCD
      import os
      import socket
      import uuid
      def getIP():
      	ip = socket.gethostbyname(socket.gethostname())
      	return 'IP:'+ str(ip)
      def getMAC():
      	node = uuid.getnode()
      	macHex = uuid.UUID(int=node).hex[-12:]
      	MAC = []
      	for i in range(len(macHex))[::2]:
      		MAC.append(macHex[i:i+2])
      	MAC =':'.join(MAC)
      	return 'MAC:' + str(MAC)
      def getDisk():
      	 disk = os.statvfs("/")
      	 disk_size = disk.f_bsize * disk.f_blocks / (1024 ** 3)  # 1G = 1024M  1M = 1024KB 1KB = 1024bytes
      	 sizeStr = str("%s" % format(disk_size, '.2f'))
      	 disk_used = disk.f_bsize * (disk.f_blocks - disk.f_bfree) / (1024 ** 3)
      	 usedStr = str("%s" % format(disk_used, '.2f'))
      	 return usedStr +  "/" + sizeStr
      if __name__ == '__main__':  
          LCD.init_lcd()
          time.sleep(1)
          ipStr = getIP()
          LCD.print_lcd(0, 0, ipStr)
          for x in range(1, 4):
              LCD.turn_light(0)
              LCD.print_lcd(4, 1, 'LIGHT OFF')
              time.sleep(0.5)
              LCD.turn_light(1)
              LCD.print_lcd(4, 1, 'LIGHT ON ')
              time.sleep(0.5)
          LCD.turn_light(0)
          while True:
              tz = pytz.timezone('Asia/Shanghai')  # 东八区
              show = getDisk() + " " + str(datetime.datetime.fromtimestamp(int(time.time()), tz).strftime('%H:%M'))
              LCD.print_lcd(0, 1, show )
              time.sleep(0.2)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
    • 设置开机自启动
      • 建立rc-local.service文件,如果存在的话可以不用创建
        sudo vim /etc/systemd/system/rc-local.service
      • 将下列内容复制进rc-local.service文件
        [Unit]
        Description=/etc/rc.local Compatibility
        ConditionPathExists=/etc/rc.local
        [Service]
        Type=forking
        ExecStart=/etc/rc.local start
        TimeoutSec=0
        StandardOutput=tty
        RemainAfterExit=yes
        SysVStartPriority=99
        [Install]
        WantedBy=multi-user.target
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
      • 创建文件rc.local
        sudo vim /etc/rc.local
      • 将下列内容复制进rc.local文件(对应脚本的位置和虚拟环境的位置自行修改)
        #!/bin/sh -e
        ##
        ## rc.local
        ##
        ## This script is executed at the end of each multiuser runlevel.
        ## Make sure that the script will "exit 0" on success or any other
        ## value on error.
        ##
        ## In order to enable or disable this script just change the execution
        ## bits.
        ##
        ## By default this script does nothing.
        # eaudio --start
        nohup /root/.virtualenvs/dj3/bin/python3 -u /opt/pi/getpi.py > test.out 2>&1 &
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
      • 给rc.local加上权限
        sudo chmod +x /etc/rc.local
      • 启用服务
        sudo systemctl enable rc-local
      • 启动服务并检查状态
        sudo systemctl start rc-local.service
        sudo systemctl status rc-local.service
      • 重启系统测试
        reboot
  • 相关阅读:
    最短路(spfa)hdu 2544
    BIM、建筑机器人、隧道工程施工关键技术
    巧用 transition 实现短视频 APP 点赞动画
    代碼隨想錄算法訓練營|第四十四天|01背包问题 二维、01背包问题 一维、416. 分割等和子集。刷题心得(c++)
    深度学习/pytoch/pycharm学习过程中遇到的问题
    Symfony 控制台命令教程
    91. 异步编程的实现方式?
    oracle 正则表达式多项匹配时,相似项有优先级
    matlab实现决策树可视化——信息增益、C4.5、基尼指数
    2023年PMP超全报考指南,速速收藏!
  • 原文地址:https://blog.csdn.net/weixin_40929065/article/details/126685611