Can't publish data to a server with an IP address on port 3000

In short:

Does the function shown at the bottom of this post work if its server parameter is an IP address and port parameter is 3000?
(The same function does work if the server parameter is a url (like x.com) address and port parameter is 80)

In detail:

I am trying to post a sensor value from the Spark Core to messaging engines on two servers, one commercial server with a URL listening to port 80 (lets call it ServerURL80), and the other one a VPS (Virtual Platform Server) with fixed IP listening on port 3000 (lets call this ServerIP3000).

The code I have uses the getrequest function below. It works fine and I can see data on the messaging engine on ServerUrl80 when I call getrequest fwith server set to url of the ServerUrl80, and the url to the publishing data, and port set to 80.

The same function doesn’t work for ServerIP3000 when I call  getrequest with server set to the ip address of the ServerIP3000, and the url to the publishing data (same as above), and port set to 3000.
To make sure that nothing is wrong on the server side of the ServerIP3000, I used the HttRequester plugin for Firfox and can successfully publish data to it.

The function running on Spark Core:

int getrequest(const char * server, const String url, int port){
    client.connect(server, port);
    if (client.connected()) {
        Serial.println(“Connected”);
        client.print(“GET “);
        client.print(url);
        client.println(” HTTP/1.0”);
        client.print("Host: ");
        client.println(server);
        client.println(“Connection: close”);
        client.println();
        return 1;
    }
    else {
        client.flush();
        client.stop();
        return 0;
    }
}

Maybe try adding some debugging lines and seeing if the server is trying to return anything

Here is some code i used when i couldn’t get a response from a server… i could see the error code the server returned and troubleshooted from there

if(DEBUG){Serial.print("connecting... ");}
    if (client.connect(server, port)) {
        
        if(DEBUG){
            Serial.print("connected to ");
            Serial.println(server);
        }
        delay(500);
        strcpy(line, "GET ");
        strcat(line, url);
        strcat(line, " HTTP/1.0\r\n");
        client.print(line);
        if(DEBUG){Serial.print(line);}
        strcpy(line, "Host: ");
        strcat(line, server);
        strcat(line, "\r\n");
        client.print(line);
        if(DEBUG){Serial.print(line);}
        strcpy(line, "Connection: close\r\n");
        strcat(line, "Accept: text/html, text/plain\r\n");
        strcat(line, "Content-Length: 0\r\n");
        strcat(line, "User-Agent: Spark\r\n\r\n");
        client.print(line);
        if(DEBUG){Serial.print(line);}
        delay(500);
        if(DEBUG){
            while (!client.available());
            while (client.available()) {
            char c = client.read();
            Serial.print(c);
            }
        Serial.println();
        }
        
        client.flush();
        delay(200);
        if(DEBUG){Serial.println("closing...");}
        client.stop();

        
    }
1 Like

Thanks Hootie​81.

That razed a new dumb question: How can I see the Serial.print messages? I don’t know how to run something like Serial Monitor (for Arduino) to see it’s messages over usb on my PC.
Could it be that you know where on the Spark Doc I can find info about that (the Spark Doc doesn’t seam to have Search function)?

You can open the Arduino Serial monitor or download a Serial Terminal like Putty for windows.

Check the COM port under device manager and see the baud rate you used in your firmware.

:slight_smile:

To get the serial port started you will need this in you setup loop

Serial.begin(115200);
    while (!Serial.available());
    while (Serial.available()) {Serial.read();}//throw it away

then you can use the arduino terminal to connect, once the core connects to the cloud and the user firmware starts running it will show up. check the baud rate is correct too. Once you have connected, if you close the window and try and open it again it will give a port already open error. and with the arduino terminal you cant just push enter to start the connection you have to write some text and push enter (the while (!Serial.available() bit waits for you to open the terminal and type something.

so if you want an easy way to turn it on and off put it in a if statement and use a pin to toggle it on or off at startup time… if you have the jumper betwwen D0 and gnd it will give the output, otherwise it wont and will run normally. I wouldnt turn it on and off once its running unless you call a function with the pin to start the serial otherwise it might have a fit if you call serial.print without the port started, get what im saying?

int DEBUGpin = D0; // Debug switch connects to D0 on the core
boolean DEBUG = false; // enable or disable Debug

in setup loop

pinMode(DEBUGpin , INPUT_PULLUP);
delay(500);
if(digitalRead(DEBUGpin) == LOW) DEBUG = true;

if(DEBUG) {
    Serial.begin(115200);
    while (!Serial.available());
    while (Serial.available()) {Serial.read();}//throw it away
}

I have found Serial.flush() doesnt work for me so i use the line marked throw it away above which does what the serial flush is meant to do…

One other thing is the way the ‘server’ is stored, if i use an address like something.com i will use a char array where if its an ip the i use a byte array ie

byte server[] = { 192,168,0,104 };

clear as mud?

2 Likes

Thanks to the info provided here from several members I can now see the serial output on PuTTY.

I get “HTTP/1.1 400 Bad Request” when I publish the sensor data (see getrequest function in the OP above).

I guess there is big difference between sending data using an IP address, or an url.