Problem with TCPclient

I have a problem with TCPclient, when i try to get some response from any server using an http method or tcp metho client.avaible() function show me 0 bytes to read.

I am using a simple method, where i send some data to server via client.println (data of server change by post) but it never sends me an http response like “201 http 1.1” …

@Metavix posting your code would be helpful :slight_smile:

My code is this one https://github.com/ubidots/ubidots-particle, but I added this line to cpp file:

float Ubidots::getValue(char* id) {
    int i = 0;
    while (!_client.connected() && i < 6) {
        i++;
        _client.connect("things.ubidots.com", 80);
    }
    if (_client.connected()) {        // Connect to the server
#ifdef DEBUG_UBIDOTS
        Serial.println("Client connected");
#endif
        _client.flush();
        _client.print("GET /api/v1.6/variables/");
        _client.print(id);
        _client.print("/values?token=");
        _client.print(_token);
        _client.println(" HTTP/1.1\nHost: things.ubidots.com\nConnection: close");
    }
    delay(1000);
    Serial.println(_client.available());
    while (_client.available()>0) {
        char c = _client.read();
#ifdef DEBUG_UBIDOTS
        Serial.write(c);
#endif
    }
    
}

and in the app I put this one:

#include "Ubidots.h"


#define TOKEN "CCN8F000lRYGulPTkbaiR9Myx8qN2o"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_TAG "3d0038000a51343334363138"  // Put here your data source name

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
}
void loop() {
    
    ubidots.getValue("571fc3e77625427c84f581b9");
    delay(5000);
}

@Metavix, first I have to ask why you are not using the ubidot’s library getValueWithDatasource() function. Second, you will notice in the library how the char string allData is filled using sprintf(). This is used so that a single _client.print() can be used instead of several. The reason is that each _client.print() sends a single TCP packet. So in your case, many are sent which is most likely causing the problem. :wink:

2 Likes

Hi @Metavix

Your HTTP request does not look well-formed to me. You need another trailing newline after final actual of a GET request. You don’t seem to have an Accept: header either which is sometimes required.

 _client.println(" HTTP/1.1\nHost: things.ubidots.com\nConnection: close\nAccept: text/html, text/plain\n\n");

Finally as @peekay123 said above: there are libraries for these things that will make your life easier!

Yes, I doesn’t use a post there, but is the same with the post line, if i use:

ubidots.add();
ubidots.sendAll();

I post the value to ubidots i see that happend, but particle photon, or electron don’t get the package of respo nse.
I use a TELNET, to confirm if my message is good, or if it is a problem from ubidots, but in telnet all is ok.
@bko it is ok i test it by a telnet

My problem is very strange because the last week the get method was ok, but this week all go down.
I know that because i create this library

Fixed, the problem was the tcp connection, thank you for all :smiley:

Hi!,
@Metavix, i’m experiencing the same issue, but using the ubidots library on a photon device … the library is extremely easy to use, so i’m worrying about myself … any clue to solve this problem? thanks

Explaining the actual problem you see rather than saying “the same issue” (which it won’t necessarily be, using an additional library) would be good.
Also have you tried to start with a running example and move on from that one?
After which addition did it start to play up?
How does your code look now?

Hi again,
the code i’m trying is the following:

#include "Ubidots/Ubidots.h"
#define TOKEN "<<token readacted>>" 
#define DATA_SOURCE_NAME "Monitor"

Ubidots ubidots(TOKEN); 

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

void loop(){
    float value2;
    value2 = ubidots.getValueWithDatasource(DATA_SOURCE_NAME, "valor2");
    Serial.println(value2);
    delay(5000); 
}

but, despite from ubidots sashboar i see that “valor2” value is 68.00, what i obtain from the photon as a response is 0.00:

Client connected
Particle/1.1|LV|<<token readacted>>|Monitor:valor2|end
Receiving TCP transaction of 65 bytes.
Particle/1.1|LV|<<token readacted>>|Monitor:valor2|endEnd of TCP transaction.End of TCP Response (670ms).
0.00

If i do the same, but from http request, it works fine. Thanks
best regards

Try to add a Tag Particle to your Monitor data source, and check again.

I did it:

but i still receive the 0.00 value for “valor2” variable
Thanks

That was quick :smiley:
I’ve been testing a bit more, and have found, that the problem seems to be the “lack” of description what DataSourceName and DataSourceTag actually are and what you need to use with getValueWithDatasource()

I found when I use the tag rather than the name I can retrieve the variable
(BTW: today is my first contact with Ubidots - specifically to answer your question)

So I tried this code

#include "Ubidots/Ubidots.h"

#define TOKEN "<<YourTokenHere>>"
#define DATA_SOURCE_NAME "Monitor" 
#define DATA_SOURCE_TAG  "Particle"  
#define VAR_NAME         "valor2"

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setDatasourceName(DATA_SOURCE_NAME);
    ubidots.setDatasourceTag(DATA_SOURCE_TAG);
    
    delay(1000);
    ubidots.add(VAR_NAME, analogRead(A0));  // this would create a new data source with tag  
    ubidots.sendAll();                      // if that combination didn't already exist
}

void loop() {
    float value;
    value = ubidots.getValueWithDatasource(DATA_SOURCE_TAG, VAR_NAME);
    Serial.println(value);
    delay(5000);
}

And it works. I can set the value and read it too.

Without setDatasourceName() your default data source will be called Particle and get an arbitrary tag (when adding variables).
Setting the the name and tag for your data source will save you some headache to guess the default values you’ll end up with.

The reason is your variable tag, you need to put the variable tag for get the value:

Sorry @fbt because the old version of libraries has that problem, in the examples said that you needed to put the variable name, but it was false, you need to put there is the variable tag! then if your variable tag is null you could not to get the variable value, but you can add a tag via front end of Ubidots!

Then is best practice to use many print() and divide the message in many package?

Hi @Metavix … thanks for the reply … could you please post a valid example for getting the value of variable “valor2” in the following scenario?. thanks

Yeah the code is like:

#include "Ubidots/Ubidots.h"

#define TOKEN "Your_Token_Here"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_TAG "Your_Data_Source_Tag"  // Put here your data source name

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
}
void loop() {
    float value;
    value = ubidots.getValueWithDatasource("Particle", "Particle2");
    Serial.println(value);
    delay(5000);
}