WebServer for Arduino without EthernetShield (with PC/Apache/PHP)

Hey guys!

A lot of people ask how to connect Arduino to internet, so it can interact getting data from the internet and actuating physically. Let's say, turning a light on/off, open/close a garage door, turning the coffee maker on. :)

To do this, we could use an EthernetShield, giving the "power" to Arduino access the internet. Or we can leave this power to a PC, which already have it, and then the PC, through the USB, will send the info to the Arduino, who will control anything we want. The opposite flow of data is also possible, getting data from sensors through the Arduino and post on the internet, following the same idea.

This post will teach how to control something using a PC connected to the Internet and an Arduino (without any shield).

First of all, we need a webserver. A system who will answer on the internet. When you access any webite, an webserver is responding to your browser, sending all the codes and info your browser arrange on the screen for you. The most common is the HTML tags (you should googling about html if you are not familiar with, for this kind of project you will certainly use them).

If you have a PC connected to the internet, you have everything you need to host an website, or have an webserver online.
On the project below, I used APACHE, which is the most common webserver, and a very, very, very good one.

Beside APACHE you will need a compiler to interprete your program which will be integrated with Arduino. This program must run online, and must be embedded in HTML code, so your browser can handle it. In this project I used  PHP, but you could use Python, ASP, or other.
So, you have to install those things in your PC to have an webserver on it. I suggest this tutorial for Windows users (although I prefer Ubuntu):

http://www.simplehelp.net/2008/08/25/how-to-install-and-setup-apach...

Once you have an webserver running with php, you can try the other (and best) part.

Important: Your router must be configured to port forward, so when someone access your IP and port, it will forward to your PC.
Let's say your PC has the internal IP 192.168.0.5, then you have to create a port forward like 8100 to that ip. So when someone access your external IP (on the internet), lets say 189.x.x.x:8100, your router will redirect to your PC, on which, the Apache will respond showing the homepage. Or 189.x.x.x:8100/myarduino.php to access directly your Arduino.

But how your PHP page will communicate with Arduino?

Simple! Through the serial port. You Arduino is connected to the PC through an USB cable. We will use the Serial library in Arduino code to keep listening the COM port. When a message comes from PC (by php code), the Arduino will interprete and respond.

This example turn the Arduino LED (pin 13) on/off, when the user press the buttons on your php page.

PHP code

This is the page your users must access to send comands to your Arduino. It must be in www folder in Apache, to be visible from outside. I named the file "index.php", so it became the home page. If someone access 189.x.x.x:8100, the index.php is automatically accessed.

This is a PHP file (include html), which will be interpreted by your webserver and, the result sent to your users browser as html. On the other hand this same page is responsible for send messages to the Serial port (USB) where Arduino is ready to answer.

When the user press a button the page "post" the info about the button pressed to itself reloading it. This second time the page is loaded, the php will send to the Serial port the info about what button was pressed. The Arduino then, will receive the info and take an action.

################# PHP ###########################

<html><head><title>WebServer and Arduino by GarageLab Community</title></head>

<body>

<?php

echo "<p>Control Page</p><p>";

$port = fopen("/dev/ttyUSB0", "w"); //You have to check which port your Arduino is connected to and change this (this one is for Ubuntu and Arduino 2009)

sleep(2);

?>

<br>

<?php

// Turn Led ON ?>

<form action="index.php" method="POST">

<input type="hidden" name="turn" value="on" />

<input type="Submit" value="on">

</form>

<?php

// Turn Led OFF ?>

<form action="index.php" method="POST">

<input type="hidden" name="turn" value="off" />

<input type="Submit" value="off">

</form></p>

<?php

if ($_POST['turn']=="on"){

echo "Turned on";

fwrite($port, "n");

}

if ($_POST['turn']=="off"){

echo "Turned off";

fwrite($port, "f");

}

fclose($port);

?>

</body></html>

So, if the user press the button "on" the LED will be turned on, if off... you understood.

Now, let's see the code for Arduino:

########### ARDUINO 2009 FIRMWARE (Arduino IDE 0022) #####################

char c;

void setup(){

Serial.begin(9600);

Serial.println("### GARAGELAB ###");

pinMode(13, OUTPUT);

}

void loop(){

if (Serial.available() > 0)

{

c = Serial.read();

Serial.println(c);

}

else

{

delay(100);

}

if (c=='n'){

Serial.println("on");

digitalWrite(13, HIGH);

}

if (c=='f'){

Serial.println("off");

digitalWrite(13, LOW);

}

c='\0';

}

I think the code above is very clear, but if you still have any doubt, please comment.

Oh! If I wrote anything wrong (even the english), please comment too. We are all here to learn and grow as developers, and I certainly will appreciate any constructive feedback. :)

Good luck for us all!

Views: 7409

Tags: Arduino, apache, php, webserver

Comment by shymn on November 11, 2012 at 10:01am

from where I can know which port is connected if I used PC with windows OS, for this line of code :

$port = fopen("/dev/ttyUSB0", "w"); //You have to check which port your Arduino is connected to and change this (this one is for Ubuntu and Arduino 2009) ?

Thank you, this is very useful blog (F)

Comment by esther maggie patricia on January 14, 2013 at 9:07am

if i want to make php read from arduino? how can i make that? this is for my final exam, can somebody help me please?

i want to send a char from arduino to my php without the ethernet shield. thank you :)

Comment by Juan Carlos Montoliu Henares on October 28, 2013 at 5:57am

In device manager you can see your COM nr. In my case is like:

$port = fopen("COM6", "w");

Another question, everything works perfect but when I restart  the PC or unplug and plug the USB cable of the arduino the serial port is blocked and doesn't work anymore...
In order to make it work again I have to unplug it, open the arduino software, close it, plug again, open again the sw and upload the sketch or start the serial monitor and then it works... Everything in that order :S

Any hint?

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

GarageLab posted a blog post

Real-life Spider-Man webshooter uses a coil gun instead of web fluid

In honor of The Amazing Spider-Man 2 opening tomorrow in Germany (among other places), German laser…See More
Thursday
Shannon Bradley and Marcelo Rodrigues are now friends
Wednesday
Terry Renner is now a member of GarageLab (arduino, electronics, robotics, hacking)
Wednesday
Andy S posted a discussion

Arduino MEGA boolean?

Hi everyone,been banging my head against the wall a few days now.i have a small project, where i…See More
Wednesday
Bobby Marshall posted a video

SELFIE Mirror

Meet the S.E.L.F.I.E., the "The Self Enhancing Live Feed Image Engine". Find out how it works: http://istrategylabs.com/2014/04/take-an-instant-selfie-with-t...
Tuesday
GarageLab posted a blog post
Tuesday
pj jones posted a status
"Designing an Home Automation Project"
Monday
Profile Iconpj jones, Robert Kribel and Lars Goldschlager joined GarageLab (arduino, electronics, robotics, hacking)
Monday
Fontaine Pierre commented on GarageLab's blog post Tutorial: Using GPS Shield as an Anti-Theft Device
"Hi, Is it possible to make a car tracker with this GPS + GSM shield to send a message if limits…"
Apr 11
GarageLab posted a blog post
Apr 11
Profile Iconeslam mohamed haredy, Wesley Castillo and Lise Pape joined GarageLab (arduino, electronics, robotics, hacking)
Apr 10
Terry Dark left a comment for Beula Isaac
"Hi Beulah, Greetings from down under (Australia) Please tell me about yourself and your interests…"
Apr 9
Terry Dark and Beula Isaac are now friends
Apr 9
GarageLab posted blog posts
Apr 9
GarageLab posted a blog post

Festo's BionicKangaroo gets the hop on energy-efficiency

The BionicKangaroo features pneumatic actuators that provide the jumping power and at the same time…See More
Apr 7
Profile IconAnthony Parker, John Nicosia and Tom Devey joined GarageLab (arduino, electronics, robotics, hacking)
Apr 7
wani sageer posted photos
Apr 6
GarageLab posted a blog post

Using a 3D Printer to Draw Tattoos: InkD

 It’s one thing to allow a professional tattoo artist to ink your arm. It’s another thing…See More
Apr 4
GarageLab posted a blog post

Dispensing Solder Paste With A 3D Printer

There’s a strange middle ground in PCB production when it comes to making a few boards. Dispensing…See More
Apr 3

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service