#! /usr/bin/perl #Name: IOT-Status.pl version 0.2 #Source & info: www.HomoFaciens.de #http://www.HomoFaciens.de/technics-computer-internet-of-things_en_navion.htm use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $passwd = ""; my $status_only = ""; my $led_red = ""; my $led_green = ""; my $led_yellow = ""; my $led_white = ""; my $servo_1 = ""; my $servo_2 = ""; my $switch_1 = ""; my $switch_2 = ""; my $motor_direction = ""; my $motor_pwm = ""; my $do_reboot = ""; my $make_snapshot = ""; $passwd = param("passwd"); $status_only = param("statusONLY"); $led_red = param("LED_RED"); $led_green = param("LED_GREEN"); $led_yellow = param("LED_YELLOW"); $led_white = param("LED_WHITE"); $servo_1 = param("SERVO_1"); $servo_2 = param("SERVO_2"); $switch_1 = param("SWITCH_1"); $switch_2 = param("SWITCH_2"); $motor_direction = param("MOTOR_DIRECTION"); $motor_pwm = param("MOTOR_PWM"); $do_reboot = param("DOREBOOT"); $make_snapshot = param("MAKE_SNAPSHOT"); my $status_led_green = 0; my $status_led_red = 0; my $status_led_yellow = 0; my $status_led_white = 0; my $status_servo_1 = 0; my $status_servo_2 = 0; my $status_switch_1 = -1; my $status_switch_2 = -1; my $status_motor_direction = 0; my $status_motor_pwm = 0; my $status_make_snapshot = 0; my $status_received = ""; my $file_name = ""; my $system_command = ""; my $allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"; my $no_matching_password = 1; my $online_status = 0; my $last_status_time = 0; my $last_status_received = ""; my $i = 0; print "Content-type: text/html\n\n"; print '' . "\n"; print "Internet Of Things" . "\n"; &CheckParameter($led_red, "led_red", 4); &CheckParameter($led_green, "led_green", 4); &CheckParameter($led_yellow, "led_yellow", 4); &CheckParameter($led_white, "led_white", 4); &CheckParameter($servo_1, "servo_1", 4); &CheckParameter($servo_2, "servo_2", 4); &CheckParameter($switch_1, "switch_1", 4); &CheckParameter($switch_2, "switch_2", 4); &CheckParameter($motor_direction, "motor_direction", 4); &CheckParameter($motor_pwm, "motor_pwm", 4); if( !($passwd eq "")){ &CheckParameter($passwd, "passwd", 100); } if(length($passwd) > 0){ #Search for status file with matching password opendir(DIR, "status") || die "Can not find directory 'status'!\n"; while (my $file = readdir(DIR)){ if(index($file, $passwd . "_") > -1){ $status_received = substr($file, index($file, "_") + 1, length($file)); $last_status_received = $status_received; $no_matching_password = 0; $last_status_time = (stat("status/" . $file))[9]; if(time() - $last_status_time < 60){ $online_status = 1; } } } closedir(DIR); } if($no_matching_password == 1 && length($passwd) > 0){ #10 seconds pause if wrong password sleep(10); } if($make_snapshot == 2){ $system_command = "rm snapshot/" . $passwd . "_photo.jpg -f"; system($system_command); } if(length($status_received) > 18){ $status_led_red=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_led_green=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_led_yellow=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_led_white=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_switch_1=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_switch_2=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_servo_1=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_servo_2=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_motor_direction=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_motor_pwm=substr($status_received, 0, index($status_received, "_")); $status_received = substr($status_received, index($status_received, "_") + 1, length($status_received)); $status_make_snapshot=$status_received; } if($status_only == 1){ print ""; if($no_matching_password == 0){ $system_command = "rm command/" . $passwd . "_* -f"; system($system_command); $file_name = $passwd . "_" . $led_red . "_" . $led_green . "_" . $led_yellow . "_" . $led_white . "_" . $switch_1 . "_" . $switch_2 . "_" . $servo_1 . "_" . $servo_2 . "_" . $motor_direction . "_" . $motor_pwm . "_" . $do_reboot . "_" . $make_snapshot; open(FILEOUT, "> command/" . $file_name) || die("Can not open command file!"); print FILEOUT ""; close(FILEOUT); } print ''; print ''; if($status_switch_1 > -1){ print ''; if($status_switch_1 == 512){ print ''; } if($status_switch_2 > -1){ print ''; if($status_switch_2 == 512){ print ''; } if($online_status == 1){ print ''; print ''; print '
'; print 'SWITCH 1'; print ''; print "CLOSED"; } else{ print ''; print "OPEN"; } print ''; print 'SWITCH 2'; print ''; print "CLOSED"; } else{ print ''; print "OPEN"; } print ''; print 'ONLINE'; } else{ print ''; print 'OFFLINE'; } print '
'; print '
'; print ''; print ''; print ''; if($status_led_red eq $led_red && $status_led_green eq $led_green && $status_led_yellow eq $led_yellow && $status_led_white eq $led_white && $status_servo_1 eq $servo_1 && $status_servo_2 eq $servo_2 && $status_motor_direction eq $motor_direction && $status_motor_pwm eq $motor_pwm){ print ''; print ''; print ''; print ''; print ''; print '
'; print 'Status received:'; print ''; } else{ print ''; } print $last_status_received; print '
'; print 'Date / time: '; print ''; print "" . localtime($last_status_time); print '
'; print "\n"; } if($status_only == 0){ print '' . "\n"; if($no_matching_password == 1){ print ""; } else{ print ""; # print ""; } print'Welcome to the "Internet Of Things" interface!
'; print '

Status

' . "\n"; if($no_matching_password == 0){ print '
' . "\n"; print '

Control panel

' . "\n"; if($no_matching_password == 1){ print "Your password is not correct!
\n"; print "Make sure your Raspberry Pi is connected to the Internet and
the script 'IOT-Raspberry.pl' is running.
\n"; print "Type the password as set in the script 'IOT-Raspberry.pl'
and press 'Send PW' to connect with your Rapberry Pi.

\n"; } print '
' . "\n"; print ' ' . "\n"; if($no_matching_password == 1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } else{ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_led_red > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_led_yellow > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_led_green > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_led_white > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_servo_1 > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_servo_2 > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_motor_pwm > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_motor_direction > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } if($status_make_snapshot > -1){ print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; } print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' Password: ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' Note that there is a delay of approximately 30s from click to GPIO action!' . "\n"; print '
' . "\n"; print ' RED LED' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' YELLOW LED' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' GREEN LED' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' WHITE LED' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' SERVO 1' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' SERVO 2' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' MOTOR PWM' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' MOTOR DIRECTION' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' ' . "\n"; print '
' . "\n"; print ' ' . "\n"; print '
' . "\n"; print '
' . "\n"; print '
' . "\n"; print'
Source & info: www.HomoFaciens.de
'; print "" . "\n"; }#if($status_only == 0){ exit (0); #++++++++++++++++++ Sub CheckParameter +++++++++++++++++++++++++++++ sub CheckParameter{ if(length($_[0]) > $_[2]){ print "Parameter $_[1] '" . $_[0] . "'too long (max. $_[2] characters)"; exit(1); } for($i = 0; $i < length($_[0]); $i++){ if(index($allowed_chars, substr($_[0], $i, 1)) < 0){ print ""; print("Wrong character in parameter $_[1]!"); print ""; exit(1); } } }