count the number of incoming serial messages?

I'm working on a project that reads incoming serial data from an ESP8266 to control an industrial light tower for a friend, and I'd like to add in a little prank with some random tones based off of how many times data is received(well, how many times a button is pressed on the esp web page). Is there a way to add a counter to count the number of incoming serial messages?

cwhummel: ...an industrial light tower...

Is that something involving human safety?

Assuming that you are not involved in a system with safety implications ...

What you want to do seems so simple that I can't envisage what you want advice about.

Look at the examples in serial input basics. They print a message when it has been received. It would be trivial to increment a counter each time.

...R

[quote author=Coding Badly link=msg=2318161 date=1437027119] Is that something involving human safety?

[/quote] No. A friend found one at a surplus store that is similar to what we have at work and wants it for her desk.

What you want to do seems so simple that I can't envisage what you want advice about.

Um....thanks for letting me know I'm dumb. Of course, you could have just said, "sure, this is how you do it."

Remember this is a world wide forum I for one have never come across an an industrial light tower that fits on a desk. I would have though that an industrial light tower was something about at least 30 ft high.

Have you got a link to one showing it on the desk. You also need to provide a lot more information like what voltage are these lights you want to control, how much current do they take and how many lights are there? Do you want individual control over each light and do you want dimming or on/off control?

This is the light tower

It runs on 22vdc, and each light is selectable with either constant or flashing pin. Video of getting it working

I have it connected to the arduino via opto-isolators and have it running just fine, with a webserver on the esp8266 with buttons to select which light to have on, or a couple little "programs" like random, chase, knight rider KIT, etc...

All I want to do is add a counter to each time the state is changed to add in some music.

Thanks for the links, never come across the term before although I have seen lights like that.

If the data is coming in over a serial link then there is probably an end character in the string, this is normally something like a CR or LF ( carriage return or line feed that is values 10 or 13). So one way would be to look at each byte that comes in and increment a counter when you see the end character.

You need to know a lot more about the nature of the messages you are getting.

THANK YOU!

yes, I do get the 10 and 13 (I knew what they were) but was having them ignored and kind of forgot about them.

cwhummel:
Of course, you could have just said, “sure, this is how you do it.”

That’s why I posted a link to serial input basics - did you read it ?

I was also concerned that I had completely misunderstood what you want to do. I certainly was not trying to say you are dumb.

…R

I certainly was not trying to say you are dumb.

no, i know. I was having a bad morning and it felt like I was getting a non-answer, I just wasn't connecting the dots in your link. Now that I know what I'm looking for it makes a lot more sense now.

I hardly post here anymore because I'm so tired of the whole "if you don't know then I'm not telling you" and "that's stupid, why are you doing it that way?" scenarios.

cwhummel: I hardly post here anymore because I'm so tired of the whole "if you don't know then I'm not telling you" and "that's stupid, why are you doing it that way?" scenarios.

There is no doubt that some replies could be written in a better style, but IMHO, none of them is intended to be a personal attack and if you read them in that light most of them have some useful information.

And there is nothing to prevent you saying "sorry, I don't understand ..." That usually elicits a more generous response.

And, also IMHO, there are some people with questions who choose to focus entirely on whatever negative aspects they can find in a reply. No doubt, in the best of all possible worlds, such people should also get a generous response - but we are in the real world and complaining generally does not endear the questioner to the person that was trying to help.

If you have more questions when you have digested the link I gave you please ask.

...R

cwhummel:
I’m working on a project that reads incoming serial data from an ESP8266 to control an industrial light tower for a friend, and I’d like to add in a little prank with some random tones based off of how many times data is received(well, how many times a button is pressed on the esp web page). Is there a way to add a counter to count the number of incoming serial messages?

Some server and web page code that includes counting the number of request sent from the browser to the server, and displaying the number in the web page.

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString; 

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

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("

");
            
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
            
            client.print("
analog input1 is: ");
            client.print(analogRead(analogInPin1));
                        
            client.print("
analog input2 is: ");
            client.print(analogRead(analogInPin2));
            
            client.print("
analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                    
            client.print("
analog input4 is: ");
            client.print(analogRead(analogInPin4));
            
            client.print("
analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("
</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}