Hello Arduino enthusiasts! I've been working on a keypad lock system for school lately and I need some advice.Basically, my code checks if the entered passcode is the same than a predefined passcode stored in a variable. I must code a local web server hosted by my Arduino Mega board that allows someone to change the correct passcode remotely via Ethernet. I have coded a working program, but I am sure it can be better designed or done in an easier way, this is why I'm asking for advice on how I could improve the performances of my program or make it shorter. If you need further explanation, let me know and I'd be happy to explain. Thank you!

Here's my code(or if you think it's plain, with colors):

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xA5, 0x7E };
IPAddress ip(192,168,1,250);
EthernetServer server(80);

String HttpHeader;
String nwCode="";
int curCode=19988;

void setup(){

    Serial.println("Failed to use DHCP");
    Ethernet.begin(mac, ip);

void loop(){

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (HttpHeader.length() < 16){
        if (c == '\n') {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.print("<form method=get>");
          client.print("<input type='password' name=code required><br>");
          client.print("<input type=submit value=submit></form>");


In the void setup() I try to connect to the internet with DHCP, and if not, just connect with specified IP and MAC addresses. I also print the IP address used to host the webpage so we know what to type in our browser. Then in the void loop() I wait till someone visits the IP address and show them the webpage. Next, we read the incoming GET request(new passcode submitted) and make sure it's not any longer than 5 chars (correct passcode is 5 chars long), then I take only the numbers (remove GET and HTTP 1.1 from the request) and store them in the nvCode variable. Then if the request has ended and the passcode is 5 chars long, set the current code value to the new code(Without this step the serial monitor outputs 11 everytime the page is loaded or I make a request, I think this comes from the HTTP 1.1 and isDigit().) I know there are better ways of doing this and the simpler and shorter the better, so any advice is much appreciated, thanks!

I have used Serial output for debugging purposes and to better show what the code is doing but I plan on removing that afterwards. Here are the outputs:

This is what the Serial monitor outputs when I power the Arduino board. img(The IP is the address where the webpage is hosted)

This is the webpage hosted by the Arduino board. img

After submitting a new passcode, it sends a GET request. img

The Serial monitor output after sending the new passcode. img

TL;DR the nwCode variable must collect the passcode entered in the HTML form and must set var curCode's value to it. nwCode being the new passcode code and curCode being the current passcode.

!UPDATE! I just realized that I can change the passcode once, but if I try to change it again after, I get the wrong numbers on the serial monitor, so my code has a problem somewhere. Any help regarding this issue would be much appreciated.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.