Arduino Strings

I want to pass some variable to a string to create a Arduinojson instance. Therefore that string is going to be transfered via dragino lora. For a reason this piece of code seems that doesent work

String g_s = "Hello";
String x = "{\"ID\":\"SERVER\",\"TYPE\":\"KEY\",\"g\":\""+g_s+"\",\"END\":\"END\"}";

Any help?

what's the error/problem?

Seems there is nothing to be print

Here is the code

#include <SPI.h>
#include <RH_RF95.h>
#include <ArduinoJson.h>
RH_RF95 rf95;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 151, 127};
String id1 = "123abc";

int led = 8;
int x;
int send_counter = 1000;
int counter1 = 0;
StaticJsonBuffer<200> jsonBuffer;
void setup()
{ pinMode(led, OUTPUT);
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
}
void loop()
{
  if (rf95.available())
  { uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH);
      Serial.print("got request: ");
      //Serial.println((char*)buf);
      String str = ((char*)buf);
      Serial.println(str);
      int length_arr = str.length();
      length_arr++;
      char json[length_arr];
      str.toCharArray(json, length_arr);
      Serial.println(json);
      JsonObject& root = jsonBuffer.parseObject(json);
      if (!root.success()) {
        Serial.println("parseObject() failed");
        return;
      }
      const char* sensor = root["ID"];
      //commandChar = sensor.charAt(2);
      if (root["ID"] == id1) {
        if (counter1 == 0) {
          int key = df_key();
          //Serial.println(key);
          counter1 = 10;
        }
        counter1--;
        char data[] = "{ \"ID\":\"SERVER\",\"TYPE\":\"OK\" }";
        rf95.send(data, sizeof(data));
        rf95.waitPacketSent();
      }
      Serial.println("Sent a reply");
      digitalWrite(led, LOW);
      jsonBuffer.clear();
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

int df_key() {
  //JsonObject& root = jsonBuffer.createObject();
  //int rund = random(1, 15);
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);
  // int  p_m = primes[rund];
  // int rund2 = random(1, 15);
  // int  g = primes[rund2];
  //int rund1 = random(1, 90);
  //String g_s = String(g);
  String g_s = "Hello";
  //int alice = rund1;
  //int to_send = (g ^ alice) % p_m;
  String x = "{\"ID\":\"SERVER\",\"TYPE\":\"KEY\",\"g\":\"" + g_s + "\",\"END\":\"END\"}";
  Serial.print(">>>>>>>>>>>");
  Serial.print(x);
  int length_arr = x.length();
  length_arr++;
  char data[length_arr];
  x.toCharArray(data, length_arr);
  rf95.send(data, sizeof(data));
  rf95.waitPacketSent();
  Serial.print("SEND : ");
  Serial.println(data);
  if (rf95.waitAvailableTimeout(3000))
  {
    // Should be a reply message for us now
    if (rf95.recv(buf, &len))
    {
      String in_str = ((char*)buf);
      Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
      Serial.print(in_str);
    }
    jsonBuffer.clear();
  }
  int to_send = 10;
  return to_send;
}

Did it enter the df_key() function? Did it print the ">>>>>>>>>" above that?

And your buf[] is not a string (i.e. not null terminated), why are you treating it like a string?

This compiled for you ?
I got incompatible types error.

String str = ((char*)buf);

Also posted at:
https://arduino.stackexchange.com/q/52259
If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

What Arduino are you using?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

I am using Arduino UNO

Nothing seems to be working!!!

#include <SPI.h>
#include <RH_RF95.h>
#include <ArduinoJson.h>


void setup() {
   Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available

}

void loop() {
  String x = "aaaa";
  String fin = "{\"dv\" : \"dfv\", \"FFFF\":\" " +x +"\", \"131\": \"23413241\"} ";
  Serial.println(fin);
  delay(1000);

}

Here is a piece of code that works fine! i used c_str() but nothing!
I need the string format to parse it in ArduinoJson butarray formt to send the data via arduino dragino! (Sorry for the cross post)