Go Down

Topic: Ethernet PHP control - I want to stay in a loop and check for a new string (Read 1 time) previous topic - next topic

spatula

It may be a matter of taste, but after writing an entire HTML page in code I would enjoy adding some logic to the program ;). If flash = false is the default state, you can just write it before the string comparisons, e.g.

Code: [Select]

flash = false;
if(readString.indexOf("?lighton") >0)
... // just set the PIN value, leave flash in the default state
else if(readString.indexOf("?flashing") >0)
{
  flash = true;
}


In order to make the logic more readable I would put the code for the HTML output in a function of its own. It is just presentation and it's probably going to grow in size, but it gets in the middle of the most important function in the loop.

dtokez

Hi Spatula

The only issue I can see with that method is won't flash will get set to false each time through the loop, I need it to stay true until something else gets turned on - does that make sense?

Thanks for the advice regarding the html code in a function, I will take a look at that :)

spatula

You are right - if you get a string that doesn't contain any of the strings you are checking, the flash state will be set to false. So it's probably better to set flash = false within each if block, as you did.

I don't know whether you actually tested the code using a browser. I wonder how it reacts to your sending the response and closing the connection right after reading the first line with the query string (ignoring the rest - host, user-agent, ...). Just curious.



dtokez

I have been running the code and it runs fine, unless I send requests very fast or the arduino is in a delay function and not ready to receive then I get a page not found error and the arduino needs a reset which can be annoying?

OK, many thanks for the input. At least the way I'm structuring the code using lots of separate functions I can call on them easily.

I have just tried to create a function for just the html request, but it won't compile because 'client' was not declared in the scope, how could I make it a global variable, or should I move the rest of the request stuff in with the html function?

Here's the code I have at the moment, needs a lot of work yet.. it won't fit in this post :(

https://dl.dropbox.com/u/33809233/TLC_AND_PHP_CONTROL_COMBINED.ino




PaulS

Quote
how could I make it a global variable

You shouldn't.

Quote
or should I move the rest of the request stuff in with the html function?

Why not pass the function a reference to the client?

Using the Additional Options link, and attaching the code is much better than using dropbox.

dtokez

I pondered over what you meant by passing a reference to the client in the function, adding EthernetClient client = server.available(); to the start works fine. Is that what you meant?

Many thanks!

PaulS

Quote
I pondered over what you meant by passing a reference to the client in the function, adding EthernetClient client = server.available(); to the start works fine. Is that what you meant?

To the start of what?

If you added client as a global variable, that won't work. Each time a client connects, a new client instance is created. You can pass that client instance to a function, using a reference (&).

Code: [Select]
void loop()
{
   EthernetClient client = server.available();

   myFun(client);
}

voif myFun(EthernetClient &joe)
{
   if(joe.connected())
   {
      // Find out what joe wants
   }
}

Go Up