Rahul Mitra
Rahul Mitra

Reputation: 23

Control an Arduino Uno with a PHP script

Is it possible to control an Arduino Uno with a PHP webpage?

Upvotes: 3

Views: 1597

Answers (1)

on8tom
on8tom

Reputation: 1894

Yes, you can. You can connect your Arduino via USB to your server, and use phpSerial.

Or you can connect to your Arduino with an Ethernet shield. In PHP you can open a stream to your Arduino.

You can write your own protocol to communicate, for example:

To read the value of an analog input:

  1. PHP: sends rA0 followed by ('\n').
  2. Arduino parses rA0: and sends the value of analog input A0 back in ASCII followed by a newline ('\n').

A little more explanation:

  1. PHP wants to read an analog value or an I/O state so the first char is r (to set an I/O port or PWM a w)
  2. To specify if it is an analog or a digital I/O port, an 'A' or a 'D'
  3. To specify the port: the ASCII number of the port 0.
  4. To know where the command ends; send a newline.

On the Arduino side;

  1. Parse the incoming command.
  2. If the first char is not equal to 'r' or 'w' then it's not a read or write command; so the command is invalid.
  3. If the second char is not equal to 'A' or 'D' then it's not an analog or a digital value, the command is invalid.
  4. Then for the number, if it can be more than one char, make a char array of three chars, and fill these until you receive a '\n', or more than three ASCII numbers. With atoi() you can parse ASCII to integers.

If it is a legal command; respond to the command.

Upvotes: 4

Related Questions