Connecting to Nano WebServer from Internet

Using Nano with ESP-01s and sketch that creates a web server to control the On/Off condition of two LEDs, so it is very similar to the issue presented in this thread:
Problems with port forward/accessing an ethernet server from outside the LAN network - Using Arduino / Networking, Protocols, and Devices - Arduino Forum
Here is my sketch in case it helps:

/*
 WiFiEsp example: WebServerLed  Uses ESP-01s -- cbfb68
 
 A simple web server that lets you turn on and of an LED via a web page.
 This sketch will print the IP address of your ESP8266 module (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn toggle the LED on pin 12 (Red) or pin 13 (Green) between ON and OFF.
 The Red LED will start off ON, Green will be OFF.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html

 added by SPL
 changed to handle two LEDS
 modified IP to be static by changing to 192.168.75.201 vs the IP provided by DHCP
*/

#include "WiFiEsp.h"

// Emulate Serial1 on pins 2/3 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3); // RX, TX
#endif

char ssid[] = "local_sid";            // your network SSID (name)
char pass[] = "password";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus[] = {LOW, LOW};
char *ledColor[] = {"Red", "Green"};
char *ledOnOff[] = {"Off","On"};
int ledPin = 12;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
{
  Serial.begin(9600);   // initialize serial for debugging
  Serial.println("WebServerTwoLeds");
  pinMode(12, OUTPUT);  // initialize digital pin 
  pinMode(13, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
    
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // next three lines added to change IP to 192.168.75.201 (make it static) and display the change  
  Serial.println("Make the IP static - 192.168.75.201");
  Serial1.println("AT+CIPSTA=\"192.168.75.201\",\"192.168.75.1\",\"255.255.255.0\"");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();

  // turn red on to start  --  added SPL
  toggleLED(12); 

}


void loop()
{

  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        // Serial.write(c);
        
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
        //  Serial.println("got two newline characters");
          sendHttpResponse(client);
          break;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) { 
          toggleLED(12); 
          }
        else if (buf.endsWith("GET /L")) {
          toggleLED(13); 
          }
        else if (buf.endsWith("GET /A")) {
          toggleLED(12); 
          toggleLED(13); 
          }
      }
    }
    
    // close the connection
    client.stop();
  }
}

void sendHttpResponse(WiFiEspClient client)
{
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println("<font size=\"10\">");
  client.println();
  
  // the content of the HTTP response follows the header:
  client.println("<br>");
  client.print("<font size=\"10\">");
  client.print("Control two LEDs");
  client.println("<br>");
  client.println("<br>");
  client.println("<br>");
  
  client.print("Click <a href=\"/H\">here</a> to turn the Red LED ");
  client.print(ledOnOff[!ledStatus[0]]);
  client.println("<br>");
  client.println("<br>");
  client.println("<br>");
  client.print("Click <a href=\"/L\">here</a> to turn the Green LED  ");
  client.print(ledOnOff[!ledStatus[1]]);
  client.println("<br>");
  client.println("<br>");
  client.println("<br>");
  client.print("Click <a href=\"/A\">here</a> to toggle both LEDs  ");
  client.println("<br>");
  client.println("<br>");
  client.println("<br>");
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();

}
void toggleLED(int whichLED)
{
  if (ledStatus[whichLED-12] == LOW) {
    ledStatus[whichLED-12] = HIGH;
    digitalWrite(whichLED, HIGH);
  }
  else {
    ledStatus[whichLED-12] = LOW;
    digitalWrite(whichLED, LOW);
  }
}

=========================
The sketch and hardware setup works fine when the accessing computer is on the same lan. I have tried using port 80 as the OP in the cited thread states fixed his issue. I've also tried port 20619 to no avail also.
I have setup port forwarding on the cable modem/router as shown here:


Using my phone to provide a hotspot so the browser device is external to the local lan, I get "503 Service Temporarily Unavailable" when I try to go to the external IP address of my modem 216.x.x.x with and without ":80" appended to the IP address.
Is there something missing in my sketch to stipulate which port it should be listening to?
Any and all help will be greatly appreciated.
I realize that this is kind of a silly thing to do in the great scheme of things, but I am doing it as a learning lesson.
Thanks in advance.

If you can access webpage from LAN/WLAN, there is no problem with your
Arduino code. The problem is in port-forwarding setting on your router, or something like firewall can block traffic from outside.

1 Like

Well, that's good to know, and the lesson you will learn is that there are more sensible and far easier ways of doing this.

I will look into the port forwarding and firewalls further, thanks for your response.

I guess if my only desire was to throw money at it and buy something pre-made, your suggestion would make sense and the only thing I'd learn is how to spend money.
I'm not into this to buy fancy stuff.

I'm not quite sure what you are thinking about here, using an IOT facility involves no more investment than the gear you seem to already have, but I guess the ability to click and read is a prerequisite.....

I'm curious. Is it your self-elected job to belittle and put down anyone that is not of your "caliber"?
If you can't be truly constructive in your responses to me, please refrain from posting at all, I have no use for your sort of help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.