登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

windfly's sky

the sky I can fly like the wind

 
 
 

日志

 
 

树莓派2装一个LCD显示SSID和ip地址  

2016-09-03 13:46:39|  分类: RPI |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
我的树莓派2开始时加了一个无线模块的和hdmi转ga模块的,之前的使用都是街上屏幕,鼠标,键盘,登录。可是有时候却需要显示器接到笔记本电脑上,而树莓派可以通过ssh登录。
那么把树莓派的显示器,鼠标键盘拔掉,直接上电启动,在笔记本上ssh登录不是更好吗?可是由于我这边有两个无线热点可用,而且IP地址是自动获取的,所以就想到了加一个LCD显示SSID和IP地址。
1.首先是硬件接线
最普通的2X16的HDD44780的LCD模块。

LCD 各个引脚的定义:

  1. Ground
  2. VCC - 5v not 3.3v
  3. Contrast adjustment (VO) from potentiometer
  4. Register Select (RS). RS=0: Command, RS=1: Data
  5. Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won’t use this pin)
  6. Clock (Enable). Falling edge triggered
  7. Bit 0 (Not used in 4-bit operation)
  8. Bit 1 (Not used in 4-bit operation)
  9. Bit 2 (Not used in 4-bit operation)
  10. Bit 3 (Not used in 4-bit operation)
  11. Bit 4
  12. Bit 5
  13. Bit 6
  14. Bit 7
  15. Backlight LED Anode (+)
  16. Backlight LED Cathode (-)

重要的是3要接到电位器的中间,我手头上没有电位器,就直接接到gnd上了,另外,没有用面包板,直接用杜邦线接的,是足够的。我的几个中重要的接线是

  1. Ground----------------------------------接0V
  2. VCC - -------------------------------------接5V
  3. Contrast adjustment (VO) from potentiometer----------------接了0V(如有电位器应接电位器)
  4. Register Select (RS). RS=0: Command, RS=1: Data-------------------------接28(wiringPI  编号)
  5. Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won’t use this pin)------接0V
  6. Clock (Enable). Falling edge triggered-----------------------------------------------接29(wiringPI编号)
  7. Bit 0 (Not used in 4-bit operation)
  8. Bit 1 (Not used in 4-bit operation)
  9. Bit 2 (Not used in 4-bit operation)
  10. Bit 3 (Not used in 4-bit operation)
  11. Bit 4-------------------------------------------------------------------------------------------接25
  12. Bit 5------------------------------------------------------------------------------------------接24
  13. Bit 6-----------------------------------------------------------------------------------接23
  14. Bit 7--------------------------------------------------------------------------------接22
  15. Backlight LED Anode (+)----------------------------------------------------接5V
  16. Backlight LED Cathode (-)---------------------------------------------------接0V
参考:https://projects.drogon.net/raspberry-pi/wiringpi/lcd-library/
参考http://www.geekfan.net/5588/
线接好上电就能看到上半屏全部点亮。
2.程序调试
安装wiringpi的步骤就不写了,参看前面的文章。
思路就是
先写一个脚本,把SSID和IP写入两个文件中。然后在程序中调用脚本,读取这两个文件的内容。
脚本如下:
sleep 10  //此处为了等待系统启动后再执行
ifconfig wlan0|grep 'netmask'|sed 's/inet//g'|sed 's/netmask.*//g'|awk '{print $1}'>ip.dat
iwconfig wlan0|grep SSID|awk '{print $4}'|sed 's/ESSID\://g'|sed 's/\"//g'>ssid.dat
修改LCD的参考代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <unistd.h>
#include <string.h>
#include <time.h>

#include <wiringPi.h>
#include <lcd.h>

#ifndef    TRUE
#  define    TRUE    (1==1)
#  define    FALSE    (1==2)
#endif

// Global lcd handle:

static int lcdHandle ;

int main (int argc, char *argv[])
{
  int i ;
  int lcd ;
  int bits, rows, cols ;

  struct tm *t ;
  time_t tim ;

  char buf [32] ;

/*  if (argc != 4)
    return usage (argv [0]) ;

  printf ("Raspberry Pi LCD test\n") ;
  printf ("=====================\n") ;

  bits = atoi (argv [1]) ;
  cols = atoi (argv [2]) ;
  rows = atoi (argv [3]) ;
*/
  bits=4;
  cols=16;
  rows=2;

  if (!((rows == 1) || (rows == 2) || (rows == 4)))
  {
    fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
    return EXIT_FAILURE ;
  }

  if (!((cols == 16) || (cols == 20)))
  {
    fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
    return EXIT_FAILURE ;
  }

  wiringPiSetup () ;

  if (bits == 4)
    lcdHandle = lcdInit (rows, cols, 4, 28,29, 25,24,23,22,0,0,0,0) ;
  else
    lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;

  if (lcdHandle < 0)
  {
    fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
    return -1 ;
  }
  //add by su
  system("./writedata");
FILE *fp;
  char strip[16];
  char strssid[16];
//for (;;)
//{
 // sleep 100;
  if((fp=fopen("ip.dat","rt"))==NULL) strcpy(strip,"Open IP error");
   else {fgets(strip,16,fp); fclose(fp);}

   if((fp=fopen("ssid.dat","rt"))==NULL) strcpy(strssid,"Open SSID error");
   else {fgets(strssid,16,fp); fclose(fp);}


  lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, strssid) ;
  lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, strip) ;
  return 0 ;
}
3加入自动启动
写一个运行脚本命名为runlcd内容为
cd /root/lcd
./lcd
然后在/etc/rc.local中加入这个脚本
就能开机启动了。
  评论这张
 
阅读(836)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018