Append char in Web IDE c++


#1

Hello i try to append strings and i can´t get it. i am a c++ greenhorn.

The append should be happen in the subfunction, in line : pushingbox(server …
I want to bring together, the received “data” value with the “url” , to trigger a pushinbox scenario.

const char server                    = "api.pushingbox.com";
const char url_who_open_house_door[] = "/pushingbox?devid=xxxxxxxxxxxxxx&user=";   // xxx hide my pushingbox ID 
 

void setup(){
    
    // here i listen for the response  by a google sheet
    Particle.subscribe("hook-response/postGoogle", subKeyCodeCheckResponse, MY_DEVICES);
}


void loop() {

    
}


void subKeyCodeCheckResponse(const char *event, const char *data) {   // the value of "data" is for example  "unlocked by Ghostbusters" 

   if(strstr(data, "unlocked")){   
   
        digitalWrite(led, HIGH);     
        
        pushingbox(server, url_who_open_house_door   );   // trigger pushingbox.com scenario, added with one own variable/value "user"

        digitalWrite(led, LOW);     
   }

}


//*********************************************************
//***  sendGetRequest()    
//*********************************************************
void pushingbox(const char * server, const char * url)
{
    if (myTCP.connect(server, 80)) {
        myTCP.print(  "GET ");
        myTCP.print(url);
        myTCP.println(" HTTP/1.0");
        myTCP.println("Connection: close");
        myTCP.print(  "Host: ");
        myTCP.println(server);
        myTCP.println("Accept: text/html, text/plain");
        myTCP.println();
        myTCP.flush();
        delay(250);
        myTCP.stop();
    } 
}

#2

You want to concatenate data with url_who_open_house_door (a somewhat unwieldy variable name I might note :wink: )
For that strcat() (string concatenation) would be a possible option or snprintf()


#3

I need someone who write the working code, because i can´t get it.
I can´t get it also with the hinte of useing orders like strcat() or snprintf()
So frustrated.


#4

What have you tried so far?


Usually we don’t spoon-feed solutions, but seeing you “admit” beeing new to C++ some little hints to get you unstuck

// you were missing the square brackets here, so only ONE char would be stored
const char server[]                  = "api.pushingbox.com";
...
void subKeyCodeCheckResponse(const char *event, const char *data) {   // the value of "data" is for example  "unlocked by Ghostbusters" 
  if(strstr(data, "unlocked")) {
    char path[strlen(url_who_open_house_door) + strlen(data) + 1];
    digitalWrite(led, HIGH);
    strcpy(path, url_who_open_house_door); // copy the path prefix to path
    strcat(path, data);                    // append the received data
    pushingbox(server, path);              // trigger pushingbox.com scenario, added with one own variable/value "user"
    digitalWrite(led, LOW);
  }
}

#5

First of all, thx again to particle big man ScruffR.

In the end, after much attempt in the last year and again more then 10 hours today, of try and error, i got the code running.

I am a AirBnb superhost with two Apartments and nearly everyday changing guests.
Last year i built my own check-in pincode keypad system, to beware of lost keys.
It is built on a photon, connected to a number keypad, that interact with a google sheet, where the numbers are stored and also log the entries. (The code behind the sheet is google action script GAS). If the pincode match to a valid record in the sheet, the sheet send back a ok to another photon in my apartment, connected to the open door button.

I also cut a YT video for my guests, to give them a manual to use this system.

Before i finished the coding today, the system give me only a email that sombody entry the door.
But now i get it worked, that the email direct deliver me the information, wich guest open the door, iam so gladful.


int    led       = D7; 
int    button  = D0;

String  url_who_open_house_door = "/pushingbox?devid=xxxxxxxxxxx&user=";  
String  username;

TCPClient myTCP;

void setup(){
    Particle.subscribe("hook-response/postGoogle", subKeyCodeCheckResponse, MY_DEVICES);
}

void loop() {
}

void subKeyCodeCheckResponse( const char *event,  const char *data) {   

   if(strstr(data, "unlocked")){    // find charackter in string
   
        digitalWrite(led, HIGH);   
        
        digitalWrite(button, LOW);  // schaltet TOE 
         delay(500);
        digitalWrite(button, HIGH);  
        
        delay(1000);
        
        digitalWrite(button, LOW);  // schaltet TOE 
         delay(500);
        digitalWrite(button, HIGH);  

        username = data;
        username = username.replace(" ", "%20");
        username = username.replace("\"", "");

        pushingbox(server, url_who_open_house_door + username  );   // trigger pushingbox.com scenario

        delay(3950);
        digitalWrite(led, LOW);     
   }
}

//*********************************************************
//***  sendGetRequest()    
//*********************************************************
void pushingbox(const char * server, const char * url)
{
    if (myTCP.connect(server, 80)) {
        myTCP.print(  "GET ");
        myTCP.print(url);
        myTCP.println(" HTTP/1.0");
        myTCP.println("Connection: close");
        myTCP.print(  "Host: ");
        myTCP.println(server);
        myTCP.println("Accept: text/html, text/plain");
        myTCP.println();
        myTCP.flush();
        delay(250);
        myTCP.stop();
    } 
}

The major tip from ScruffR was in a other post, using “String” with big letter at first, because “string” do not work in the web ide.


#6

You could use std::string tho’

However, I’d stay clear of String and std::string as they are using dynamic memory allocation which may over time lead to heap fragmentation which in turn can cause your firmware to crash or even worse become unable to reconnect after a cloud disconnect.

That’s why I used standard C strings in my code above.