极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 30916|回复: 4

raspberry pi使用lcd1602开机自动显示ip地址

[复制链接]
发表于 2013-8-5 20:55:22 | 显示全部楼层 |阅读模式
本帖最后由 histamine 于 2013-8-5 20:57 编辑

使用raspberry pi + 树莓派arduino扩展板 + lcd1602扩展板 + arduPi库 + 自己移植的LiquidCrystal库实现
(源代码下载见附件)


主要部分代码:
  1. #include "arduPi.h"
  2. #include "LiquidCrystal.h"
  3. #include <stdio.h>

  4. ArduPiInit g_init;
  5. SerialPi Serial;
  6. WirePi Wire;
  7. SPIPi SPI;

  8. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

  9. int main (){
  10.         setup();
  11.         return (0);
  12. }


  13. void setup()
  14. {
  15.         pinMode(3, OUTPUT);
  16.         digitalWrite(3, LOW);
  17.         lcd.begin(16,2);
  18.         FILE* file = popen("ifconfig eth0 | perl -ne 'print $1 if /addr:([0-9.]+)/'", "r");
  19.         if (file != NULL)
  20.         {
  21.                 char buf[17] = {0};
  22.                 fgets(buf, 16, file);
  23.                 lcd.clear();
  24.                 lcd.print(buf);
  25.                 pclose(file);
  26.         }
  27. }
复制代码
解压源代码压缩包,执行make命令,编译生成lcd1602-showip执行文件
然后在/etc/network/if-up.d目录下创建新的脚本showip
sudo vi showip

  1. #!/bin/sh
  2. PATH=/sbin:/usr/sbin:/bin:/usr/bin

  3. if [ "$IFACE" = lo ]; then
  4.         exit 0
  5. fi

  6. #lcd1602-showip所在路径
  7. /home/pi/lcd1602-showip &
复制代码
添加执行权限sudo chmod +x showip

然后即可在开机时自动显示eth0的ip地址

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2018-1-28 13:21:44 | 显示全部楼层
本帖最后由 a461624201 于 2018-1-28 13:24 编辑

电脑2个网卡,python为
eth0 = os.popen("ifconfig eth0 | perl -ne 'print $1 if /addr: ([0-9.]+)/'" ).read()

wlan0 = os.popen("ifconfig wlan0 | perl -ne 'print $1 if /addr: ([0-9.]+)/'" ).read()
time.sleep(3)


print "eth0 IP:",eth0


print "wlan0 IP:",wlan0

perl的正则表达确实很好。
回复 支持 1 反对 0

使用道具 举报

发表于 2013-8-5 21:15:25 | 显示全部楼层
不错,这个有点意思。回头试试
回复 支持 反对

使用道具 举报

发表于 2013-8-12 17:52:15 | 显示全部楼层
硬件看起来有点复杂
回复 支持 反对

使用道具 举报

发表于 2017-4-9 07:55:13 | 显示全部楼层
能说说你这块板子是树莓派几?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 01:44 , Processed in 0.044802 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表