switch status via URL query string

howdy,
I am currently using a UNO + Ethernet shield which shows me the status of garage door on a basic web server i.e. http://192.168.1.50 - shows the web page with the info . What I was needing ( if at all possible ) if for the status of the door to be show via a query string i.e. http://192.168.1.50/?=open or http://192.168.1.50/?=closed. I am running an npm homebridge server which can send GET requests to see if my garage door is open or not, and as I have nor provided a URL my iPhone freaks out every time I open the garage door using homekit.

I was wondering if this is possible ? any help would be greatly appreciated :slight_smile:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,50); 
EthernetServer server(80);  

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

void loop()
{
   EthernetClient client = server.available();  

   if (client) {  
       boolean currentLineIsBlank = true;
       while (client.connected()) {
           if (client.available()) {   
               char c = client.read(); 
               
                  if (c == '\n' && currentLineIsBlank) {
                   
                   client.println("HTTP/1.1 200 OK");
                   client.println("Content-Type: text/html");
                   client.println("Connnection: close");
                   client.println();
                  
                   client.println("<!DOCTYPE html>");
                   client.println("<html>");
                   client.println("<head>");
                   client.println("<meta http-equiv=\"refresh\" content=\"1\">");
                   client.println("</head>");
                   client.println("<body>");
                   GetSwitchState(client);
                   client.println("</body>");
                   client.println("</html>");
                   break;
               }
               
               if (c == '\n') {
                  
               currentLineIsBlank = true;
               } 
               else if (c != '\r') {
               
                   currentLineIsBlank = false;
               }
           } 
       }
       delay(1);     
       client.stop(); 
   } 
}

void GetSwitchState(EthernetClient cl)
{
   if (digitalRead(5)) {
       cl.println("<p>main door is open</p>");
   }
   else {
         }
   if (digitalRead(6)) {
       cl.println("<p>main door is in the middle</p>");
   }
   else {
   }
   if (digitalRead(7)) {
       cl.println("<p>main door is closed</p>");
   }
   else {
   }  
   if (digitalRead(8)) {
       cl.println("<p>manual door is closed</p>");
   }
   else {
   }
   if (digitalRead(9)) {
       cl.println("<p>manual door is open</p>");
   }
   else {
   }
     
}

Capture1.PNG

sorry new to this forum , I did read it but completely forgot :confused: , updated now.

I'm not sure I understand your problem. Query strings are used for the client (in this case the browser or your homebridge server) to pass information to the server (in this case your Arduino). The server can't pass information back in the query string, it can only pass it back in the HTTP response.

Typically, you could use the query string in the way you have described to tell the server to open or close the garage door.

It looks like you are already responding with HTML that says whether the door is open or closed. This is good for humans with browsers, but not so good for other servers. Have a look at returning JSON as an alternative, which is pretty standard and well supported by both browsers and server side programming languages. You can really return whatever you like (e.g. CSV) but the HTTP response should indicate what type of content it is with the Content-Type header.

If you want to be really fancy, you can support both HTML and JSON by looking at the HTTP Accept header in the request to see what the client wants.

sounds like you understand, I don't need anything to go from the homebridge server to the Arduino, I just need to Arduino to, I guess " broadcast " ( probably not the correct terminology ) the state of my reed switch in the form of a URL. that way when the homebridge server wants to know if the state of my reed switch is open or closed all it has to do it look the URL and it gets back "open " or " closed " or say a 0 or a 1 representing open or closed.

You have that already, except you are returning HTML. That is this piece of the code:

                   client.println("HTTP/1.1 200 OK");
                   client.println("Content-Type: text/html");
                   client.println("Connnection: close");
                   client.println();
                  
                   client.println("<!DOCTYPE html>");
                   client.println("<html>");
                   client.println("<head>");
                   client.println("<meta http-equiv=\"refresh\" content=\"1\">");
                   client.println("</head>");
                   client.println("<body>");
                   GetSwitchState(client);
                   client.println("</body>");
                   client.println("</html>");

You need to decide whether you want to keep the HTML working and have the ability to also return JSON or CSV, or just modify this code to return JSON or CSV instead of HTML.

The simplest is to just change this code to return what you want, but then you lose the pretty web page with the automatic refresh.

If you want both, you will need to change the Arduino code to look at the incoming request to see whether it is from a browser or your application. If browser, write out HTML to the client like what you are doing now. If an application, create a new function to write out a HTTP response containing JSON or CSV or whatever format you want.

You can determine whether the client is a browser or application by using a different URL for requests from each and checking the incoming URL in the HTTP request on the Arduino, or using the Accept HTTP header to decide what format to return.

You're going beyond the basic example now, so you will need to get a basic understanding of HTTP.

ahha than makes more sense now, i don't care about being able to view the web page anymore, i also realise that i have bitten off more than i can chew - much research to be done ! thank you kind sir for explaining this.

Don't give up too easily. You aren't far away with the example that you have.