#! /usr/bin/perl #Name: IOT-Raspberry.pl version 0.2 #Source & info: www.HomoFaciens.de #http://www.HomoFaciens.de/technics-computer-internet-of-things_en_navion.htm use strict; use LWP::UserAgent; use HTTP::Request::Common; use Getopt::Long; use File::Basename; use LWP::Simple; use Time::HiRes qw(usleep nanosleep); $|=1 ; #Password for access through browser interface. Use a-z, A-Z and 0-1, NO SPECIAL CHARACTERS, NO SPACE!!! #Min length 8 characters, max length 30 characters my $password = "TypeYourPassWordHere"; #$encrypt_key_shift must have all alphanumeric characters with random order and must be identical to IOT-Status.pl my $encrypt_key_shift="GLZPDWOKFISVUHYTQBNJMAERxswCgzdpkoilXfujhtybqmnvcare0987654321"; my $i = 0; my $allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-"; if(length($password) < 8){ print "Password '" . $password . "'too short (min. 8 characters)"; } if(length($password) > 30){ print "Password '" . $password . "'too long (max. 30 characters)"; } if( !($password eq "")){ for($i = 0; $i < length($password); $i++){ if(index($allowed_chars, substr($password, $i, 1)) < 0){ print("Invalid character in password!"); exit(1); } } } else{ print("Password not set!"); exit(1); } my $encode_string = ""; my $char_pos = 0; for($i = 0; $i < length($password); $i++){ $char_pos = index($encrypt_key_shift, substr($password, $i, 1)); $encode_string = $encode_string . $char_pos . "-"; } $password = $encode_string; print "passwd=" . $password . "\n"; #Set -1 to disable 0 to enable a device (GPIO) #Disabled devices are removed from the browser interface my $led_green_active = 0; my $led_yellow_active = 0; my $led_red_active = 0; my $led_white_active = 0; my $motor_direction_active = 0; my $motor_pwm_active = 0; my $servo_1_active = 0; my $servo_2_active = 0; my $switch_1_active = 0; my $switch_2_active = 0; my $make_snapshot_active = 0; my $led_green = $led_green_active; my $led_yellow = $led_yellow_active; my $led_red = $led_red_active; my $led_white = $led_white_active; my $motor_direction = $motor_direction_active; my $motor_pwm = $motor_pwm_active; my $servo_1 = $servo_1_active; my $servo_2 = $servo_2_active; my $switch_1 = $switch_1_active; my $switch_2 = $switch_2_active; my $make_snapshot = $make_snapshot_active; my $camera_only = 1; my $script_answer=""; my $temp = 0; my $char_temp = ""; my $led_green_old = -1000; my $led_yellow_old = -1000; my $led_red_old = -1000; my $led_white_old = -1000; my $motor_direction_old = -1000; my $motor_pwm_old = -1000; my $servo_1_old = -1000; my $servo_2_old = -1000; my $make_snapshot_old = 0; my $servo_1_duration = 2; my $servo_1_duration_start = 0; my $servo_2_duration = 2; my $servo_2_duration_start = 0; my $last_call = 0; my $do_reboot = 0; my $snapshot_file = "photo.jpg"; my $upload_command = ""; my $url_next_command = 'http://robospatium.de/IOT/IOT-Command.pl?passwd=' . $password ; #restart GPIO-pwm print "Stopping GPIO-pwm\n"; system("killall GPIO-pwm"); sleep(2); if($led_red_active > -1){ $camera_only = 0; } if($led_green_active > -1){ $camera_only = 0; } if($led_yellow_active > -1){ $camera_only = 0; } if($led_white_active > -1){ $camera_only = 0; } if($servo_1_active > -1){ $camera_only = 0; } if($servo_2_active > -1){ $camera_only = 0; } if($motor_pwm_active > -1){ $camera_only = 0; } if($motor_direction_active > -1){ $camera_only = 0; } if($camera_only == 0){ print "Restarting GPIO-pwm\n"; system("nice -n 0 ./GPIO-pwm&"); sleep(5); print "Initializing peripherals\n"; } #initialize the GPIOs if($led_white_active > -1){ system("> /dev/GPIO-pwm/0-0");#White LED } if($motor_pwm_active > -1){ system("> /dev/GPIO-pwm/2-0");#PWM H bridge } if($motor_direction_active > -1){ system("> /dev/GPIO-pwm/3-0");#Direction H bridge } if($servo_1_active > -1){ system("> /dev/GPIO-pwm/4-0");#Servo 1 } if($servo_2_active > -1){ system("> /dev/GPIO-pwm/5-0");#Servo 2 } if($led_green_active > -1){ system("> /dev/GPIO-pwm/12-0");#Green LED } if($led_yellow_active > -1){ system("> /dev/GPIO-pwm/13-0");#Yellow LED } if($led_red_active > -1){ system("> /dev/GPIO-pwm/14-0");#Red LED } #State of switch is either open (256) or closed (512) if($switch_1_active > -1){ $switch_1 = system("./GPIO-status 15 > /dev/null"); } if($switch_2_active > -1){ $switch_2 = system("./GPIO-status 16 > /dev/null"); } #print "Switch 1 = " . $switch_1 . "\n"; #print "Switch 2 = " . $switch_2 . "\n"; $last_call = time(); while (1 == 1){#Program loop runs for ever if(time - $last_call > 10){ $last_call = time(); $script_answer = get $url_next_command . "&LED_RED=" . $led_red . "&LED_GREEN=" . $led_green . "&LED_YELLOW=" . $led_yellow . "&LED_WHITE=" . $led_white . "&SWITCH_1=" . $switch_1 . "&SWITCH_2=" . $switch_2 . "&SERVO_1=" . $servo_1 . "&SERVO_2=" . $servo_2 . "&MOTOR_DIRECTION=" . $motor_direction . "&MOTOR_PWM=" . $motor_pwm . "&MAKE_SNAPSHOT=" . $make_snapshot; print "Script answer=\"" . $script_answer . "\" at " . (localtime) . "\n"; if(length($script_answer) > 18){ $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($led_red_active > -1){ if($temp < 0){ $temp = 0; } $led_red = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($led_green_active > -1){ if($temp < 0){ $temp = 0; } $led_green = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($led_yellow_active > -1){ if($temp < 0){ $temp = 0; } $led_yellow = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($led_white_active > -1){ if($temp < 0){ $temp = 0; } $led_white = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($switch_1_active > -1){ if($temp < 0){ $temp = 0; } $switch_1 = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($switch_2_active > -1){ if($temp < 0){ $temp = 0; } $switch_2 = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($servo_1_active > -1){ if($temp < 0){ $temp = 0; } $servo_1 = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($servo_2_active > -1){ if($temp < 0){ $temp = 0; } $servo_2 = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($motor_direction_active > -1){ if($temp < 0){ $temp = 0; } $motor_direction = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($motor_pwm_active > -1){ if($temp < 0){ $temp = 0; } $motor_pwm = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($do_reboot > -1){ if($temp < 0){ $temp = 0; } $do_reboot = $temp; } $temp=substr($script_answer, 0, index($script_answer, "_")); $script_answer = substr($script_answer, index($script_answer, "_") + 1, length($script_answer)); if($make_snapshot_active > -1){ if($temp < 0){ $temp = 0; } $make_snapshot = $temp; } }#if(length($script_answer) > 18){ }#if(time - $last_call > 10){ #State of switch is either open (256) or closed (512) if($switch_1_active > -1){ $switch_1 = system("./GPIO-status 15 > /dev/null"); } if($switch_2_active > -1){ $switch_2 = system("./GPIO-status 16 > /dev/null"); } if($led_green_old != $led_green){ if($led_green > -1 && $led_green < 201 && $led_green_active > -1){ system ("> /dev/GPIO-pwm/12-$led_green"); } } if($led_red_old != $led_red){ if($led_red > -1 && $led_red < 201 && $led_red_active > -1){ system ("> /dev/GPIO-pwm/14-$led_red"); } } if($led_yellow_old != $led_yellow){ if($led_yellow > -1 && $led_yellow < 201 && $led_yellow_active > -1){ system ("> /dev/GPIO-pwm/13-$led_yellow"); } } if($led_white_old != $led_white){ if($led_white > -1 && $led_white < 201 && $led_white_active > -1){ system ("> /dev/GPIO-pwm/0-$led_white"); } } #Set servos if($servo_1_active > -1){ if($servo_1_old != $servo_1){ if($servo_1 > -1 && $servo_1 < 201){ system ("> /dev/GPIO-pwm/4-$servo_1"); $servo_1_duration_start = time(); } } if(time() - $servo_1_duration_start > $servo_1_duration){ system ("> /dev/GPIO-pwm/4-0");#Stop servo pulses } } if($servo_2_active > -1){ if($servo_2_old != $servo_2){ if($servo_2 > -1 && $servo_2 < 201){ system ("> /dev/GPIO-pwm/5-$servo_2"); $servo_2_duration_start = time(); } } if(time() - $servo_2_duration_start > $servo_2_duration){ system ("> /dev/GPIO-pwm/5-0");#Stop servo pulses } } #Set H bridge if($motor_direction_old != $motor_direction || $motor_pwm_old != $motor_pwm){ if($motor_direction == 0){ if($motor_pwm > -1 && $motor_pwm < 201){ if($motor_direction_active > -1){ system ("> /dev/GPIO-pwm/3-0");#Direction } if($motor_pwm_active > -1){ system ("> /dev/GPIO-pwm/2-$motor_pwm");#PWM } } } else{ if($motor_pwm > -1 && $motor_pwm < 201){ if($motor_direction_active > -1){ system ("> /dev/GPIO-pwm/3-200");#Direction } $temp = 200 - $motor_pwm; if($motor_pwm_active > -1){ system ("> /dev/GPIO-pwm/2-$temp");#PWM } } } } if($do_reboot == 1){ print "Rebooting...\n"; system ("init 6"); } if($do_reboot == 2){ print "Shuting down!\n"; system ("init 0"); } if($make_snapshot == 1 && $make_snapshot_old > 25){ $make_snapshot = 0; system("raspistill -w 320 -h 240 -o " . $snapshot_file); #When using an USB webcam, comment out the line above and uncoment the line below #system("avconv -y -f video4linux2 -i /dev/video0 -frames 10 -s 320x240 %08d.jpg");system("cp 00000010.jpg " . $snapshot_file); if ( -e $snapshot_file ) { $upload_command = "snapshot"; my $ua = LWP::UserAgent->new(); my $req = POST $url_next_command, Content_Type => 'form-data', Content => [submit => 1, TRANSFER_FILE => [ $snapshot_file ], passwd => $password, UPLOAD_COMMAND => $upload_command]; my $response = $ua->request($req); if ($response->is_success()) { print "Sending snapshot OK: " . $response->content . "\n"; } else{ print "ERROR sending snapshot: " . $response->as_string . "\n"; } } else{ print $snapshot_file . " not found, probably camera not installed?\n"; } $make_snapshot_old = 0; } $led_green_old = $led_green; $led_yellow_old = $led_yellow; $led_red_old = $led_red; $led_white_old = $led_white; $motor_direction_old = $motor_direction; $motor_pwm_old = $motor_pwm; $servo_1_old = $servo_1; $servo_2_old = $servo_2; $make_snapshot_old++; # print "led_red=" . $led_red . "\n"; # print "led_green=" . $led_green . "\n"; # print "led_yellow=" . $led_yellow . "\n"; # print "led_white=" . $led_white . "\n"; # print "switch_1=" . $switch_1 . "\n"; # print "switch_2=" . $switch_2 . "\n"; # print "servo_1=" . $servo_1 . "\n"; # print "servo_2=" . $servo_2 . "\n"; # print "motor_direction=" . $motor_direction . "\n"; # print "motor_pwm=" . $motor_pwm . "\n"; sleep(1); }#while (1 == 1)