Php arduino
-
Upload
jonadri-bundo -
Category
Technology
-
view
112 -
download
1
Transcript of Php arduino
![Page 1: Php arduino](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/1.jpg)
>_ Things Lab
Connecting PHP with Arduino
![Page 2: Php arduino](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/2.jpg)
Circuit
![Page 3: Php arduino](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022071815/55a9f54e1a28ab421c8b474b/html5/thumbnails/6.jpg)
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