New Jersey
Offline
Sr. Member
Karma: 0
Posts: 356
|
 |
« on: July 03, 2012, 05:08:41 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #1 on: July 03, 2012, 09:34:19 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #2 on: July 04, 2012, 09:14:16 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Sr. Member
Karma: 0
Posts: 356
|
 |
« Reply #3 on: July 04, 2012, 09:36:35 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #4 on: July 04, 2012, 09:53:23 am » |
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"};
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #5 on: July 04, 2012, 10:36:37 am » |
// 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.
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Sr. Member
Karma: 0
Posts: 356
|
 |
« Reply #6 on: July 04, 2012, 10:51:08 am » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #7 on: July 04, 2012, 10:57:17 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #8 on: July 04, 2012, 11:06:21 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #9 on: July 04, 2012, 11:13:45 am » |
Thanks, PaulS.
So after it gets the two bytes for the pointer, it allocates 4 bytes? Or 5 bytes with the zero terminator?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #10 on: July 05, 2012, 05:17:32 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #11 on: July 05, 2012, 07:08:56 am » |
@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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #12 on: July 05, 2012, 07:22:36 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Miramar Beach, Florida
Online
Faraday Member
Karma: 50
Posts: 3453
|
 |
« Reply #13 on: July 05, 2012, 07:50:43 am » |
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".
|
|
|
|
« Last Edit: July 05, 2012, 07:54:43 am by SurferTim »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35526
Seattle, WA USA
|
 |
« Reply #14 on: July 05, 2012, 08:04:42 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|