D-Link Forums

D-Link Connected Home => DSP-W215 => Topic started by: pzelenka on December 24, 2015, 06:37:00 AM

Title: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on December 24, 2015, 06:37:00 AM
Hi,

since I want to directly receive measured values from the smart plug I found this:
On the address http://<ip> you can login using PIN code. Then you are provided with basic device info.
On the address http://<ip>/HNAP1 there is a list of actions that you query the device.
After some days of playing with the HNAP I finally managed to retrieve measured values from the plug!
If anyone is intrested in I can provide more detailed info.

Pavel
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: ark0n3 on January 04, 2016, 07:53:35 AM
I'm interested! I would like to connect my nas (synology) to the smartplug in order to plot its values and keep yearly statistics.

thanks
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 04, 2016, 08:41:00 AM
Coincidentally I applied the same approach. You can download the source code (JavaScript + Node.js) here: https://github.com/bikerp/dsp-w215-hnap
From Synology packages install Node.js
copy the app to desired folder
connect to Synology using Putty or similar app
In order to run the application as a background process I use NodeJS module "forever"

Cheers
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: ark0n3 on January 12, 2016, 02:37:00 AM
Coincidentally I applied the same approach. You can download the source code (JavaScript + Node.js) here: https://github.com/bikerp/dsp-w215-hnap
From Synology packages install Node.js
copy the app to desired folder
connect to Synology using Putty or similar app
In order to run the application as a background process I use NodeJS module "forever"

Cheers
thanks a lot, I'll try it asap!
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: tombevers on January 13, 2016, 01:37:35 PM
Hi,

I'm also interested to read values from the plug,
and even better: control the plug with http commands.

Thanks in advance for tips and advise.

Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 13, 2016, 01:42:01 PM
Hi,
it is possible to fully control the device and read measured values. Check my github project here: https://github.com/bikerp/dsp-w215-hnap
You can find there basic info how to control the device and code written in JavaScript
I hope it will help you

Pavel

Title: Re: DSP-W215 - read values via HNAP protocol
Post by: tombevers on January 14, 2016, 06:44:46 AM
Hi,

I am not familiar with javascript, but I have notice of programing.

Do i have to compile the javascript?
How do I load the script into the plug? with putty software?

thanks a lot!

Tom
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 14, 2016, 12:07:22 PM
The script is not supposed to run in the plug. It's a client that communicates with the plug via HTTP protocol. You have to run it on some computer.
As a runtime environment is used NodeJs. Steps how to run the app are described on wiki: https://github.com/bikerp/dsp-w215-hnap/wiki/How-to
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: tombevers on January 15, 2016, 07:40:16 AM
OK,

in meanwhile I installed the node JS on my synology DS213, and uploaded the files from github.

Can you give me some more hints for controlling the smartplug by means of a http command to my diskstation?

as you can read... node js and this kind of software is new for me...

Thanks a lot!
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 15, 2016, 01:01:03 PM
Download Putty which is a SSH client which provides terminal connection to your Synology diskstation.
Modify app.js and put PIC CODE to LOGIN_PWD and update IP address of the plug in HNAP_URL.
Connect to diskstation using Putty. Navigate to folder where you download the application (e.g. cd /volume1/home/hnap)
run the application: node app.js
once the application starts it connects to the plug, turn it on and every 60 seconds reads power consumption and temperature and saves those values into result.txt file.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Angakok on January 19, 2016, 08:10:56 AM
Hi Pavel,

i've tried to use your documentation here: https://github.com/bikerp/dsp-w215-hnap/wiki/Authentication-process (https://github.com/bikerp/dsp-w215-hnap/wiki/Authentication-process) to proceed authentication on the plug, either with curl, either in python (with a soap client), but eachtime the server respond with the list of available soap action.

Could you explain me how exactly do you manage authentication ?
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 19, 2016, 10:22:45 AM
Hi,

you have to send the requests via POST method. Keep in mind that the authentication process has two steps. In the first step you receive  Cookie,
Challenge and PublicKey values that are used to construct the second request. If you follow principle described on wiki than it should work. Be carefull that this was tested on firmware version 2.20

Pavel
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Angakok on January 20, 2016, 12:38:02 AM
Hi Pavel,

my request is a POST request but i don't receive a cookie ... the response is a GetDeviceSettingsResponse and not a LoginResponse.
I've probably missed something ...

my request :
 curl --header "Content-Type: text/xml; charset=utf-8" --header "SOAPAction: http://purenetworks.com/HNAP1/Login" --data @auth.xml http://192.168.60.9/HNAP1
With the input message in the auth.xml file.

Regards.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 20, 2016, 02:02:36 AM
Do you also send the proper body content?
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Angakok on January 20, 2016, 03:41:57 AM
here is the auth.xml file :
Code: [Select]
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Login xmlns="http://purenetworks.com/HNAP1/">
      <Action>request</Action>
      <Username>admin</Username>
      <LoginPassword/>
      <Captcha/>
    </Login>
  </soap:Body>
</soap:Envelope>
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 20, 2016, 02:39:47 PM
It seems ok. What firmware version do you have? Maybe you culd run the script from github and see if it works. You only have to install nodejs.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: zidekmat on January 22, 2016, 04:41:05 PM
Hi Pavel, curl command is not working for me neither, but it doesn't matter since the Node app is. Awesome work! Thanks a lot.
I have a question thou. How did you figure out /Login endpoint and the mechanism? In CISCO's HNAP whitepaper it says it uses basic auth. If I query DEVICE_IP/HNAP it gives me WSDL without /Login action and I couldn't find it even in mobile app's source code... so that's really mystery for me.
The thing is I have more of D-Link devices like that and I'd like to extend your app for those too and I can't find WSDL describing response formats.
If you don't want to discuss it here, please contact me by email.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 23, 2016, 01:27:07 AM
I tried to put the IP address of the plug into the browser and login page appeared. The login page also contained some Javascripts with the login procedure logic. This was the starting point. I also tried wireshark. But it was quite useless for me because the plug sends data to Amazon WS via HTTS. I reccomend this video https://www.youtube.com/watch?v=rjry28Ym7kI&index=32&list=WL
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Kryptokrates on January 25, 2016, 12:32:09 AM
You can capture some traffic during the setup-phase. The socket creates an unencrypted network in which you'll see requests/response for Login, Get-/SetDeviceSettings(2) etc. while configuring the device with your Smartphone app.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on January 25, 2016, 01:11:42 AM
Yes I know however I was able to find out those steps without Wireshark. More interesting would be to find out how to get the data from Amazon WS
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Angakok on January 28, 2016, 07:02:51 AM
hi pzelenka,

i'm using the same firmware as you.
Originally i've tried to access the plug in python (because i've many pkug to manage and i prefer python to do that) but with the same result (i receive a GetDeviceSetting response) that's why i've tried to come back to basic with curl.
I will try your code soon in order to identify if it's an environnement issue or a coder issue :)

Angakok

Title: Re: DSP-W215 - read values via HNAP protocol
Post by: Angakok on February 20, 2016, 12:41:47 AM
Hi,

it works well with tour code, i think the problem is arround the SOAPAction ... curl expect a word and not a complete URL.

Regards,
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: infy on July 17, 2016, 01:38:51 PM
Hi Pzelenka,
thank you for sharing your js code. I'm interested to manage my plug from http request as have you made. If I use your app.js with node I can turn on the plug with >node app.js on  . If I run >node app.js off plug still on... what's wrong? My firmware is 2.22.
Thank you for any suggestion.

Now I've analyzed your app.js and I understand that execute only information about the plug. Now I've implemented an url request to my service that read the current temperautre in my cellar and swtich the plug on off. Thank you for your code. Great job!

Regards
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: garce on August 31, 2016, 11:57:10 AM
Hi, I want to buy the DSP-W215 solely to measure/monitor temperature and have a record over time (as granularly as possible but certainly every 10 minutes at least).
It sounds to me that I would be able to do this from earlier comments. I havent read the instructions in detail. I am planning to leverage a laptop PC that I have running at home to do this. I assume that I need to follow instructions in github.
1. I assume the result is having something like a text file with columns with the raw data?
2. Would there be a column for time and another one for Watts?
Thanks for your help.
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: pzelenka on August 31, 2016, 12:05:10 PM
Hi,
the sample code on github does exactly what you described. It polls data from the plug at specified interval and writes them into text file including the time.  If you want to run the code as background task you can utilize npm module "forever".
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: nhsqr on March 02, 2017, 06:16:47 AM
Hi Pavel,

my request is a POST request but i don't receive a cookie ... the response is a GetDeviceSettingsResponse and not a LoginResponse.
I've probably missed something ...

my request :
 curl --header "Content-Type: text/xml; charset=utf-8" --header "SOAPAction: http://purenetworks.com/HNAP1/Login" --data @auth.xml http://192.168.60.9/HNAP1
With the input message in the auth.xml file.

Regards.

Hello,
If anyone is still interested I figured out what is wrong with the curl request.
First I thought it was something more complex and I wasted a lot of time trying all bunch of stuff.
And finally after a tcpdump of SOAP request of nodejs program I noticed that http://purenetworks.com/HNAP1/Login should be inside quotes. So Simple! :)
So the request should be with escaped quotes like this:
curl --header "Content-Type: text/xml; charset=utf-8" --header "SOAPAction: \"http://purenetworks.com/HNAP1/Login\"" --data @auth.xml http://192.168.60.9/HNAP1
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: FurryNutz on March 02, 2017, 01:58:36 PM
Thanks for posting this information. Hope it's helpful for future users.
 ;)
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: nhsqr on March 04, 2017, 06:10:46 AM
I am now trying the next step which is to authenticate.
The authentication process uses HMAC-MD5.
Unfortunately I spent several days already trying to use openssl to generate md5 hash for login with no success.
I am using openssl like this:

$PrivateKey=`echo -n "$Challenge" | openssl dgst -md5 -hmac "$PublicKey" -binary | xxd -ps -u`
$Login=`echo -n "$Challenge" | openssl dgst -md5 -hmac "$PrivateKey" -binary | xxd -ps -u`

Where:
$Challenge is the return string from first step and also hex encoded with "xxd -ps -u"
$PublicKey is the return string from first step concatenated with the password and also hex encoded with "xxd -ps -u"

I did a tcpdump from node app.js from bikerp/github and with:
$Challenge="tzd1skQFcoArsdTU7D4I" #not hex encoded
$PublicKey="u2eNyVyvK22JlCTtvA2e" #also not hex encoded
$password="777260" #password for the DSP-W215

echo "$Login" should have given me: 2942D0DF1E77B62BB2DBD4A511C63169
but instead it gives me: 30FD3BDA28A864484AD9AEA3FBDE6811

I can't figure out what is the problem. I tried a lot of different ways and none of them worked.
If anyone has an idea, please let me know

EDIT: Disregard what I wrote :) $PublicKey and $Challenge should not be hax encoded and all is working :)

EDIT 2: I can't believe I figured it out! I'm gonna make bash script for the DSP-W215 API and I'm gonna post it on GitHub :)

EDIT 3: Here is the GitHub link: https://github.com/nhsqr/hnap-dsp-w215
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: ghainbach on June 06, 2017, 02:25:24 PM
Downloaded .sh file and changed the IP address and PIN, but getting the following SOAP error:

Grahams-MacBook-Pro:hnap-dsp-w215-master graham$ ./hnap-dsp-w215.sh --getpower
Login:
Timestamp=1496783442   SOAPAction=SOAPAction: "http://purenetworks.com/HNAP1/GetCurrentPowerConsumption"   AuthStr=1496783442"http://purenetworks.com/HNAP1/GetCurrentPowerConsumption"   AUTH=7165F989EAB0348EEBD2BE87EE3A79AC   HNAP=HNAP_AUTH: 7165F989EAB0348EEBD2BE87EE3A79AC 1496783442   RET = <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>401 Unauthorized</faultstring></soap:Fault></soap:Body></soap:Envelope>
Power:  W
Title: Re: DSP-W215 - read values via HNAP protocol
Post by: nhsqr on June 09, 2017, 03:28:31 AM
First sorry for the late reply.

It seems like either you don't have openssl installed or your PIN is wrong or your firmware version is not the latest. Also which hardware version of DSP-W215 do you have?