RaspberryPi python+php web switch control or car controll with camera

python code:

#!/usr/bin/python
import MySQLdb
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

def dbConn():
        db = MySQLdb.connect("localhost",user=" ",passwd=" ",db=" ")
        cur = db.cursor(MySQLdb.cursors.DictCursor)
        cur.execute("SELECT * FROM pinMap")
        return cur
# you must create a Cursor object. It will let
#  you execute all the queries you need

# Use all the SQL you like

# print all the first cell of all the rows

try:
   while True:
        cur=dbConn()
        print "|ID  |GPOI num\t|Status\t|"
        print "+-----------------------+"
        for row in cur.fetchall() :
            print "|",row['id'], "|" ,row["pin"], "\t|" , row['status'], "\t|"
            GPIO.setup(int(row["pin"]), GPIO.OUT)
            GPIO.output(int(row["pin"]),int(row["status"]))
        print "+-----------------------+"
        time.sleep(2)
except KeyboardInterrupt:
                pass
GPIO.cleanup()

PHP code:

<!DOCTYPE html>
<html>
<head>
<title>PiCar</title>

</head>

<body>

<div style="text-align:center">
    <h1>Raspberry Pi GPIO</h1>
<form action="index.php" method="post">
    <input type="image" src="forward.jpg" name="forward"  value="forward">
<br>
    <input type="image" src="left.jpg" name="left"  value="left">
    <input type="image" src="right.jpg" name="right"  value="right">

<br>
        <input type="image" src="backward.jpg" name="backward"  value="backward">
<br>
        <input type="image" src="stop.jpg" name="stop"  value="stop">
    </div>
</form>

</body>
</html>
<?php
function fileRead($num){
    $myFile = "example.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "$num\n";
    fwrite($fh, $stringData);
    fclose($fh);
}
function db($pin,$setting){
    include('db.php');
    //$setting = "1";
    //$pin=4;
    mysql_query("UPDATE pinMap SET status='$setting' WHERE pin='$pin';");
    mysql_close();
    header('Location: index.php');
}
function resetPinZero($id,$setting){
    include('db.php');
    //$setting = "1";
    //$pin=4;
    mysql_query("UPDATE pinMap SET status='$setting' WHERE id='$id';");
    mysql_close();
    header('Location: index.php');
}

if (isset($_POST['forward']) && ($_POST['forward'] == 'forward')) {
    echo "forward";
        fileRead("1");
        db("8","0");
        db("7","1");
/*        sleep(1);
        fileRead("0");
        header("Location: fileOne.php");*/
}
else if (isset($_POST['backward']) && ($_POST['backward'] == 'backward')) {
    echo "backward";
        db("7","0");
        db("8","1");
        fileRead("2");
}

else if (isset($_POST['left']) && ($_POST['left'] == 'left')) {
    echo "left";
        db("23","0");
        db("24","1");
        fileRead("2");
}
else if (isset($_POST['right']) && ($_POST['right'] == 'right')) {
    echo "right";
        db("24","0");
        db("23","1");
        fileRead("3");
}
else if (isset($_POST['stop']) && ($_POST['stop'] == 'stop')) {
    echo "stop";
        for($i=0;$i<=17;$i++)
            resetPinZero($i,0);
        fileRead("4");
}
?>
db.php:
<?php
      $conn = mysql_connect('localhost', ' ', ' ');
      $db   = mysql_select_db(' ');
            if($conn)
                echo "success";
            else
                echo "not success!";
?>

MySql:-- phpMyAdmin SQL Dump
-- version 3.4.11.1deb2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 14, 2014 at 04:40 PM
-- Server version: 5.5.37
-- PHP Version: 5.4.4-14+deb7u12

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `gpioPin`
--

-- --------------------------------------------------------

--
-- Table structure for table `pinMap`
--

CREATE TABLE IF NOT EXISTS `pinMap` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `pin` int(10) NOT NULL,
  `status` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

--
-- Dumping data for table `pinMap`
--

INSERT INTO `pinMap` (`id`, `pin`, `status`) VALUES
(0, 7, 0),
(2, 8, 0),
(3, 10, 0),
(4, 11, 0),
(5, 12, 0),
(6, 13, 0),
(7, 15, 0),
(8, 16, 0),
(9, 18, 0),
(10, 19, 0),
(11, 21, 0),
(12, 22, 0),
(13, 23, 0),
(14, 24, 0),
(15, 26, 0),
(16, 3, 0),
(17, 5, 0);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Picture:




motorControl.py :
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

"""back motor"""
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
"""front motor"""
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
"""
GPIO.output(11,1)
GPIO.output(12,0)
time.sleep(0.5)
GPIO.output(11,0)
GPIO.output(12,1)
time.sleep(0.5)
"""
speed=80
clock=50
p=GPIO.PWM(11,clock)
q=GPIO.PWM(12,clock)

p.start(0)
q.start(0)

try:
    while True:
                    for i in range(speed):
                                p.ChangeDutyCycle(i)
                                time.sleep(0.02)
                    for i in range(speed):
                                p.ChangeDutyCycle(speed-i)

                    p.ChangeDutyCycle(0)

                    for i in range(speed):
                                q.ChangeDutyCycle(i)
                                time.sleep(0.02)
                    for i in range(speed):
                                q.ChangeDutyCycle(speed-i)

                    q.ChangeDutyCycle(0)
except KeyboardInterrupt:
                pass
p.stop()
"""
GPIO.output(8,1)
GPIO.output(10,0)
time.sleep(0.5)
GPIO.output(8,0)
GPIO.output(10,1)
time.sleep(0.5)
"""
GPIO.cleanup()

I am using ebay cheap motor driver and it's cost 1$. You can control 2 dc motor.
 

No comments:

Post a Comment