Using CURL with a website

So I have a website that when visited outputs a database value of either 0 or 1 … I want to use CURL to get this value but when I visit using the arduino it can’t get the value!

I think the reason is because in the source code the value is only outputted on line 4 with lines 1 - 3 being empty so when I check if any content is there it fails?

I can store it in a txt file and read it fine but I would prefer to use the website method as it then saves making files for each user.

Also for some reason my YUN stops working after around 1 0minutes of running and requires a reset does anyone know why that is also?

If I understand you correctly you are reading from a website with curl and want to filter the output to strip leading blank lines, using linux?

I am not familiar with the Yun, so I am not sure what facilities the Linux installation provides, but if you are working in a shell environment, then you could pipe the website output through other programs, like tail, or grep, for instance.

Yes so my curl command looks at a site that outputs the source code:

1| 2| 3| 4| 0

Where the numbers are the line numbers.

If I try reading that with curl it hangs (it works if I add more data but for some reason it wont pick up just 1 number)

Is this a web page being generated by the Yun, or is it one elsewhere?

Its on my server. It’s a php script that takes the variables posted to it via the URL and saves them in a database … it then returns a single value from the database and echos it out.

I want the arduino to get this value and turn a light on or off depending on what value is returned.

I have an UNO which uses the text finder to get the string but I thought the arduno YUN could use the curl command and get this value with ease.

Should be able to, yes.

How about

$ curl --trace-ascii -s -o /dev/null http://whatever-address.com/script.php

What does that give you?

How do I even use that?

I\m using a YUN which uses the process library for the bridge.

The current code is:

/*
#include <Process.h>

void setup()
{
  /* Initialize Bridge */
  Bridge.begin();
  
  /* Initialize Console */
  Console.begin();
  
  pinMode(13, OUTPUT);
  digitalWrite(13,HIGH);
  
  Console.println("Starting...");
 /* Check whether the heater is turned on */
    if (runCurl() == true) {
     Console.println("ON");
     digitalWrite(13,HIGH);
    } else {
      Console.println("OFF");
      digitalWrite(13,LOW);
    }
}

void loop()
{
    /* Check whether the heater is turned on */
    if (runCurl() == true) {
     //Console.println("ON");
     digitalWrite(13,HIGH);
    } else {
      //Console.println("OFF");
      digitalWrite(13,LOW);
    }
   
    
    /* Wait for 1s */
    delay(5000);
}

bool runCurl() {
  /* Launch "curl" command to check whether the heater is turned on
     curl is command line program for transferring data using different internet protocols */
  /* Create a process and call it "p" */   
  Process p;  

  /* Process that launch the "curl" command */  
  p.begin("curl"); 
  
  /* Add the URL parameter to "curl". It actually checks a .txt file on my server. If it is 0, then the heater is switched off. Else it is switched on. */
  p.addParameter("website"); 
  
  /* Run the process and wait for its termination */
  p.run();

  /* A process output can be read with the stream methods */
  while (p.available()>0) {
    char c = p.read();
    //Console.println(c);
    /* Check whether the heater is turned on */ 
    if (c=='0') {
      /* If it is 0, then the heater is switched off... */
      return false;
    } else {
      /* ...else it is switched on. */
      return true; 
    }
  }
}

As I said the txt file works … but if I plug the url to the website it can’t get the single value.

Ah, like I said, I am not familiar with the Yun. It's obviously presenting some noddy interface to Linux.

That's a shame - I was thinking I should get one, but now I don't know if I'll bother...

Well, it's something I can't help you with now then I'm afraid - not unless you can get real access to the Linux part of it...

So apparently using the runshellcommand() allows you to run a shell command on the YUN. There is a gmail script that uses :

p.runShellCommand(“curl -u " + username + “:” + password + " “https://mail.google.com/mail/feed/atom/” + label + “” -k --silent |grep -o “[0-9]" |grep -o "[0-9]””);

to get the unread emails of a certain label. This seems like something I could use but I’m not sure about the parameters used. Any ideas?

The -u username:password is for logging in to a remote site. --silent stops it displaying the progress. -k allows insecure SSL connections (invalid certificates). You can ignore all of those.

Then the output is fed through grep twice to clean up the data.

p.runShellCommand("curl --silent http://my.served/script.php | tail -n 1");

That should get the data from the site, and return just the very last line of it.

So far using:

p.runShellCommand("curl --silent http://eherbi.com/Admin/Update.php?page=status | tail -n 1");

  int result = p.parseInt();
  Console.println("Result:"+result);

returns just "Result: "

Should I try outputting the html structure also? Ie

or will that not effect it?

I think you will find it easier to work with the CURL command and other unix/lineno commands if yoy log on to your Yun.

I am on windows, so I use putty.

I think you need to find out exactly what that command is returning. Maybe drop everything from the | onwards so as to get the full untrimmed output and print it all out.