Skip to content

Instantly share code, notes, and snippets.

@alysdal
Last active April 27, 2016 10:15
Show Gist options
  • Save alysdal/eec7b70aaec47da6110faf55707ec62f to your computer and use it in GitHub Desktop.
Save alysdal/eec7b70aaec47da6110faf55707ec62f to your computer and use it in GitHub Desktop.
<?php
// device info
$deviceID = "390024001347343338333633";
$accessToken = "fa2405080d01b918e0574a0f23a0b06c64332305";
$command = "setcolor";
// api url
$url = "https://api.particle.io/v1/devices/".$deviceID."/".$command."?access_token=".$accessToken;
/**
* Do a POST requests
* @param string $url
* @param string $data
* @return string
*/
function makePostRequest($url, $data) {
// konfiguration for POST-request
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
// noget gik galt
die("http error, check photon is online");
}
return $result;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Led Control</title>
</head>
<body>
<form action="" method="POST">
<input type="radio" name="color" value="0,0,255"> Blue
<input type="radio" name="color" value="0,255,0"> Green
<input type="radio" name="color" value="255,0,0"> Red
<input type="radio" name="color" value="0,0,0"> Sluk<br>
<input type="submit" value="Update led">
</form>
<h3>Output</h3>
<?php
// check if POST data has been submitted
if (isset($_POST['color'])) {
$color = $_POST['color'];
// photon forventer et parameter kaldet "args" med værdien.
$data = [];
$data['args'] = $color;
// send post request og læs svar.
$response = makePostRequest($url, $data);
var_dump($response);
}
?>
</body>
</html>
<?php
require_once('dbConnection.php');
// device info
$deviceID = "390024001347343338333633";
$accessToken = "fa2405080d01b918e0574a0f23a0b06c64332305";
$command = "setcolor";
$sensor_name = 'tricolor_led';
// api url
$url = "https://api.particle.io/v1/devices/".$deviceID."/".$command."?access_token=".$accessToken;
/**
* Do a POST requests
* @param string $url
* @param string $data
* @return string
*/
function makePostRequest($url, $data) {
// konfiguration for POST-request
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
// noget gik galt
die("http error, check photon is online");
}
return $result;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Led Control</title>
<style type="text/css">
span{
display: inline-block;
height: 20px;
margin: 5px;
width: 20px;
}
</style>
</head>
<body>
<form action="" method="POST">
<input type="radio" name="color" value="0,0,255"> Blue
<input type="radio" name="color" value="0,255,0"> Green
<input type="radio" name="color" value="255,0,0"> Red
<input type="radio" name="color" value="0,0,0"> Sluk<br>
<input type="submit" value="Update led">
</form>
<h3>Output</h3>
<?php
// check if POST data has been submitted
if (isset($_POST['color'])) {
$color = $_POST['color'];
// photon forventer et parameter kaldet "args" med værdien.
$data = [];
$data['args'] = $color;
$sql = "INSERT INTO sensor_data(sensor_name, value)
VALUES (:sensor_name, :value)";
$sth = $db->prepare($sql);
$sth->bindValue(':sensor_name' , $sensor_name);
$sth->bindValue(':value', $color);
$sth->execute();
// send post request og læs svar.
$response = makePostRequest($url, $data);
var_dump($response);
}
?>
<br>
<br>
<?php
$sql = "SELECT sensor_name, value, reg_date
FROM sensor_data";
$sth = $db->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
// echo "<pre>";
// print_r($result);
// echo "</pre>";
foreach ($result as $sensorReading)
{
// echo "<pre>";
// print_r($sensorReading);
// echo "</pre>";
echo '<span style="background-color: rgb('.$sensorReading['value'].')"></span>';
}
?>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script type="text/javascript">
// change background according to sensor value
function updateBackgroundColor() {
var url = "https://api.particle.io/v1/devices/390024001347343338333633/light?access_token=fa2405080d01b918e0574a0f23a0b06c64332305";
$.getJSON( url, function( data ) {
//var color = data.result / 4 + 100 ;
var color = colorTemperatureToRGB(data.result * 10);
console.log(color, "rgb(" + color.r + "," + color.g + "," + color.b + ")");
// update text
$('#lightval').text(data.result);
// update bg color
$('body,html').css('background-color', "rgb(" + color.r + "," + color.g + "," + color.b + ")");
});
setTimeout(updateBackgroundColor, 0);
}
setTimeout(updateBackgroundColor, 100);
//
function proportion(value,max,minrange,maxrange) {
return Math.round(((max-value)/(max))*(maxrange-minrange))+minrange;
}
function colorTemperatureToRGB(kelvin){
var temp = kelvin / 100;
var red, green, blue;
if( temp <= 66 ){
red = 255;
green = temp;
green = 99.4708025861 * Math.log(green) - 161.1195681661;
if( temp <= 19){
blue = 0;
} else {
blue = temp-10;
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
}
} else {
red = temp - 60;
red = 329.698727446 * Math.pow(red, -0.1332047592);
green = temp - 60;
green = 288.1221695283 * Math.pow(green, -0.0755148492 );
blue = 255;
}
return {
r : Math.floor(clamp(red, 0, 255)),
g : Math.floor(clamp(green, 0, 255)),
b : Math.floor(clamp(blue, 0, 255))
}
}
function clamp( x, min, max ) {
if(x<min){ return min; }
if(x>max){ return max; }
return x;
}
</script>
</head>
<body>
<h2>Lysstyrke: <span id="lightval" ></span></h2>
</body>
</html>
<?php
// API address
// DEVICE ID VARIABLE ACCESS TOKEN
$url = "https://api.particle.io/v1/devices/390024001347343338333633/light?access_token=fa2405080d01b918e0574a0f23a0b06c64332305";
// download from url
$resultString = file_get_contents($url);
$resultObject = json_decode($resultString);
echo $resultObject->result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment