<?php
exec("mode com3: BAUD=9600 PARITY=N data=8 stop=1 xon=off dtr=off");
$fp = fopen("COM3", "w+");
fwrite($fp, "<ReadAnalog|2>");
sleep(2);
fwrite($fp, "<ReadAnalog|3>");;
sleep(1);
$content = fgets($fp, 2096);
fclose($fp);
?>
Avec un programme arduino : Code : Tout sélectionner
/*
ADXL3xx
Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer. The pins used are designed to be easily
compatible with the breakout boards from Sparkfun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80
http://www.arduino.cc/en/Tutorial/ADXL3xx
The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc
created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// these constants describe the pins. They won't change:
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
// Provide ground and power by using the analog inputs as normal
// digital pins. This makes it possible to directly connect the
// breakout board to the Arduino. If you use the normal 5V and
// GND pins on the Arduino, you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop()
{
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
delay(100);
}
<?php
function readArduino() {
// Code PHP pour lire une ligne de ce qui est envoyé sur le port série de l'Arduino;
return explode("\t",$line); // Transforme la ligne en tableau
}
try {
$arduinoValues = readArduino();
$json['isSuccess'] = true;
$json['X'] = $arduinoValues[0];
$json['Y'] = $arduinoValues[1];
$json['Z'] = $arduinoValues[2];
return json_encode($json);
}
catch (\Exception $e) {
$json['isSuccess'] = false;
$json['error'] = $e->getMessage();
return json_encode($json);
}
Puis une page HTML (test.html) du style :
<!DOCTYPE html>
<html>
<head>
<title>Arduino Reader</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script type="text/javascript">
// Voir script dessous
</script>
</head>
<body>
<div id="maDivInfo"></div>
</body>
</html>
Avec jQuery ca pourrait donner dans je JavaScript: