Got me 2 sets of LittleBits (Space Kit & CloudBit Starter Kit)
Wanted to see how the CloudBit API works and how to integrate it with some data.
Thought of 2 main ideas:
1) display temperature in Tel-Aviv
2) display My Blog’s error rate from New Relic
The steps:
1) use the power + CloudBit + number modules to display the weather in tel aviv / application’s error rate
2) write a short PHP (GitHub) wrapper for the CloudBit api to easily send output to it.
Send output to CloudBit: (this will send 0-99 to the CloudBit output)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php function sendToCloudBit($cloudBitKey, $deviceId, $durationInMillis, $value){ $url = "https://api-http.littlebitscloud.cc/v3/devices/$deviceId/output"; $data = array('percent' => $value, 'duration_ms' => $durationInMillis); $options = array( 'http' => array( 'header' => "Authorization: Bearer $cloudBitKey\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo "<br>LittleBits said: $result<br>"; } ?> |
3) pull some data and send it to the cloudBit using the previous script
Get Temperature from Yahoo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function getTemperatureFor($city, $countryCode, $metricsInFahrenheit){ echo "<h3>Yahoo Weather In Little Bit</h3>"; $city = urlencode($city); $countryCode = urlencode($countryCode); $metricUrl=""; if (!$metricsInFahrenheit){ $metricUrl = urlencode(" and u='c'"); } $yahooApiURL = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22$city%2C%20$countryCode%22)$metricUrl&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; $jsonFromYahoo = json_decode(file_get_contents($yahooApiURL),true); $tmp = $jsonFromYahoo['query']['results']['channel']['item']['condition']['temp']; echo "<h4>The temp is:$tmp </h4>"; return $tmp; }?> |
Get my blog’s error rate from new relic (for the previous 30 minutes)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php echo "New Relic Error And Little Bits<br>"; $appId = 90210; //new relic's application id $relicKey = 'XXX'; // new relic's seacret key $timePicker = 30; //time to measure error rate for (this is now - 30 minutes) date_default_timezone_set('Asia/Jerusalem'); //set for your current timezone $errorRate = getMetricFromRelic($relicKey, $appId, $timePicker); function getMetricFromRelic($relicKey, $appId, $timePicker){ $errorURL = 'Errors/all&values[]=error_count'; $countURL = 'HttpDispatcher&values[]=call_count'; $otherTransactionsURL = 'OtherTransaction/all&values[]=call_count'; $errorKey = 'error_count'; $countKey = 'call_count'; $nowMinus30 = strtotime("-$timePicker minutes"); $startTimeDate = date('Y-m-d', $nowMinus30); $startTimeMinutes = date('h:i:s', $nowMinus30); $startTimeStr = "{$startTimeDate}T{$startTimeMinutes}+02:00"; $now = time(); $endTimeDate = date('Y-m-d', $now); $endTimeMinutes = date('h:i:s', $now); $endTimeStr = "{$endTimeDate}T{$endTimeMinutes}+02:00"; $errorCount = getMetrics($relicKey, $errorURL, $appId, $errorKey, $startTimeStr, $endTimeStr); $callCount = getMetrics($relicKey, $countURL, $appId, $countKey, $startTimeStr, $endTimeStr); $otherTransactionsCount = getMetrics($relicKey, $otherTransactionsURL, $appId, $countKey, $startTimeStr, $endTimeStr); echo "Error Count: $errorCount<br>"; echo "Call Count: $callCount<br>"; $errorRate = 100 * $errorCount / ($callCount + $otherTransactionsCount); echo "The error rate is: $errorRate<br>"; return $errorRate; } function getMetrics($relicKey, $url, $appId, $jsonKey, $start, $end){ $url = "https://api.newrelic.com/v2/applications/$appId/metrics/data.JSON?names[]=$url&from=$start&to=$end&summarize=true"; $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"X-Api-Key: $relicKey\r\n" . "Accept: application/json\r\n" ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); $json = json_decode($result,true); $count = $json['metric_data']['metrics'][0]['timeslices'][0]['values'][$jsonKey]; return $count; } ?> |
Put it all together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php include 'cloudBit.php'; //send command to CloudBit example: $cloudBitKey = 'YOUR-CLOUDBIT-KEY'; $deviceId = 'YOUR-CLOUDBIT-ID'; $durationInMillis = 6000; // max is 32000 $value = 99; //(this is the value sent to the bit 0-100) sendToCloudBit($cloudBitKey, $deviceId, $durationInMillis, $value); //send temp to cloudBit from Yahoo include 'yahooWeather.php'; $city = "tel aviv"; $countryCode = "il"; $isFahrenheit = false; $temp = getTemperatureFor($city, $countryCode, $isFahrenheit); sendToCloudBit($cloudBitKey, $deviceId, $durationInMillis, $temp); //get application error rate from relic to cloudbit include 'newRelic.php'; $appId = 90210; //new relic's application id $relicKey = 'XXX'; // new relic's secret key $timePicker = 30; //time to measure error rate for (this is now - 30 minutes) $errorRate = getMetricFromRelic($relicKey, $appId, $timePicker); sendToCloudBit($cloudKitKey, $deviceId, $durationInMillis, $errorRate); ?> |
3) using a cron job on the machine the PHP script will run every minute updating the display.
1 |
* * * * * /path/to/php /var/www/html/yourPHPscript.php |
Done! now the cloudBit is displaying the current temperature in Tel-Aviv (Fahrenheit) or nimrodstech.com error rate from new relic. next step is to print some dashboard for this.