Serial.print not working with HTTPClient Library example

I’m just starting with the HTTPClient (great lib btw, props) and I don’t see the serial messages on the Particle IDE (desktop, OSX).

I tried a basic program to make sure I’m actually getting ANY serial communication and I am just fine. I flashed the Photon with the HTTPClient www.timeapi.org example and nothing spits out on the serial monitor. My photon connects to the Particle Cloud just fine as well.

Hi @craigfoo

It is not clear to me where you wanted to see your serial debug print statements.

Did you use a terminal emulator program like CoolTerm or screen to connect to the serial port?

Or did you use the CLI with particle serial monitor /dev/cu.modem4321 or similar?

@bko, it doesn’t matter where I see the print statements but I was using the serial monitor that is included with the Particle IDE. It may not even be getting to the Serial.print statements and I will need to see where it’s getting hung up.

Hi @craigfoo

I think your problems are on the PC side, not the Photon. Can you try a really simple program like this:

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

void loop() {
  Serial.println("Hello World!");
  delay(1000);
}

1 Like

@bko, I know for sure this works. Actually Serial.print() works with every other program I’ve written. I’m trying to use the Spark REST library now, https://github.com/llad/spark-restclient. and serial works but I’m getting a SOS LED, hmph.

1 Like

I tried plugging the hello world example in place of the code in the HTTPClient example and it still never prints to the serial monitor.


#include "application.h"
#include "HttpClient.h"

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Content-Type", "application/json" },
    { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { "User-agent", "Particle HttpClient"},
    { NULL, NULL } // Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

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

void loop() {
  Serial.println("Hello World!");
  delay(1000);
}

As I recall there is define that needs to set in the library #define LOGGING.

Maybe that is the problem?

@craigfoo, I’ll have to test this on a Photon later. Am I correct in assuming that you are compiling using Particle DEV?

@HardWater, I tried what you suggested with no luck :confused:

@peekay123, yes you are correct. I’ve compiled using both the Windows 7 version and Mac OSX version.

1 Like

Hi @craigfoo

I was able to test this tonight using the CLI to compile for my Photon and it worked fine for me. I then turned on logging in the HTTPClient library and it continued to work fine, but like your code above, I didn’t call any http functions other than the constructor.

There is one thing I wanted to caution against: because of the order things happen in Particle firmware, you cannot have a Serial.print() call in the constructor of an object since the order of object construction is not predictable.

Have you added any Serial.print() statements to the libraries you are using?

Which version of the HttpClient library are you using?

1 Like

@bko, I’m using this one, https://github.com/nmattisson/HttpClient

I haven’t added any extra lines of code in either the HTTPclient.cpp or HTTPclient.h.

Hi @craigfoo

Here is what I tried this morning and it worked for me:

I cut and pasted those exact libraries from the raw text (there’s a button on github) into files in a directory.

I used the same program I used before, copied here:

// This #include statement was automatically added by the Spark IDE.
#include "HttpClient.h"


int count = 0;

unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Content-Type", "application/json" },
    { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { "User-agent", "Particle HttpClient"},
    { NULL, NULL } // Always terminate headers will NULL
};
http_request_t request;
http_response_t response;

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

void loop() {
    Serial.print(count++);
    Serial.println("Hello there!");
    delay(1000);
}

Using CoolTerm on my Mac, this printed out:

0Hello there!
1Hello there!
2Hello there!
3Hello there!
4Hello there!
5Hello there!
6Hello there!
7Hello there!
8Hello there!
9Hello there!
10Hello there!
11Hello there!
12Hello there!
13Hello there!
14Hello there!

I think you should try CoolTerm instead of the Particle DEV serial monitor and see if that changes things.

@bko, with CoolTerm and a fresh HttpClient, it seems to work. I must’ve dorked with it somehow. Thanks!

1 Like