Connect to ADAX Neo heater API

Hello,
I try to connect to a ADAX Neo heater with NodeRed. I have a working code in python:
(snippet)

     url = 'https://' + HEATER_IP + '/api'
     payload = { 'command': 'set_target', 'time': int(time.time()), 'value': target_temperature }
     headers = { 'Authorization': 'Basic ' + HEATER_TOKEN }
     return requests.get(url, params = payload, headers = headers, timeout = 15, verify = False)

that I try to recreate with a function and httprequest-node in NodeRed.
This is what i have in the function-node:

 var d = Math.floor(new Date().getTime() / 1000);
 var token = 12345678;
 
 msg.url = "https://172.16.x.y/api";
 msg.rejectUnauthorized = false;
 
 msg.headers = { 'Authorization': 'Basic ' + token };
 //msg.headers = {}; 
 //msg.headers['Authorization'] = 'Basic 12345678';
 
 msg.timeout = 15;
 
 msg.payload = {
    "command":"set_target",
    "time": d,
    "value": "1800"
 };
 
 return msg;

But I cant get the heater to respond other than 404, and that I read it as authentification error.
How can I duplicate the function from python to nodered?
Any help is appreciated!

Regards
HĆ„kan

PS.
File upload was disabled for me (new users), so I paste in my full nodered code here:

[{"id":"89fb703cf6594aac","type":"http request","z":"e961ad82.557ed","name":"","method":"GET","ret":"obj","paytoqs":"body","url":"https://172.16.6.2/api","tls":"604655aceaad0408","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"basic","senderr":false,"headers":[],"x":510,"y":860,"wires":[["59eaed8789db5ee7"]]},{"id":"59eaed8789db5ee7","type":"debug","z":"e961ad82.557ed","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":860,"wires":[]},{"id":"7b7a69fa8e099eed","type":"inject","z":"e961ad82.557ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":860,"wires":[["0a03c04733728ae0"]]},{"id":"0a03c04733728ae0","type":"function","z":"e961ad82.557ed","name":"function 1","func":"var d = Math.floor(new Date().getTime() / 1000);\nvar token = 12345678;\n\nmsg.url = \"https://172.16.6.2/api\";\nmsg.rejectUnauthorized = false;\n\nmsg.headers = { 'Authorization': 'Basic' + token };\n//msg.headers = {}; \n//msg.headers['Authorization'] = 'Basic 12345678';\n\nmsg.timeout = 15;\n\nmsg.payload = {\n   \"command\":\"set_target\",\n   \"time\": d,\n   \"value\": \"1800\"\n};\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":860,"wires":[["89fb703cf6594aac","8b2d9ea2bbaf71ce"]]},{"id":"8b2d9ea2bbaf71ce","type":"debug","z":"e961ad82.557ed","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":940,"wires":[]},{"id":"604655aceaad0408","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false,"alpnprotocol":""}]

ADMIN EDIT: Wrapped flow JSON in triple backticks

404 Not Found - HTTP | MDN

also is the basic api key in base64 encoded

Finally you flow.json is corrupt as you have not posted it correctly

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

[edit]
you can try this example If it does not work try setting the method to POST to.

[{"id":"7b7a69fa8e099eed","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":800,"y":1740,"wires":[["0a03c04733728ae0"]]},{"id":"0a03c04733728ae0","type":"function","z":"d1395164b4eec73e","name":"function 1","func":"var d = Math.floor(new Date().getTime() / 1000);\n var token = 12345678;\n \n msg.url = \"https://172.16.6.2/api\";\n msg.rejectUnauthorized = false;\n \n msg.headers = { 'Authorization': 'Basic ' + token };\n //msg.headers = {}; \n //msg.headers['Authorization'] = 'Basic 12345678';\n \n msg.timeout = 15;\n \n msg.payload = {\n    \"command\":\"set_target\",\n    \"time\": d,\n    \"value\": \"1800\"\n };\n \n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":1740,"wires":[["89fb703cf6594aac","8b2d9ea2bbaf71ce"]]},{"id":"89fb703cf6594aac","type":"http request","z":"d1395164b4eec73e","name":"","method":"GET","ret":"obj","paytoqs":"body","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":1210,"y":1740,"wires":[["59eaed8789db5ee7"]]},{"id":"8b2d9ea2bbaf71ce","type":"debug","z":"d1395164b4eec73e","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":1820,"wires":[]},{"id":"59eaed8789db5ee7","type":"debug","z":"d1395164b4eec73e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1350,"y":1740,"wires":[]}]

Thank you for your reply!
Maybe I'm stupid but I can't find the pencil icon to edit my post, and my editor (on this web page) doesn't have the icon to wrap in code either, you have to do that beforehand with "Ģ Ģ Ģ"...

To the topic:
"Token" is actually "12345678" in this case, no "base64" encoding or username should be entered in the Python code (where the communication with the heater works).

What I'm trying to do is recreate the python code:
requests.get(url, params=payload, headers=headers, timeout=15, verify = False)
with the nodered function-block through:
python ==> nodered
url ==> msg.url
payload ==> msg.payload
headers ==> msg.headers
verify=False ==> msg.rejectUnauthorized = false
timeout=15 ==> msg.timeout=15

But this only results in me getting 404 (405 when POST is used) back from the server in the heater when I use nodered, while pyhton variant works and gives back a JSON encoded string with data.

I've also tested your version of my code (also with POST) without any success.

Regards HĆ„kan

404 is a standard "not found" HTTP status code

Can node-red communicate with the IP you are using? For example, if Node-RED is inside a docker container is it configured to communicate with 172.16.x.y? Try installing node-red-node-ping and see if your Node-RED can actually connect to 172.16.x.y (assuming the device responds to ping)

Looking ay your python 'value': target_temperature it is unclear if target_temperature is a string or number. Try setting "value" as a number e.g:

 msg.payload = {
    "command":"set_target",
    "time": d,
    "value": 1800
 };

Hello again!

Thanks for your replys!

I solved it by debugging against netcat.

I change my function-node to:

var d = Math.floor(new Date().getTime() / 1000);
var token = '12345678';

msg = {};
msg.url = "https://172.16.6.2/api?command=set_target&time=" + d + "&value=1700";
msg.rejectUnauthorized = false;

msg.headers = { 'Authorization': 'Basic ' + token ,
                'Accept-Encoding': 'gzip, deflate',
                'Accept': '*/*',
                'Connection': 'keep-alive'
                };

msg.timeout = 15;

return msg;

and now it worked!

Regards
HĆ„kan

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.