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

Thanks!

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 setup(){

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

}

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=\"/?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();

          ///////////////////// control arduino pin
          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
          }
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

PaulS

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

dtokez

Hi Paul.

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?

Code: [Select]
//////////////////////
//INCLUDES
//////////////////////
#include <SPI.h>
#include <Ethernet.h>
#include "Tlc5940.h"
//////////////////////
//END OF INCLUDES
//////////////////////


//////////////////////
//DEFINES
//////////////////////
int TLCDelay = 50;  //Delay after setting TLC
int brightness = 4095;  //Initial brightness to display
int dimStep = 0;
boolean flag = false;  //flag for animation or single shot, false = single shot
int pinNumber = 0;

// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))

//int vals [] = { 1000, 1000, 500, 500, 250, 200, 175, 175, 100, 75, 40, 5};
int vals [] = {
  2047, 1024, 512, 256, 128, 64, 16, 16, 32 };
int numVal = NUMITEMS (vals); //number of values in array


//////////////////////
//END OF DEFINES
//////////////////////


//////////////////////
//ETHERNET SETUP
//////////////////////
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;
//////////////////////
//END OF ETHERNET SETUP
//////////////////////


//////////////////////
//START OF MODES
//////////////////////
void allOn(){
  Tlc.clear();
  digitalWrite(7, HIGH);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}

void sides(){
  Tlc.clear();
  digitalWrite(7, HIGH);
  for (int i = 0; i < 4; i++)
  {
    Tlc.set(i, brightness);
  }
  for (int i = 7; i < 11; i++)
  {
    Tlc.set(i, brightness);
  }
  Tlc.update();
  delay(TLCDelay);
}

void gradient(){
  Tlc.clear();
  for (int i = 0; i < 4; i++)
    Tlc.set(i, 4095);

  for (int i = 4; i < 8; i++)
    Tlc.set(i, 1000);

  for (int i = 8; i < 12; i++)
    Tlc.set(i, 250);

  for (int i = 12; i < 16; i++)
    Tlc.set(i, 100);

  Tlc.update();
  delay(TLCDelay);
}

void frontRow(){
  Tlc.clear();
  Tlc.set(0, brightness);
  Tlc.set(4, brightness);
  Tlc.set(7, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void middle(){
  Tlc.clear();
  Tlc.set(4, brightness);
  Tlc.set(5, brightness);
  Tlc.set(6, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void alternate(){
  Tlc.clear();
  Tlc.set(0, brightness);
  Tlc.set(2, brightness);
  Tlc.set(5, brightness);
  Tlc.set(7, brightness);
  Tlc.set(9, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void stars(){
  Tlc.clear();
  Tlc.set(pinNumber, brightness);
  Tlc.update();
  pinNumber++;
  delay(100);
  delay(TLCDelay);
  flag = true;
}

//////////////////////
//END OF MODES
//////////////////////


//////////////////////
//START OF DIMMING CONTROL
//////////////////////
void up(){

  if ((brightness + vals[dimStep]) < 4095)
  {
    brightness = (brightness + vals[(dimStep - 1)]) ; //set new brightness level
    dimStep--;
  }

  Serial.println(brightness);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}

void down(){
  if (dimStep < numVal)
  {
    brightness = (brightness - vals[dimStep]); //set new brightness level
    dimStep++;
  }

  Serial.println(brightness);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}
//////////////////////
//END OF DIMMING CONTROL
//////////////////////


void setup(){

  Tlc.init(0);

  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='https://dl.dropbox.com/u/33809233/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("<meta name=\"viewport\" content=\"user-scalable = yes\" />"); //?
          client.println("<meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1, maximum-scale=1\" />");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?lighton\"\">All On</a>");
          client.println("<a href=\"/?lightoff\"\">All Off</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?up\"\">Up</a>");
          client.println("<a href=\"/?down\"\">Down</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?sides\"\">Sides</a>"); 
          client.println("<a href=\"/?gradient\"\">Gradient</a><br />");
          client.println("<br />");
          client.println("<br />");
          client.println("<a href=\"/?frontRow\"\">Front Row</a>");
          client.println("<a href=\"/?alternate\"\">Alternate</a>");
          client.println("<a href=\"/?middle\"\">Middle</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?stars\"\">Stars</a><br />");
          client.println("</BODY>");
          client.println("</HTML>");

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

       
          if(readString.indexOf("?lighton") >0)
          {
            allOn();
            flag = false;
          }
          else if(readString.indexOf("?lightoff") >0)
          {
            Tlc.setAll(0);
            Tlc.update();
            delay(TLCDelay);
            flag = false;
          }
          else if(readString.indexOf("?sides") >0)
          {
            sides();
            flag = false;
          }
          else if(readString.indexOf("?gradient") >0)
          {
            gradient();
            flag = false;
          }
          else if(readString.indexOf("?up") >0)
          {
            up();
          }
          else if(readString.indexOf("?down") >0)
          {
            down();
          }
          else if(readString.indexOf("?frontRow") >0)
          {
            frontRow();
            flag = false;
          }
          else if(readString.indexOf("?middle") >0)
          {
            middle();
            flag = false;
          }
          else if(readString.indexOf("?alternate") >0)
          {
            alternate();
            flag = false;
          }
          else if(readString.indexOf("?stars") >0 || flag == true)
          {
            stars();
          }



          //clearing string for next read
          readString="";

        }
      }
    }
  }
}


/*
            digitalWrite(7, LOW);
//Tlc.set(1, 0);
//Tlc.set(15, 0);
for (int i = 4095; i > (0-1) ; i--)
{
Tlc.set(15, i);
Tlc.set(10, i);
Tlc.update();
delay(1);
}
Tlc.set(9, 0);
Tlc.update();
delay(100);

*/


dtokez

Hi Paul.

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?

Code: [Select]
//////////////////////
//INCLUDES
//////////////////////
#include <SPI.h>
#include <Ethernet.h>
#include "Tlc5940.h"
//////////////////////
//END OF INCLUDES
//////////////////////


//////////////////////
//DEFINES
//////////////////////
int TLCDelay = 50;  //Delay after setting TLC
int brightness = 4095;  //Initial brightness to display
int dimStep = 0;
boolean flag = false;  //flag for animation or single shot, false = single shot
int pinNumber = 0;

// number of items in an array
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))

//int vals [] = { 1000, 1000, 500, 500, 250, 200, 175, 175, 100, 75, 40, 5};
int vals [] = {
  2047, 1024, 512, 256, 128, 64, 16, 16, 32 };
int numVal = NUMITEMS (vals); //number of values in array


//////////////////////
//END OF DEFINES
//////////////////////


//////////////////////
//ETHERNET SETUP
//////////////////////
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;
//////////////////////
//END OF ETHERNET SETUP
//////////////////////


//////////////////////
//START OF MODES
//////////////////////
void allOn(){
  Tlc.clear();
  digitalWrite(7, HIGH);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}

void sides(){
  Tlc.clear();
  digitalWrite(7, HIGH);
  for (int i = 0; i < 4; i++)
  {
    Tlc.set(i, brightness);
  }
  for (int i = 7; i < 11; i++)
  {
    Tlc.set(i, brightness);
  }
  Tlc.update();
  delay(TLCDelay);
}

void gradient(){
  Tlc.clear();
  for (int i = 0; i < 4; i++)
    Tlc.set(i, 4095);

  for (int i = 4; i < 8; i++)
    Tlc.set(i, 1000);

  for (int i = 8; i < 12; i++)
    Tlc.set(i, 250);

  for (int i = 12; i < 16; i++)
    Tlc.set(i, 100);

  Tlc.update();
  delay(TLCDelay);
}

void frontRow(){
  Tlc.clear();
  Tlc.set(0, brightness);
  Tlc.set(4, brightness);
  Tlc.set(7, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void middle(){
  Tlc.clear();
  Tlc.set(4, brightness);
  Tlc.set(5, brightness);
  Tlc.set(6, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void alternate(){
  Tlc.clear();
  Tlc.set(0, brightness);
  Tlc.set(2, brightness);
  Tlc.set(5, brightness);
  Tlc.set(7, brightness);
  Tlc.set(9, brightness);
  Tlc.update();
  delay(TLCDelay);
}

void stars(){
  Tlc.clear();
  Tlc.set(pinNumber, brightness);
  Tlc.update();
  pinNumber++;
  delay(100);
  delay(TLCDelay);
  flag = true;
}

//////////////////////
//END OF MODES
//////////////////////


//////////////////////
//START OF DIMMING CONTROL
//////////////////////
void up(){

  if ((brightness + vals[dimStep]) < 4095)
  {
    brightness = (brightness + vals[(dimStep - 1)]) ; //set new brightness level
    dimStep--;
  }

  Serial.println(brightness);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}

void down(){
  if (dimStep < numVal)
  {
    brightness = (brightness - vals[dimStep]); //set new brightness level
    dimStep++;
  }

  Serial.println(brightness);
  Tlc.setAll(brightness);
  Tlc.update();
  delay(TLCDelay);
}
//////////////////////
//END OF DIMMING CONTROL
//////////////////////


void setup(){

  Tlc.init(0);

  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='https://dl.dropbox.com/u/33809233/a.css' />");
          client.println("<TITLE>Home Automation</TITLE>");
          client.println("<meta name=\"viewport\" content=\"user-scalable = yes\" />"); //?
          client.println("<meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1, maximum-scale=1\" />");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation</H1>");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?lighton\"\">All On</a>");
          client.println("<a href=\"/?lightoff\"\">All Off</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?up\"\">Up</a>");
          client.println("<a href=\"/?down\"\">Down</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?sides\"\">Sides</a>"); 
          client.println("<a href=\"/?gradient\"\">Gradient</a><br />");
          client.println("<br />");
          client.println("<br />");
          client.println("<a href=\"/?frontRow\"\">Front Row</a>");
          client.println("<a href=\"/?alternate\"\">Alternate</a>");
          client.println("<a href=\"/?middle\"\">Middle</a><br />");
          client.println("<br />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<a href=\"/?stars\"\">Stars</a><br />");
          client.println("</BODY>");
          client.println("</HTML>");

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

       
          if(readString.indexOf("?lighton") >0)
          {
            allOn();
            flag = false;
          }
          else if(readString.indexOf("?lightoff") >0)
          {
            Tlc.setAll(0);
            Tlc.update();
            delay(TLCDelay);
            flag = false;
          }
          else if(readString.indexOf("?sides") >0)
          {
            sides();
            flag = false;
          }
          else if(readString.indexOf("?gradient") >0)
          {
            gradient();
            flag = false;
          }
          else if(readString.indexOf("?up") >0)
          {
            up();
          }
          else if(readString.indexOf("?down") >0)
          {
            down();
          }
          else if(readString.indexOf("?frontRow") >0)
          {
            frontRow();
            flag = false;
          }
          else if(readString.indexOf("?middle") >0)
          {
            middle();
            flag = false;
          }
          else if(readString.indexOf("?alternate") >0)
          {
            alternate();
            flag = false;
          }
          else if(readString.indexOf("?stars") >0 || flag == true)
          {
            stars();
          }



          //clearing string for next read
          readString="";

        }
      }
    }
  }
}


/*
            digitalWrite(7, LOW);
//Tlc.set(1, 0);
//Tlc.set(15, 0);
for (int i = 4095; i > (0-1) ; i--)
{
Tlc.set(15, i);
Tlc.set(10, i);
Tlc.update();
delay(1);
}
Tlc.set(9, 0);
Tlc.update();
delay(100);

*/


PaulS

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

Go Up