A compiled C program that reads a text file and flashes an IP address via LED. The text file that
is read is "ifconfig.txt" and is made in directory /home/pi by a script that will
be mentioned later. For testing purposes, this text file can be created by starting a
terminal session at location /home/pi and entering the following at the prompt:
ifconfig > ifconfig.txt
which will simply redirect the ifconfig information to the text file.
You can either grab the program text for captPike.c below or download it
here.
Place it in the /home/pi directory. Use the following command to compile it as to include the location
of the needed wiringPi library:
gcc -Wall -I/usr/local/include -L/usr/local/lib -lwiringPi captPike.c -o captPike
Make sure the file has correct privileges by
sudo chmod 755 captPike
If you have a good ifconfig.txt file then test captPike by entering:
sudo ./captPike lo
This should blink the loopback "lo" IP address 127.0.0.1
If you have ethernet or WiFi sucessfully configured and connected then you can
replace "lo" on the command line above with "eth0" or "wlan0".
Note that the sudo is needed for the wiringPi component.
Also note that if you have not aquired an IP address for these, captPike will do nothing.
The captPIke blink protocol is as follows:
3 seconds 'on' as a start 'get ready' indicator
digits are displayed as half second 'on'/'off'
space between digits is a second and a half
'dots' are displayed as a 150 millisecond 'blip'
Note: digit 0 (zero) is displayed as 10 blinks (you can't blink zero times!)
/*=================================================================
Program: captPike.c
Purpose: To flash an LED indicating an IP address.
Usage : sudo ./captPIke wlan0 (example: could be eth0, lo, etc...)
Author : Bob Schumann (SampleBandit)
Date : July 2013
Notes : Prior to calling this program, a script runs the command
: ifconfg > ifconfig.txt. This program opens that text file
: and searches for the ip address that matches the command
: line argument interface (see Usage above ^ ) Using the
: wiringPi component requires the 'sudo' preface.
:
: The captPIke blink protocol is as follows:
: ------------------------------------------
: 3 seconds 'on' as a start 'get ready' indicator
: digits are displayed as half second 'on'/'off'
: space between digits is a second and a half
: 'dots' are displayed as a 150 millisecond 'blip'
: Note: digit 0 (zero) is displayed as 10 blinks
: you can't blink zero times!
Thanks : Gordon Henderson for wiringPi
=================================================================*/
#include <stdio.h>
#include <string.h>
#include <wiringPi.h>
int main(int argc, char *argv[])
{
int i, j;
char *infilename = "ifconfig.txt";
FILE *infile;
char line_buffer[BUFSIZ];
char *search = "inet addr:";
char *found;
int ip[15];
int ip_count;
/* open text file */
infile = fopen(infilename, "r");
/* wiringPi settings */
wiringPiSetup();
pinMode(1,OUTPUT);
/* get to the section where argument interface is defined */
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
found = strstr(line_buffer, argv[1]);
if (found != NULL) {
/* the next line has the ip address */
fgets(line_buffer, sizeof(line_buffer), infile);
/* the ip address is after "inet addr:" */
found = strstr(line_buffer, search);
found += strlen(search);
/* copy ip to array */
i=0;
while (found[i] != 32) {
/* normalize ASCII to integer */
ip[i]=found[i]-48;
i++;
}
ip_count=i;
}
}
/* close text file */
fclose(infile);
/* 3 second start indicator */
digitalWrite(1, HIGH);
delay(3000);
digitalWrite(1, LOW);
delay(3000);
/* display IP to LED */
for (i=0;i<ip_count;i++) {
if (ip[i] >= 0) {
/* digits */
if (ip[i] == 0)
/* Zero is special case blink 10 times */
ip[i]=10;
for (j=0;j<ip[i];j++) {
digitalWrite(1, HIGH);
delay(500);
digitalWrite(1, LOW);
delay(500);
}
delay(1500);
}
else
{
/* dot */
digitalWrite(1, HIGH);
delay(150);
digitalWrite(1, LOW);
delay(1500);
}
}
return(0);
}
At this point captPike is ready to use but I wanted to execute it at start up and
show me the IP address it had aquired. I had to make a startup script and install it. I had
a little trouble at first because captPike
would run before the lan drivers were going and I got nothing. The '# Required-Start: $all'
line in the following script ensures everything is loaded before my script is called. Notice
the start and stop sections in the case statement. These are called by the system at startup
and shutdown once the system has installed the script.
You can either grab the script text for captPike.sh below or download it
here.
Copy this file to location /etc/init.d (use sudo if needed)
sudo cp captPike.sh /etc/init.d
Make sure the file has correct privileges by
sudo chmod 775 etc/init.d/captPike.sh
To install the script:
sudo update-rc.d captPike.sh defaults
To uninstall the script:
sudo update-rc.d captPike.sh remove
### BEGIN INIT INFO
# Provides: captPike
# Required-Start: $all
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Display IP address via LED at boot time
# Description: Display IP address via LED at boot time
### END INIT INFO
#! /bin/sh
# /etc/init.d/captPike
case "$1" in
start)
echo "captPike Blinks IP Address"
# Replace wlan0 with alternate interface if needed
cd /home/pi
ifconfig > ifconfig.txt
sudo ./captPike wlan0
;;
stop)
echo "Stopping captPike"
;;
*)
echo "Usage: /etc/init.d/captPike.sh"
exit 1
;;
esac
exit 0