WebServer With LED Control / Buttons / Status Display / Auto Status Update etc

I spent the past month learning, trial and error and finally putting it all together.
I still know very little, but I know a lot more than when I started

I have posted the full code for my Webserver in the attachment -(It would not fit in the insert code - as it exceeds 9500 characters)

The Server runs with multiple connections - LAN or internet.

8 buttons for turning items on / off - I used colors, and sizing for examples

It Displays the status of each device / auto updates the status

Refreshes the sensor status every 35 secs.
The Sensor displays if room is light or dark.
++++++++++++++++++++++++++++++++++++++++++++++++

Next on the list to add:

Display "connected" on LCD when a user connects - should be easy - LCD is on order - 2 line serial ---- looking for coding ideas

Display the actual IP - ? hmm

PIR sensor - should be easy

Very Simple password protection -? ?

I am also open to coding improvements to make it more efficient, if any one is willing to help this project along


The hours of trying to code, trial and error and put it all together was a joint effort, I thank all those who helped!.

Special Thanks to zoomkat for some test code - It made me redo a large portion of my server but , it works much better now!!

Hopefully this will provide a starting point...for others to build from.

Joe :smiley:

Enjoy
The retired hobbyist

webserver999forum.ino (8.9 KB)

Display "connected" on LCD when a user connects - should be easy - LCD is on order - 2 line serial ---- looking for coding ideas

Be sure to display disconnected, too, a few milliseconds later when the connection ends.

Display the actual IP - ? hmm

There was a thread in the Networking section talking about how to add code to get the IP address. It is not available with the Ethernet library as-is.

PIR sensor - should be easy

Yes.

Very Simple password protection -? ?

It would be easy to create a web site that knew the IP address of your Arduino, but did not publish it. Password protection could then be that site's responsibility, and the Arduino could ignore connections from other IP addressed. Of course, this means that you need to go find that thread, and learn how to get the IP address of the client.

PaulS,

Thanks for the feedback.

I will start testing each add-on in detail, as soon as the lcd and pir arrive.

LCD is first on the list

Good point about Displaying - "disconnected"

I am looking at my code to determine exactly at what point in the code a connection can be determined.

Joe

I am looking at my code to determine exactly at what point in the code a connection can be determined.

Perhaps the "if(client.connected())" code would be a good place to start.

PaulS,

Thanks for the input.

I must have been sleeping, I know where to put (test if connected) -- the "connected " was a typo -- oops....

It was the supposed to say "disconnected".. I was looking for the best spot to turn off a led and display "disconnected"

Joe

joeman:
Good point about Displaying - "disconnected"

I am looking at my code to determine exactly at what point in the code a connection can be determined.

I'd suggest displaying the address -- the connection may last such a short amount of time that you might not be able to see it otherwise.

jfhaugh,

Displaying the connection address is definitely one to add on.

Goal:

  1. To display "CONNECTED" on line 1 of Serial lcd - this should be easy, currently have added a led to (test) turn on when this occurs - it works. The Serial LCD display will be added as soon as it arrives.

  2. To display "DIS-CONNECTED" on line 1 of Serial lcd - when the connection drops - -- looking at the code now to see the best place to detect this and turn off test led

  3. To display the Connection address (ip) on line 2 - I am looking at some info that PaulS pointed me to ....

Thanks
Joe

long waitUntil=0;
boolean LED4state = true;



void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      digitalWrite(4, HIGH); // turns on an led every time a connection or command occurs
      if (client.available()) {
        char c = client.read();

The digital write acts like a system status LED - a connection, a refresh or a command and the led flashes for about a sec then goes off.

/stopping client
          client.stop();
          
          digitalWrite(4, LOW); // turn OFF "connected" LED 
          
          //clearing string for next read
         readString="";

The last digital write (LOW) shuts the led off

With the 2 digital writes i added it now causes a led to flash on for about a sec then off for each connection, a refresh or a command - like a connection / activity LED.

This is ok for a status led but if i used an LCD I would only see the Word "connection" displayed for a second-.


As this test is in preparation for a future LCD, I need the device (led, or LCD to stay on and only off when there is no connection and if not then stay on at least long enough to see it displayed .

TCP:
I believe a connection is only valid for the amount of time it takes to execute the command, or when a refresh occurs.
If this is true then the first code using just a digital write (HIGH) at the "while connect" makes sense as to why the led is on.

The next digital write ( LOW) at the client stop area of the code causes the led to go off because the client stop / all commands are done

I inserted the code below just after the "while connected" but gave me odd behavour.

I also tried inserting the code just after the client stop but this did not work.

   //digitalWrite(4, HIGH); // turn on "connected" LED
      // Each time through loop(), set Pin 4 to the same value as the state variable
  // digitalWrite(4, LED4state);
  // Nothing will change until millis() increments by 20000 - 20 seconds
   //if (millis() >= waitUntil) {
   //   LED4state = !(LED4state);        // Toggle the LED's state
    //  waitUntil = millis() + 20000;  // Make sure we wait for another 20000 milliseconds.

Where am I going wrong??

Joe

I believe a connection is only valid for the amount of time it takes to execute the command, or when a refresh occurs.

True.

If this is true then the first code using just a digital write (HIGH) at the "while connect" makes sense as to why the led is on.

I'm glad it makes sense, then.

I inserted the code below just after the "while connected" but gave me odd behavour.

The commented out shouldn't have done anything. If parts of it were not commented out, you should tell us which parts, and what the odd behavior was.

I also tried inserting the code just after the client stop but this did not work.

It is almost a certainty that it did something. It is almost a certainty that you wanted something to happen. That you posted this comment at all is an indication that those two somethings are not the same thing. What either of them is, though, is a mystery.

Where am I going wrong??

Back there a little ways is a clue.

In a simple server code why not print to the serial monitor the millisecond value when the connection is made and when the disconnection is made to get an idea just how long the connection is in effect?

  Serial.print("Time: ");
  time = millis();
  //prints time since program started
  Serial.println(time);