Skip to main content
Updated my issue, my code doesn't work more than once.
Source Link
Arxkz
  • 23
  • 6

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 webserverweb 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!

In the void setup() I try to connect to the internet with DHCP, and if not, just connect with specified IP and MAC adressesaddresses. I also print the IP adressaddress 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 adressaddress 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 apreciatedappreciated, 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'sHere are the outputs:

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.

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 variable. I must code a local webserver 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!

In the void setup() I try to connect to the internet with DHCP, and if not, just connect with specified IP and MAC adresses. I also print the IP adress 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 adress 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 apreciated, 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's the outputs:

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.

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!

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:

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.

Source Link
Arxkz
  • 23
  • 6

Sending data from webserver to Arduino

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 variable. I must code a local webserver 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.begin(9600);

  if(Ethernet.begin(mac)==0){
    Serial.println("Failed to use DHCP");
    Ethernet.begin(mac, ip);
  }
  Serial.println(Ethernet.localIP());
  HttpHeader="";
}

void loop(){

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

          client.stop();
        }
      }
    }
  }
}

In the void setup() I try to connect to the internet with DHCP, and if not, just connect with specified IP and MAC adresses. I also print the IP adress 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 adress 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 apreciated, 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's 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.