ENC28J60 and javascript

Hi

I am writing a small web server for my arduino nano so I can turn a relay on and off. For the purpose of testing I am using an LED to blink or off. My command is as follows:

To turn ON, I use this:
http://192.168.100.15 or http://192.168.100.15/?1

To turn OFF, I use this:
http://192.168.100.15/?0

Based on the value of the ?0 or ?1, I assign a boolean variable isBlink a true or false.
So far I was able to turn the blinking on or off by directly typing the addresses above in the browser. The JavaScript code below also works, except for the window.location.ref…, etc.
I was able to get the javascript to display an alert window, but not reload the page with the onclick event… wait, this is not a javascript question. My question is, I need to assign the code below to a variable javascriptCode, so I can add the /?0 or /?1 programmatically.

Can someone help me with that?
Thanks

          client.print("\
            <!DOCTYPE html>\
            <html>\
            <body>\
            <h2>Arduino Control</h2>\
            <button onclick=\"myFunction()\">Switch</button>\
            <script>\
              function myFunction() {\        
              window.location.href = \"192.168.100.15/?\";\              
              </script>\
            </body>\
            </html>\
          ");

Maybe, instead of one client.print() statement, issue three. That is one for the top part of the code, a second for the parameter 0 or 1 controlled by an if statement, and a third for the remaining code.
You could also assign your code to a String variable and put, say, @@@ in place of the parameter. You could then use a replace method to change the place marker @@@ into either a 0 or a 1.

The ENC28J60 library uses a LOT of FLASH and RAM because it has to implement the whole TCP/IP stack. You should use the F() macro around your large string constants to keep them from using up RAM space:

          client.print(F("\
            <!DOCTYPE html>\
            <html>\
            <body>\
            <h2>Arduino Control</h2>\
            <button onclick=\"myFunction()\">Switch</button>\
            <script>\
              function myFunction() {\        
              window.location.href = \"192.168.100.15/?\";\              
              </script>\
            </body>\
            </html>\
          "));

Thanks you both for your suggestions. I actually found that a simpler solution would be to have two buttons, one for on and one for off. It works to turn on the blinking, but I can’t always turn it off. I have to admit I don’t fully understand how this library works. I mean I understand that the client.print() is what sends the data to the browser, but it is not clear to me what happens once the browser receives the data. At times the browser seems frozen.

I tried adding a couple of … around my buttons code and the address doesn’t show the “/?0” or “/?1” anymore, but this may be because of the form tag.

I did notice that my code takes 80% of the memory which leaves me very little to use this to control something. I was going to see if I can remove some functions from the .h and .cpp files. Not sure if this will work, but I am experimenting.

I will post the updated code when I get home.

This is my updated code

          client.print(F("\
            <!DOCTYPE html>\
            <html>\
            <body>\
            <h2>Arduino Control 2</h2>\
            <button onclick=\"window.location = 'http://192.168.100.15/?1'\">Switch ON</button>\
            <button onclick=\"window.location = 'http://192.168.100.15/?0'\">Switch OFF</button>\
            </body>\
            </html>\
          "));

This is my complete code. I noticed that:

  • it is very slow to run when I click the buttons.
  • it will only run on a windows machine and not on an iphone
  • it is not reliable if the computer I am connecting from is on wifi.

Here is the complete code. Any comments are appreciated.

#include <UIPEthernet.h> // Used for Ethernet

// **** ETHERNET SETTING ****
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };                                      
IPAddress ip(192, 168, 1, 15);                        
EthernetServer server(80);
String readString = "";
String javascriptCode = "";
int readIndex = 0;

boolean isBlink = false;
const int LED = 7;

void setup() {
  Serial.begin(9600);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());  

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED, OUTPUT);
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

  if (client)
  {  
    Serial.println("-> New Connection");

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();

        //store characters to string
        readString = readString + c;

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n')
        {
          client.print(F("\
            <!DOCTYPE html>\
            <html>\
            <body>\
            <h2>Arduino Control 2</h2>\
            <button onclick=\"window.location = 'http://192.168.100.15/?1'\">Switch ON</button>\
            <button onclick=\"window.location = 'http://192.168.100.15/?0'\">Switch OFF</button>\
            </body>\
            </html>\
          "));

          Serial.println("readString " + readString);
          readIndex = readString.indexOf("?");
          Serial.println(readIndex);
          Serial.println("readStringVal >" + readString.substring(readIndex + 1, readIndex + 2)+"<");
          if(readString.indexOf("?") > 0) {
            if(readString.substring(readIndex + 1, readIndex + 2) == "1") {
              isBlink = true;          
            } else {
              isBlink = false;
            }
          }          
          Serial.println(isBlink);
          readString = "";          
          break;
        }
      }
    }

    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("Disconnected\n");
  }

  if(isBlink == true) {
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
    delay(1000); 
  }
}