Simple counter with serial LCD and PoE adapter

Hello, I builded a small counter that uses an infrared sensor to count objects on a conveyor.
However i want the counter to send the number of the counted object by network into a database.

Here is the code i use for counter to display the number on LCD:

int buttonPin = 2; // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = -1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);

// initialize serial communication:
Serial.begin(115200);
clearScreen();
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
clearScreen();
Serial.print("Numar placute: ");
Serial.println(buttonPushCounter);
}

}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

}
void clearScreen(){

Serial.write(byte(0x7C));

Serial.write(byte(0x00));
}

And here is the code that i think i have to combine with the previos one to get the data on the network:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // 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
int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonPushCounter = -1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
String readString;

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

void setup(){

// initialize the button pin as a input:
pinMode(buttonPin, INPUT);

// initialize serial communication:
Serial.begin(115200);
clearScreen();
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
pinMode(8, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);
Serial.println("server multi pin button test 1.0"); // so I can keep track of what is loaded
}

void loop(){
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
clearScreen();
Serial.print("Numar placute: ");
Serial.println(buttonPushCounter);
}

}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

// 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') {

///////////////
Serial.println(readString); //print to serial monitor for debuging

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

client.println("");
client.println("");
client.println("Arduino GET test page");
client.println("");
client.println("");

client.println("

Zoomkat's simple Arduino button

");

// For simple testing, pin 5, 6, 7, and 8 are used in buttons
// DIY buttons
client.println("ON");
client.println("OFF");
client.println(" ALL OFF

");

// mousedown buttons
client.println("<input type=button value=ON onmousedown=location.href='/?on4'>");
client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");
client.println(" <input type=button value='ALL OFF' onmousedown=location.href='/?off3579'>

");

// mousedown radio buttons
client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>");
client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>");
client.println(" <input type=radio onmousedown=location.href='/?off3579'>ALL OFF</>

");

// custom buttons
client.print("<input type=submit value=ON style=width:100px;height:45px onClick=location.href='/?on8'>");
client.print("<input type=submit value=OFF style=width:100px;height:45px onClick=location.href='/?off9'>");
client.print(" <input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579'>");

client.println("");
client.println("");

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

///////////////////// control arduino pin
if(readString.indexOf('2') >0)//checks for 2
{
digitalWrite(5, HIGH); // set pin 5 high
Serial.println("Led 5 On");
}
if(readString.indexOf('3') >0)//checks for 3
{
digitalWrite(5, LOW); // set pin 5 low
Serial.println("Led 5 Off");
}

if(readString.indexOf('4') >0)//checks for 4
{
digitalWrite(6, HIGH); // set pin 6 high
Serial.println("Led 6 On");
}
if(readString.indexOf('5') >0)//checks for 5
{
digitalWrite(6, LOW); // set pin 6 low
Serial.println("Led 6 Off");
}

if(readString.indexOf('6') >0)//checks for 6
{
digitalWrite(7, HIGH); // set pin 7 high
Serial.println("Led 7 On");
}
if(readString.indexOf('7') >0)//checks for 7
{
digitalWrite(7, LOW); // set pin 7 low
Serial.println("Led 7 Off");
}

if(readString.indexOf('8') >0)//checks for 8
{
digitalWrite(8, HIGH); // set pin 8 high
Serial.println("Led 8 On");
}
if(readString.indexOf('9') >0)//checks for 9
{
digitalWrite(8, LOW); // set pin 8 low
Serial.println("Led 8 Off");
}

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

}
}
}
}
}
void clearScreen(){

Serial.write(byte(0x7C));

Serial.write(byte(0x00));
}

Some help would be appreciated. Thanks!

Liviu

It is virtually impossible to read your lengthy code as it is. Please edit your Post and put the code in code tags using the code button</> so that it looks like this and can be selected and copied to a text editor.

...R

// mousedown buttons
          client.println("<input type=button value=ON onmousedown=location.href='/?on4'>");
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");       
          client.println("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579'>

");       
                   
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>");
          client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>");
          client.println("&nbsp;<input type=radio onmousedown=location.href='/?off3579'>ALL OFF</>

");

What do these have to do with putting the counter value in a database?

PaulS:

// mousedown buttons

client.println("<input type=button value=ON onmousedown=location.href='/?on4'>");
          client.println("<input type=button value=OFF onmousedown=location.href='/?off5'>");     
          client.println(" <input type=button value='ALL OFF' onmousedown=location.href='/?off3579'>

");     
                 
          // mousedown radio buttons
          client.println("<input type=radio onmousedown=location.href='/?on6'>ON</>");
          client.println("<input type=radio onmousedown=location.href='/?off7'>OFF</>");
          client.println(" <input type=radio onmousedown=location.href='/?off3579'>ALL OFF</>

");



What do these have to do with putting the counter value in a database?

I don't know the answer to that question, but they do provide a really effective RAM sink

but they do provide a really effective RAM sink

Just in case anyone is looking for one... 8)

Hello and thanks for the answers. The first code is what i done to display the number of the counted objects on an lcd display:

int  buttonPin = 2;    // the pin that the pushbutton is attached to


// Variables will change:
int buttonPushCounter = -1;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  
  // initialize serial communication:
  Serial.begin(115200);
  clearScreen();
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      clearScreen();
      Serial.print("Numar placute:  ");
      Serial.println(buttonPushCounter);
    } 
    
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
}
void clearScreen(){
 
  Serial.write(byte(0x7C));
 
  Serial.write(byte(0x00));
}

This is working ok, but i also need to send the number of counted object into a database by using that PoE adapter. That is where i have problems. The second code is something i found on the net, but didn't test it.
What i need is to add some lines in the first code, that will make the use of the PoE adapter to transmit those data in the database

The main thing that confuses me is that i use "Serial.begin(115200);" command to initiate the lcd screen, and in every ethernet shield example that i see, i notice it is the same command but at a different bitrate "Serial.begin(9600);" .
So would the two commands work together in the same program? like one for lcd screen and one for ethernet shield?

Best regards

Liviu