2017-05-24 2 views
0

HC-Sr04超音波センサーからの距離と、私のラップトップに表示されるLEDライトの色をPHP経由で取得したいのですが誰でも助けてください。事前に.. おかげhcsr04超音波センサーの距離をphp経由でデスクトップ上で取得する方法

私のPHPコード..

<?php 
echo "<p>Control Page</p><p>"; 
$port = fopen("COM4", "w+"); 
sleep(2); 
?> 
<br> 
</form> 
<?php 
if ($_GET['distance']<30) 
{ 
$data = $_GET['distance']; 
echo '<td bgcolor="#FF0000"> red'; 
} 
else if ($_GET['distance']>35){ 
    $data = $_GET['distance']; 
    echo '<td bgcolor="#00FF00"> green'; 
} 
else { 
    echo "Empty"; 
    echo '<td bgcolor="#00FF00"> green'; 
} 
fclose($port); 
?> 
</body> 
</html> 

私のArduinoのコード:...

#define trigPin 10 
#define echoPin 2 
#define led 4 
#define led2 6 

void setup() { 
    Serial.begin (9600); 
    pinMode(trigPin, OUTPUT); 
    pinMode(echoPin, INPUT); 
    pinMode(led, OUTPUT); 
    pinMode(led2, OUTPUT); 
} 

void loop() { 
    long duration, distance; 
    digitalWrite(trigPin, LOW); // Added this line 
    delayMicroseconds(2); // Added this line 
    digitalWrite(trigPin, HIGH); 
    delayMicroseconds(10); // Added this line 
    digitalWrite(trigPin, LOW); 
    duration = pulseIn(echoPin, HIGH); 
    delay(1000); 
    distance = (duration/2)/29.1; 
    if (distance < 30) { // This is where the LED On/Off happens 
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off 
    digitalWrite(led2,LOW); 
} 
    else if(distance >35) { 
    digitalWrite(led,LOW); 
    digitalWrite(led2,HIGH); 
    } 
    else{ 
    digitalWrite(led,LOW); 
    digitalWrite(led2,LOW); 
    } 
    } 

答えて

1

私はあなたが働くと仮定Linuxで。

あなたのシリアルポートを設定する必要があり、すべての最初:次に、あなたはArduinoのは、あなたが接続するたびに再起動されます関数fread/fwriteの

$fp =fopen("/dev/ttyACM0", "w+"); 
if(!$fp) { 
     echo "Error";die(); 
} 

fwrite($fp, $_SERVER['argv'][1] . 0x00); 
echo fread($fp, 10); 

fclose($fp); 

を使用することができます

stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts 

arduinoのシリアルモニタを使用してデバッグしてください!

幸運を祈る!

詳細: https://systemsarchitect.net/2013/01/26/connecting-php-with-arduino-via-serial-port-on-linux/

関連する問題