3

I currently have an Arduino Mega + an Arduino Ethernet shield, I would like to know if it's possible to send data to the arduino from a web-site hosted on the shield itself. So that when someone press a Button on the web-site it sends a signal to the arduino to trigger an action. Could anyone kindly help me.

  • Have you tried typing this question into Google? There are many, many examples of this being done. – Mark Smith Dec 31 '16 at 13:48
  • @MarkSmith There aren't in the specific way am trying, and I tried stack since its friendly to me compared to google which will bring me to spam sites, yet I didn't found my answer off google, it wasn't precise. – The Little Cousin Jan 4 '17 at 13:30
3

Yes of course!

First things first - Yes, it is possible to accomplish what you're thinking.

The idea is to create a server on the Ethernet Shield and open the webpage hosted by it on any device and based on the options selected on the web page we can trigger desired actions on the Arduino.

I've modified the web server code of Arduino built-in examples to match your requirements, just upload it and make necessary changes as required, here it is-

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

byte mac[] = {
   0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
 };


 IPAddress ip(192,168,1,199); // Any ip address not on your network.
 EthernetServer server(80);   // Port 80 is default for http.

  int led=2;

  String request;



  void setup(){
    Ethernet.begin(mac,ip);
    pinMode(led,OUTPUT);
    Serial.begin(9600);
   }



  void loop(){

      EthernetClient client = server.available();
  if(client)
         {
       Serial.println("Client is trying to connect......");
       while(client.connected())
       {
  if(client.available())
  {
    char c = client.read();
    request.concat(c);
    if(c=='\n'){
    Serial.print(request);
    if(request.indexOf("ON")>0){
      digitalWrite(led,HIGH);
      Serial.println("ON");
      }

      else if(request.indexOf("OFF")>0){
        digitalWrite(led,LOW);
        Serial.println("OFF");
        }
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
    client.println();

    client.println("<!doctype html>");
    client.println("<html>");
    client.println("<head><title>LED</title></head>");
    client.print("<!DOCTYPE HTML><html><body ><a href ='/?LED_ON'><p style = ' text-align:center'>LED ON</p></a><br><a href ='/?LED_OFF'><p style = ' text-align:center'>LED OFF</P></a><br></body></html>" );
    client.println("</html>");
    client.stop();
    request="";
    
  }
  }
 }
  
    }


   }

This sketch will turn an LED on or off attached to digital pin 2.

You have to go to the IP address that you provide to the shield (192.168.1.199 in my case). You'll see 2 hyperlinks to switch on or off the led and that would pass a query which the Arduino looks for again and again in the loop and takes action accordingly.

Hope this helps, try this out, and do let me know the result.

NOTE

Make sure that you access the Ethernet Shield from a device within your LAN only otherwise you'll have to use port forwarding.

  • It work all fine, yet I don't know if it's possible to have another file on the SD card in HTML and CSS to do the same exact thing. Instead of having an entire web-page in the sketch itself. – The Little Cousin Jan 2 '17 at 6:24
  • Obviously that's possible! Give me a day or two I'll help you out – Manav Jan 2 '17 at 16:18
  • 1
    The only limitation to the applications of an arduino is the user's imagination! :) – Manav Jan 2 '17 at 16:19
  • 1
    That is what it is, The process of parsing the url includes the query operator ('?') this operator would take our link and place it in the url under the same domain (IP in our case) and when the server (Arduino) gets the request it toggles the LED accordingly. – Manav Jan 3 '17 at 11:08
  • 1
    Hum isn't there any other way such as Serial to communicate to the arduino from a web interface. – The Little Cousin Jan 3 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.