LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading the API from Energenie/MiHome products

Hi All.

Bit of a newbie here, needing some help!

 

I have been playing around with HTTP calls with labview and getting back Header and Body data ok on some IOT devices.

 

Using the HTTP pallet in Labview I am trying to connect to my MiHome products via their exposed API.

https://mihome4u.co.uk/docs/api-documentation/subdevices-api/list-all-subdevices

 

Using Postman - I do the Following GET command...

Get: https://mihome4u.co.uk/api/v1/subdevices/list

 

with Basic AUTH with my username and password. This returns the expected json....

 

Trying to do a similar thing in Labview i get a body message of "Access denied"

{"status":"access-denied","time":null,"flags":{},"data":{"message":"Access to subdevices/list is not permitted."}}

 

The Header comes back ok:

HTTP/1.1 200 OK
Date: Wed, 21 Nov 2018 12:17:08 GMT
Content-Type: application/json
Content-Length: 114
Connection: keep-alive
Set-Cookie: __cfduid=d99f2a227e93c2577fe894128d74f8c581542802628; expires=Thu, 21-Nov-19 12:17:08 GMT; path=/; domain=.mihome4u.co.uk; HttpOnly
Status: 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
ETag: W/"82762f798be137d965924687297f1780"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: b29640f8-7cb8-44e7-b2da-d5a7ced5d5cd
X-Runtime: 0.001099
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 47d3176c78243446-LHR

 

Now - I think its related to the Auth not being set up to allow access..... I've seen a few examples but don't have the knowledge to get around it..... (No examples for MiHome by the way)

Code snippet attached..... just a pile of HTTP VI's trying to get a response!

 

Please help!!

Cheers 

James

 

0 Kudos
Message 1 of 14
(4,863 Views)

An Example Curl script that their support team emailed me was this....:

Fetch Usage Data:
Curl –L –X POST –H "X-Auth-Username:myaccount@hotmail.com" –H "X-Auth-Password:mypassword" –H "Content-Type: application/json" –d ’{"id" :49819, "start_time": "2017-02-16 12:34", "end_time":"2017-02-28", "data_type":"reported_temperature", "resolution":"daily"}’ https://mihome4u.co.uk/api/v1/subdevices/get_data 

 

I however (to Start with) just want to be able to get back some Body Data with no Action.

0 Kudos
Message 2 of 14
(4,862 Views)

I got it working(*) by changing the header syntax using hints from this forum thread:

LabVIEW_2018-11-23_09-50-41.png

(I attached this code in both LabVIEW 2018 and 2015 format).

 

*: I connected to my private server which has a Basic Auth section. It did not work with your original code, but works with mine. Let us know if this code works with your server/API as well!

 

Edit: Base64Encode.vi is found in C:\Program Files (x86)\National Instruments\LabVIEW 201x\vi.lib\httpClient\


Ingo – LabVIEW 2013, 2014, 2015, 2016, 2017, 2018, NXG 2.0, 2.1, 3.0
CLADMSD
Download All
0 Kudos
Message 3 of 14
(4,809 Views)

Ikaiser,

 

Thanks so much for your reply, and the code.

I've just tried this and unfortunately I get the same errors in the body. (Snippet attached)

 

I wonder why this is not working, it still seems like its an Auth issue...

 

James  

0 Kudos
Message 4 of 14
(4,800 Views)

Hey James,

 

I executed my Stigwood_LV18.vi as attached above and got this result:
LabVIEW_2018-12-10_15-47-02.png
Note the different status in the reply's body.

 

This will be very tricky to troubleshoot for everyone on the forum, as this might be related to something happening after the actual authentication. Have you tried logging in with wrong username and password? Or, in other words: What result do you see when you execute my VI without changing anything? I hope you get exactly the same message as I do.

Afterwards, when you type in your actual username and password, what do you see then?

 

And, lastly: Do you maybe have guest credentials that you could share, either publicly, or only with me via PM?


Ingo – LabVIEW 2013, 2014, 2015, 2016, 2017, 2018, NXG 2.0, 2.1, 3.0
CLADMSD
0 Kudos
Message 5 of 14
(4,756 Views)

Thanks again,

 

So - yes same Body as you if I try with the incorrect credentials. I've just tried again and I get this;

Capture1.PNG

0 Kudos
Message 6 of 14
(4,743 Views)

So - the Access is good....

 

Maybe the way the URL message is formatted is not right. As in the Get command. It seems close......

0 Kudos
Message 7 of 14
(4,742 Views)

Ok...

 

I've changed the API its calling and its actually worked!!

just running some tests, but now i need to understand how to pass some parameters to read back the status of an ID.

 

Thanks so much!! Could you help me with the last bit as well..... 

0 Kudos
Message 8 of 14
(4,739 Views)

Hey,

good to read you had progress.

 

With "last bit", do you mean "how to pass some parameters to read back the status of an ID"? What I would do in such a case is play around with the Webinterface and check, which URLs are used there. Modern browsers have a built-in inspector (press F12 in Firefox or Chrome to open it). This should be enough needed to see all URL or POST parameters they use.

 

Deducing from the API documentation, I'd try using a url similar to this one first: /api/v1/subdevices/power_on?id=1&socket=0.

This is to switch a subdevice's power socket on/off, change it accordingly to something you want to read from.


Ingo – LabVIEW 2013, 2014, 2015, 2016, 2017, 2018, NXG 2.0, 2.1, 3.0
CLADMSD
0 Kudos
Message 9 of 14
(4,729 Views)

Yeah!

 

So I've tried playing around with this;

 

https://mihome4u.co.uk/docs/api-documentation/triggers-api/list-alert-triggers-for-a-subdevice

 

on this web page you need to add the Subdevice ID,

Looking at the parameters section here, i am not sure how my string should look....

 

https://mihome4u.co.uk/docs/api-documentation

 

I looked on the web and couldn't find the right section under F12 to show me the HTMl code used...

I'll try your example first

0 Kudos
Message 10 of 14
(4,725 Views)