Pages: [1]   Go Down
Author Topic: Go Online with my project  (Read 2590 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I am creating a project for the control of a fan and door via Arduino.
I have arduino wiznet ethernet shield.

My program is as follows:

Code:
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 200 };           // ip in lan
byte gateway[] = { 192, 168, 1, 1 };            // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)

int ledPin = 5;  // LED pin
int ledPin2=7;




char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address


boolean LEDON=false; //LED status flag
boolean LED2ON=false;


 

void setup(){
//start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
 
pinMode(ledPin, OUTPUT);   //Set pin 5 to output
pinMode(ledPin2, OUTPUT);  //Set pin 7 to output

 
//enable serial datada print
  Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
   if (client.available()) {
    char c = client.read();
     //read char by char HTTP request
    if (readString.length() < 100)
      {
        //store characters to string
        readString += c; //replaces readString.append(c);
      }
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          //dirty skip of "GET /favicon.ico HTTP/1.1"
          if (readString.indexOf("?") <0)
          {
            //skip everything
          }
          else
          //lets check if LED should be lighted
        if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))
           {
             //led has to be turned ON
             digitalWrite(ledPin, HIGH);    // set the LED on
             LEDON = true;
             
             
           }else{
             //led has to be turned OFF
             digitalWrite(ledPin, LOW);    // set the LED OFF
             LEDON = false;
           
           }
           
            if(readString.indexOf("M=1")>=0)
           {
             //led has to be turned ON
             digitalWrite(ledPin2, HIGH);    // set the LED on
             LED2ON = true;
             
           }else{
             //led has to be turned OFF
             digitalWrite(ledPin2, LOW);    // set the LED OFF
             LED2ON = false;
             
            }
           
           
           
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:White>");
          //send first heading
         client.println("<font color='red'><h1>Group 10 Server</font></h1>");
          client.println("<hr />");
          client.println("<hr />");

         
                     
          client.println("<font color='red'><h3>Please select your choice</font></h3>");
          client.println("<hr />");
          client.println("<hr />");



if(LEDON==true && LED2ON==true)
 {
       client.println("<h3>FAN</h3><form method=get name=LED><input type=checkbox name=L value=1 CHECKED>ON<br><input type=checkbox name=L value=0>OFF<br><h3>DOOR</h3><input type=checkbox name=M value=1 CHECKED>OPEN<br><input type=checkbox name=M value=0>CLOSE<br><input type=submit value=submit></form>");
   }
 


if (LEDON==true && LED2ON==false)
{
       client.println("<h3>FAN</h3><form method=get name=LED><input type=checkbox name=L value=1 CHECKED>OPEN<br><input type=checkbox name=L value=0>CLOSE<br><h3>DOOR</h3><input type=checkbox name=M value=1>OPEN<br><input type=checkbox name=M value=0 >CLOSE<br><input type=submit value=submit></form>");
}




if (LEDON==false && LED2ON==true)
{
        client.println("<h3>FAN</h3><form method=get name=LED><input type=checkbox name=L value=1 >ON<br><input type=checkbox name=L value=0>OFF<br><h3>DOOR</h3><input type=checkbox name=M value=1 CHECKED>OPEN<br><input type=checkbox name=M value=0 >CLOSE<br><input type=submit value=submit></form>");
      }


if (LEDON==false && LED2ON==false )
{
        client.println("<h3>FAN</h3><form method=get name=LED><input type=checkbox name=L value=1 >ON<br><input type=checkbox name=L value=0>OFF<br><h3>DOOR</h3><input type=checkbox name=M value=1>OPEN<br><input type=checkbox name=M value=0 >CLOSE<br><input type=submit value=submit></form>");
      }


          client.println("<br />");

         
           readString="";
           
           
           
           //printing status
          client.print("<font size='5'>Fan status: ");
          if (LEDON)
              client.println("<font color='green' size='5'>ON");
          else
              client.println("<font color='red' size='5'>OFF");     
          client.println("<hr />");
          client.println("<hr />");
         
            client.print("<font color='black' size='5'>Door status: ");
          if (LED2ON)
              client.println("<font color='green' size='5'>OPENED");
          else
              client.println("<font color='red' size='5'>CLOSED");     
          client.println("<hr />");
          client.println("<hr />");



         
          //stopping client
          client.stop();
            }
          }
        }
      }
 }


Now this program works fine in LAN.
But how can I use this program via internet. I have a nokia siemens router. When I enter my IP address(of internet) my router page opens but I want to open the web server interface.

Please help me.
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Go look at dyndns.com.  Not to sign up with their service, but to read the pages on making a personal web server work.  They have links and descriptions of how to do what you want as well as how to do it through a name server.  Every dsl modem is set up differently and I couldn't begin to describe the process you will need.  However, dyndns has addressed this for us already.
Logged

Trying to keep my house under control http://www.desert-home.com/

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is a work in progress, with a different ethernet shield, built on a different chip... and (at 18 April 2011) the buttons on my Arduino-served webpage don't work outside my LAN (they work inside it!!)....

BUT....

... you may find "ah ha!" stuff in....

http://sheepdogguides.com/arduino/art5serv.htm

... which is chasing a similar goal.

===
P.S.: Apologies for errors in that page previously... and for any that remain... but since 9am, UK time, Wednesday 20 April, the page has been improved, and the device does now... on several systems which have tested it for me across the internet, work fully: Inputs to server across 'net, LED on server turned on or off, reading from light sensor on server displayed on served web-page.
« Last Edit: April 20, 2011, 07:40:09 am by tkbyd » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not trying to hijack this thread but I have a question regarding your setup. I also just purchased my Arduino Ethernet Shield and have started to play with it. Do I need to get a static IP or can I use a Dynamic to get the card to work?
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

<hijack>

It's possible to use a dynamic ip address.  The idea is that you register the ip with a name service and then, every time the ISP changes it, you reregister the new ip.  Dyndns is only one of the services that will handle this for you.  Some of the services have client code you can run on a home computer that periodically checks your ip address and compares it to what is registered and updates as necessary.  However, my experience has been that my ISP changes my darn ip address as much as 6 times a freaking day!  So it takes my system around 15 minutes to notice it and then it updates the ip address and a few minutes later stuff starts working again.

So, since it costs nothing to try it out, get a dynamic ip name service and try it out.  If it doesn't work for you, then fork out the money for a static ip.

One thing in our favor is that you can use the ip address (static or dynamic) as the address of your router and use port forwarding to direct traffic to your arduino.  That way you can still have a hard-coded address on the arduino and talk over the internet both in and out.  You can also redirect traffic to another device by changing the port forwarding.  The one thing that will drive you nuts is what's called NAT Loopback (google it); if your router doesn't support it, you won't be able to address your arduino from inside your network.  You have to use an outside machine.  You can use a proxy service to test from the outside to be sure it's visible.

I'm eventually going to have to go to the static ip address, the ISP changing my address so often is a real pain.  Or, maybe a different ISP.  I've even built code into my arduino to sample the ip address and update dyndns because of this.

</hijack>
Logged

Trying to keep my house under control http://www.desert-home.com/

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are (marginally?) lucky, you can have dyndns without needing to have a "big computer" online all the time to run the dyndns updater.

Some (many?) routers have dyndns updater software in them, but usually they are not turned on in the default configuration. Go to your router's control panel and hunt around.

===
"dyndns updater": Some software running in your router, or a computer in the LAN it is connected to. It periodically checks what your latest IP address is, and if it has changed, sends word to the people at your dyndns service, giving them the news, so that when things are sent towards you, the dyndns people can direct it to the right numeric address.

Much of this is re-stated at pages connected to....

ArduServer.com

... and again in the FarWatch setup material...

But there's not much NEW in any of that... just the same material stated a different way, in case the first things you read didn't make sense!
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now this program works fine in LAN.
But how can I use this program via internet. I have a nokia siemens router. When I enter my IP address(of internet) my router page opens but I want to open the web server interface.

Generally you port foward (or virtual server) on your router to the lan ip address of your arduino. Many routers have a feature that can be set up to update the dynamic ip services. A $40 netgear 614 router can do this.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

San Francisco
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a device that might make your connection very simple, see: www.swiftEnterprises.biz.
Logged

Creator of RD-11 wireless data sharing, see www.swiftEnterprises.biz

0
Offline Offline
Full Member
***
Karma: 1
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

MOST newer router will do DDNS update automatically...

check with your router to see if it has this feature. My $10 rosewill router from newegg has this feature... as well as my 80 Asus router running on Tomato... so that's a pretty wide range of routers that support it.

So here goes... my personal IP address from my ISP is [111.111.111.111] for example...
My arduino is [198.236.0.2] since it's on the local network... my router is going to be [192.236.0.1].

Now, my router HTTP server is set to be on port 8080 instead of port 80, or any other port... This leave port 80, the default web server port open for the arduino.

Now, go to port forwarding, and forward port 80 to IP address [192.168.0.2] or whatever the port of your arduino is.

Once all this is done, you should be able to go to your IP address, [111.111.111.111], and see the arduino HTML interface... and then go to 111.111.111.111:8080, and you'll see the router interface.

Next step is DDNS.... since my IP is dynamic... meaning 111.111.111.111 will change at the will of the ISP, so I wouldn't want to memorize this number... instead I sign up for www.dyndns.org, which will mask my IP and monitor it for change automatically and route it to vinhtvu2.whatever.org... or whatever your heart desires...

So go to dyndns, sign up for an account, then create a dns name. Once you have this information, look in your router for the DDNS section, Dyndns.org is pretty big, so it should be supported if your router support DDNS. Then put in your username, and password... then set your refresh rate to be however often they change your IP address... I set mine to refresh every 48 hours... and it hasn't failed yet...

If you look at my other post... you can see it work pretty well. I use this to control all the lightings in my house now... if I'm home, I use 192.168.0.2 instead, since it's faster.
Logged

Pages: [1]   Go Up
Jump to: