Skip to content

Instantly share code, notes, and snippets.

@ghostbitmeta
Last active December 1, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ghostbitmeta/824474ceea327da629c7 to your computer and use it in GitHub Desktop.
Save ghostbitmeta/824474ceea327da629c7 to your computer and use it in GitHub Desktop.
Amazon Echo PHP file to control Honeywell Thermostat through a python script
<?php
//https://www.ourace.com/145-amazon-echo-alexa-with-php-hello-world
$EchoJArray = json_decode(file_get_contents('php://input'));
$RequestType = $EchoJArray->request->type;
$JsonOut = GetJsonMessageResponse($RequestType,$EchoJArray);
$size = strlen($JsonOut);
header('Content-Type: application/json');
header("Content-length: $size");
echo $JsonOut;
function GetJsonMessageResponse($RequestMessageType,$EchoJArray){
// Variable to confirm everything was successful;
$pass = True;
// ******************** Get action ********************;
$action = $EchoJArray->request->intent->slots->action->value;
if(empty($action)) {
$action = "cool";
}
// ******************** Get the temperature ********************;
$temp = $EchoJArray->request->intent->slots->temp->value;
if(empty($temp) and (($action == "cool") or ($action == "heat"))) {
$pass = False;
$text_response = "Error: Temperature is required but missing.";
$voice_response = "Error: Temperature is required but missing.";
}
// ******************** Get duration ********************;
$duration = $EchoJArray->request->intent->slots->duration->value;
if(empty($duration)) {
$duration = "1";
}
// ******************** Execute ********************;
if($pass){
$cmd = "/usr/bin/python /volume1/home_automation/therm.py";
switch ($action) {
case 'cancel':
$output=shell_exec("$cmd -x");
break;
case 'status':
$status = shell_exec("$cmd -s");
break;
case 'settings':
$settings = shell_exec("$cmd -s");
break;
case 'cool':
$output=shell_exec("$cmd -c $temp -t $duration");
break;
case 'heat':
$output=shell_exec("$cmd -h $temp -t $duration");
break;
}
}
// ******************** Responses ********************;
if ($pass and $action=="cancel") {
$text_title = "Canceling Thermostat Setting";
$text_response = "Canceled previous setting. Returning to schedule.";
$voice_response = "Canceled previous setting. Returning to schedule.";
$voice_reprompt = "";
} else if ($pass and $status) {
$thermo_data = explode("\n",$status);
// Array 0 is indoor temp
preg_match("/.*\s*:\s*(\S+)\./", $thermo_data[0], $indoor_temp[1]);
// Array 1 is indoor humidity
preg_match("/.*\s*:\s*(\S+)\./", $thermo_data[1], $indoor_hum[1]);
$text_title = "Thermostat settings";
$text_response = "The indoor temperature is {$indoor_temp[1][1]}F with {$indoor_hum[1][1]}% humidity.";
$voice_response = "The indoor temperature is {$indoor_temp[1][1]} degrees fahrenheit with {$indoor_hum[1][1]} percent humidity.";
$voice_reprompt = "";
} else if ($pass and $settings) {
$thermo_data = explode("\n",$settings);
// Array 2 is set cool
preg_match("/.*\s*:\s*(\S+)./", $thermo_data[2], $set_cool[1]);
// Array 3 is set heat
preg_match("/.*\s*:\s*(\S+)./", $thermo_data[3], $set_heat[1]);
$text_title = "Thermostat status";
$text_response = "The temperature is set for {$set_cool[1][1]}F.";
$voice_response = "The temperature is set for {$set_cool[1][1]} degrees fahrenheit.";
$voice_reprompt = "";
} else if($pass) {
$text_title = "Setting Thermostat";
$text_response = "Setting the temperature to $temp F for $duration hour(s).";
$voice_response = "Setting the temperature to $temp degrees fahrenheit for $duration hour.";
$voice_reprompt = "";
}
// ******************** JSON reply built below ********************;
$RequestId = $EchoJArray->request->requestId;
$ReturnValue = "";
if( $RequestMessageType == "LaunchRequest" ){
$ReturnValue= '
{
"version": "1.0",
"sessionAttributes": {
"countActionList": {
"read": true,
"category": true,
"currentTask": "none",
"currentStep": 0
}
},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Done."
}
},
"card": {
"type": "Simple",
"title": "' . $text_title . '",
"content": "' . $text_response . '"
}
}';
}
if( $RequestMessageType == "SessionEndedRequest" )
{
$ReturnValue = '{
"type": "SessionEndedRequest",
"requestId": "$RequestId",
"timestamp": "' . date("c") . '",
"reason": "USER_INITIATED "
}
';
}
if( $RequestMessageType == "IntentRequest" ){
$ReturnValue= '
{
"version": "1.0",
"sessionAttributes": {
"countActionList": {
"read": true,
"category": true,
"currentTask": "none",
"currentStep": 0
}
},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "' . $voice_response . '"
},
"card": {
"type": "Simple",
"title": "' . $text_title . '",
"content": "' . $text_response . '"
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "' . $voice_reprompt . '"
}
},
"shouldEndSession": true
}
}';
}
return $ReturnValue;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment