Search code examples
wordpresshttpgetnetwork-programmingnode-red

Control Node-Red with button on wordpress, HTTP Get


So, Im working on something, and I need to basically control node red with a button on my wordpress site. Did a little research and it looks like http is the way to go. Problem is, I know nothing about it. So I managed to send a value to node red by putting the following into a browser-

localhost:1880/test/?value=1

Here is the flow-

[{"id":"a247c8e.caffb38","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"93b2ec9.0acfa1","type":"debug","z":"a247c8e.caffb38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":470,"y":160,"wires":[]},{"id":"a3129f89.1dfe3","type":"http in","z":"a247c8e.caffb38","name":"","url":"test","method":"get","upload":false,"swaggerDoc":"","x":160,"y":160,"wires":[["93b2ec9.0acfa1"]]}]

But now Im stuck. 2 questions-

1- If I just put that link in a button on the site, it will work, but it will try to go to that site and then give an error. I dont want to go to that site, I want to stay on the site where the button is. Do I need to send some type of http response to say "Got it, now stay on the same site" or something? How?

2- How do I make this work from another network? Do I need to do something like-

publicIP/localIP/port/test/?value=1

Something like that?

Thanks so much for the help!


Solution

    1. You will need to make the request using Javascript from the Wordpress page. You can attach a onclick listener to the button and then use a XMLHttpRequest to trigger the listener. There are plenty examples of using XMLHttpRequest online.

    2. Assuming you are running Node-RED on a device inside your home network then you will need to set up some form port forwarding on your router to expose the Node-RED instance to the outside. Before doing this you MUST enable the Admin Authentication which will require a username/password to access the Node-RED editor. Without this you run the risk of hackers gaining access and installing a cryto miner.

      You will also need either a static IP address or a DynamicDNS solution if you want this to work for anybody on the internet.