HTTPS Post Option
  • Would love an https post option and options to set auth headers a plus.
  • 8 Comments sorted by
  • Indeed, it feels weird to get an HTTP GET instead of a POST. The Twine POSTS data to the HTTP endpoint, it doesn't GET data.
  • Anyone consider using cosm.com (formerly Pachube)? Could use it to consolidate multiple twines/sensors into one JSON or XML feed, plus it has a pretty charting UI for your data.

    Would need support for PUT and auth headers for this particular API.
  • While waiting for PUT support from Twine, I wrote a php curl script to handle receiving a single datapoint and sending it to Cosm. You'll need to register at Cosm and create a feed and datastream for this to work.

    If you have a php server, you can use this code and direct your HTTP GET there instead.

    Example: http://www.yourdomain.com/cosm/index.php?feed=#####&stream=#####&value=[temperature]

    <?php<br />// Collect temperature and send to cosm
    $base = 'http://api.cosm.com/v2/feeds/';
    $key = 'wluWqOkcvdV2bcsAOjKPk3DIZnuSAKx2M3g5MGRrMXBvbz0g';
    $feed = $_GET['feed'];
    $stream = $_GET['stream'];
    $url = $base.$feed.'/datastreams/'.$stream.'.json?key='.$key;

    // Start curl
    $ch = curl_init();
    // Clean up string
    $putString = '{"current_value":"'.$_GET['value'].'","id":"'.$_GET['stream'].'"}';
    // Put string into a temporary file
    $putData = tmpfile();
    // Write the string to the temporary file
    fwrite($putData, $putString);
    // Move back to the beginning of the file
    fseek($putData, 0);

    // Headers
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Binary transfer i.e. --data-BINARY
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    // Using a PUT method i.e. -XPUT
    curl_setopt($ch, CURLOPT_PUT, true);
    // Instead of POST fields use these settings
    curl_setopt($ch, CURLOPT_INFILE, $putData);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));

    $output = curl_exec($ch);
    echo $output;

    // Close the file
    fclose($putData);
    // Stop curl
    curl_close($ch);
    ?>
  • Next go make a new Temperature rule that triggers when the Temp rises above 0 degrees, triggers after 0 seconds, and resets after 60 seconds (if you want an update every 60 seconds). I think I have those settings right. Have it post to your url and fill in the Cosm feed and datastream numbers.
  • Ryan, how do you get it to tell you the temperature?
    Sorry I mean the temperature value. If the rule is that it is > 0.
  • Ignore that. I've just read the code you posted and it makes sense.
  • Hi Ryan
    question on your script. i tried it but it doesnt provide data to my cosm's feed. so 2 questions
    - is there a specific way to configure cosm to get those data?
    - i had a look at cosm API and i'm not sure the json url looks like the way you describe it. https://api.cosm.com/v2/feeds/xxxx/datastreams/1.json is retrieving an error even by submitting the API key behind it

    thanks for your help! For now I'm stuck with the lack of flexibility from my twine :(

    Jeremie
  • Hi Ryan,

    Thanks for the code - it worked well. I just did a little modification to convert the temperature to Celcius:

    <?php<br />// Collect temperature and send to cosm
    $base = 'http://api.cosm.com/v2/feeds/';
    $key = '';
    $feed = $_GET['feed'];
    $stream = $_GET['stream'];
    $url = $base.$feed.'/datastreams/'.$stream.'.json?key='.$key;

    $f=$_GET['value'];

    //Convert Fahrenheit into Celsius
    $c=intval((5/9)*($f-32));

    // Start curl
    $ch = curl_init();
    // Clean up string
    $putString = '{"current_value":"'.$c.'","id":"'.$_GET['stream'].'"}';
    // Put string into a temporary file
    $putData = tmpfile();
    // Write the string to the temporary file
    fwrite($putData, $putString);
    // Move back to the beginning of the file
    fseek($putData, 0);

    // Headers
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Binary transfer i.e. --data-BINARY
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    // Using a PUT method i.e. -XPUT
    curl_setopt($ch, CURLOPT_PUT, true);
    // Instead of POST fields use these settings
    curl_setopt($ch, CURLOPT_INFILE, $putData);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));

    $output = curl_exec($ch);
    echo $output;

    // Close the file
    fclose($putData);
    // Stop curl
    curl_close($ch);
    echo 'Update Twine Temperature to COSM feed '.$feed.' with values C='.$c.'.';
    ?>


    Thanks,
    Michael

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!