Pages: 1 [2]   Go Down
Author Topic: Ethernet PHP control - I want to stay in a loop and check for a new string  (Read 1542 times)
0 Members and 1 Guest are viewing this topic.
Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink. If flash = false is the default state, you can just write it before the string comparisons, e.g.

Code:
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.
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

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



« Last Edit: February 20, 2013, 01:50:40 pm by dtokez » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

South UK
Offline Offline
God Member
*****
Karma: 5
Posts: 587
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void loop()
{
   EthernetClient client = server.available();

   myFun(client);
}

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

Pages: 1 [2]   Go Up
Jump to: