How to Setup a Raspberry Pi Web Server Excerpts

Please add the following corrections:

 

 

On pg. 248 add the following:<br>To restore your website from a usb stick:

   cp -r -p /mnt/usb/* var/www/html.

 

On pg. 249 add the following:<br>to restore a single file from a remote web server type:
  scp index.html dave@192.168.1.6:/home/dave

 

On pg. 254 line #8 should read:

   mysql -uroot -p<i>password</i> dictionary < /home/pi/dictionary.backup

 

 

The VLC app has changed since the writing of this book.  If you are having difficulty getting the web cam to stream try this:

 

1.      Create a folder under the /var/www/html folder called "live" like this:

sudo mkdir /var/www/html/live

 

2.      Now enter this as one line into the command terminal for your Raspberry Pi, making sure to use the correct IP address for your Pi (Note that the command line is cvlc, not just vlc in the beginning):

cvlc v4l2:// :v4l2-vdev="/dev/video0" --sout '#transcode{vcodec=h264,vb=512,scale=1,acodec=none,venc=x264{aud,profile=high,level=60,keyint=15,bframes=0,ref=1,nocabac}}:duplicate{dst=std{access=livehttp{seglen=10,delsegs=true,numsegs=5,index=/var/www/html/live/mystream.m3u8,index-url=http://192.168.0.163/live/mystream-########.ts},mux=ts{use-key-frames},dst=/var/www/html/live/mystream-########.ts},dst=std{access=http,mux=ts,dst=:8082/video.mp4}}'

 

3.      Open the VLC player in your remote device or on your Pi desktop environment:

 

4.      In the top menu of VLC, click on "Media" then "Open Network" stream.

 

5.      Click on the "Network" tab and type the following, making sure to type the correct IP address for your Pi:

 

http://192.168.0.163/live/mystream.m3u8

 

48

 

sudo nano /etc/apt/sources.list

 

deb http://repozytorium.mati75.eu/raspbian jessie-backports main contrib non-free

 

deb-src http://repozytorium.mati75.eu/raspbian jessie-backports main contrib non-free

 

49

 

sudo gpg --keyserver pgpkeys.mit.edu --recv-key CCD91D6111A06851

 

sudo apt-get update

 

sudo apt-get install apache2 php7.0 php7.0-curl php7.0-gd php7.0-imap php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-xmlrpc libapache2-mod-php7.0

 

sudo apt-get install mysql-server php7.0-mysql -y

 

69

https://dynamicdns.park-your-domain.com/update?host=@&domain=ikiribatidictionary.us&password=abcd1234defg5678

 

70

https://dynamicdns.park-your-domain.com/update?host=www&domain=ikiribatidictionary.us&password=abcd1234defg5678

 

71

30 6 1-31 1-12 1-7 wget -q "https://dynamicdns.park-your-domain.com/update?host=@&domain=ikiribatidictionary.us &password=abcd1234defg5678

 

72

30 6 1-31 1-12 1-7 wget -q "https://dynamicdns.park-your-domain.com/update?host=@&domain=ikiribatidictionary.us&password=abcd1234defg5678

 

89

 

create database counter;

use counter;

create table hits (pagetitle varchar(30),hitcount int);

insert into

insert into hits (pagetitle,hitcount) values (‘Other Places to Travel’,1);

 

90

 

create user counterguest@localhost;

set password for

counterguest@localhost=password('hitman');

grant select, update on counter.hits to counterguest@localhost;

 

89

create database counter;

use counter;

create table hits (pagetitle varchar(30),hitcount int);

insert into hits (pagetitle,hitcount) values (‘I-Kiribati Dictionary’,1);

insert into hits (pagetitle,hitcount) values (‘Other Places to Travel’,1);

 

90

 

create user counterguest@localhost;

set password for counterguest@localhost=password('hitman');

grant select, update on counter.hits to counterguest@localhost;

91-92

create database dictionary;

use dictionary;

create table word (wordnum int primary key auto_increment, english varchar (100), part_of_speech varchar(20), ikiribati varchar(100),comments text);

insert into word (english,part_of_speech,ikiribati,comments) values ('abandon','vt','kitana',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('airplane','n','wanikiba',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('happy','a','kukurei',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('hello','exclamation','Ko na mauri',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('goodbye','exclamation','tiabo',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('yes','exclamation','eng',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('beautiful','a','tikaraoi',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('beautifully','adv','n te aro ae tikaraoi',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('friend','n','rao',' ');

insert into word (english,part_of_speech,ikiribati,comments) values ('school','n','reirei',' ');

create user dictionaryguest@localhost;

set password for dictionaryguest@localhost=password('bookworm');

grant select on dictionary.word to dictionaryguest@localhost;

 

94-95

 

<?php

//Save this file as /var/www/html/kiribati/counter.php

echo "<body bgcolor=white text=darkblue>";

$server = "localhost";

$dbusername = "counterguest";

$dbpassword = "hitman";

$database="counter";

$con=mysqli_connect($server,$dbusername,$dbpassword,$database);

if (mysqli_connect_errno()){

  echo "Access Denied.";

  quit();

}

$sql="select hitcount from hits where pagetitle='I-Kiribati Dictionary'";

$result = mysqli_query($con,$sql);

if ($rs = mysqli_fetch_array($result)){

$hitcount=$rs['hitcount']+1;

echo "<div style=position:absolute;top:0;left:0;font-size:18>".$hitcount."</div>";

$sql="update hits set hitcount=".$hitcount." where pagetitle='I-Kiribati Dictionary'";

mysqli_query($con,$sql);

}

$con.close();

?>

 

 

97 -98

 

<!DOCTYPE HTML>

<head>

  <title>I-Kiribati Dictionary</title>

  <meta charset="UTF-8">

  <meta name="description" content="I-Kiribati Dictionary">

  <meta name="keywords" content="I-Kiribati,Kiribati,Gilbertese,Kiribati Dictionary">

  <meta name="author" content="Michael Wright">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<center>

# of Visitors =

<iframe src="counter.php" style=width:50px;height:20px;border-width:0>

  <p>Your browser does not support iframes.</p>

</iframe>

<h1>I-Kiribati Dictionary</h1>

<img src=ikiribatiflag.jpg><p>

<form action=translate.php target=frame2 method=post>

Type the English or I-Kiribati word or phrase:<p>

<input type=text name=word size=20><p>

<input type=submit name="toenglish" value="Translate to English"> 

<input type=submit name="toikiribati" value="Translate to I-Kiribati">

</form>

<p>

<iframe name=frame2 cols=40 rows=10 style=border-style:solid;border-color:black;border-width:1>

</center>

</body>

 

99-100

 

<?php

//Save this file as /var/www/html/kiribati/translate.php

$word=$_POST['word'];

if(notsecure($word)){

  echo "Your username has one or more illegal characters.<br>";

  $e=$e+1;

  exit();

}

//check which submit button was pressed

if (isset($_POST['toenglish'])) {

   $mode="Translate to English";

   $sql="select * from word where ikiribati='".$word."'";

  

}

if (isset($_POST['toikiribati'])) {

   $mode="Translate to I-Kiribati";

   $sql="select * from word where english='".$word."'";

}

$server = "localhost";

$dbusername = "dictionaryguest";

$dbpassword = "skippy";

$database="dictionary";

//connect to the database and immediately check for a connection error

$con=mysqli_connect($server,$dbusername,$dbpassword,$database);

if (mysqli_connect_errno()){

  echo "Access Denied<br>";

  quit();

}

//execute the query

$result = mysqli_query($con,$sql);

if (mysqli_num_rows($result)==0){

  echo "There are no matching words.";

  exit();

}

while ($rs = mysqli_fetch_array($result)) {

  $english=$rs['english'];

  $part_of_speech=$rs['part_of_speech'];

  $ikiribati=$rs['ikiribati'];

  if ($mode=="Translate to I-Kiribati"){

     echo $ikiribati." (".$part_of_speech.")<br>";

  }

  if ($mode=="Translate to English"){

     echo $english." (".$part_of_speech.")<br>";

  }

}

//----------------------------------------------

function notsecure($mystring){

//----------------------------------------------

   if (strpos($mystring,"")){

         return 1;

   }

   if (strpos($mystring,"'") || strpos($mystring,'"') | strpos($mystring,"=")){

      return 1;

   }

   $str = strtoupper($mystring);

   if (strpos($str,"OR") && strpos($str,"=")){

       return 1;

   }

}

 

122

gpio –g mode 4 out

gpio –g 4 1

gpio –g 4 0

gpio –g 4 blink

 

123

<?PHP

//save this as /var/www/html/simplerelay.php

$gpionum=$_GET['gpionum'];

$status=$_GET['status'];

$n="gpio -g mode ".$gpionum." out";

echo $n."<br>";

shell_exec($n);

$n="gpio -g write ".$gpionum." ".$status;

shell_exec($n);

echo $n."<br>";

?>

 

http://192.168.1.12/simplerelay.php?gpionum=17&status=0

 

http://192.168.1.12/simplerelay.php?gpionum=17&status=1

 

125-126

 

<?php

//togglerelay.php

session_start();

//conduct a security and password check

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

if (!preg_match('/^[a-z0-9 .\-]+$/i', $_POST['password']) && $_POST['password']!=""){

echo " Password contains illegal characters.<p>";

echo "Click <a href=index.php>here</a> to start again.";

exit();

}

if ($_POST['password']=="pinhead"){

$_SESSION['password']=$_POST['password'];

}

}

if ($_SESSION['password']!="pinhead"){

echo "You must type the password.<br>";

echo "<form action=index.php method=post>";

echo "<input type=password name=password>";

echo "<input type=submit value=Login>";

exit();

}

 

129

 

create database relays;

use relays;

create table pins (recordnum int primary key auto_increment,description varchar(30),pinnum int unique not null, status int default 1);

insert into pins (description,pinnum) values ("Front Porch",4);

insert into pins (description,pinnum) values ("Living Room",17);

create user relayguest@localhost;

set password for relayguest@localhost=password('pinhead');

grant insert, select, update, delete on relays.pins to relayguest@localhost;

 

130-132

 

<?php

//Relay Switching using a Password-Protected PHP Page and MySQL

//Save this file as /var/www/html/mysqlrelays/index.php

session_start();

//check for illegal characters in password field

$v=$_POST['password'];

if (!preg_match('/^[a-z0-9 .\-]+$/i', $v)&&$v!=""){

echo " Password contains illegal characters.<p>";

echo "Click <a href=index.php>here</a> to start again.";

exit();

}

//if a password was just posted then assign it to the session variable

if ($_POST['password']==""){

echo "You must type a password.<br>";

echo "<form action=index.php method=post>";

echo "<input type=password name=password>";

echo "<input type=submit value=Continue>";

exit();

}

//remember the password if correct

$_SESSION['password']=$_POST['password'];

//if the logout link was clicked then logout

if ($_GET['mode']=='logout'){

$_SESSION['password']="";

}

//open the database

$server = "localhost";

$dbusername = "relayguest";

$dbpassword = $_SESSION['password']; 131

 

$database="relays";

$con=mysqli_connect($server,$dbusername,$dbpassword,$database);

if (mysqli_connect_errno()){

echo "Access Denied.<br>";

exit();

}

echo "<html><body>";

echo "<h1>Relay Controller with MySQL</h1>";

echo "<form action=save.php method=post target=frame1>";

echo "<table>";

echo "<tr><td>Description</td><td>GPIO<br>Pin#</td><td>On/Off</td></tr>";

$sql="select * from pins";

$result = mysqli_query($con,$sql);

$r=0;

while ($rs = mysqli_fetch_array($result)) {

$recordnum=$rs['recordnum'];

$desc=$rs['description'];

$pinnum=$rs['pinnum'];

$status=$rs['status'];

//prepare to tell the checkbox to be checked or not

if ($status==0){$st="checked";}else{$st="";}

echo "<tr>";

//plot the fields on the screen in the table

echo "<td>";

echo "<input type=hidden name=recordnum".$r." value=".$recordnum.">";

echo "<input type=text name=description".$r." value='".$desc."' size=10>";

echo "</td>";

echo "<td><input type=text name=pinnum".$r." value=".$pinnum." size=2></td>";

echo "<td><input type=checkbox ".$st." name=status".$r."></td>";

echo "</tr>";

$r=$r+1;

}

mysqli_close($con);

//add a blank column to add a relay

echo "<tr>";

echo "<td><input type=text name=newdescription value='' size=10></td>";

echo "<td><input type=text name=newpinnum size=2></td>"; 132

 

echo "</tr>";

echo "<tr><td colspan=2>&nbsp</td></tr>";

echo "<tr><td colspan=3 align=right><input type=submit value=SAVE></td></tr>";

echo "</table>";

echo "</form>";

//create iframe box to show output from the form submission

echo "<iframe name=frame1 id=frame1 style=width:300;height:100></iframe><p>";

echo "<b>Instructions:</b><br>";

echo "<ul>";

echo "<li>Click on the check box to turn on/off the relay.";

echo "<li>Change the description or the GPIO pin number if needed.";

echo "<li>Enter info in the bottom row to add a new relay.";

echo "<li>To delete an item type delete as the description.";

echo "<li>Click the SAVE button to execute all commands.";

echo "<li>Progress of the save or delete will be displayed in the iframe window.";

echo "<li>Click <a href=index.php?mode=logout>here</a> to log out.";

echo "</ul>";

echo "</body></html>";

?>

 

133-135

<?php

//Relay Switching using a Password-Protected PHP Page and MySQL

//Save this file as /var/www/html/mysqlrelays/save.php

//check for illegal characters in all posted fields

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

foreach($_POST as $key => $value) {

//echo $key." = ".$value."<br>";

if (!preg_match('/^[a-z0-9 .\-]+$/i', $value) && $value!=""){

echo $key." contains illegal characters.<p>";

exit();

}

}

}

session_start();

//open the database

$server = "localhost";

$dbusername = "relayguest";

$dbpassword = $_SESSION['password'];

$database="relays";

$con=mysqli_connect($server,$dbusername,$dbpassword,$database);

if (mysqli_connect_errno()){

echo "Access Denied.<p>";

exit();

}

//updatate the fields with any changes

$r=0;

while(1){

$n="recordnum".$r;

$recordnum=$_POST[$n];

$d="description".$r;

$desc=$_POST[$d];

$p="pinnum".$r;

$pinnum=$_POST[$p];

$s="status".$r;

$status=$_POST[$s];

if ($status=="on"){$status=0;}else{$status=1;}

 

 

137

 

sudo chown pi /var/www/html

sudo mkdir /var/www/html/relays

cd /var/www/html/relays

echo Lamp,4,1 > pins.txt

 

138

 

.htaccess

<Files pins.txt>

deny from all

allow from 127.0.0.1

</Files>

 

139-140

 

#!/usr/bin/env python

#Save file as /var/www/html/relays/relay.py

import time

import RPi.GPIO as mygpio

mygpio.setmode(mygpio.BCM)

mygpio.setwarnings(False)

while True:

file=open("/var/www/html/relays/pins.txt","r")

while True:

r=file.readline();

if r=="":

break

n=r.split(",")

description=n[0]

pinnum=n[1]

status=n[2]

mygpio.setup(int(pinnum),mygpio.OUT)

mygpio.output(int(pinnum),int(status))

time.sleep(.1)

file.close()

 

144-146

 

<?php

//This is the /var/www/html/relays/index.php file.

//How to Setup a Raspberry Pi Web Server

//www.y2kLeader.com

session_start();

//check for security threats & unwanted characters in password field

$v=$_POST['password'];

if (!preg_match('/^[a-z0-9 .\-]+$/i', $v)&&$v!=""){

echo " Password contains illegal characters.<p>";

echo "Click <a href=index.php>here</a> to start again.";

exit();

}

//if a password was just posted then assign it to the session variable

$_SESSION['password']=$_POST['password'];

//if the logout link was clicked then logout

if ($_GET['mode']=='logout'){

$_SESSION['password']="";

}

//check the password

if ($_SESSION['password']!="pinhead"){

echo "<form action=index.php method=post>";

echo "Enter password <input type=password name=password>";

echo " <input type=submit value='Log in'>";

echo "</form>";

exit();

}

//open the pins.txt file

$myfile = fopen("pins2.txt", "r");

if (!$myfile){

echo "There was a problem opening the file.<b>";

exit();

}

echo "<html><body>";

echo "<h1>Raspberry Pi<br>Relay Controller</h1>";

echo "<form action=save.php method=post target=frame1>";

echo "<table>"; echo "<tr><td>Description</td><td>GPIO<br>Pin#</td><td>On/Off</td></tr>";

$r=0;

while(!feof($myfile)) {

$r++;

//read a record from the file

$line=fgets($myfile);

//extract the fields separated by commas

$n=explode(",",$line);

$desc=$n[0];

//if there is no record then exit the loop

if ($desc==""){$r=$r-1;break;}

$pinnum=$n[1];

$status=$n[2];

//prepare to tell the checkbox to be checked or not

if ($status==0){$st="checked";}else{$st="";}

echo "<tr>";

//plot the fields on the screen in the table

echo "<td><input type=text name=description".$r." value='".$desc."' size=10></td>";

echo "<td><input type=text name=pinnum".$r." value=".$pinnum." size=2></td>";

echo "<td><input type=checkbox ".$st." name=status".$r."></td>";

echo "</tr>";

}

fclose($myfile);

//add a blank column to add a relay

echo "<tr>";

echo "<td><input type=text name=newdescription value='' size=10></td>";

echo "<td><input type=text name=newpinnum size=2></td>";

echo "<td><input type=checkbox name=newstatus></td>";

echo "</tr>";

echo "<tr><td colspan=2>&nbsp</td></tr>";

echo "<tr><td colspan=3 align=right><input type=submit value=SAVE></td></tr>";

echo "</table>";

//save a hidden field with the total number of records

echo "<input type=hidden name=total value=".$r.">";

echo "</form>";

//create iframe box to show output from the form submission echo "<iframe name=frame1 id=frame1 style=width:200;height:100></iframe><p>";

echo "<b>Instructions:</b><br>";

echo "<ul>";

echo "<li>Click on the check box to turn on/off the relay.";

echo "<li>Change the description or the GPIO pin number if needed.";

echo "<li>Enter info in the bottom row to add a new relay.";

echo "<li>To delete an item, erase the description.";

echo "<li>Click the SAVE button to execute all commands."; echo "<li>Progress of the save or delete will be displayed in the iframe window.";

echo "<li>Click <a href=index.php?mode=logout>here</a> to log out.";

echo "</ul>";

 

 

148-149

/var/www/html/relays/save.php

<?php

//Save this file as /var/www/html/relays/save.php

//any output shows up in the index.php's iframe

//How to Setup a Raspberry Pi Web Server

//www.y2kLeader.com

session_start();

//check to see if logged in

if ($_SESSION['password']!="pinhead"){ echo "<body onload='javascript:window.parent.location.reload()'>";

exit();

}

//check for security threats & unwanted chars in fields

foreach($_POST as $key => $value){

$v=$_POST[$key];

if (!preg_match('/^[a-z0-9 .\-]+$/i', $v)&&$v!=""){

$x=$x.$key." Contains illegal characters.<p>";

}

}

if ($x!=""){echo $x;exit();}

//check to see if a GPIO pin was entered

if ($_POST['newdescription']!=""){

if ($_POST['newpinnum']==""){

echo "You must include a pin number.<p>";

exit();

}

//create a line with the new info for appending later

$new=$_POST['newdescription'].",".$_POST['newpinnum'].",1\n";

}

echo "updating..<br>";

//open the text file $myfile = fopen("pins2.txt", "w") or die("Unable to open file!");

//cycle through input fields & update database with the info

for ($i=1;$i<$_POST['total']+1;$i++){

$d="description".$i;

$p="pinnum".$i;

$s="status".$i;

//check if the check box is checked

if ($_POST[$s]=="on"){$status=0;}else{$status=1;}

//if the desc field is filled out save the curr record

if ($_POST[$d]!=""){

$t=$_POST[$d].",".$_POST[$p].",".$status."\n";

fwrite($myfile,$t);

echo $t."<br>";

}

else{

//if the desc field is blank don't save record

$t="";

$deletion=1;

}

}

//add the new record (if any) then refresh the parent page

if ($new!=""){

fwrite($myfile,$new);

echo $new."<br>"; echo "<body onload='javascript:window.parent.location.reload()'>";

}

fclose($myfile);

//if in delete mode, refresh the parent page.

if ($deletion==1){ echo "<body onload='javascript:window.parent.location.reload()'>";

}

?>

 

154

 

15 21 * * * gpio –g mode 4 out;gpio –g write 4 0

30 07 * * * gpio –g mode 4 out;gpio –g write 4 1

 

0 0 * * 1,3,5 gpio –g mode 4 out;gpio –g write 4 0

10 0 * * 1,3,5 gpio –g mode 4 out;gpio –g write 4 1

 

155

 

0,15,30,45 1,3,5 * * 1-5 gpio –g mode 4 out;gpio –g write 4 0

1,16,31,46 1,3,5 * * 1-5 gpio –g mode 4 out;gpio –g write 4 1

 

 

20 0-11 gpio –g mode 7 out;gpio –g write 7 0

25 0-11 gpio –g mode 7 out;gpio –g write 7 1

 

 

*/5 22-06 gpio -g mode 4 out;gpio –g 4 blink

*/6 22-06 gpio -g mode 4 out;gpio –g 4 1

*/6 22-06 gpio -g mode 5 out;gpio –g 5 blink

*/7 22-06 gpio -g mode 5 out;gpio –g 5 1

*/7 22-06 gpio -g mode 6 out;gpio –g 6 blink

*/8 22-06 gpio -g mode 6 out;gpio –g 6 1

*/8 22-06 gpio -g mode 7 out;gpio –g 7 blink

*/9 22-06 gpio -g mode 7 out;gpio –g 7 1

 

 

159

 

vlc -vvv v4l2:///dev/video0:chroma=h264:width=320:height=200 --sout '#standard{access=http,mux=ts,dst=:5000/,name=stream,mime=video/ts}'

 

160

 

vlc -vvv v4l2:///dev/video0:chroma=h264:width=320:height=200 --sout-http-user=mike --sout-http-pwd=abcd --sout '#standard{access=http,mux=ts,dst=:5000/,name=stream,mime=video/ts}'

175

 

//arduino_serialout.ino

//Outputs numbers through the serial port

//run readserial.py script on the Raspberry Pi to input numbers

int a=0;

void setup() {

Serial.begin(9600);

}

void loop() {

a++;

Serial.println(a);

delay(500);

}

177

 

#!/usr/bin/python

import time

import serial

ser = serial.Serial()

ser.port = "/dev/ttyUSB0"

ser.baudrate = 9600

ser.open()

while True:

x=ser.readline()

print x

 

179

 

#!/usr/bin/python

import time

import serial

ser = serial.Serial()

ser.port = "/dev/ttyUSB0"

ser.baudrate = 9600

ser.open()

while True:

ser.write("Hello from Raspberry Pi\n")

time.sleep(2)

 

181

 

//I2C Address Scanner

//SDA to A4 Pin

//SCL to A5 Pin

//GND - GND Pin

//VCC - 5V Pin

#include <Wire.h>

byte add;

void setup(){

Wire.begin();

Serial.begin(9600);

for(add = 1; add < 127; add++ ){

Wire.beginTransmission(add);

if(Wire.endTransmission()==0){

Serial.print("I2C address is 0x");

if (add<16){Serial.print("0");}

Serial.println(add,HEX);

}

}

}

void loop(){}

 

182-183

/*

arduino_serialin.ino

Receives messages from the USB port attached to the Raspberry Pi

and prints the message in the LCD Display.

For use with a 20 character 4 line I2C Display

Links to the librarary downloads at www.y2kleader.com

Connections:

//SDA to A4 Pin

//SCL to A5 Pin

//GND - GND Pin

//VCC - 5V Pin

*/

#include <Wire.h> // Comes with Arduino IDE

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

int x=-1;

int y=0;

char n;

void setup(){

Serial.begin(9600); // Used to type in characters

lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight

lcd.backlight();

lcd.setCursor(x,y);

}

void loop(){

if (Serial.available()) {

delay(100);

while (Serial.available() > 0) {

n=Serial.read();

x++;

if (x==20){

x=0;

183

y++;

}

if (y==4){

lcd.clear();

y=0;

}

lcd.setCursor(x,y);

lcd.write(n);

}//end while

}//end if(Serial.avaiable)

}//end loop

 

189-190

/*soil_humidity_meter

Probe board VCC to Arduino 5V Out pin

Probe board GND to Arduino GND pin

Probe Data to Arduino A0 pin.

*/

int sensorValue = 0;

int percentValue=0;

int dry=1023;

int wet=0;

int count=0;

void setup() {

Serial.begin(9600);

pinMode(2,OUTPUT);

digitalWrite(2,HIGH);

}

void loop() {

count++;

sensorValue = analogRead(0);

percentValue=map(sensorValue,dry,wet,0,100);

if (count>3 && count<30){

Serial.println(percentValue);

}

else{

if (count>29){

digitalWrite(2,LOW);

190

}

}

delay(1000);

}

 

191

 

<?php

//readsoil.php

//will open port and wait 3 seconds for Arduino to reboot

$f=fopen('/dev/ttyUSB0',"r");

sleep(3);

$data=fgets($f);

echo "<h1>Soil Humidity</h1>";

echo "<h2>".$data."%</h2>";

fclose($f);

?>

Press the button and wait 3 seconds.<P>

<button type="button" onClick="window.location.reload();">

Read Again

</button>

 

195

 

sudo crontab -e

@reboot fswebcam -q -b -d /dev/video0 -l 1 --jpeg 95 --save /var/www/html/simplestream/camera1.jpg

 

<head>

<title>Simple Stream</title>

</head>

<body onload=init()>

<center><h1>Simple Stream</h1>

<img id=camera1 src=camera1.jpg>

<script>

function init(){

var x=setInterval("reload()",1000)

}

function reload(){

camera1.src="camera1.jpg?" + new Date().getTime();

}

</script>

 

203-204

 

/var/www/html/pantiltusb/index.html

 

<head>

<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />

<meta http-equiv="cache-control" content="max-age=0" />

<meta http-equiv="expires" content="0" />

<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />

<meta http-equiv="pragma" content="no-cache" />

</head>

<body onload=send()>

<center>

<form name=form1 action=pancam.php method=post target=frame2>

<table>

<tr><td colspan=2><h2>Pan/Tilt Camera #1</h2></tr></td>

<tr><td>

<img id=pic src=pic.jpg style=width:320;height:200;border-style:ridge;border-width:5;border-color:darkred>

</td>

<td width=50>

<input type=range orient="vertical" id=yslider name=yslider min=1 max=180 onchange="send()" style="color:blue;width:50;-webkit-appearance: slider-vertical;writing-mode: bt-lr"></slider>

</td></tr>

<tr><td align=center>

<input type=range id=xslider name=xslider min=1 max=180 onchange="send()"></slider>

</td>

<td>&nbsp</td>

</tr>

<input type=submit name=slidersubmitbutton id=slidersubmitbutton style=visibility:hidden>

<tr><td colspan=2>

<iframe name=frame2 id=frame2 style=height:50;border:0></iframe>

</td></tr></table>

</form>

<script>

//uncomment the next line if you want continous 1 second streaming

//var xw=setInterval("send()",1000);

function send(){

var x=document.getElementById("xslider");

var x=document.getElementById("yslider");

form1.slidersubmitbutton.click();

pic.src = 'pic.jpg?'+new Date().getTime();

}

</script>

 

205

<?php

echo "<body style=font-size:24 onload=init()><center>";

$xangle=$_POST['xslider'];

$yangle=$_POST['yslider'];

//inverse axes if necessary

$xangle = map($xangle, 1, 180, 180, 1);

//$yangle = map($yangle, 1, 180, 180, 1);

$result=shell_exec("python pantilt.py 4 ".$xangle." 14 ".$yangle);

echo $result;

function map($value, $fromLow, $fromHigh, $toLow, $toHigh) {

$fromRange = $fromHigh - $fromLow;

$toRange = $toHigh - $toLow;

$scaleFactor = $toRange / $fromRange;

$tmpValue = $value - $fromLow;

$tmpValue *= $scaleFactor;

return $tmpValue + $toLow;

}

?>

<script>

function init(){

parent.pic.src = 'pic.jpg?'+new Date().getTime();

}

</script>

 

206

#/var/www/html/pantiltusb/pantilt.py

import RPi.GPIO as GPIO

import time

import sys

import os

xpin=int(str(sys.argv[1]))

xangle=float(str(sys.argv[2]))

xangle=xangle/180*15

xangle = round(xangle, 1)

ypin=int(str(sys.argv[3]))

yangle=float(str(sys.argv[4]))

yangle=yangle/180*15

yangle = round(yangle,1)

sleeptime=1

hertz=50 #20 ms

GPIO.setmode(GPIO.BCM)

GPIO.setup(xpin,GPIO.OUT)

GPIO.setup(ypin,GPIO.OUT)

xpwm=GPIO.PWM(xpin,hertz)

xpwm.start(xangle)

print "X = ",sys.argv[2]

time.sleep(sleeptime)

xpwm.stop()

print " "

ypwm=GPIO.PWM(ypin,hertz)

ypwm.start(yangle)

print "Y = ",sys.argv[4]

time.sleep(sleeptime)

ypwm.stop()

GPIO.cleanup()

 

207

 

sudo usermod -a -G gpio www-data

sudo apt-get install fswebcam

fswebcam -r 320x200 --jpeg 100 /var/www/html/pancam/pic.jpg

sudo chmod 666 /var/www/html/p/pic.jpg

 

224

 

raspivid -o - -t 0 -w 800 -h 600 -br 60 -vf | cvlc stream:///dev/stdin --sout '#rtp{sdp=rtsp://:5000/}' :demux=h264

raspivid -o - -t 0 -w 800 -h 600 -br 60 -vf | cvlc stream:///dev/stdin --sout '#rtp{sdp=rtsp://:5000/}' :demux=h264 &

227

raspivid -o - -t 0 -w 800 -h 600 -br 60 -vf -hf| cvlc stream:///dev/stdin --sout '#rtp{sdp=rtsp://:5000}' :demux=h264

 

245

cat /home/pi/.ssh/id_rsa.pub | ssh dave@192.168.1.14 'cat >> .ssh/authorized_keys'

 

247

mkdir /home/pi/webfolder

 

cp -r -p /var/www/html /home/pi/webfolder/

 

cp -r -p /home/pi/webfolder/* /var/www/html/

 

249

 

 

0 21 1-31 1-12 0-6 scp –r /var/www/html pi@192.168.1.14: /var/www/html

 

45 18 1-31 1-12 1-5 scp /var/www/html/images pi@192.168.1.14:/var/www/html/images

 

30 13 1-31 1-12 6 cp -R /var/www/html /mnt/usb

 

 

250

 

mysqldump –udatabase_username –pdatabase_password databasename > backup_directory/backupfilename

mysqldump –uroot –ppassword dictionary > dictionary_backup.sql

 

269

 

sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db

270

 

smtp_sasl_auth_enable=yes

smtp_sasl_security_options=noanonymous

smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd

smtp_tls_security_level = encrypt

smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

 

273

 

echo -e "Subject:Appointment Needed \n\n I would like make an appointment for Monday.\n" | sendmail y2kleader@gmail.com

 

274

 

<?php

$to = "recipientemail@gmail.com";

$from = "youremail@gmail.com";

$subject="Basic Email Test";

$header="From: ".$from."\n";

$msg="Hello World!";

mail($to,$subject,$msg,$header);

?>

275

 

<?php

$to = "recipient1@gmail.com";

$from = "youremail@gmail.com";

$cc="recipient2@gmail.com";

$bcc="recipient3@gmail.com";

$subject="Basic Emails with CC and BCC";

$header="From: ".$from."\n";

$header.="Cc: ".$cc."\n";

$header.="Bcc: ".$cc."\n";

$msg="Hello World!";

mail($to,$subject,$msg,$header);

?>

 

279-280

 

<?php

$from = "youremail@gmail.com";

$to = "recipient1@gmail.com";

$cc="recipient2@gmail.com";

$bcc="recipient3@gmail.com";

$subject="Email Test";

$header="From: ".$from."\n";

$header="Cc: ".$cc."\n";

$header.="Bcc: ".$bcc."\n";

$msg="This is the body of the email.";

$files[0]="./pics/image1.jpg";

$files[1]="./pics/powerpoint1.pptx";

$files[2]="./pics/spreadsheet.xlsx";

$boundary = md5(time());

$header.="Content-Type: multipart/mixed; boundary=\"==Multipart_Boundary_".$boundary."\"\n";

$header.="\nMIME-Version:1.0"."\n";

$message="--==Multipart_Boundary_".$boundary."\n";

$message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";

$message.="Content-Transfer-Encoding:7bit\n";

$message.=$msg."\n";

for($x=0;$x<count($files);$x++){

$file=fopen($files[$x],"rb");

$data=fread($file,filesize($files[$x]));

fclose($file);

$data=chunk_split(base64_encode($data));

$path_parts = pathinfo($files[$x]);

$name=$path_parts['basename'];

$message.="--==Multipart_Boundary_".$boundary."\n";

$message.="Content-Type: {\"application/octet-stream\"};\n";

$message.=" name=\"$name\"\n";

$message.="Content-Disposition: attachment;\n";

$message.=" filename=\"$name\"\n";

$message.="Content-Transfer-Encoding: base64\n\n";

$message.=$data."\n\n";

}

$message.="--==Multipart_Boundary_".$boundary."\n";

$message.="--==Multipart_Boundary_".$boundary."--\n";

mail($to, $subject, $message, $header);

?>