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

dtokez

Hi again Paul

Sorry I was babbling a bit last night and not making much sense!

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


This is what I would like to do...
check for string
if string arrived - turn something on or off, or start animation sequence
continue 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?

Still not sure on the best way of doing it though

PaulS

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

afremont

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.
Experience, it's what you get when you were expecting something else.

dtokez


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 :)

dtokez

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?

Code: [Select]

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 0, 177 }; // ip in lan
byte gateway[] = {
  192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;

//////////////////////

void checkString(){
  if(readString.indexOf("?lighton") >0)//checks for on
  {
    digitalWrite(7, HIGH);    // set pin 4 high
  }
  else if(readString.indexOf("?lightoff") >0)//checks for off
  {
    digitalWrite(7, LOW);    // set pin 4 low
  }
  else if(readString.indexOf("?flash") >0)
  {
    Serial.println("test");
    flash();
  }
  //clearing string for next read
  readString="";
}


void flash(){
  int i = 0;
  while (i == 0){
    digitalWrite(7, LOW);
    delay(100);
    digitalWrite(7, HIGH);
    delay(100);
    checkString(); 
  }
}



void setup(){

  pinMode(7, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.begin(9600);

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?flash\"\">Flash</a>");
          client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
          client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");       

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          checkString();

        }
      }
    }
  }
}


Go Up