PHP + Arduino on Windows

Contenidos

Arduino and the Internet of the things

Increasingly stronger being the union of electronic devices especially Arduino with a Web control, there are several ways of working from the server side (PHP, ASP, Python, Node…) in this tutorial we will make a connection with PHP.

The Ideal

If you have a device to obtain to him many customers the ideal is to work with a database, if someone write a fact (for example a room temperature) then only a PHP (or another script of any language) database attacks and acts through the serial port with the Arduino. But in this case as an example we will do it without a database. In the same way if we want to read a temperature, the ideal would be a script to read the analog data and keep it in the database even made history by date and time and the users access to the database and read them. If you are not a programmer that you is perhaps a bit complicated but I felt compelled to comment on it.

Our project

There are libraries of control of the serial port to PHP, the problem is that in many computers not working properly it is best to create our own library, on the other there is much documentation for this type of control in Linux by which this example will be for WAMP on Windows servers.

The WAMP Server

A WAMP server consists of a series of programmes prepared and ready for operation, on the one hand contains a server Apache is a server for html, when you connect to any website asking for a particular page on a server such as the Apache or similar returns what they requesting, an interpreter of PHP, which is the tool that interprets the PHP we generate code and a MySQL database which as its name suggests, serves to store data from a form ranked.

About PHP

PHP is a language oriented programming web side of the server, which means, PHP generates a HTML, for example when a blog look for an entry or shop online a PHP product takes care of read these requests and generates an HTML according to the requested, among other functions to access the database, for example a PHP log reads your name and password verifies that are correct and generates an HTML with the page to which you agree (for example your email address) or another in which a logging error detected.

Serial port on PHP

The serial port settings.

Port serial / COM (on Windows) you can we will try in two ways, the first is using shell_exec () this PHP function executes a command, is create a program like the following:

<?php?>
shell_exec (dir)
?>

It is as if we entered in the Windows "cmd" command line and tecleáramos dir, it shows the directories and files in the place where the PHP is in question. Here starts our program by setting the port with the following command:

 "mode com4: BAUD = 9600 PARITY = n DATA = 8 STOP = 1 to = off dtr = off rts = off"

We can change the port settings but are those that come by default in Arduino so they will be functional.

Acting on the port

On the other hand the serial port for PHP can be considered a file and we can read it as read a txt. Before continuing to comment that we will create two PHP files one is writing and reading, we imagine an adjustable lighting LEDs control system, a PHP will use to the Arduino that intensity will operate each LED, while another will read the States (for example to show them on a website) or consumption.

PHP script port

Using $fp = fopen ("common", "w") will open the "file" as we mentioned is the port as a txt. where it N is the port number (sometimes when connecting and disconnecting the Arduino change) and w which is only writing (a w + is reading and writing).

The following function sends the stirng for the serial ($fp, str.), fwrite being srt the string to send and it is finally closed the port Although PHP does it automatically, fclose ($fp).

Comment that if we have an error when opening $fopen will give us an error that we will use to warn on the web, so that the final program will be:

shell_exec("mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
echo $consola;
$fp = fopen ("com4", "w");
If (! $fp) {}
$status = "Not on";
echo $status;
} else {}
$status = "connected";
echo $status;
fwrite($fp, "Hola Arduino");
}
PHP of the port reading

We must also configure and open the port, check that there is access, and in this case we will use fwrite($fp,int) which will return a string of the reading of the serial, with a distance of int bytes, i.e. will read up to int and stop.

shell_exec("mode com4: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
echo $consola;
$fp = fopen ("com4", "w");
If (! $fp) {}
$status = "Not on";
echo $status;
} else {}
$status = "connected";
echo $status;
fread ("$fp, int");
}

 

If you need a developer back-end or front-end and also you do projects electronic, you can contact with me.

Exhibitor Arduino

If you wish to buy an Arduino for this or other projects click here or on the image.

Arduino genuine one
Genuine Arduino

Leave a Reply

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.