giovedì 31 luglio 2014

RaspberryPi and Arduino - PHP Serial Communication

ARDUINO SIDE

Connect Arduino to Raspberry through USB.

Load on Arduino this simple sketch that sends back everything on serial port.
byte input;
void setup() {
        Serial.begin(9600);
}
void loop() {
        if (Serial.available()) {
                input = Serial.read();
                Serial.write((char)input);
        }
}
Test the Arduino connection using screen. Probably Arduino interface is /dev/ttyACM0
sudo screen /dev/ttyACM0 9600

RASPBERRY SIDE

To use serial port within PHP I suggest this library PhpSerial.php

Load on the WebServer this php page
<?php
  include 'include/PhpSerial.php';
  $serial = new phpSerial;
  $serial->deviceSet("/dev/ttyACM0");
  $serial->confBaudRate(9600);
  $serial->confParity("none");
  $serial->confCharacterLength(8);
  $serial->confStopBits(1);
  $serial->confFlowControl("none");
  $serial->deviceOpen();
  $serial->sendMessage("Hello World!");
  $read = $serial->readPort();
  echo $read;
  $serial->deviceClose();
?>
That's all!

Nessun commento:

Posta un commento