Ethernet shield - multiple connections

I'm using a Leonardo with Ethernet shield R3. I am sending data to COSM (ie Pachube) using ERXPachube.h library and I want to also send a Twitter message using twitter.h library. Both work fine if they are in different sketches, but when I try to do both from the same sketch, I get a 401 error from COSM. I don't know much about Ethernet protocol, but from searching around I found that the Ethernet shield can support 4 connections. I assumes this means I can connect to 4 different servers. To make sure there were no naming conflicts, in twitter.cpp I changed "client." to "tweetclient.", but that didn't help. I'd appreciate any suggestions on how to make this work?

If you get a 401 response from the COSM server, then something in your code is not correct with the individual request to each server, not the sockets. I use two sockets simultaneously in my ftp sketch.

Maybe posting your code would help.

I don't know much about Ethernet protocol, but from searching around I found that the Ethernet shield can support 4 connections. I assumes this means I can connect to 4 different servers.

Or, if the Arduino is acting as a server, that up to 4 clients at a time can connect to it.

But, SurferTim is right. We really need to see your code. The full text of the 401 error message might contain a clue, too.

I made am bare bones sketch using both ERxPachube.h and twitter.h and it worked. Now I think the real problem is I’m not using pointers correctly when passing them to a function. I’m just learning this and I think there might be some malloc() functions needed. For now, I’m going to must make the variables global and not worry about passing them to a function as a parameter. At least that way I can rule or verify this pointer issue.

Careful how you declare character pointers.

// Not good. 
// This allocates one pointer to a text string, but the string memory is not allocated.
char *testPTR = "test";
// This allocates memory for 4 pointers, but allocates no memory for the arrays.
char *testPTR[4];

// Good.
// This allocates memory for an array of 16 characters and puts "testing" in it
char testPTR[16] = "testing";
// This allocates memory for 4 character arrays of 16 characters each and puts testX in them
char testPTR[4][16] = {"test1","test2","test3","test4"};
// This allocates one pointer to a text string, but the string memory is not allocated.
char *testPTR = "test";

Yes, memory is, and testPTR points to it.

What is bad is:

char *testPTR;

testPTR = "test":

In this case, no memory is allocated.

It wasn't the pointers. I think I was running out of memory, I changed a bunch of Serial.print() to Serial.print(F()) and it's working now.

Regarding the pointers, this is what I am doing:

void PrintPoolData(float *poolData);  // function prototype

void loop(){
  static float poolData[10];  // define array of floats

   poolData[0] = 100;
   poolData[1] = 101;  
  // and so on

   PrintPoolData(poolData); // prints poolData array, poolData is sent to function as pointer
}

void PrintPoolData(float *poolinfo) {
  Serial.print(F("Pool Temp = ");  
  Serial.println(poolinfo[0]); 
  // and so on
}
char *testPTR = "test";
char testArray[16] = "test";

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

  Serial.println(sizeof(testPTR));  
  Serial.println(sizeof(testArray));
}

void loop() {
}

This generates this output:

2
16

That is the confusing part.

That is the confusing part.

Why? One prints the size of a pointer, which is always two bytes. The other prints the size of an array, which can vary.

Thanks, PaulS.

So after it gets the two bytes for the pointer, it allocates 4 bytes? Or 5 bytes with the zero terminator?

So after it gets the two bytes for the pointer, it allocates 4 bytes? Or 5 bytes with the zero terminator?

The compiler allocates 5 bytes for the text and NULL terminator, and allocates 2 bytes for the pointer, which points to the text memory location.

The sizeof() function reports the size of the object in the parentheses. For properly NULL terminated arrays of chars, sizeof() should not be used to determine the number of characters. strlen() should.

@PaulS: Thanks for that. I did not expect the allocation to be at memory position zero. I assumed (you know what that does!) since it was the second declaration and the second to be referred to in the code, it would be the second string in memory, not the first. The pointer was set to 00:00, which led me to believe it had not been initialized.

That caused the confusion.

OK, now I'm confused.

I assumed (you know what that does!) since it was the second declaration and the second to be referred to in the code, it would be the second string in memory, not the first.

The code that I think we are talking about is this:

char *testPTR = "test";
char testArray[16] = "test";

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

  Serial.println(sizeof(testPTR));  
  Serial.println(sizeof(testArray));
}

void loop() {
}

Shoes on or off, I can not make testPTR come second.

My bad! I did not save my test code from a couple days ago, but it was easy to rewrite. I don't know what I did different, but I see the pointer is now 0:2. I don't know where in memory that is. I was actually expecting 0:4. The 'test' variable should have taken the first 4 (test[0] to test[3]), and test2 should have been after the testPTR string in memory, but it isn't. It follows the pointer.

char test[4] = {'a','b','c','d'};
char *testPTR = "pointer test";
char test2[3] = {'1','2','3'};
char testend = 0;

void setup()
{
  Serial.begin(9600);
  
  Serial.println(test);
  Serial.println(testPTR);
  Serial.println(test2);

  Serial.print(test[4],HEX);
  Serial.print(':');
  Serial.println(test[5],HEX);  
  // this was just to insure the memory spacing was as I expected
  // it should print test2
  Serial.println(&test[6]);
}

void loop() {
}

edit: I did the last print to see if it would print "123" or "pointer test". It printed "123".

  Serial.println(test);
  Serial.println(test2);

Tsk. Tsk. test and test2 are not NULL terminated arrays of chars. They should NOT be treated as though they were.

I don't know what I did different, but I see the pointer is now 0:2.

I don't see where you are getting these values. The pointer should occupy only two bytes. This statement seems to imply that it is using 3. Where the pointer is and where the data pointed to is are two different things, too.

Ahh! That was the point! I was not testing originally for the pointer. It was a test to check how buffers overflowed into each other. I think you participated in that thread. The "terminating zero" in that "string" is testend.

edit: The testPTR between the test and test2 was an afterthought to see if it had allocated memory.
The way I see it, if memory was allocated, Serial.println(&test[6]) should have printed "test", not "123". That would have been Serial.println(&test[11]).