-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelcloud-query.php
72 lines (60 loc) · 2.62 KB
/
melcloud-query.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// Retrieve device data from MELCloud (Mitsubishi electric A/C Cloud) for export to cacti
// Note that you must have connected your pump to the cloud. You should be able to query the
// device from the cloud before using this.
// https://github.com/AmahaShadow
// based on the work of http://mgeek.fr/blog/un-peu-de-reverse-engineering-sur-melcloud
// who originally reversed engineer the API (french)
// this only returns the Actual Temp, Set Temp, Actual Fan Speed, and Energy Consumption
// which were what I wanted to check, but there are a lot more data available.
//config
$mail='[email protected]';
$pass='password';
//for debugging purpose, uncomment the line at the end if you need it
$log='';
// First, we need to login to the service and get the contextKey
$url='https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin';
$loginData=array(
'AppVersion' => '1.9.3.0',
'Language' => '7',
'CaptchaChallenge' => '',
'CaptchaResponse' => '',
'Persist' => 'true',
'Email' => $mail,
'Password' => $pass
);
$c = curl_init($url);
$encodedData = json_encode($loginData);
curl_setopt($c, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $encodedData);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($c);
$log.="====== AUTH CALL =======\n\n".$result."\n\n\n";
curl_close($c);
$res=json_decode($result,true);
$cKey=$res['LoginData']['ContextKey'];
if (!$cKey)
die("Error during auth, no ContextKey returned\n\n".$result);
// now we get the data.
$url='https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices';
$c = curl_init($url);
$encodedData = json_encode($loginData);
curl_setopt($c, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-MitsContextKey:'.$cKey));
$result = curl_exec($c);
$log.="====== DEVICE QUERY =======\n\n".$result."\n\n\n";
curl_close($c);
//data decoding
if (substr($result,0,1)=='[')
$result=substr($result,1,-1);
$res=json_decode($result,true);
if (!$res['ID'])
die("Error during data retrieval, format change ?\n\n".$result);
//The following assumes only one device, but the call actually returns everything, so you'll only need to iterate the devices to get the rest
$dev=$res['Structure']['Devices'][0]['Device'];
//Print the result
echo "temp:".$dev['RoomTemperature']." reqtemp:".$dev['SetTemperature']." fanspeed:".$dev['ActualFanSpeed']." energy:".$dev['CurrentEnergyConsumed'];
//If this line is uncommented, it will store all call results info into a text file
//file_put_contents('melcloud_last_transact.txt',$log);
?>