Photon sends to webserver in local network, but not to a remote webserver

Hello,

I am currently trying to send information from my photon to a remote webserver where it will be stored. The idea is to have my photon in a room and send information to my home web server (the photon is in another house)

Well, when I test it to send information to my home serve with my photon connected to my home network, it works and it sends everything correctly, but, when I change my photon network to my cellphone hotspot and try to send the same information to my home web server (using its no-ip address) it does not work.

I thought it could be my router who wasn’t accepting the information and was dropping because of a bad firewall configuration, but I tried with a computer and it worked, so it is not that. Does anyone have any idea what it might be? I will provide the code so you can tell me what it may be happening, the photon says everytime “Connected” so it seems like it was connected to the home server.

TCPClient client;
IPAddress ip;
unsigned int port = 5000;

String password = "mypass";
String username = "myuser";
String bufsend;
#ifdef home_server
  byte server[] = {192, 168, 1, 36};
#else
  String hostname = "http://something.hopto.org";
#endif

byte remoteserver[] = {176,250,54,43};
// setup() runs once, when the device is first turned on.

void SendInfo(String str)
{
  //sprintf(bufsend, "{\"user\" : \"%s\", \"psswd\" : \"%s\", \"request\" : \" %s \"}", username, password, str);
  bufsend = "{\"user\" : \"" + username + "\", \"psswd\" : \""+password+"\", \"request\" : "+str+"}";
  if(client.connect(hostname, port)) //works with homeserver, but not with remoteserver ip
  {
    Serial.println("Connected"); //It goes here everytime
    client.print(bufsend);
    client.flush();
    client.stop();
  }
  else
  {
    Serial.println("Not Connected");
  }
}

This code snippet is a bit confusing.
You have git remoteserver declared, but I can’t see where it’s used :confused:

Just for style.
If you have conditional compiling you can just stick with one variable name (one of server | hostname | remoteserver) to prevent confusion and make switching between scenarios easier.

And if you use IP addresses, it’s better to use an IPAddress object rather than byte[] for typesafety.

This is just a test.

Where I have client.connect(hostname, port) I change the hostname to remoteserver or server and compile it again, just to test if it would be working with the ip address. I works with server variable (which is the home server ip) but nor with remoteserver or localhost.

I don’t know what the problem may be

Just for the heck of it, you should add this line to see if your formatting is correct.
Serial.printf(Connection String: %s\r\n", hostname);

Hi,

I can’t do has you said, because Serial.printf does not work with strings, but, using Serial.println(hostname) it gave me the expected output

This is not a hostname. Perhaps you meant just something.hopto.org instead?

3 Likes

Yes, I also thought it should be something.hopto.org, the problem is that if I do this instead of http://something.hopto.org it says “Not Connected”, and I do not know why, since I tried with a python tcp/ip client and it connected and send to the remote server

@asergio.branco, you may want to try WiFi.resolve(hostname) and printing the resulting IP to see if the Photon can properly get a DNS ip for that host. You may also want to print the dns and gateway IPs to make sure these are correct as well, assuming you are using DHCP on the Photon.

I will try it. How can I know if I am using dhcp on photon?

But even when I give my IP address it does not connect correctly

@asergio.branco, setting the IP with WiFi.useStaticIP() on the Photon is “sticky” and you need to reset it to dynamic IP with WiFi.useDynamicIP().

If you use static IP, you need to make sure to specify the IP, netmask, gateway and dns IPs as well. I suggest you reset your Photon by explicitly specifying WiFi.useDynamicIP() and printing out the various IPs allocated by the DHCP server (IP, netmask, gateway and dns IPs). These should match the IPs that Python has as well.

Yes, but I do not want my photon to have a static IP, I want it to connect to a server in my home while I am at school. I can connect using my computer and a python TCP client, I simply cannot connect to my home server using photon, unless I have my photon in my home, connected to my home network

@asergio.branco, did you read my comment? I suggest MAKING SURE your Photon is using dynamic IP by including WiFi.useDynamicIP() in your code. Next, you need to make sure the Photon is getting a valid set of IPs from the DHCP server in your home (your router most likely). Verify that the allocated gateway and DNS IPs are correct.

Now, take the allocated IP and set your router to reserve that IP for the Photon, making it pre-allocated (static IP using DHCP). You then need to set your router to open CoAP port (UDP 5683) to the Photon’s IP (Photon acts as server). Without this, your router will block inboud CoAP traffic and your Photon won’t be reachable.

Yes, I read your comment, but like I said, my problem is not make my photon act like a server, I want it to be a client, my server is a computer with linux.

I simply cannot connect my photon to the server if not in the same network, but I will try some of your ideas and see if it will work, specially to check if it is connecting to the correct IP

Thanks

@asergio.branco, I understand a bit more. For the Photon to connect to your server while the Photon is remote, you server port(s) need to setup on your router so they become available remotely. The network on which the Photon is connected (remotely to your home network) needs to also have those ports open and the Photon needs to have the correct gateway and DNS passed by the DHCP server it connects to.

Your PC running Python is a lot more sophisticated than the Photon so you need to pay attention to more details. :wink:

Thanks,

I do not believe the error is from my home router, because I checked if the ports are open. Yes, my computer running in my school can connect to my home server, I will try with my photon again and changing some of the things you said.

If you can provide more information or ideas it will also be great. Thanks

@asergio.branco, I am assuming your Photon correctly connects to the school network (breathing green or breathing cyan). Are you running in SEMI_AUTOMATIC mode (wifi connect only)? Your code should wait for the Photon to connect locally then print out the IP config details to Serial so you can verify the gateway and DNS IPs. Without valid those, the Photon will not be able to resolve your hostname or get out to the internet via the gateway.

You can also use WiFi.ping() to make sure you have internet connectivity by pinging a known site like google for example.

Once you have all this verified, they you can test whether the Photon can get to your server. You could do this with basic TCPClient code (not the full httpClient library) to see if you can open your server port from the Photon. All this to say that you need to do small incremental tests to find the problem.

Yea, it is connected to the network, o connected using the “particle setup” command and i can upload code using the cloud.

But I will try to use those functions you talk about, and check if I can reach Google or another known website.

The code for my TCP client I used in photon is displayed in my question.

Thanks

So, I tried and I can do ping and Wifi.resolve(hostname) returns not null, also ping returns 5, so I believe it is working and connecting. I can’t figure out what is wrong then…

@asergio.branco, I can’t see setup() or loop() in your code. However, in SendInfo() you call client.flush() assuming it waits will all data is sent. Unfortunately, it doesn’t. The docs say:

NOTE: That this function does nothing at present.

With no waiting, calling client.stop() will kill the connection! You may want to put non-blocking delay which calls Particle.process() while waiting in order to give the system firmware time to process the send buffer.

Right Now this is my code:

TCPClient client;
IPAddress ip;
unsigned int port = 5000;

String password = "mypass";
String username = "myuser";
String bufsend;
#ifdef home_server
  byte server[] = {192, 168, 1, 36};
#else
  String hostname = "http://something.hopto.org";
#endif

void SendInfo(String str)
{
  bufsend = "{\"user\" : \"" + username + "\", \"psswd\" : \""+password+"\", \"request\" : "+str+"}";
  if(client.connect(hostname, port)) //works with homeserver, but not with remoteserver ip
  {
    Serial.println("Connected"); //It goes here everytime
    client.print(bufsend);
    delay(5000);
    client.stop();
  }
  else
  {
    Serial.println("Not Connected");
  }
}

void setup()
{
  Serial.begin(9600);
  ip = WiFi.resolve(hostname);
}

void loop()
{
  SendInfo("HelloWorld");
  delay(5000);
}