Interfacing with php

From Pinguino-Wiki
Jump to: navigation, search

Here is how you can get your Pinguino simply communicate with your local Web Server.
We are going to use 1-wire bus to communicate with a DS18B20 Temperature Sensor and USB CDC driver to communicate with the machine which host the Web Server. USB CDC allow you to take advantage of the simplicity of the USB (you only need a USB cable) while providing the functionality of a Serial port.

You must have a DS18B20 temperature sensor wired to your Pinguino to run this example :

  • pin 1: GND
  • pin 2: DQ (Data in/out) must be connected to the Pinguino (pin 2 (RB2) in this example)
  • pin 3: VDD (+5V)

NB : 1-wire bus (DQ line) must have 4K7 pull-up resistor (connected to +5V)

Data's are sent to /dev/ttyACM0.
Type :

  • ls -all /dev/ttyACM0

to see permissions and owner.
Make sure you have persmissions on it :

  • sudo chmod 666 /dev/ttyACM0 (ex. on a GNU/Linux System)
  • if it doesn't work try 777 : sudo chmod 777 /dev/ttyACM0

Maybe you will have to add your user name to the dialup group :

  • adduser YOURUSERNAME dialout


On Pinguino side : temp18b20.pde

/*	-----------------------------------------------------------------------
	Pinguino example to read ds18b20 1wire temperature sensor
	Result is sent on usb-serial bus and can be read with index.php
	author		Régis Blanchot
	first release	14/09/2010
	last update	21/06/2011
	IDE		Pinguino X
	Boards		All
	----------------------------------------------------------------------*/
 
// 1-wire bus is on pin 14, just change it to suit your board
#define ONEWIREBUS 14						
 
void setup()
{
}
 
void loop()
{
	TEMPERATURE t;
 
	if (DS18B20.read(ONEWIREBUS, SKIPROM, RES12BIT, &t))
		CDC.printf("%d.%d°C \r", t.integer, t.fraction);
}

Test it first on your computer in your favorite console :

cat /dev/ttyACM0

On Computer (or NAS or Router) side : index.php

These notes assume you already have a Web Server (Apache, lighttpd, ...) running.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
   "http://www.w3.org/TR/html4/strict.dtd">
 
<html>
	<head>
		<meta http-equiv="refresh" content="1">			// refresh this page every 1 sec.
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	</head>
 
	<body>
		<p>The Temperature in my Office is :</p>
		<?php
			if ($handle = fopen("/dev/ttyACM0", "rb"))	// we just read data
			{
				$data = fread($handle, 8);		// we get 8 bytes from our device
				print "<center><h1>";
				print $data;
				print "</h1></center>";
				fclose($handle);
			}
		?>
	</body>
</html>

Here is the result on my D-link DNS 323 running a lighttpd web server : http://rblanchot.no-ip.org

Personal tools