Home Automation ENC28J60 UIPEthernet problem

Hello guys,

My name is Perry and I am a 27 years old System and Network Administrator (specialized in Routing and Switching).

I’m very new to this forum and to Arduino programming.
If I’m in the wrong place here I hope the mods will make it right :D.

I have a Arduino Mega2560 set up with a ENC28J60 board and a 25A Solid State Relay.

I scraped some code together and now have a working website on my Arduino where I can switch my heater on (more to be added).

All works well locally but when I forward port 80 to my Arduino and try to reach from outside it doesn’t load the page.

To check if my port forwarding is OK, I changed IP it forwards to to my pc with XAMMP and TADAAH I get XAMMP welcome screen from outside.

I can see that the data is being forwarded to the Arduino on my router stats (Mikrotik).

Am I missing something here???!!!

I will attach my code so that maybe someone can spot my error.

Looking forward to playing around with you guys in the wonderfull world of Arduino.

#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
#include <SPI.h>

int REL1 = 30;
int REL2 = 31;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 88, 178 };                     // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 88, 1 };                  // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask

EthernetServer server(80);                             //server port
String readString;

void setup() {
  pinMode(REL1, OUTPUT);
  pinMode(REL2, OUTPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // 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("<HTML>");
          client.println("<HEAD>");
          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
          client.println("<TITLE>Perry Home Automation</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>Home Automation Project</H1>");
          client.println("<hr />");
          client.println("
");
          client.println("<H2>Arduino MEGA with ENC28J60 and Relay</H2>");
          client.println("
");
          if (digitalRead(30)) {
            client.print("Kachel is <font color=’green’>AAN</font>");
          }
          else {
            client.print("Kachel is <font color='red'>UIT</font>");
          }
          client.println("
");
          client.println("<a href=\"/?button1on\"\">AAN</a>");
          client.println("<a href=\"/?button1off\"\">UIT</a>");
          client.println("
");
          client.println("
");
          if (digitalRead(31)) {
            client.print("Relais is <font color=’green’>AAN</font>");
          }
          else {
            client.print("Relais is <font color='red'>UIT</font>");
          }
          client.println("
");
          client.println("<a href=\"/?button2on\"\">AAN</a>");
          client.println("<a href=\"/?button2off\"\">UIT</a>");
          client.println("
");
          client.println("
");
          client.println("Alles");
          client.println("
");
          client.println("<a href=\"/?buttonALLon\"\">AAN</a>");
          client.println("<a href=\"/?buttonALLoff\"\">UIT</a>");
          client.println("
");
          client.println("<p>Created by Perry Boerboom</p>");
          client.println("
");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          if (readString.indexOf("?button1on") > 0) {
            digitalWrite(REL1, HIGH);
          }
          if (readString.indexOf("?button1off") > 0) {
            digitalWrite(REL1, LOW);
          }
          if (readString.indexOf("?button2on") > 0) {
            digitalWrite(REL2, HIGH);
          }
          if (readString.indexOf("?button2off") > 0) {
            digitalWrite(REL2, LOW);
          }
          if (readString.indexOf("?buttonALLon") > 0) {
            digitalWrite(REL1, HIGH);
            digitalWrite(REL2, HIGH);
          }
          if (readString.indexOf("?buttonALLoff") > 0) {
            digitalWrite(REL1, LOW);
            digitalWrite(REL2, LOW);
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}

Hi Perry/ Do you see anything printed on the serial monitor when you access the page from outside? Its possible that port-forwarding is messing with the line endings. Its also possible that forwarding extends the request length beyond the 100 characters you currently allow. You may learn something if you re-enable the character-by-character printing that is currently commented out.

Hi rw950431,

First off thanks for your reply.
To be honest with you, I don’t get anything in the serial monitor.
Is there something I have to activate or so?
Second, I have a problem with the Arduino webserver hanging all the time.
Anyone know anything about this?

Greetings Perry

Do you see the initial “server is at…” message in the serial monitor when you first power it on?

Suggest you remove the comments from this section like so

//read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          Serial.print(c);
        }

Which will print out characters received in real time. Use this to compare local requests with the port-forwarded one.

Silly me didn't add Serial.begin in my setup. Now I get the serial monitor. When I open the page from outside of my network, nothing shows up in the serial monitor. When I do it locally and press a button it gives me output as follows: GET / HTTP/1.1 GET / HTTP/1.1

GET /favicon.ico HTTP/1.1 GET /favicon.ico HTTP/1.1

GET /favicon.ico HTTP/1.1 GET /favicon.ico HTTP/1.1

GET /?button1on HTTP/1.1 GET /?button1on HTTP/1.1

GET /favicon.ico HTTP/1.1 GET /favicon.ico HTTP/1.1

GET /favicon.ico HTTP/1.1 GET /favicon.ico HTTP/1.1

Hope you guys can help.