Smart Home Architecture

Figure 1. Smart Home Architecture

As shown in figure 1, the smart home structure works as a bi-directional network. It can be explained with two simple general examples:

1. Controlling output components:

If the user wishes to turn ON a light bulb located inside the home, they would simply have to press the button widget associated with the light bulb. After the request is generated, the Boolean data (LOW or HIGH) travels to the Blynk servers, which tell Arduino to turn the bulb ON. The Arduino then processes the data and begins the I2C transmission to the Silego GreenPAK (SLG46538V in this case, but it can work similarly with SLG46537V, SLG46533V and other common GP5). The value of the register 0xF4 is changed to 0x01, which turns the first bit of the register I2C block virtual output to HIGH. This is associated at the same time with pin 7 (light bulb) in GP design which finally forces that digital output pin to HIGH, energizing the relay through a circuit created. When the button in the button widget of the app is un-pressed, the output is turned OFF.

2. Monitoring input sensors:

Let’s suppose at night, when everybody is sleeping, an intruder decides to enter the house through the window. As soon as they try to open the window, an alarm and specific lights will be turned ON (it was set that way in GP Design). Arduino will be frequently monitoring all the GP5’s connected sensors through I2C (reading the register 0xF0), so it will detect a change in the register value. As in our scenario, the value will be 0x08, which will mean that the digital input pin 4 is active, which will eventually turn a widget LED ON and send a notification to the app, prompting the user just for security.

Blynk Application

Just download the app, and go to the link http://docs.blynk.cc/. It is very easy for anyone to create their own Blynk account. For the purpose of this app note, a project with some buttons, LEDs, tabs, email, notifications, slider, gauge, graph, and history graph has been created. (Figure 2).

Blynk Smartphone HMI

Figure 2. Blynk Smartphone HMI

It is just a matter of configuring pins (hardware pins directly, or just virtual pins like in this project), and some other few options for each widget to get the Human Machine Interface (HMI) ready to run the target project.

Widgets justification:

Notifications events:

Two kinds of notifications are supported: by email, and via smartphones (you can also add twitter notifications). It is just a matter of adding the notifications widgets into the app, and writing the right functions from the Blynk library on Arduino IDE, to enable notifications module. See figure 3.

Notifications examples

Figure 3. Notifications examples

ESP8266 Wi-Fi Module

The ESP8266 is a low-cost Wi-Fi chip with a full TCP/IP stack and Micro Controller Unit capability. It is actually one of the least expensive Wi-Fi modules designed for IoT purposes. Through some libraries and protocols, you can not only achieve good communication with your hardware, but also a stable one too. It can communicate with any other Micro Controller, or programmable device through UART protocol, via pre-programmed AT commands inside the chip.

There are many kinds of ESP8266 modules, but for this project the ESP8266-01 is being used. It’s connected to the Arduino as shown in the following diagram (Figure 4):

Arduino Mega and ESP8266 Wiring Diagram

Figure 4. Arduino Mega and ESP8266 Wiring Diagram

The Wi-Fi module works with 3.3V levels, so there are two options: the easier one is shown in figure 4. It involves connecting ESP8266’s VDD pin to the 3.3V pin of Arduino, and Arduino’s TX to a voltage divider (converting the 5v to 3.3V), and then wiring that output voltage level to RX pin of the module. The other option is to buy a voltage level converter chip (or maybe a 3.3V power source board) to feed the ESP8266 circuit.

Blynk libraries will get requests to control the entire module via AT commands: from connecting to your WLAN at home, to sending and receiving as many data bytes as you need.

The I2C protocol in SLG46538V is a very powerful tool for many kinds of projects. For this one, the GreenPAK 5 is used as a I/O pin extension, leaving almost all the pins free on the Arduino. There are some app notes available (such as AN-1107 and AN-1090), where I2C is well explained. Almost all the GreenPAK device hardware can be controlled from I2C: configuring a general Micro Controller as a Master, and GreenPAK as a Slave. Via I2C, sending some byte commands, pins, ASM RAM, counters, LUT and many other elements in the GreenPAK can be read and written.

The GreenPAK design is based on the GPIO pins, and the I2C protocol tool blocks connections. Digital inputs such as Movement Sensor, Door Sensor, Window Sensor and Door Bell Push Button in the automated home are associated with pins 2, 3, 4 and 5 respectively. They can easily be read from I2C tool, but also it’s well designed with gates (LUTs) to trigger one or both of the digital outputs included on the project. For example, when a door bell push button is pressed, it will activate the alarm to simulate just the doorbell sound. When the Movement Sensor detects someone it will turn the lights ON (simulating someone in a room). Finally, if door sensor or window sensor (through hall effect) detects some intruder, they will trigger the alarm and lights at the same time.

There is also a dimming lightbulb at pin 10, connected with two counters which have this pin as a PWM output, where CNT0/DLY0 sets the frequency (100Hz) and CNT6/DLY6 sets the pulse width from 0 to 255. However, there is an error range of approximately 3% to set the output width, so it’s suggested to send bytes from 0 to 248 from Blynk app, to get a great PWM.

Furthermore, part of the ultrasonic sensor circuit is controlled by the GreenPAK 5. It controls the trigger pulse of the module through two counters: CNT3/DLY3 to set the frequency (10Hz) and CNT4/DLY4 to set the pulse width necessary to activate the sensor circuit process, thereby creating an “echo” output which will be received by the Arduino board to process the data.

The I2C block is connected as default with pin 8 (SCL) and pin 9 (SDA). The device address is 0x00, but any other address from 0x00 to 0xF can also be set. It is important to know (Based on SLG46538V datasheet) that I2C writing frame is composed of a start bit, followed by a control byte, word address, data and a stop bit (figure 7 illustration). I2C reading frame is composed of a start bit, followed by a control byte, word address, a start bit (again), control byte, data and stop bit to end the transmission (check the app note AN-1090 for more information about how I2C works).

Arduino Code

For Arduino (from the hardware perspective), there is not much to say, apart from the connection to the Wi-Fi module. Echo pin output of the ultrasonic sensor is connected to Arduino’s digital input pin 12. This way the Arduino can process echo pulse data (Talked more about below).

Regarding Arduino code design, some libraries were used that solve many things and makes programming easier. These libraries are: “ESP8266_Lib.h”, “BlynkSimpleShieldEsp8266.h”, “SimpleTimer.h” and “Silego.h”. The code is well commented and explained (check it out by downloading the Arduino file). Some general things are explained:

Arduino Code

First, to install the project at home, the Auth Token number has to be typed. To find it, just open the Blynk app, login with your account (If you have not created one, just register), create a project, and go to project settings. There, the Auth Token number (check the example in Figure 6) can be seen. Once you find it, just type the number in the Arduino code, or to make things easier, send that code to your email over the app. Then it can be just copied and pasted in the PC. In addition, the name and the password of the WLAN have to be typed, as any other Wi-Fi based electronic device.

Project Settings

Figure 6. Project Settings

If working with LED widgets in the app is desired, just add as many WidgetLED objects as desired in Arduino code. For this app note, all sensors that are used to build the project have been added. There are other important objects to define such as Wi-Fi from ESP8266 library classes and timer from simpler timer classes.

Also, the Address of the GreenPAK 5 has to be defined, in this case 0x00.

Arduino Code

If any value has to be written from the app to a virtual pin, the recommended way to work with it is through a function called BLYNK_WRITE(Virtual pin name), to know the virtual pin that’s to be read from Blynk. That has to be followed by typing the pin’s name as a parameter in the function. In this way, param.asInt() has to be called within the function, which returns an integer value (It could be a byte or Boolean too), that has to be processed to create a trigger to execute other sentences in the code.

Arduino Code

It is very important while using Blynk with Arduino, to leave the void loop function, with only Blynk.run() function if possible, and avoid any kind of long millisecond delays with delay(). The consequence of this could halt the communication between Arduino and Blynk, or delay data transmission. However, it is always desirable to add some other features to the Arduino code in addition to what the app services offers, and that’s why timer.setInterval(milliseconds, functionToExecute) from “SimplerTimer.h” function is used in the code’s setup, to check a general purpose project function that’s needed to execute it to add other features. For this example it is timer.setInterval(100L, readInputs), that means for each 100 milliseconds, Arduino will run the function readInputs().

Arduino Code

readInput() function, is one of the most important functions in the project because it reads the GreenPAK digital inputs with the I2C tool, then it evaluates the returned byte number to know what sensor is being activated. Once Arduino knows what sensor it is reading, it triggers (turns ON) the associated LED widget by writing to the virtual number object previously set up. This way, the user can observe when a sensor detects something by just checking the app, but this will also trigger some notifications with Blynk.notification() function; e.g. Arduino reads GreenPAK 5 and knows that movement sensor detected someone, so it will trigger movementSensor.on(), and a notification to the smartphone saying “There is someone in the Living Room”.

Arduino Code

Finally, this function also reads ultrasonic echo pin, through a function called pulseIn(), it measures how long the echo pulse remains HIGH in microseconds, then it translates that time into centimeters, using the following formula: distance=microseconds/58 (check the app note AN-1050 for more information about how ultrasonic sensor works); Once the value of distance is known, Blynk.virtualWrite() function is used to write any virtual on Blynk e.g. for this Smart Home design module, the distance value is sent to virtual pin 6 and 7, where virtual pin 6 is associated to a gauge widget, and pin 7 to a graph widget in the app, to show how this variable behaves (simulating the water level of the tank through distance).

Arduino Code

Regarding tank’s level notification, some basic conditions were used: to tell the user when water level is low and when it is between 100-150cm (this means that the water level is below 50%). Also, when it is between 160-200cm, water level is below 20%.

Isolated Relay Circuit

Figure 7. Isolated Relay Circuit

GPIO Relevant Circuit Schematics

A common isolated circuit (with optocouplers and a 5V relay and 120VAC output on the normally open pin) was applied to the light bulb’s digital output. (See Figure 8)

About the rest of the circuits, sensors as digital inputs are connected directly to the GreenPAK 5. The other digital outputs (like the buzzer and the LED) are connected directly with some basic resistors to protect them.

Hall effect sensor circuit

Figure 8. Hall effect sensor circuit

Conclusion

GreenPAK 5 chip is a very powerful device when we refer to Smart Home systems’ design. Just imagine setting sixteen (16) GreenPAK as slave devices controlled by a master Microcontroller, how many sensors and elements can be controlled with that system? Even a whole building can be controlled in many aspects with a very low-cost solution, (at least from the controller part in the system’s architecture).

GreenPAK 5 is a great solution that should definitely be considered when when designing successful Smart Home/Buildings in the area of Home Automation and IOT.

Appendix A

B.O.M and Tools

The components and tools used in the project are:

1. SLG46538V GreenPAK Device

2. Arduino Mega 2560

3. ESP8266-01

4. HC-SR501

5. HC-SR04

6. US1881 (2)

7. Alarm buzzer

8. GreenPAK Designer

9. Arduino IDE

10. Blynk

Other components and tools:

About the Author

Name: Antonio Martínez

Background: Antonio studied Electronics Engineering in the area of Automation and Control at URBE University in Zulia state, Maracaibo city. He is currently working in an entrepreneurship project of a new social network, in parallel of a new technology solutions company.

Contact: appnotes@silego.com