-2

How to write a char[] with unknown length including a variable integer ( length of integer is variable) like this:

char data[] = "{""LDRValue"":" + LDRReading + "}";

LDRReading is the only variable.

here is the code:

int LDRReading = analogRead(LDR);
int d = LDRReading;
int b = d - c;
if (b > 50 || b < -50) {
  c = d;
//    String data = "{""LDRValue"": ";
//    data = data + LDRReading;
//    data = data + "}";
  char data[] = "{\"LDRValue\": 300}";
  Serial.println("\nStarting connection to LDR server...");
  if (client.connect(server, 8060)) {
    Serial.println("connected to LDR post server");
    client.println("POST /api/sensor HTTP/1.1");
    client.println("Host: 192.168.2.123");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(strlen(data));
    client.println();
    client.print("{\"LDRValue\":");
    client.print(LDRReading);
    client.print("}");
    delay(300);
   }
  Serial.println(d);
  client.stop();
MBS
  • 673
  • 2
  • 16
  • 48
  • Use `std::ostringstream` to format strings. – πάντα ῥεῖ Jun 29 '16 at 06:58
  • is this question related to `arduino` or `c++`? They are **not** the same thing. – Patrick Trentin Jun 29 '16 at 07:21
  • 1
    @PatrickTrentin I thought I was wrong to mention C++ in my question, because some answers seems to be right but when I compile it give me "Error compiling for board Arduino/Genuino MKR1000" so can you help me with this problem? – MBS Jun 29 '16 at 07:33
  • @bilal1409 Is there any particular reason why you don't use the [String Object](https://www.arduino.cc/en/Reference/StringObject) provided by the Arduino Library rather than a *character array*? It is designed exactly for the use case you are presenting here: handling strings of variable length. – Patrick Trentin Jun 29 '16 at 07:38
  • As I said in other comment, when I use String data = ..... it does not work with http post request. I works only when I use char data[] = ......... – MBS Jun 29 '16 at 07:44
  • @bilal1409 Could you please add the code related your http post request to your question? – Patrick Trentin Jun 29 '16 at 07:45

2 Answers2

1

EthernetClient, which is the object-class you are using, is sub-class of Client which is sub-class of Stream which is sub-class of Print, so it should work printing a String object through the client instance, as Print has the following methods:

size_t print(const String &);
size_t print(const char[]);
size_t println(const String &s);
size_t println(const char[]);

Therefore, this should work:

int LDRReading = analogRead(LDR);
int d = LDRReading;
int b = d - c;
if (b > 50 || b < -50) {
  c = d;

  String data = "{\"LDRValue\": ";   // FIXED: escaped " symbols
  data = data + LDRReading;
  data = data + "}";

  Serial.println("\nStarting connection to LDR server...");
  if (client.connect(server, 8060)) {
    Serial.println("connected to LDR post server");
    client.println("POST /api/sensor HTTP/1.1");
    client.println("Host: 192.168.2.123");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();

    client.print(data); // FIXME: why don't you use println() here?
    client.flush();     // NOTE: this is strongly advised to push all
                        //      data in a stream out if you don't add
                        //      a line feed.

    delay(300);
  }
  Serial.println(d);
  client.stop();
}

Notice the comment that I added in the code: I think you should either use a println() or invoke client.flush() afterwards to ensure that the data you are printing on the stream is effectively pushed out. I wrote client.print() only to not alter your original code.


If, for some specific reason, you believe that there is a specific issue with printing data as a String, then you can always obtain its char[] equivalent by invoking data.c_str(); on it:

    client.print(data.c_str());
    client.flush();
Patrick Trentin
  • 7,126
  • 3
  • 23
  • 40
  • Thank you it works but I use "client.println(data.length() );" instead of "client.println(strlen(data));" – MBS Jun 29 '16 at 08:28
  • @bilal1409 right, I didn't check the other lines, fixed it :) – Patrick Trentin Jun 29 '16 at 08:29
  • One last thing. DO you know something about Web socket on Arduino? – MBS Jun 29 '16 at 08:32
  • @bilal1409 I don't use them in my projects, as I don't have a shield with network capability. So I know only what I can read from the source code of the Arduino library. ;) – Patrick Trentin Jun 29 '16 at 08:34
  • I use Websocket between Raspberry Pi and server and it works perfect, as the theory of websocket. But when I search for a websocket for Arduino I found a lot of libraries and may be all of them is just like http post not like the real websocket. – MBS Jun 29 '16 at 08:34
-2

Why not just use std::string?

std::string s("{""LDRValue"":" + LDRReading + "}");
const char* szData = s.c_str();

If you cannot use std::string for whatever reason you could determine the length of your content first by using for example strlen and then creating a char array by calling new

buffy
  • 536
  • 2
  • 11