Blynk PHP Controller

Hi,

I am trying to build control page for my home automation system in PHP and HTML. I am familiar with these languages but i want to show states of virtual pins so here is the question;

How can i grab value from blynk cloud link and put into if else condition?

This will activate or deactivate bootstrap button states.

P.S. I guess this is not %100 about blynk but if i fix that, it will be blynk related :smiley:

Edit: I solved my problem but i am still working on it. I need some suggestions to tidy up my code

<?php

$key = '539blablablablabla1537f9c04';

$v0 = file_get_contents("http://blynk-cloud.com/$key/get/V0");
$temp = file_get_contents("http://blynk-cloud.com/$key/get/V2");

$v4 = file_get_contents("http://blynk-cloud.com/$key/get/V4");


if ($v0 == '["1"]') {
    $alarmButton = "Alarm On";
    $v0Val = 0;
} else {
    $alarmButton = "Alarm Off";
    $v0Val = 1;
}

if ($v4 == '["1"]') {
    $v4Button = "%100 Music Off";
    $v4Val = 0;
} else {
    $v4Button = "%100 Music On";
    $v4Val = 1;
}


?>

and this is the part of my html codes…

  <a href="http://blynk-cloud.com/<?php echo "$key"; ?>/update/V0?value=<?php echo "$v0Val"; ?>" class="btn btn-success btn-lg"><?php echo "$alarmButton"; ?></a>

This is the full code of my project. I will use this on my smart tv. It will be useful while doing something on tv. I am planning to add slider widget, lcd like widget and slider to adjust boilers temp.

<?php

$key = '539cac....................7f9c04';

$v0 = file_get_contents("http://blynk-cloud.com/$key/get/V0");
$temp = file_get_contents("http://blynk-cloud.com/$key/get/V2");

$v4 = file_get_contents("http://blynk-cloud.com/$key/get/V4");


if ($v0 == '["1"]') {
    $alarmButton = "Alarm Devrede";
    $v0Val = 0;
} else {
    $alarmButton = "Alarm Devre Dışı";
    $v0Val = 1;
}

if ($v4 == '["1"]') {
    $v4Button = "%100 Music Kapalı";
    $v4Val = 0;
} else {
    $v4Button = "%100 Music Açık";
    $v4Val = 1;
}


?>
<html>
<title> Ev Kontrol Sistemi </title>
<head>
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<center><br><hr>

<div class="container">
  <h2>Ev Kontrol</h2>
<br>

      <button type="button" onclick="v0func()" class="btn btn-success btn-lg"><?php echo "$alarmButton"; ?></button>
      
<hr>
    <button type="button" onclick="v4func()" class="btn btn-warning btn-lg"><?php echo "$v4Button"; ?></button>

  
  <p id="demo" ></p>

<hr> 
  
</div>


<script>
function v4func() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
      location.reload();
    }
  };
  xhttp.open("GET", "http://blynk-cloud.com/<?php echo "$key"; ?>/update/V4?value=<?php echo "$v4Val"; ?>", true);
  xhttp.send();
}

function v0func() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = this.responseText;
      location.reload();
    }
  };
  xhttp.open("GET", "http://blynk-cloud.com/<?php echo "$key"; ?>/update/V0?value=<?php echo "$v4Val"; ?>", true);
  xhttp.send();
}
</script>
2 Likes