3 Replies Latest reply on Jan 23, 2015 1:55 AM by X_Y

    How to make dynamic system call in sketch

    X_Y

      I'd like to make system() calls in Arduino sketches with dynamic values.

       

        String dateCmd="date +'%Y%m%d %H:%M:%S' -s ";

        dateCmd+="'20150120 10:42:00'";

        system(dateCmd);

       

      However it generates an error:

       

      error: cannot convert 'String' to 'const char*' for argument '1' to 'int system(const char*)'

       

      I've tried to use c_str() method of String, but it doesn't exist.

       

      What shoud I do to get it to work?

        • 1. Re: How to make dynamic system call in sketch
          Intel_Peter

          Hello X_y,

           

          The error the IDE shows you is due to a mistake on how you enter the parameters in the system command, the parameter should be entered as char, so, I suggest you to try this instead char* dateCmd = "date +'%Y%m%d %H:%M:%S'";

           

          Peter.

          • 2. Re: How to make dynamic system call in sketch
            DomiWag

            Hi X_Y,

            usually, you would use c_str() to convert your String, but as you stated, it does not exist. Instead, try this:

              String dateCmd="date +'%Y%m%d %H:%M:%S' -s ";
              dateCmd+="'20150120 10:42:00'";
              system(dateCmd.buffer);
            
            

            This compiled and executed for me.

            Please note though, that your command seems to contain an error:

            root@clanton:~# date +'%Y%m%d %H:%M:%S' -s '20150120 10:42:00'
            date: invalid date '20150120 10:42:00'
            
            

            Apart from that, you won't see date's output from the program.

            Regards,

            Dominik

            • 3. Re: How to make dynamic system call in sketch
              X_Y

              Thanks DomiWag, the buffer method works!

              I don't see the date error here, guess I'm using IoT DevKit image, so the datetime format is configurable. From what I see in other threads, the format is fixed in the "official" image, one needs to set the datetime in a specific way.