String to char array for publish

Hey everyone

I'm trying for a project to pass a large string of ones and zeros through PubNub
The function works as follows:

    char msg[]  = "\"arduino-WiFi101-hello-world!\"";
    WiFiClient* client = PubNub.publish(channel, msg);
    if (!client) {
        Serial.println("publishing error");
        delay(1000);
        return;
    }
    client->stop();

The problem is also that I haven't really worked with such char arrays yet. It does work if I give the msg variable this value:

    char msg[]  = "\"0110110111010101100\""; //random combination

I can save the data as a String or bool array, but if I then publish it this way, I automatically receive a number of, so 1.101101110101e5 via pubnub.

  int length = Data.length()+1;
  char charArray[length];
  Data.toCharArray(charArray, length);

This is definitely not what I mean. Does anyone know what I can do?

Here I form a String and publish:

 powerInfo.concat( String(CalculatedVoltage,2) );
      xSemaphoreGive( sema_CalculatedVoltage );
      powerInfo.concat( ",");
      powerInfo.concat( String(mA,2) );
      powerInfo.concat( ",");
      powerInfo.concat( String(Power,4) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // whiles MQTTlient.loop() is running no other mqtt operations should be in process
      MQTTclient.publish( topicPower, powerInfo.c_str() );

Might give you a clue.

Oh yes thanks. With concat I was able to add the bits and the quote at the end by:

String bits = "\"";
//add some bits...
bits.concat("\"");

and then send it with that c_str () function

  WiFiClient* = PubNub.publish(channel, bits.c_str()); // Publish message.

If you are using Arduino Strings, check out my tutorial on
Taming Arduino Strings - How to avoid memory issues

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.