linking my arduino webserver button to my website

Sorry if i had posted in wrong forums

But i have some serious issue with linking my home automation web servers button to my web site
here is my codes

#include <SPI.h>
#include <Ethernet.h>

int led1 = 4;
int led2 = 5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(25565);                             //server port     
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient 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;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://smarthome.thesouk.in/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("");
           client.println("<hr />");
           client.println("
");  
           client.println("");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">Turn On LED1</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off LED1</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">Turn On LED 2</a>");
           client.println("<a href=\"/?button2off\"\">Turn Off LED 2</a>
"); 
           client.println("");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led1, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led1, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                 digitalWrite(led2, HIGH);
           }
           if (readString.indexOf("?button2off") >0){
                digitalWrite(led2, LOW);
           }
            //clearing string for next read
            readString="";  
           
         }
       }
    }
}
}

i tried linking the button to my website but when i click it. it got redirected to arduino server

for eg this the ip to turn the button on :1.186.221.121:25565/?button1on
i link this ip to my button in my website but when i click on it . it got redirected to my arduino webserver

for eg this the ip to turn the button on :1.186.221.121:25565/?button1on

Have you tried directing the request to the desired IP address like below?

client.println("<a href=\"http://1.186.221.121:25565/?button1on\"\">Turn On LED1</a>");

zoomkat:
Have you tried directing the request to the desired IP address like below?

client.println("<a href=\"http://1.186.221.121:25565/?button1on\"\">Turn On LED1</a>");

Yes I tried but I am still getting redirected toward my arduino webserver

Your ethernet.begin should look like below.

Ethernet.begin(mac, ip, gateway, gateway, subnet);

zoomkat:
Your ethernet.begin should look like below.

Ethernet.begin(mac, ip, gateway, gateway, subnet);

no you are not getting my point i have my arduino set up for home automation which act like a websever locally so i port forwarded my router so i can access it from internet.now i want to link my button which is in arduino webserver to a web site so i link this http://1.186.221.121:25565/?button1on\ to a button on my web site but when i click on it it get redirected to my arduino webserver. so i dont want to get redirected to my webserver

no you are not getting my point

That is correct, I really don't understand what you are talking about. The below does not make much sense.

now i want to link my button which is in arduino webserver to a web site so i link this http://1.186.221.121:25565/?button1on\ to a button on my web site

so you have a website(not on your arduino) to send a message to webserver on your arduino- did I understand right?
I dont think I can help, but I was looking for something similar, and would be interested to follow.

saiko:
so you have a website(not on your arduino) to send a message to webserver on your arduino- did I understand right?
I dont think I can help, but I was looking for something similar, and would be interested to follow.

I too might be on a similar quest and was wondering if the issue was resolved, and if so? How?

Can someone provide insight how the server works and can work? Does the website need to launch from the SD card, or can the sketch be programmed to send data to and receive commands from an already existing website?

I think I see..

if you use the href of your router forwarding port as suggested for the links you will get 'redirected' to your arduino, but isnt that what u want?

what I dont see in the project is code to handle the request these links send.

If Im right you need to look in the message msg from client to find the string GET /button1On

if you find a plain GET/ this may be a request for the page you are sending in your code.

so just add a flag if you get a /button.. request and test this after the coms is over. Then if we didnt receive a button message we send the page as you do now.

could give you a code example if this sounds right...

Sorry. My last post is probably nonsense as I imagine the browser won't like the link not working.

I have done this. Using javascript. (which doesn't have to be served by the arduino) You can't get anything back from the arduino and the browser is not 'redirected' to it. But you then don't know if it did what it was supposed to. Of course if you can see the LEDs this may not matter! It's dead easy to use this to add this function to any web page element you like. Happy to share this folly! (tested only in Firefox)

But have you tried putting a form on the page with 2 radio btns and then 'submit' the form to your arduino - see what if anything arrives at arduino and go from there? I didn't try this because I wanted to avoid the need to press a submit button.

Thanks for the offer, and happily accepted, though I am still in the learning curve for sure. Aside from the fact that Arduino is new to me, web design is really new and understanding how everything works is challenging at the moment. It was suggested here in the forum to devote my energy into understanding the relation of PHP and Arduino, but there is limited info out there on this specific subject. Was actually hoping to find a good tutorial to work through to gain a broader understanding, but analyzing your code will also give me ample material to read through and try to understand. I know there are immense free resources out there for looking up key words and phrases so analyzing your code probably wouldn't be as difficult as I made it sound.

Thanks for sharing your code. Any other leads you can share that relates to PHP linking to Arduino would be greatly appreciated. I am very eager to learn and already have a PHP to link to.

It was suggested here in the forum to devote my energy into understanding the relation of PHP and Arduino, but there is limited info out there on this specific subject.

That is because PHP and the arduino have little in common. PHP is a file/database handling application and has very little useful I/O functionality, at least on windows servers. I would consider chasing PHP waste of time as far as arduinos are concerned.

zoomkat:
That is because PHP and the arduino have little in common. PHP is a file/database handling application and has very little useful I/O functionality, at least on windows servers. I would consider chasing PHP waste of time as far as arduinos are concerned.

Thanks Zoomkat. Can you share more wisdom on the subject in terms of what would be better for making my project accessible on the internet? My project is "going to be" something of an indoor garden controller. Most events will be triggered by time or air conditions within a room. I want to view and log data to a website if possible, and also add on/off buttons to represent the relays aside from that. I hope PHP can be used as I already spent $50 for the PHP and domain name.

Ok myggle I'll tidy it a bit - and post a url here?

I work in home integration and this idea only really makes sense to me if the arduino server is behind the house firewall and not accessible over the internet (beacuse it sends nothing back to the web client).

Thus in my example the address of the arduino is a local address 192.168 etc.

The web files can all be stored locally on the machine which is accessing the arduino. Doesn't need any web server. Thus to control say a light in the house you open the file in any browser (pc, ipad/phone whatever) and this 'web' page sends messages to the arduino server to turn on the lights.

But actually you can have this 'website' anywhere on the net. If you access this page from within your house LAN it will still talk to your arduino. If anyone else accesses the page they cannot drive your lights! (because the arduino has a local network address which isn't routeable). To me this is rather handy. Tho why you'd want to access this on a website when you could just open the files locally I can't imagine.

We end up with what is more like a smart IR handset because it has no feedback from arduino. This is a massive limitation. But it's still useful. I mean you can switch the lights on from your armchair! You dont need control system feedback for this.

There may well be other ways to do this and it's not at all how to learn anything about how the web works. But if you want to build a set of web pages and use a browser as your UI and this is for home use it's a bodge which seems to work for me. It isnt 'correct' web coding. It's a crude hack. And I'm a newbie to boot. So Caveat emptor!

I'm sure the same could be said of all this as zoomkat says for PHP.

If you dont mind everyone on the planet turning your lights on you could change the local address to an href (perhaps using dynamic dns) open a router port, site this webpage on any server and it would then work from anywhere as saifkasi proposed (I think), but i wouldn't do this.

It uses javascript.

(this script looks thru the web page and attaches a click event to any element you want. This event sends the element id to the arduino. - using javascript XMLHttpRequest - and Job done. It may not work in all browsers, but it does work in Firefox and I've not tested any others)

Can you share more wisdom on the subject in terms of what would be better for making my project accessible on the internet?

There have been previous post by others with very comprehensive home, aquarium, etc., web controlled systems with logging previously posted. Research these to see if they have what you need. Control and logging are somewhat two different operations which you may want to develop independently first. The main issue with windows PHP is it does not have very good serial port capabilities and may require disabling the arduino auto reset when the pc serial port is opened.

And another brick wall. It seems the correct thread will unearth itself to me in due time. Thanks I guess.
Peace!

here's a demo of what I'm saying

it's something to play with

dunno how practical it is but it's fun to watch!

see

www.drfranklin.webspace.virginmedia.com/arduinoServer/arduinoButtons.htm

maybe someone can say how best to post the sketch if this looks worth the candle

Dave