1

I am working on a web server project for work. I have a web server setup with an Arduino Meg2560 with Ethernet. I also have a 16 relay board connected to Ethernet with its own IP. I wanted to see if there is a way I can make calls to the relay board via IP from the Arduino Webserver. For example Relay 1 on the relay board can be turned on by typing 192.168.1.4/30000/00 but this opens the web GUI on the board. I need to do the same thing but without opening the webpage or web GUI. enter image description here

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

    // MAC address from Ethernet shield sticker under board
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    IPAddress ip(192, 168, 1, 177); // IP address
    EthernetServer server(80);  // Server Port 80

    void setup()
    {
      Ethernet.begin(mac, ip);  
      server.begin();           
    }

    void loop()
{
EthernetClient client = server.available();  // try to get client

if (client) {  // got client?
    boolean currentLineIsBlank = true;
    while (client.connected()) {
        if (client.available()) {   // client data available to read
            char c = client.read(); // read 1 byte (character) from client
            // last line of client request is blank and ends with \n
            // respond to client only after last line received
            if (c == '\n' && currentLineIsBlank) {
                // send a standard http response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                client.println("Connection: close");
                client.println();
                // send web page
                client.println("<!DOCTYPE html>");
                client.println("<html>");
                client.println("<head>");
                client.println("<title>Arduino Web Page</title>");
                client.println("</head>");
                client.println("<body>");
                client.println("<h1>Hello from Arduino!</h1>");
                client.println("<p>A web page from the Arduino server</p>");
                client.println("</body>");
                client.println("</html>");
                break;
            }
            // every line of text received from the client ends with \r\n
            if (c == '\n') {
                // last character on line of received text
                // starting new line with next character read
                currentLineIsBlank = true;
            } 
            else if (c != '\r') {
                // a text character was received from client
                currentLineIsBlank = false;
            }
        } // end if (client.available())
    } // end while (client.connected())
    delay(1);      // give the web browser time to receive the data
    client.stop(); // close the connection
} // end if (client)

}

  • I have my web server working and ready to go on my Arduino I just need help adding code to it for calling my relay board. Thank you – Kevin Jul 17 '15 at 16:53
  • Which model of relay board is this? I don't see any need for server in arduino as you already have one in the relay module. – user.dz Jul 17 '15 at 21:50
  • So what if it "opens a GUI" when you send a GET request to that URL. That doesn't mean you have to DO anything with that GUI data - you can just ignore it completely. Just send the GET request then close the connection. – Majenko Jul 17 '15 at 21:56
1

Documentation on the controller you are using is pretty scant, but it looks like you just need to make a GET request to the URI corresponding to the action you need to take.

http://192.168.1.4/30000/00 : Relay-01 OFF

http://192.168.1.4/30000/01 : Relay-01 ON

http://192.168.1.4/30000/02 : Relay-02 OFF

http://192.168.1.4/30000/03 : Relay-02 ON

http://192.168.1.4/30000/04 : Relay-03 OFF

http://192.168.1.4/30000/05 : Relay-03 ON

http://192.168.1.4/30000/14 : Relay-8 OFF

http://192.168.1.4/30000/15 : Relay-8 ON

http://192.168.1.4/30000/41 : Enter

http://192.168.1.4/30000/40 : Exit

http://192.168.1.4/30000/42 : Next Page

http://192.168.1.4/30000/43 : Next Page

For example If I wanted to turn off relay 3, I would simple make a GET request to http://192.168.1.4/30000/04 and close the connection. Example code:

IPAddress server(192,168,1,4);
EthernetClient client;
client.connect(server, 80);
client.println("GET /30000/04 HTTP/1.1");
client.println("Connection: close");
client.println();
client.stop();
  • How do I add that to the Arduino web server I think that is exacly what I am needing to add I would like to just have a link for now say Relay 1 On and it do the Get /30000/01 to turn it on. I added my web server Sketch to the main question. Thank you – Kevin Jul 18 '15 at 7:04
  • Thank you Jake I was able to get the GET code to work. I am new to the Arduino so it took me awhile to figure out what the code is doing. – Kevin Jul 19 '15 at 7:07

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.