<?php

require('phpMQTT.php');
// echo $data =$_POST["action1"];
echo var_dump($_POST);

$server = '149.129.244.110';     // change if necessary
$port = 1883;                     // change if necessary
$username = '';                   // set your username
$password = '';                   // set your password222
$client_id = 'opt'; // make sure this is unique for connecting to sever - you could use uniqid()
$topic = 'opt'; // make sure this is unique for connecting to sever - you could use uniqid()
$topic1 = 'opt1';
$topic2 = 'opt2';


// $message1=$_POST["action1"];

foreach ($_POST as $message => $value) {


if($value == "sw1"){
// $message (isset($_POST["action1"])? $_POST["action1"]:"");
//if(empty( $message )){$message = 'test';}
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish($topic,$message, 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}
echo "topic = ".$topic."1,mengirim  = ".$message."=>".$value;
}


if($value == "sw0"){
// $message (isset($_POST["action1"])? $_POST["action1"]:"");
//if(empty( $message )){$message = 'test';}
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish($topic,$message, 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}
echo "topic = ".$topic."0,mengirim  = ".$message."=>".$value;
}



if($value == "sw2"){
// $message (isset($_POST["action1"])? $_POST["action1"]:"");
//if(empty( $message )){$message = 'test';}
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish($topic,$message, 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}
echo "topic = ".$topic.",2mengirim  = ".$message."=>".$value;
}

// $message3 = isset($_POST["action3"])?$_POST["action3"]:"";
if($value == "sw3"){
// $message (isset($_POST["action1"])? $_POST["action1"]:"");
//if(empty( $message )){$message = 'test';}
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish($topic,$message, 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}
echo "topic = ".$topic.",mengirim  = ".$message."=>".$value;
}

if($message == "item_id"){

$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);

if ($mqtt->connect(true, NULL, $username, $password)) {
	$mqtt->publish("opt1",$value, 0, false);
	$mqtt->close();
} else {
    echo "Time out!\n";
}
echo "topic anmalog = ".$topic1.",mengirim  = ".$message."=>".$value;
}


}




















// if($_GET){

// // Set the destination URL (can be absolute or relative)
// $newURL = "kendali.php";

// // Redirect to the new URL
// header("Location: " . $newURL);

// // Stop further script execution
// exit;
// }