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

Hi all, basically I'm working on controlling some LED's via Ethernet. The main code is working nicely and I'm checking for a quite a few strings to switch things on and off via calling separate functions, but I have gone back to the bare bones code now to try and get something new working

I would now like to add a function with a loop that runs until another string comes in from the Ethernet controller, is this possible?

I want to create a animation that runs without running through the rest of the code. I tried a method using flags so I could decipher from a run once operation and a animation, but this was long winded and is causing me some headaches.

I would now like to add a function with a loop that runs until another string comes in from the Ethernet controller, is this possible?

You have that already. It's called loop().

If are planning to wait for another string from the same client, you are going to be disappointed. The client has made a request and gone away. If another client request appears, you have no way of knowing that it is the same client you hear from last time, or another client on the other side of the world. Nor should you try to know that.

Quote

I want to create a animation that runs without running through the rest of the code

Have you looked at what the client is actually sending? Each GET request is standalone. The same client might make a series of requests, but the server has no way of knowing that it is the same client.

Thanks for the input. That makes sense, so each time the arduino asks the controller if it has x y z string waiting for it?

so, I want to check for the animation string from the ethernet controller, then run the animation loop until another string arrives - which I have to check for.

I'm using 'if' statements to check for certain strings, and they will only be true once then the string will be gone, so how could I make the 'if' loop run continuously and check for other strings without running the rest of the loop() function running?

I hope that makes sense, maybe I am being daft and over complicating this matter?

The main code is below, currently if you go into the stars() function it appears to run only twice, but I can't see where the flag is being set back to false?

Thanks for the input. That makes sense, so each time the arduino asks the controller if it has x y z string waiting for it?

so, I want to check for the animation string from the ethernet controller, then run the animation loop until another string arrives - which I have to check for.

I'm using 'if' statements to check for certain strings, and they will only be true once then the string will be gone, so how could I make the 'if' loop run continuously and check for other strings without running the rest of the loop() function running?

I hope that makes sense, maybe I am being daft and over complicating this matter?

The main code is below, currently if you go into the stars() function it appears to run only twice, but I can't see where the flag is being set back to false? - I'm sure there must be a less clunky way of doing this without messing around with flags?

That makes sense, so each time the arduino asks the controller if it has x y z string waiting for it?

The Arduino is a server, when running your code. It doesn't ask anything anything. It responds when asked.

What might be confusing is the nature of the questions asked by the clients. What a client is asking the server is "Can you do something (with this data)?". That isn't really any different from the client asking "What is the temperature?" or "What are all the URLs on the Arduino site containing "PROGMEM"?" The supplied data doesn't change the fact that the client is asking the server a question.

Quote

I'm using 'if' statements to check for certain strings, and they will only be true once then the string will be gone

If you mean that the if statement will only resolve to true once per client request, that is probably true. The client data doesn't go away just because you checked whether it contains a particular string, though. So, I'm not entirely sure that I understand the question.

Quote

I hope that makes sense, maybe I am being daft and over complicating this matter?

I'm not sure that either assertion is true. Without seeing what the client asks, it's hard to understand what you want to do. From looking at the server page, though, it appears that each request from the client is a separate event, not something that could be used to put together any kind of animation. That is, the client request defines one frame of a movie, not a whole movie.

Basically, this is what my code does currently...check for stringif string arrived - turn something either on or offrun loop again to check for strings (if statements)

This is what I would like to do...check for stringif string arrived - turn something on or off, or start animation sequencecontinue running animation sequence and run main loop again to check for strings (if statements)

I think I have over complicated the matter, I have an idea. If I put all the if statements to test for strings into a function I could sit in a loop to run the animation and call the function to check for other strings, if any are true it would exit the loop?

continue running animation sequence and run main loop again to check for strings (if statements)

There is still the assumption, incorrect, that the client can send more strings. It can't. It sends all the data needed to satisfy one request at one time. The "animation" you are looking for comes from the same client making multiple requests in the proper order with minimum delay between requests. There is nothing that the Arduino can do to get multiple requests from the same client any faster than the client makes requests.

If I understand you, you wish to have an animation (something like a screensaver) running until a network request comes in. I take it that you "poll" something to see if the request has come in. You then want to process the single request of adjusting some LEDs I think. You then want to go back to the animation. Is that correct?

If so, then you insert your poll of the network controller somewhere inside the animation process like inside a loop or procedure that frequently executes. When you detect something, just handle it from within the animation or float some kind of signal up to the main level loop() to handle it.

If I understand you, you wish to have an animation (something like a screensaver) running until a network request comes in. I take it that you "poll" something to see if the request has come in. You then want to process the single request of adjusting some LEDs I think. You then want to go back to the animation. Is that correct?

If so, then you insert your poll of the network controller somewhere inside the animation process like inside a loop or procedure that frequently executes. When you detect something, just handle it from within the animation or float some kind of signal up to the main level loop() to handle it.

This is pretty much what I'm trying to do, sorry for being rubbish at explaining myself :~ lol I will try the approach you mentioned, many thanks

OK, right I'm trying this idea on my basic working code but having some trouble.

The command enters the flash function, in which I have an infinite while loop. The LED flashes as I would expect and the program stays in the loop, but when I press another button on the browser I get no response from the arduino and get a 404 error.

I'm not sure where I'm going wrong here, I'm calling the checkString function on each loop?

You are calling checkString() from inside flash(), but then that recursively calls flash() again. I don't think that's what you want. Are the other two functions working as you expect?

I don't want to be a jerk, but I think the problem here is the design. In my opinion, you need to get the arduino to do the flashing for you as a background operation, that way you just turn it on or off same as the other functions. Don't let that discourage you, because we can probably get it to work without doing that yet by reversing your approach.

Perhaps we could look at the problem from the standpoint that you spend most time flashing the LED and then checking for a client connection each loop. It's not the best way to do it, but it should work. You can't really call checkString() unless you have a new string to look at and that only happens when you get a new client connection. I also notice that you spit back the HTML form before processing the request, this precludes you from sending some feedback to the user about what happened should you later want to.

Save your existing sketch somewhere new, because we are about to perform major surgery on its structure. It's time to simplify your loop() by calling functions instead of having all that code in it. Here is some pseudo code for what I'm talking about:

//simple button GET server code to control servo and arduino pin 5//for use with IDE 1.0//open serial monitor to see what the arduino receives//use the \ slash to escape the " in the html//for use with W5100 based ethernet shields//Powering a servo from the arduino usually DOES NOT WORK.//note that the below bug fix may be required// http://code.google.com/p/arduino/issues/detail?id=605