Press "Enter" to skip to content

Particle Photon in Openhab with Node-Red

bist 0

Tinkering with a particle photon is kinda cool. Wanting to integrate this in Openhab for a long time. I’m not very familiar with any of these technologies. This is my attempt of getting the photon integrated in openhab.

First of all I’m going to assume that you have openhab setup and u have used openhabian-config or something similar to install Node-Red. There are plenty of tutorials out there that describe how to do that.

The components we will be using are:

  • Raspberry pi with openhabian
  • Particle Photon with the old relay shield

The first thing we are going to do is install the particle libs to node-red.

Do this by navigating to Node-Red and click on Manage palette

Click on the install tab and search for particle and install

Now let’s move over to the Web IDE for your particle Photon

This is the code that needs to be on your photon. I have a 4xrelay shield so I need to control these. You should adjust this to your board. But it all comes down to changing the correct output to high or low.

void setup() {
  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  Particle.function("relay1", relay1);
  Particle.function("relay2", relay2);
  Particle.function("relay3", relay3);
  Particle.function("relay4", relay4);
  
  }

void loop() {
  }

int relay1(String cmd) {
  bool ledState = atoi(cmd);
  digitalWrite(D0, ledState);
}

int relay2(String cmd) {
  bool ledState = atoi(cmd);
  digitalWrite(D1, ledState);
}

int relay3(String cmd) {
  bool ledState = atoi(cmd);
  digitalWrite(D2, ledState);
}

int relay4(String cmd) {
  bool ledState = atoi(cmd);
  digitalWrite(D3, ledState);
}

Next move to your openhab installation and create a new items file. I use items files because they are clear and simple. You could also do this from the PaperUI but I prefer an items. What we are doing to do in the items file is create button to control each relay later on. The next step would be to add the items to a sitemap.

I use Microsoft Visual Code to write items files but any editor will work. Below is my example of the items file.

Switch  nodeswitch1 	"nodeswitch1"
Switch  nodeswitch2 	"nodeswitch2"
Switch  nodeswitch3 	"nodeswitch3"
Switch  nodeswitch4 	"nodeswitch4"

Since we are working in Openhab at the moment. Let’s create the sitemap entries so we have beatifull switches to use. Again use your favorite editor to edit or create a sitemap file. My entries are below. This is not my full sitemap file. This is the part that shows the switches.

Frame label="Node Red Test"{
		
        Switch      item=nodeswitch1
        Switch      item=nodeswitch2
        Switch      item=nodeswitch3
        Switch      item=nodeswitch4
            
        
        
        
	}

Now that we have configured openhab we are going to setup the Node-Red workflow. So open up the Node-Red interface. On the left side when you scroll down you will see openhab nodes and Particle nodes which we will use. First choose the openhab2-in node and drag it on the screen. Do this 4 times, One for each switch/relay. Because the actual state of the openhab-in node is OFF or ON we cannot use the output directly to control the particle function. In other words we need to translate the OFF and ON command to 1 and 0. There might be better way to do this but I used a change node found on the left. The rule I have put in the change node can be seen below. You will also need 4 of these.

Next we are going to use the Particle Function node. Again 4 of then are needed. To be able to use these functions we need to connect to the particle cloud with the access token. You can find this token in de Particle Web IDE.

Copy this key and in Node-Red double click on the particle function. Next create a new connection to the particle cloud by clicking on the pencil button

Add your secret access key and click update. The function is now connected to the particle cloud

In the node properties add the function your named in the code on the particle photon. Have a look at the code again and you will see this is called relay1 on MyDevice. You can of course name it whatever you want as long as it is consistent in the code and in the node-red function.

Now let’s add the Openhab2-in node also 4 times and edit each one by double clicking. The first node you are going to add you need to configure the connection to openhab. This is very simple. Just as we did to connect to the particle cloud click on the pencil sign and add the connection to you openhab installation. If you installed node-red on the same machine via the openhabian configuration tool this is what the config needs to look like

When this connection is succesful you will now be able to select the Openhab Item switch ItemName we created earlier on

Again this step you need to do 4 times. One for each relay you might have.

When all is configured the only thing left is to connect the dots. Link the nodes together. You should end up with something like this.

Visit your sitemap in Openhab and you should see 4 switches which you can operate. If you flip the switch the corresponding relay should turn on or off.

Hope this works for you. I was always reluctant to use Node-Red because I had no clue what it could do. After using this for the first time I believe I will start using this more for complex tasks.

If you have tried this let me know how it went.

Leave a Reply

Your email address will not be published. Required fields are marked *