0

I am working on code where I am able to manually enter the value in the URL to let the PHP code save it in the database, but I'm unsure how to do it with a variable in a loop running. This is the URL:

student.cs.hioa.no/~s180343/updatedb.php?verdi=25

Code:

void setup() {
  // Initialize Bridge
  Bridge.begin();

  // Initialize Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial);
  Serial.println("Ready");

  // run various example processes

}

void loop() {
   runCurl();
   delay(10000);
}

void runCurl() {
  // Launch "curl" command and get Arduino ascii art logo from the network
  // curl is command line program for transferring data using different internet protocols
  Process p;        // Create a process and call it "p"
  p.begin("curl");  // Process that launch the "curl" command
  p.addParameter("student.cs.hioa.no/~s180343/updatedb.php?verdi=25"); // Add the URL parameter to "curl"
  p.run();      // Run the process and wait for its termination

  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}
  • What do you mean by "manually entering" do you want to do subsequent curls as: verdi=25, verdi=26...? – hithwen Apr 3 '14 at 17:04
  • 1
    If that's the case take a look to string appender – hithwen Apr 3 '14 at 17:13
  • yes! hthwen. I want to do it in a loop so you can put a variable in verdi=$variable – user3270211 Apr 3 '14 at 17:32
  • Please note that duplicates (or close variants) of this question were also cross-posted to Stack Overflow and EE.SE. Cross-posting is against Stack Exchange policy, so they have been migrated/merged where possible. Some variations in question information have been lost in this process. – Peter Bloomfield Apr 7 '14 at 16:24
2

You can pass in a variable into a function. Something like this:

void runCurl(int verdi) {
    ...
    String myUrl = "student.cs.hioa.no/~s180343/updatedb.php?verdi=";
    myUrl += verdi;
    p.addParameter(myUrl);
    ...
}

You call the function like this, now:

runCurl(1);
runCurl(2);
runCurl(someVar);

and so on. Since it's already in your main loop() function, you just need to set pass in a different number based on whatever it is you're trying to do.

  • String myUrl = "student.cs.hioa.no/~s180343/updatedb.php?verdi=" + verdi; is wrong, you can't concatenate array of char (pointer to char) to a int, you'll get erratic result. you need to do concatenation: solution 1: String myUrl = "student.cs.hioa.no/~s180343/updatedb.php?verdi="; myUrl += verdi; that mean: string(array of char) is casted to String(object) and then verdi is concatenated to the String objcet; or String myUrl = String("student.cs.hioa.no/~s180343/updatedb.php?verdi=") + String(verdi); – Lesto Apr 4 '14 at 10:18
  • @lesto you're right. I fixed it. – sachleen Apr 4 '14 at 17:49
0

first of all, you declare your buffer as a string of 50 characters, but

student.cs.hioa.no/~s180343/updatedb.php?verdi=

is already 48, and you start your value at 1200 and increments over that, so basically

sprintf (buffer, "student.cs.hioa.no/~s180343/updatedb.php?verdi=%d", val)

will create a buffer of 53 characters (52 characters + \0).

Secondly, it's always a better idea to give the full URL to a program such as curl, as it might work but may give unexpected results as well if you do not provide the protocol… curl could legally assume you want ftp or gopher to access that address!

http://student.cs.hioa.no/~s180343/updatedb.php?verdi=%d

Third, here's how you setup and use your val variable:

int val=1200;
sprintf (…, "…", val);  
val++;

so once you've reached 32767 your int is overflowing, getting back to -32,768.

Oh, and by the way, it is good practice on a Q&A site such as Stack Overflow to actually ask a question and say what your problem is. All my hints here are insights based on reading your flawed code.

HTH

0

Alright. I'm assuming you mean that you need the last part of the url varied from, say 1 to 100. Like Java, Processing, and, by extension, Arduino, allow you to add an integer to a String to get a String. I haven't tested this out, but it should work:

#include <Process.h>
#define MIN 0
#define MAX 100

int i;

void setup() {
  i = MIN;

  // Initialize Bridge
  Bridge.begin();

  // Initialize Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial);
  Serial.println("Ready");

  // run various example processes

}

void loop() {
   if (i<=MAX) {
     runCurl();  
     i = i+1; 
   }
   delay(10000);
}

void runCurl() {
  // Launch "curl" command and get Arduino ascii art logo from the network
  // curl is command line program for transferring data using different internet protocols
  Process p;        // Create a process and call it "p"
  p.begin("curl");  // Process that launch the "curl" command
  String myUrl = "student.cs.hioa.no/~s180343/updatedb.php?verdi=";
  p.addParameter(myUrl+i); // Add the URL parameter to "curl"
  p.run();      // Run the process and wait for its termination

  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.