SUCCESS with Port Forwarding

I've just followed the advice from JimboZA & now have WebServer working fine adding the line of code
EthernetServer server(8085);
I then went back and changed code to the original EthernetServer server(80); and that also works OK.

For others following this post in my TP-Link modem router I have set up NAT Virtual Server - which is Port Forwarding as follows:
Server Name: Arduino-8085
External Port Start: 8085
External Port End: 8085
Protocol: TCP/UDP
Internal Port Start: 8085
Internal Port End: 8085
Server IP Address: 192.168.1.177
WAN Interface: ppp0.2 (this may just relate to my ISP iprimus here in Australia
Status: Enabled

I also have a similar entry for port 80 instead of 8085 and that works fine.

Now as previously mentioned to get around my ISP providing me with just a dynamic external IP I have been using both noip.com and also duckdns.org . I notice JimboZA uses some redirection server gotdns which must be similar. e.g. http://jimboza.gotdns.com:8085

Although duckdns periodically changed my external IP address all I need to do is enter username.duckdns.org:8085/ and if finds its way to to my Arduino Server

I've noticed duckdns redirection seems to over-ride and external IP address specified bu NOIP.Com

QUESTION:
This has been a learning curve for myself & maybe others following this post with respect to Service Port numbers but does anyone know the full range of ports numbers available and is their any restriction on what can be used by Arduinos ???

Yep my ".gotdns.com" is one of the domain name styles from dyn (p.k.a dyndns).

It was one of the providers in my router's drop down list. The router keeps dyn updated on my ip addy and they keep my domain name pointed to that.

@JimboZA: Your server code failed. I locked it up. If you haven't gotten my PM, reset it now.

This fix for that is here:
http://playground.arduino.cc/Code/WebServerST
Look for the checkSockStatus() function in the new server code. It is about 3/4 down the page at the end of the first server example.

edit: I see you reset it. I can reach it again.

OK ST.... I see you call it from myStuff() which is called from loop(), so I'll just stick the call to that in loop()?

@rbright we kind of hijacked your thread, but probably a good thing for you to add too...

Thanks for letting me test your server. :slight_smile:

You can put the call in loop(). I put it in the myStuff() function because that is where I call ShowSockStatus() when checking my sockets during the debug. The ShowSockStatus() function is also very handy.

@rbright: If you are going to expose your Arduino server code to the internet, you should also include that checkSockStatus() function.

Here is how to add that function. Then call it every iteration of your loop() function.

// add these includes
#include <utility/w5100.h>
#include <utility/socket.h>

// add these arrays to your global variables
unsigned long connectTime[MAX_SOCK_NUM];
byte socketStat[MAX_SOCK_NUM];

// and this function to the end of your sketch
void checkSockStatus()
{
  unsigned long thisTime = millis();

  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    uint8_t s = W5100.readSnSR(i);

    if(s == 0x17) {
      if(socketStat[i] == 0x14) {
        connectTime[i] = thisTime;
      }
      else if(socketStat[i] == 0x17) {
        if(thisTime - connectTime[i] > 30000UL) {
          Serial.print(F("\r\nSocket frozen: "));
          Serial.println(i);
          close(i);
        }
      }
    }
    else connectTime[i] = thisTime;

    socketStat[i] = W5100.readSnSR(i);
  }
}

Ok, socket fix thingy compiled and uploaded.... test away ST.

@JimboZA: I gave it my best shot. I can't lock it now. It keeps responding.

All of the above is excellent information for which I thank the contributors.

As I asked above "with respect to Service Port numbers but does anyone know the full range of ports numbers available and is there any restriction on what can be used by Arduino"

To assist others searching on Port Forwarding problems, how do we change the heading of the topic to something like "SUCCESS - Port Forwarding"

Alternatively maybe Moderator can make something like this a "sticky topic".

Regards all

Port forwarding and the number of available ports depends on the type of router and how the maker set up port forwarding. Below is a screen capture of my Netgear 614 wireless router.

rbright:
To assist others searching on Port Forwarding problems, how do we change the heading of the topic to something like "SUCCESS - Port Forwarding"

You can modify the heading yourself: just make sure you do it in the opening post not one of the replies.

Go to the post and choose More > Modify in the bottom right corner.

I wouldn't advocate this particular thread as a sticky- it's a bit haphazard, as threads are wont to be. Maybe "someone" 8) could write a "port-forwarding 101" though...

Zoomcat what I see is the specific ports you have set up on your Modem/Router but what is the theoretical highest port number e.g. could someone add a port number 9999 or higher.

To add to this question could someone add on their modem/router a Start Port of 0 and End Port of 9999. I'd expect this may be a security risk but so what if you don't have hardware connected to specified ports. I'd expect there may be some PC system devices which may be in that range.

Comments?

The below user's manual indicates the port number range for my router is from 1 to 65534.

http://documentation.netgear.com/wgr614v9/enu/202-10308-01/WGR614v9-07-08.html

@zoomkat: I don't know if you noticed, but that is your server code I was crashing. Insure you include my fix to your server code or you may become a victim of that hack.

CatweasleNZ was the user who brought this bug to my attention. Thanks again to him.

I'll look at the server fix. Port 84 below is the only one with an arduino currently connected. Not sure just what was running on the arduino, but it probably was server code that off loaded files from the SD card.

[LAN access from remote] from 125.64.35.67:40044 to 192.168.1.102:84 Sunday, Nov 23,2014 05:45:55

I don't see a server at that IP and port. Did you want me to test your server?

I looked up the IP in the log and it appears to be some Chinese scanning.

General IP Information

IP:
125.64.35.67

Decimal:
2101355331

Hostname:
125.64.35.67

ISP:
China Telecom Sichuan

Organization:
China Telecom SiChuan Telecom Internet Data Center

Services:
None detected

Type:
Broadband

Assignment:
Static IP

Upgraded my hardware so my server now shows temp and humidity using a DHT11 and the time from a DS1307RTC

JimboZA thanks for your previous posts and published code which I'm using as a template for doing something similar. Can you please post your current code ?

For newbies following this post I'd strongly recommend looking at following tutorial post as previously mentioned. Arduino Ethernet Shield Web Server Tutorial

Now that I have a basic web server running successfully from remote sites using port forwarding & as I've also been able to get twitter posting working, I'm hoping to now merge the webserver & twitter at some time into a UNO hopefully within the memory restraints else I'll need to buy a MEGA to run the lot all from same hardware along with some I/O monitoring from a standalone Arduino plugged into a spare modem/router port.

End aim it to be able to receive tweets when a front gate opens / closes with timestamping from a RTC and also be able to look at my site from a remote browser for a general overview of what is happening at home.

Regards all & keep posting

rbright:
Can you please post your current code ?

Here it is. Very untidy, I'm going to clean it up a bit sometime...

// based on zoomkat 4-1-12

//jim 7 dec 2013 with my details- ip address etc
// surfer tim socket fix thingy added 22 nov 2014
// added temp and rh with dht11 30 nov 2014
// added ds1307 30 nov 2014

#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h> // st fix
#include <utility/socket.h> //st fix
#include <dht11.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>


dht11 DHT;
#define DHT11_PIN 5 //ethernet shield uses 4



unsigned long connectTime[MAX_SOCK_NUM]; //st fix

byte socketStat[MAX_SOCK_NUM]; //st fix


byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  10, 0, 0, 200}; // ip in lan
byte gateway[] = { 
  10, 0, 0, 2 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8085); //server port

String readString; 

const int chipSelect = 4;
//////////////////////

void setup(){
  Serial.begin(9600);

  // to be safe, disabling SD explicitly
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);


  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();


  Serial.println("Server page test"); // so I can keep track of what is loaded
  Serial.print("Server is at lan ip ");
  Serial.println(Ethernet.localIP());
  Serial.println(millis());
  Serial.print("Setup finished");
}

void loop(){
  //read dht
  //int chk;
  //chk = DHT.read(DHT11_PIN);
  DHT.read(DHT11_PIN);
  
    tmElements_t tm;
    
    
  // 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("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 600");  // refresh the page automatically every 10 mins
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          //client.println("<TITLE>ArduinoMation test page</TITLE>");
          //display temp in browser tab
          client.print("<title>");
          client.print("JimboZA");
          client.println("</title>");
          //client.println("<link href=\"jimza.ico\" rel=\"icon\" type=\"image/x-icon\" />");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1><font  color=\"red\">JimboZA's Arduino web server</font></H1>");

          client.println("<H3>Thanks to Arduino forum guys for help</H1>");
          client.println("
"); 
          client.print("Especially zk for the underlying code and ST for the socket problem fix");

          client.println("
"); 
          client.println("
"); 
          client.print("Refreshes every 10 mins, or hit browser's refresh");

          client.println("
"); 
          client.println("
"); 

          client.print(millis());
          client.print(" milliseconds since Arduino power up or reset");
          client.println("
"); 
          
          client.print("Temperature ");
          //client.print(analogRead(5)/2);
          client.print(DHT.temperature,1);
          client.print("C");
          client.println("
"); 
          
          client.print("Relative Humidity  ");
          //client.print(analogRead(5)/2);
          client.print(DHT.humidity,1);
          client.print("%");
          client.println("
");
          
           if (RTC.read(tm)) {
           client.print("Time (SAST) and Date (DMY) ");
           client.print(tm.Hour);
           //client.println("
");
           client.write(':');
    client.print(tm.Minute);
    //client.print(':');
    //client.print(tm.Second);
    //Serial.print(", Date (D/M/Y) = ");
    client.write('   ');
    client.print(tm.Day);
    client.print('/');
    client.print(tm.Month);
    client.print('/');
    client.print(tmYearToCalendar(tm.Year));
        //client.write(' DMY');
           }
          

          client.println("<H4>Hardware:</H4>");
          //client.println("
");  
          //client.print("<a href=\"http://arduino.cc/en/Main/ArduinoBoardUnoArduino\">Arduino UNO</a>");
          client.println("Arduino UNO");
          client.println("
");
          client.println("Ethernet shield (includes SD card slot)"); 
          client.println("
");
          client.println("DHT11 temerature and humidity sensor (DFR0067)"); 
          client.println("
");
          client.println("DS1307 RTC (Sparkfun BOB-00099)");



          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();


        }
      }
    }
  }
  
  checkSockStatus();
} 

void checkSockStatus()
{
  unsigned long thisTime = millis();

  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    uint8_t s = W5100.readSnSR(i);

    if(s == 0x17) {
      if(socketStat[i] == 0x14) {
        connectTime[i] = thisTime;
      }
      else if(socketStat[i] == 0x17) {
        if(thisTime - connectTime[i] > 30000UL) {
          Serial.print(F("\r\nSocket frozen: "));
          Serial.println(i);
          close(i);
        }
      }
    }
    else connectTime[i] = thisTime;

    socketStat[i] = W5100.readSnSR(i);
  }
}