Php arduino

6
>_ Things Lab Connecting PHP with Arduino

Transcript of Php arduino

Page 1: Php arduino

>_ Things Lab

Connecting PHP with Arduino

Page 2: Php arduino

Circuit

Page 3: Php arduino

Arduino Code : Part 1

int led = 12;

void setup() {

// Open serial communications

Serial.begin(9600);

// send an intro:

Serial.println("0=off, 1=on");

Serial.println();

pinMode(led,OUTPUT);

}

Page 4: Php arduino

Arduino Code : Part 2

void loop() {

// get any incoming bytes:

if (Serial.available() > 0) {

int input = Serial.read();

if (input == 48) { //received 0

digitalWrite(led,LOW);

}

else if (input == 49) { //received 1

digitalWrite(led,HIGH);

}

}

}

Page 5: Php arduino

PHP code (same dir as "php_serial.class.php")

<?php

include "php_serial.class.php";

$msg = $argv[1]; // 0=off, 1=on

$serial = new phpSerial;

$serial->deviceSet("/dev/ttyUSB0");

$serial->confBaudRate(9600);

$serial->confParity("none");

$serial->confCharacterLength(8);

$serial->confStopBits(1);

$serial->deviceOpen();

if (intval($msg)==0) $serial->sendMessage(0); //turn the led off

else $serial->sendMessage(1); //turn the led on

$serial->deviceClose();

?>

Page 6: Php arduino

Running the code

• Upload code into Arduino

• Execute: # get the PHP cli

sudo apt-get install php5-cli

php test.php [args] # run the program

• Pass 0 to turn the led off, or others to turn it on