I am completely new to this so excuse my ignorance of all this. I have a 2 channel relay board no brand that I know of that I am trying to hook to a Windows 7 computer (although I have a linux system too if it is not possible on Windows.) using openhab to control it. I need my light to come on in the evening and turn off 12 hours later. Okay. So I have openhab and its designer installed and I have accessed my demo files and even edited the sitemap correctly as all the start here places say but I am having a really hard time with connecting the program to the relay. I tried doing the UCProjects.eu relay board binding but I am lost on that page. I uncommented the part in the openhab.cfg file as it sad but I am lost at items and rules and scripts. I understand you have to create new ones you need to write the commands in the text files. I understand that in order to get them ‘connected’, I need to have rules, scripts, items, etc. telling it what to do. But I am unsure of what to write in them to get the relay board to 1 recognize or show in openhab and 2 get it to function. I am not asking for you to do it for me but if someone could get me at the very least in the right direction perhaps? I would be eternally grateful. My husband used to work alot with linux and knew a lot but since his head injuries (as well as much time passing) he has a hard time remembering commands etc. So I am googling everything to try and figure this out for him. I do have basic linux knowledge and like I said some openhab knowledge just not nearly enough. Thank you!!
Lets step back a sec.
How have you/do you plan on wiring the relay to a computer? The relay by itself cannot talk on its own. It just has three wires, power, signal, and ground. When the relay gets 3.5v or 5v on the signal wire it switches to allow the power to go to the output. Otherwise it blocks the power to the output.
So you need to connect these wires to something the computer can talk to. Common approaches are to use a micro controller like an Arduino with a wifi shield or ESP8266 or to use a board computer with GPIO pins like a Raspberry Pi.
These controllers are directly wired to the relay and run software that allow them to communicate with openHAB. This communication may be MQTT messaging or making HTTP calls into openHAB’s API.
The following is how I set up my garage door openers which work on relay so it should be a good example.
I have openHAB running on an old laptop in my office on an Ubuntu 16 OS.
On this same machine is the MQTT broker which is a central program that allows clients (i.e. openHAB and the remove device I’m about to talk about) to exchange messages with each other. In openHAB I use the MQTT Binding and I use Mosquitto as the broker.
In the garage I have a Raspberry Pi 3. Normally this would be overkill but they are small enough and cheap enough to not be that big of a deal if I’m using a way overpowered computer to flip some relays. Besides, I’ve also connected a camera and some other stuff to it. This Raspberry Pi is wired through the GPIO pins to a dual relay and a couple of magnetic door sensors.
The Garage Pi is on wifi so it has network access to the OH server.
I’ve written a Python script that reads and controls the GPIO pins. It receives commands to toggle a pin from MQTT to control the relay and it reports changes in the state of the sensors using MQTT.
In openHAB I have two Switch Items (two garage doors so two relays) bound to the MQTT binding. These Switches publish ON to the indicated MQTT topic when toggled on. Since I’m simulating a button press instead of controlling something like a light, I don’t have to send a message for OFF. But the script is written to support both (though there is a bug I need to fix on this).
The flow of messaging is as follows:
- user presses the “Open” button on the sitemap
- “ON” gets published to the garage door opener’s MQTT topic
- the script on the raspberry pi receives the message and sends the configured GPIO pin to HIGH for half a second then sends it to low
- the relay allows current to pass for half a second then stops it.
The above approach would work with ESP8266 boards as these are basically just an Arduino with built in wifi. There are libraries that will allow these devices to talk via MQTT. So only step 3 would be different in the steps above.
I hope this helps and if not please come back with more questions.
@rlkoshak nailed it on the head. Personally, if it were me and I was set on using the relay board, then I would get an ESP12 (Amazon) and send MQTT data to and from it (commands and status) like Rick mentioned. This is all if you are talking about something like this.
There are other options though and it depends on what else you plan on integrating with OH. If nothing, it may be easier to build a standalone Arduino with a light sensor (I’m assuming you want this on all night when it’s dark). There are plenty of easier solutions though if you are looking to have an extensive system because many times there is an initial cost to start down certain roads. For instance, ZWave requires a controller (USB stick) that costs about $45US. Then you could add something like… this outdoor outlet. ZWave does have range limitations so it depends on how far away that will be from your controller. The cool thing is that each ZWave item adds to the network so if you used one in between, you could ‘extend’ it.
There are also WiFi controllers that can do the same but I don’t have any examples of an outdoor one off hand.
Thank you so much for your replies. Very helpful.