connecting to IPaddress:Port

I am experementing with Sparkfun's Phant server again. This time i want to host the data on my own server so i posted it to Digital Ocean fora few dollars. However, i cannot get the arduino to connect to it using the SFE_CC3000 library/shield. I get a TCP error (error 4). This happens with both the Phant code as well as the webserver example/test sketch. I can connect to Phant fine when i use their server.

The address for the server looks like this: http://###.236.2#1.#98:8080. The server entry line in the header is then called up in the loop() with this entry: "if ( !client.connect(server, 80) )". i have tried changing the "80" to "8080" to and get a connection, but still no luck. The example libraries use the same structure to call up the server line from the header.

I can connect to the site using chrome just fine. However, if i take the port off (:8080) it will not make the connection. I think that is what is throwing off the arduino, and that if i could have the IP without the port, id be fine. But... no such luck yet.

Ive tried changing the IP to a Hex in the arduino code, but again it gets thrown off and and spits out a TCP error.

Maybe i should use a proxy that forwards to where i want to go? IE, something easy for the arduino to connect to that will then redirect. That will make implementation grossly inefficient, but may get me by for experimenting. I just thought it now.

Post your code, not just a snippet.

Do you use that IP when you use Chrome or a domain name?

Your IP address in your code should not include the port number, but the client.connect port should be 8080. It's not SSL is it?

SurferTim: Unfortunately I dont have my code at the moment, but its basically the same as this: Pushing Data to Data.SparkFun.com - learn.sparkfun.com

The address above is what i use to connect to the server. I must include the port or it will not connect. Maybe i should assign a static IP and domain name?

Barry,

I dont think that it is SSL. I have tried leaving the port off of the IP but it still fails to connect. This is the git for the server side: GitHub - sparkfun/phant: the data logging engine behind data.sparkfun.com . I think the arduino would have trouble in general if it was SSL, wouldnt it?

If you are going to use an IP for the server rather than a domain name, use this.

IPAddress server(###,236,2#1,#98);
// then in your loop
  if (!client.connect(server, 8080))

still getting error 4…

#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
//#include <Progmem.h>

#define CC3000_INT      2   // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN       7  // Can be any digital pin
#define CC3000_CS       10  // Preferred is pin 10 on Uno
#define IP_ADDR_LEN     4   // Length of IP address in bytes

// WiFi Constants //
////////////////////
char ap_ssid[] = "HOME-61B2";                  // SSID of network
char ap_password[] = "mypassword";          // Password of network
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
// ap_security can be any of: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
//  WLAN_SEC_WPA, or WLAN_SEC_WPA2
unsigned int timeout = 45000;             // Milliseconds
IPAddress server = (###,236,2#1,#98);      // Remote host site 

// Initialize the CC3000 objects (shield and client):
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);

/////////////////
// Phant Stuff //
/////////////////
const String publicKey = "mypublickey";
const String privateKey = "myprivatekey";
const byte NUM_FIELDS = 4;
const String fieldNames[NUM_FIELDS] = {"inches", "name", "photo","switch"};
String fieldData[NUM_FIELDS];

//sensor info
const int photoPin=0;
int light;
const int sonicPin=4;
long sensor,distance;
const int switchPin = 10;
//string name line itmes
String name = "0000";
boolean newName1 = true;

//post timer info
int countdown = 0; //when countdown reaches 0 repost
unsigned long
sketchTime = 0L; //CPU miliseconds since last post
long previousMillis = 0;  
long interval = 10000;  

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

 // Setup Input Pins:
 pinMode(sonicPin,INPUT);

 // Set Up WiFi:


 Serial.println(F("=========== Ready to Stream ==========="));
 Serial.println(F("Press the button (D3) to send an update"));
 Serial.println(F("Type your name, followed by '!' to update name"));
}
void loop()
{
// Read sensor
sensor=pulseIn(sonicPin,HIGH);
distance=sensor/147;
light=analogRead(photoPin);
time();
}

void time()
{
 unsigned long currentMillis = millis();
 
 if(currentMillis - previousMillis > interval) {
 // save the last time you blinked the LED
 previousMillis = currentMillis;
 setupWiFi();
 Serial.print(F("Inches: "));
 Serial.println(distance);
 Serial.print(F("Lumens"));
 Serial.println(light);
 dataPost();
 wifi.disconnect();

}
}
void dataPost()
{
 // Gather data:

 fieldData[0] = String(distance);
 fieldData[1] = String(digitalRead(switchPin));
 fieldData[2] = name;
 fieldData[3] = String (light);

 // Post data:
 Serial.println("Posting!");
// Make a TCP connection to remote host
if ( !client.connect(server, 8080) ) //removed ", 80"
{
 // Error: 4 - Could not make a TCP connection
 Serial.println(F("Error: 4"));
}

// Post the data! Request should look a little something like:
// GET /input/publicKey?private_key=privateKey&light=1024&switch=0&time=5201 HTTP/1.1\n
// Host: data.sparkfun.com\n
// Connection: close\n
// \n
client.print("GET /input/");
client.print(publicKey);
client.print("?private_key=");
client.print(privateKey);
for (int i=0; i<NUM_FIELDS; i++)
{
 client.print("&");
 client.print(fieldNames[i]);
 client.print("=");
 client.print(fieldData[i]);
}
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();

while (client.connected())
{
 if ( client.available() )
 {
 char c = client.read();
 Serial.print(c);
 }
}
Serial.println();
}

void setupWiFi()
{
 ConnectionInfo connection_info;
 int i;

 // Initialize CC3000 (configure SPI communications)
 if ( wifi.init() )
 {
 Serial.println(F("CC3000 Ready!"));
 }
 else
 {
 // Error: 0 - Something went wrong during CC3000 init!
 Serial.println(F("Error: 0"));
 }

 // Connect using DHCP
 Serial.print(F("Connecting to: "));
 Serial.println(ap_ssid);
 if(!wifi.connect(ap_ssid, ap_security, ap_password, timeout))
 {
 // Error: 1 - Could not connect to AP
 Serial.println(F("Error: 1"));
 }

 // Gather connection details and print IP address
 if ( !wifi.getConnectionInfo(connection_info) )
 {
 // Error: 2 - Could not obtain connection details
 Serial.println(F("Error: 2"));
 }
 else
 {
 Serial.print(F("My IP: "));
 for (i = 0; i < IP_ADDR_LEN; i++)
 {
 Serial.print(connection_info.ip_address[i]);
 if ( i < IP_ADDR_LEN - 1 )
 {
 Serial.print(".");
 }
 }
 Serial.println();
 }
}
int freeRam () {
 extern int __heap_start, *__brkval;
 int v;
 return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
[/code[

Why are you calling setupWiFi() and wifi.disconnect() in loop (actually the time function called in loop)? You really should just call setupWiFi() in setup and leave it connected.

honestly, i forget. i think i was having issues with it. when this is running properly the delay timeout is in minutes, not seconds. the code functions perfectly in my other tests with the standard sparkfun server, data.sparkfun.com.

So you have tried this code with other servers, and it works well, but on your "few dollars" cloud server is where you have the problem?

correct. I can access my server on digital ocean via web browser perfectly, but i cannot access it from the arduino in either the code posted above, or one of the example libraries for the CC_3000. I can access what is ostensibly the same web code being run at data.sparkfun.com without issue.

Also, i think i remembered the issue that lead to the reconnect in the loop: when i left it connected it would eventually lock up and stop responding until the power was pulled (even the reset didnt work sometimes). Making the process include reconnecting the router solved that problem. different issue, and it has been proven to work on both data.sparkfun.com as well as ATT's m2X server.

when i left it connected it would eventually lock up and stop responding until the power was pulled (even the reset didnt work sometimes).

Then you code has a bug. Maybe not enough error checking or fault tolerance. Those are required for internet communication.

I can't help you troubleshoot this unless I have more details about the web browser connection. When you connect to the server using your web browser, does it have the same IP in the address bar as you entered? It is an IP, correct?

If you want me to help and you don't want your IP published on the forum (I don't blame you), send me a PM with your server IP and port and I will try it from here. Otherwise I am at a loss what to tell you.

It is the same IP as the one posted here - and i've tripple checked it. I'll send you a PM. Thanks a lot for the help - i really appreciate it.

I got the PM with the IP, and I connected to that IP and port with both my ethernet shield and wifi shield.

edit: That was the Phant server, not the Digital Ocean server.

So contrary to what i said in the PM, i think i actually found the section of the library that refers to how to connect to an IP... but I;m having trouble implementing it

/* CC3000 Client class */
class SFE_CC3000_Client : public Client {
public:
    SFE_CC3000_Client(SFE_CC3000 &cc3000);
    ~SFE_CC3000_Client();

    int connect(   const char *hostname, //changedx all of the ports from 80 to 8080 January 30-2015 @7:18pm
                    uint16_t port = 8080);
    int connect(   IPAddress ip_address, 
                    uint16_t port = 8080);
    int connectUDP(   const char *hostname, 
                    uint16_t port = 8080);
    int connectUDP(   IPAddress ip_address, 
                    uint16_t port = 8080);

To implement i tried changing "server" to ip_address and changed the body to "connectUDP"

IPAddress ip_address = (numbers)
( !client.connectUDP(ip_address, 8080) )

It still fails to connect.

In the meantime i have reached out via github to see if sparkfun can help me.

edit: i also tried it without having the UDP in the connect line, still failed.

I don't know about Sparkfun's library. Maybe they are having trouble with it. Adafruit has a library for the CC3000 also. Just a thought.

I've tried going down that road before. While it should be easier than it was - it was a real struggle. There were bits of the code that needed to be referenced in the body rather than just changing the header (this was before i embedded the connect/disconnect in the body).

At any rate, the good news is that i was able to get a connection to my server by running a custom config of the Phant set to port 80. There were some more complications that i wont go into as they dont have much to do with the wifi board anymore i dont think . The short story is that i still cant see my data, but at least im not getting TCP errors anymore.

The bad news is that i think i fried my board when i accidentally shocked it when i went to move it. My adafruit board is working fine, though, as far as i can tell.

EDIT
funny how things change, right after i hit send i noticed my serial print gave me a "success" message. Complications are all on the phant side at this point. (aside from a possibly fried board... it wont even initialize for a board test - with either SFE or adafruit libraries)

Complications are all on the phant side at this point.

I had no problems connecting, sending a request, and reading a response from the Phant server IP you sent me. Worked with both the ethernet shield and wifi shield. ??

i had setup a different program/server at that point. I am now up and running totally, though. Promise not to overburden me with data or give the access out and you’re free to use my secure server if you wish… I am now running on port 80 with full HTTP support etc.

What happens if you leave out the equal sign in this statement:

IPAddress ip_address = (numbers);

christop:
What happens if you leave out the equal sign in this statement:

IPAddress ip_address = (numbers);

It will work correctly if the numbers are separated by commas.

edit: I don't know how that works with the CC3000 library. Maybe you have a point there, christop. I didn't notice that is the OP's code.