Go Down

Topic: Dynamic DNS only working locally (Read 1 time) previous topic - next topic

ryanmcclure4

Hi,
I have an Arduino project using an ethernet shield and I wanted to be able to access it remotely, so I set up a dynamic DNS service with www.no-ip.com.  It works fine when I access it through my local network but if I try from an outside network it gives me the following error:

"Response Error.

Technical  Description:
502 Bad Gateway - Response Error, a bad response was received from another proxy server or the destination origin server"

From what I understand, this means that www.no-ip.com has sent the request to the correct IP address but there was no server there?

Here's my Arduino code (not sure if this would be part of the problem though):

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
EthernetServer server(80);
int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30];
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF"};
char trueFalse;
int val;
int cursorPin;

//LCD
LiquidCrystal lcd(14,15,16,17,18,19);
int backLight = 1;

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
 
  //Sets the LEDpin as an output
  for(int i = 0; i < 8; i++){
    pinMode(pin[i],OUTPUT);
    digitalWrite(pin[i],HIGH);
}

   //LCD Setup
   pinMode(backLight, OUTPUT);
   digitalWrite(backLight, HIGH);
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("SprinkDuino");
   lcd.setCursor(0,1);
   lcd.print("Version 1.0");
   delay(5000);
   lcd.clear();
}

void loop()
{
EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        int pos = strlen(readString);
        if(pos < 30){
         readString[pos++] = c;
         readString[pos] = '\0';
        }
       
        if (c == '\n' && currentLineIsBlank)
        {
         
          char *response = strtok(readString, "?");

          response = strtok(NULL, "=");
          if(strcmp (response, "A") == 0){
            val = 1;
          }
          else {
            val = atoi(response);
          }
          response = strtok(NULL, ";");
          if(strcmp (response, "T") == 0){
            trueFalse = 'T';
          }
          else {
            trueFalse = 'F';
          }
       
          client.println("<html>");
          //Refresh page every 5 seconds to keep page live
          client.print("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<h1>SprinkDuino</h1>");
          client.println("<br><br>");
          client.print("<table border=1><tr>");
          client.print("<td><a href=\"./?A=T;\"><button>Turn All On</button></a>");
          client.print("<a href=\"./?A=F;\"><button>Turn All Off</button></a><br><br>");
         
          //Individual pin control
          for(int i = 0; i < 8; i++){
            if ((val == (i + 2)) && (trueFalse == 'T')) {
              digitalWrite(pin[i],LOW);
            }
            else if ((val == (i + 2)) && (trueFalse == 'F')) {
              digitalWrite(pin[i],HIGH);
            }
           
            //Turn all on or off
            else if ((val == 1) && (trueFalse == 'T')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],LOW);
              }
            }
            else if ((val == 1) && (trueFalse == 'F')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],HIGH);
              }
            }
           
            //Changes state[] depending on pin status           
            for(int h = 0; h < 8; h++){
             if(digitalRead(pin[h]) == LOW){
              state[h] = "ON";             
             }
             else{
              state[h] = "OFF";
             }
            }

            client.print("Sprinkler ");
            client.print(pin[i]);
            client.print(" is ");
            client.print(state[i]);
           
            if (state[i] == "ON") {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=F;\"><button>Turn Off</button><a>");
            }
            else {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=T;\"><button>Turn On</button><a>");
            }           
            client.print("<br>");
          }
          client.print("</td><td>");
          client.print("<h1>Schedule</h1>");
          client.print("</td></tr>");
          client.print("</table>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    readString[0] = '\0';
    // close the connection:
    client.stop();
  }
}
void digitalClockDisplay(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Time: ");
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
  lcd.setCursor(0,2);
  lcd.print("1|2|3|4|5|6|7");
}

void printDigits(int digits){
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}


Another quick question...If I change the port to anything but 80 like 8080, it still works but only prints the output text from the Arduino.  I tried changing my firewall settings and even turning it off, but it still didn't interpret the code as html.  This isn't too big of a problem for me since port 80 seems to be doing the trick, but just thought I'd ask.

Thanks!

billroy

The Arduino needs the IP address of a "gateway" to forward packets to the outside internet.  Without a gateway, the symptoms are just what you're seeing: works okay locally, can't hit it from outside.

Normally the gateway IP is the IP address of your router, which for your network it looks like might be 192.168.1.1  Also, a network mask is required.

I don't see a gateway or network mask being initialized in the code you posted.  If you look at the Ethernet Chat Server example that comes with the Arduino ethernet library you'll see an example of how it's done.

-br


SurferTim

Many ISPs block port 80 requests to residential accounts. If it works on other than port 80, that is probably the case.

And that is a localnet ip in that code. You can access that ip only on your localnet. You must port forward the public ip on your router to that localnet ip. In other words, put it in the DMZ. Then register your public ip with no-ip.

@billroy: If the code does not specify those values, they are not left blank. With this ip specified
IPAddress ip(192,168,1,134);
it will use
subnet mask = 255.255.255.0
gateway = 192.168.1.1
dns server = 192.168.1.1

If those are not correct for the OP's network, then that would be one of the symptoms.

ryanmcclure4

#3
Dec 03, 2012, 09:11 pm Last Edit: Dec 03, 2012, 09:15 pm by ryanmcclure4 Reason: 1
I added the subnet and gateway and now I have this:
Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,0,0);
EthernetServer server(80);
int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30];
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF"};
char trueFalse;
int val;
int cursorPin;

//LCD
LiquidCrystal lcd(14,15,16,17,18,19);
int backLight = 1;

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


However, it is still not working...I had also already port forwarded port 80 to 192.168.1.134 on my router, although I didn't enable DMZ.  Should I be using DMZ instead of port forwarding?

EDIT: I deleted my port forwarding settings, and enabled DMZ for the Arduino IP address and it is working the same as before; still no access from outside networks

SurferTim

Either way should work. It is a matter of forwarding that port to the Arduino. Port forwarding usually does one port, and the DMZ forwards all ports.

Like I said earlier, it may be your ISP blocks port 80. Both of my ISPs do. If you think you are doing this according to your contract with your ISP, call tech support.

I can usually get around that block by using a different port, like 8088. They might shut you off if they catch you tho.  :(

ryanmcclure4

Ah alright, I see.  Well something interesting is happening when I port forward to 8088; now it is working fine on my iPhone, in Firefox it is just displaying the html text code, and in Internet Explorer it is displaying the page correctly but none of the buttons work.  Any ideas of why this might be happening?

SurferTim

You are not sending a header with the response. Take a look at this code. It has the correct header to display html.
http://www.arduino.cc/playground/Code/WebServerST

This is the part you are missing.
Code: [Select]
Serial.println("Sending response");
client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");

ryanmcclure4

That was it! It's working in Firefox now, but still not in Internet explorer.  Although this isn't very important to me since I never use IE.

SurferTim

Mine works with IE, Firefox, and Safari. ??

ryanmcclure4

Interesting...well it seems to be working for what I need right now so I can't complain. Thanks a lot for the help!

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview