This is the second part of my little home automation project. In the first part I was laying out the basic idea and first trials. This part is covering the test setup with an arduino and has a few words on the data structure sent by the electric counter and the software used to interpret it.
The drawing of the breadboard and the schematics below were both done with Fritzing. It’s my first time to use the tool and I think I have to practice a bit more. The autorouting did not work that well, so I had to redraw all the connections manually. Also the wiring on the breadboard could be a bit nicer… There was no I2C LCD hardware interface in the libraries, so I just drew two wires from the SDA and SCL ports to the display. Anyhow, here it is:
For the optocoupler it doesn’t matter which wire goes to which connector of the counter, either way works. The signal is being rectified by the white LED. The output of the photoresistor is analog and does not comply with the zeros and ones that are required by serial communication. That’s where the PNP transistor comes in. The photoresistor triggers the transistor, which then drains the RX pin of its high signal resulting in the bitstream we are interested in.
The data groups can be displayed on the LCD screen and scrolled through via the toggle button.
Depending on the high / low cost period, pin 8 toggles the signal for the NPN transistor, which then enables / disables the relay. I chose a fail safe setup, where the water heater would be connected to the NC part of the relay. This means a high signal on pin 8 actually turns the heater off. That way the heater would still be working even when the circuit gets disconnected. The relay itself is fed by an external 12VDC power supply. Please note that the GND of that supply needs to be connected to the GND of the circuit for the current to flow!
A word of WARNING: Whatever you want to control with the circuit (e.g. a relay with a water heater connected), it will most probably run on a much higher current than the 5VDC the Arduino is using. If you are not familiar with or feel uncomfortable working with high voltage, don’t do it on your own. Ask a professional electrician / experienced person to support you and above all: Make sure the power is turned off (CBs out, lines tested with a current tester) before you start connecting anything. There is the definite potential of severe injury or death if anything goes wrong, not only for yourself, but also for your loved ones when you leave wires unsecured or currents running wild!
Visualizing the signals on an oscilloscope looked like this:
The software can be found in my github repository.
The code is rather straight forward: After setting up the pins for input / output and putting them in an initial state, the LCD would show “OK.”. Then the main loop would wait for a new frame transmission to start. At each start of a frame, the status LED (pin 13 for the Arduino Uno) is being toggeled to have some kind of heart beat indication. Then the frame is being read. As per specification a frame would look like this:
<Label (String with up to 8 characters)>
<SP (0x20) or HT (0x09)>
<Value (String with up to 12 characters)>
<SP (0x20) or HT (0x09)>
<Checksum (single character)>
Each frame (starting with STX and ending with ETX) contains multiple groups and each group consists of a label, a value and a checksum.
After reading a group, the status of the water heater is being checked and changed depending on the high / low cost period. Then the button is being checked and when pressed, the group to be displayed on the LCD gets toggled.
Finally, the LCD gets updated.
This is far from perfect. First of all, the checksum is not being used. Then the water heater status could only be checked when the specific group is being processed (in order to save some cycles). At the end, the LCD should only be updated if the toggle button has been pressed or the present value has changed. But this was only a proof of concept and used as the baseline for the python code that is now running on the Raspberry Pi.
The next step was to move the logic from the Arduino to a more sophisticated platform, connect it to the LAN and Internet, have it log the consumption and control the radiators and water heater. But this is another story to be told…