Introduction: Coffee Timer and Radio, Arduino, Logitech Harmony and OpenHAB
This instructable assumes you know what you're doing on the AC!
Also, make sure to use undamaged cables that are not too long, nor too short.
The following box is part of the home-automation project described here:
[instructable link will be displayed as soon as the project is finished]
The idea is to build a AC relay with an Arduino that switches on the coffee machine. At the same time the OH server switches on your favorite radio station. So in the morning you then can go into the kitchen and pour coffee and listen to your station. The reason for a timer simply is that my Espresso machine takes 30 minutes to heat up and be ready, and my radio station is a bit of a pain to set... even with a Harmony...
Material used
- 1x circuit case (11cm x 11cm x 5cm in my case), 7$
- 1x Arduino Mini Pro 3.3V, 1.75$
- 1x RFM69H (3.3V), 4.50$
- 1x step-down 5V->3.3V (to run Arduino and the RFM transmitter), 0.35$
- 2 or 3 press button (LED or not), 2.90$ each
- 1x 5V power supply (to run the step-down and the relays) or 1x USB in socket
- 2x 3 pin wiring connector, negligible $
- some resistors for the buttons (check the specs of the buttons), negligible $
- cables to connect everything; either connect with Duponts or solder... (AWG 26 for Arduino, AWG 16 for AC connections in my case), approx. 2$
- 1x AC relay, 1.75$
Unfortunately I cannot create a correct wiring diagram, nor do I know the correct electronic symbols, so all you get is a powerpoint... sorry.
Note: The next version of this timer will have the 5V power supply inside the box so to get rid of the external power supply. I wanted to try another approach... I should have stayed with the internal power supply. Also the coffee machine's power button could be changed and the whole Arduino, AC relay, RFM housed in the coffee machine itself since the base is big enough (the espresso machine is an Elektra Microcasa).
Step 1: Putting the Things Together
Make sure everything fits into the box and fix it with some hot glue here and there.
The AC cables are connected as they should on the relay.
The whole box is fixed to the wall with double sided tape.
Step 2: Arduino Code
Initialization
Only the WDT (WatchDogTimer) and the RFM libraries are included. The WDT is used in case the Arduino hangs somewhere and with the WDT it would reset by itself. However with this box, the Arduino never lost itself until now.
The code is attached and should be quite straight-forward. It is assumed that you know that stuff at least to half-way read it.
The Arduino code might look familiar as it has been copied from different sources, modified and put together.
Don't get too picky on the NodeID; keep it 2 digits since you probably won't have more than 90 nodes in your house. Don't change the number of digits, or you will need to change it practically everywhere later.
Define the frequency band as given by the module you bought. The ChannelFreq might need to be adjusted to the standards where you live; the number is in Hertz.
// RFM69
#define NODEID 21 //unique for each node on same network Floor&Room&Node#=X&XX&X
#define NETWORKID 666 //the same on all nodes that talk to each other
#define GATEWAYID 1
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "1234567890ABCDEF" //exactly the same 16 characters/bytes on all nodes!
#define ChannelFreq 434520000
The message structure has to be the same on your whole network
typedef struct { // Radio packet format
int nodeID; // node identifier
int devID; // device identifier
int cmd; // read or write long intVal; // integer payload
float fltVal; // floating payload
char payLoad[32]; // string payload
} Message;
The buttons were not directly attached to the 3.3V, but to the Arduino data pins so they could be switched on and off as wished.
Also there are many variables to store time stamps, status and previous-status of buttons.
Setup
Obviously the pin-modes are "out" for power and "in" for the button-press. The buttons blink to show that the setup phase finished, then they pass into their normal state.
Loop
First the Arduino checks if it received a command from the OH server. The command is parsed outside the loop-code.
Then the status of the buttons are read if the status changed since the last iteration. Note that nothing is sent until now.
Depending on the status of the buttons, the relay opens or closes to switch the coffee machine on or off.
After that the button colors change depending on their state. It made more sense to me to make it that way instead of changing them directly during the button-read-status.
Near to the end of the loop the status of the relay is sent back to the OH server as well as the status of the blue button. Red and green status are not sent back as they are controlled by the status of the relay
The signal strengths (RSSI) are sent back to the OH server in order to know the signal strength for both ways: from and to the RFM gateway.
Note that there is a buffer time "StateChangeInterv" in the code. This is written for the funny guys that come to your home to see after how many times on/off switching the fuse will blow, or the coffee machine go up into smoke... The variable (here set to 2.5 sec) prevents a change of status for 2.5 seconds. The machine can therefore switch only after that variable time; and people will lose interest very quickly...
The BlueBlink is initiated only by the OH server 30 minutes before the machine is supposed to switch off. By pressing the blue blinking button, it changes into a stale blue and the switch off time is supposed to move to the next 00 or 30 minutes past the hour. Of course 30 minutes before switching off, the blinking will return.
Attachments
Step 3: OpenHAB Code
Items
The item configuration is quite straightforward.
For the Switch items make sure to have a part to send ON and another for OFF, and yet another to only receive the state of the relay (remember, it's being sent periodically in case the command wasn't received correctly by the switch box).
The switch "GetNode_GF_Coffee_RSSI2" is used to send the GetRSSI to the coffee machine in order to get the signal strengths back.
The reset switch is to reset all values to read them from scratch if you're not sure the Arduino sends back what it should.
The Harmony part took a while until I figured out what to send. At the end some commands are needed to fire up an activity, wait and set the channel.
Sitemap
Only the coffee machine is needed here. Of course you can chose to add the Harmony to the sitemap too. This actually could be interesting in case you're not home. You could switch on and off the TV as you wish to simulate occupancy; need to think about that.
Rules
Probably the most interesting part...
The variable "Log_MorningJobCoffee" states if something should be logged in the openhab.log It's good for troubleshooting.
Try to work with variables, it will be easier than chaning numbers in the code. For example variables for the volumes and wait-times.
The first real rule is "Coffee machine cronjob ON" actually sends the ON command to the coffee machine. Also the cable box is started (and yes, it takes nearly 40 seconds to start...) and because the design is not that intelligent, I always need to switch to the TV channels and set to 1, then switch back to the Radio channels and select my channel.
The volume could actually be set with a number, but it's probably better for the speakers to crank up to volume like pushing the button on the remote (this is what the while loop is for)
The BlueBlink is quite obvious. If the machine is on, the blink command is sent.
The machine-OFF job is obvious too.
The strange rule "Coffee machine status change" is explained as following. I noticed that the OH server perdiodically send its last status to the nodes. Meaning that when you switch the machine on by pressing the button on the box, it will update the OH, but in the OH's memory its last send state was OFF. So the machine will switch off within moments you switched it on. The workaround would then be to switch the machine on from OH only. But since we built in buttons, we want to use them. This is why when OH receives the ON status from the machine, it will also send ON; simply to have the correct command that was sent last. The code of the Arduino will discard the received ON command anyway if the machine is already on.
Unfortunately the BlueBlink and BlueLight rules are not implemented yet, which means the OFF command cannot be delayed. This will be done on a later stage and I will then update this instructable.
The next 3 rules will request the signal strengths, update the last-update, and reset all values of the coffee machine.
The Harmony rules increases and decreases the volume. The radio station rule simply sends the numbers to the cable box to chose the channel. Each command is a pressed button. A command like "150" cannot be sent because the cable box does not understand that.
Note that the OH server needs the Logitech binding, as well as the Onky binding in my case.
Make sure to define their IP addresses in the cfg file. The IP can be set on the receiver, but the Harmony needs to get a reserved DHCP address (check you DHCP server to assign an address to your Harmony's MAC address).