Home automation is the new trend among urban households. The home automation system allows you to control household appliances using a remote control. The remote control can be an IR remote control, an RF remote control, or even a cell phone. An internet-based home automation system can be the most affordable setup because such a system can be controlled from anywhere and at any time. Like, the person is coming home and before leaving the office and getting into the car they can turn on the air conditioning in their living room so that when they get home the room is already cooled. This advantage can only be enjoyed in home automation controlled via the internet.
This is an IOT project for home automation based on Particle Photon. Home appliances are connected to the Particle Photon through a relay circuit. Some sensors, such as the DHT-11 temperature and humidity sensor and the LDR sensor for light intensity detection, are also interfaced with Particle Photon. The Photon is connected to the cloud service via a Wi-Fi connection and commands to control the devices are passed through a web page that is transferred via the Particle Cloud Service.
Particle Photon is an Arduino compatible IOT board. To write program code for any Photon, the developer needs to create an account on the Particle website and register the Photon board with his user account. The program code can then be written in the Web IDE on the Particle website and transferred to a registered IOT board over the Internet. If the selected Cluster, Photon here, is turned on and connected to the Particle cloud service, the code is written to the selected card over the air via the internet connection and the card starts operating according to the transferred code.
To control the board over the internet, a web page was developed that uses Ajax and Jquery to send data to the board using the HTTP POST method. The web page identifies the board by a device ID and connects to Particle's Cloud Service via an access token.
Required components –
Receiver side:
1. Photon Particle.
2. DHT11 sensor.
3. LDR sensor.
4. A home Wi-Fi connection.
5. 12V Relay
6. Transistor BC547
7. 1K ohm resistor
8. 5mm LED
9. Two-pin plug
10. Three-prong bracket
Transmitter side:
1. Web Page (EngineersGarageIoT.html)
Block diagram –
Fig. 1: Block diagram of photon-particle based IoT home automation system
Circuit Connections –
Fig. 2: Prototype IoT home automation system based on photon particles
The relay circuit that operates the switching function of the appliances is connected to the Particle Photon. There are also two sensors – DHT-11 temperature and humidity sensor and LDR sensor connected to the board to monitor temperature, humidity and light intensity. The circuit is assembled as follows –
Power supply – In the circuit, the photon particle and sensor modules need a regulated DC of 5 V, while the relays need a regulated DC of 12 V for their operation. The AC electrical grid is used as the primary source of energy. The mains supply is stepped down by a transformer and rectified by a full bridge rectifier. The rectified output is regulated to 5V and 12V using 7805 and 7812 ICs. Pin 1 of both voltage regulator ICs is connected to the battery anode and pin 2 of both ICs is connected to ground. The respective voltage outputs are taken from pin 3 of the respective voltage regulator ICs. An LED along with a 10K Ω pull-up resistor is also connected between the common ground and the output pin to get a visual cue of power continuity.
Relays – 12V 2A relays are used to turn on or off the AC appliances in the project. The relays are connected to the D0, D1, D2 and D3 pins of the Particle Photon through BC547 transistor circuits connected in a common emitter configuration. The AC power phase wire is provided at the COM terminal of the relays. When a HIGH logic is received on the interconnected microcontroller pins, the COM point changes from the NC point to the NO point, where a relay short-circuits the phase with the neutral wire connecting the device's power supply. The LEDs are connected in parallel to the relay circuit with pull-up resistors in series. These LEDs provide a visual indication of the ON/OFF status of the devices.
DHT-11 Temperature and Humidity Sensor – The DHT-11 sensor reads the ambient temperature and humidity and relays the data to the microcontroller as digital data. The data pin of the DHT11 temperature and humidity sensor is connected to the A0 pin of the Particle Photon, and the VCC and ground are connected to the common VCC and ground.
LDR sensor – The LDR is used to detect light intensity. The sensor is connected to Particle Photon pin A1. The sensor is connected to a potential divider circuit. The LDR provides an analog voltage that is converted to a digital readout by the built-in ADC.
How the circuit works –
Fig. 3: Image showing the Particle Photon-based IoT home automation system in action
Once the program code is transferred to Particle Photon, it starts operating accordingly. The Particle Photon must be connected to any internet hotspot via Wi-Fi. The Arduino-compatible code in the Photon initially turns off all devices, passing a LOW logic on the pins that connect the relays and starts fetching data from the sensors.
The DHT11 Temperature and Humidity Sensor is a digital sensor with a built-in capacitive humidity sensor and thermistor. It transmits a real-time temperature and humidity reading every 2 seconds. The sensor operates with a power supply of 3.5 to 5.5 V and can read temperatures between 0° C and 50° C and relative humidity between 20% and 95%. The DHT 11 Sensor sends data in digital format to a controller pin in the one-wire protocol that must be implemented on the firmware side. First, the data pin is configured for input and a start signal is sent to it. The start signal comprises a LOW for 18 milliseconds followed by a HIGH for 20 to 40 microseconds followed by a LOW again for 80 microseconds and a HIGH for 80 microseconds.
After sending the start signal, the pin is configured for digital output and the 40-bit data consisting of temperature and humidity reading is locked. Of the 5-byte data, the first two bytes are an integer part and a decimal part of the relative humidity reading respectively, the third and fourth bytes are an integer part and a decimal part of the temperature reading, and the last byte is the of the checksum. The one-wire protocol is implemented in the firmware using an open source library available for Photon.
The IR sensor outputs an analog voltage at the pin of the interfaced controller. Analog voltage is read and digitized using the built-in ADC channel. The analogRead function is used to read the analog voltage at the controller pin.
The data read from the sensor is passed to the Particle Cloud via Wi-Fi connection and the board waits for the command to turn on the devices on air. The user displayed sensor data on the web page and has buttons to turn devices on or off on the same web page. As a user taps a button to turn a device on or off, the command is transferred over the Internet via Particle's Cloud service in the form of serial strings. Strings are received via the HTTP POST method by Particle Photon. Upon detecting the command, the board changes the digital logic on the respective pin to HIGH to turn on the device or LOW to turn off the device.
Check out the Photon code to learn how the Arduino-compatible code detects commands in the air and reads data from sensors and publishes it to the linked data logging URL.
Programming guide –
Photon code
First of all, the DHT sensor library is imported. The library is automatically added by the Particle Web IDE. A constant is defined to denote the pin to which the DHT sensor 11 is connected and a constant is defined to denote a variant of the DHT sensor. Variables are declared to contain the value of temperature, humidity and light intensity. Variables denoting home appliances are declared and assigned to microcontroller pins. An object of type DHT is declared.
A character array is declared to contain the sensor data to be sent to the web page.
The setup function is called in which the pin connected to the relays is declared as output pin using the pinMode function. The DHT sensor is initialized by calling the Begin method on the DHT object. A getpos variable and setpos function are exposed from the cloud and retrieved through the POST method by calling the Particle.variable and Particle.function methods. The setup function is executed only once at the beginning of the code.
The loop function is called and iterates infinitely. In the loop function, DHT sensor data is obtained using the getTempCelcius and getHumidity methods on the DHT object. LDR sensor data is obtained by calling the analogRead method and converted to light intensity using standard formulas. Data from different sensors are stored in variables, grouped into suitable strings, and sent to the data logging URL using the publish method on the Particle object.
Fig. 4: Screenshot of Loop function in Photon code for IoT based home automation system
This is an HTML file that needs to be executed to send data to Particle's Cloud service. The cloud service automatically connects to Photon and Code on Photon operates based on the data received from the cloud.
HTML code
Figure 5: Screenshot of the web page controlling the IoT-based home automation system
This is an HTML file that needs to be executed to send data to Particle's Cloud service. The cloud service automatically connects to Photon and Code on Photon operates based on the data received from the cloud.
The complete code for Internet-based home automation can be found in the source code tab.
Note: The HTML file can be downloaded from the attachment mentioned below.
Project source code
###
//Program to // This #include statement was automatically added by the Particle IDE. #include//DHTparameters #define DHTPIN A0 #define DHTTYPE DHT11 //Variables int temperature; int humidity; int light; int movement; int ultraviolet; int light_sensor_pin = A1; int uv = A3; int pos = 0; // Pins int Device0 = D0; int Device1 = D1; int Device2 = D2; int Device3 = D3; // DHT sensor DHT dht(DHTPIN, DHTTYPE); // publishjson.ino -- Spark Publishing Example unsigned long lastTime = 0UL; char publishString(64); void setup { Serial1.begin(9600); pinMode(Device0,OUTPUT); pinMode(Device1,OUTPUT); pinMode(Device2,OUTPUT); pinMode(Device3,OUTPUT); digitalWrite(Device0,LOW); digitalWrite(Device1,LOW); digitalWrite(Device2,LOW); digitalWrite(Device3,LOW); dht.begin; Particle.function("setpos", setPosition); Particle.variable("getpos", &pos, INT); } void loop { // Temperature measurement temperature = dht.getTempCelcius; // Humidity measurement humidity = dht.getHumidity; // Light level measurement float light_measurement = analogRead(light_sensor_pin); light = (int)(light_measurement/4096*100); int uvValue=analogRead(uv); ultraviolet = (uvValue*100)/4023; unsigned long now = millis ; Particle.function("led",ledControl); //Every 5 seconds publish uptime if (now-lastTime>5000UL) { lastTime = now; sprintf(publishString,"{"Temperature": %u, "Humidity": %u, "Light": %u}",temperature,humidity,light); Particle.publish("Uptime",publishString); } } int setPosition(String posValue) { pos = posValue.toInt; Serial1.printf("%u", pos); return 0; } int ledControl(String command) { intstate =0; int pinNumber = (command.charAt(1)-'0') - 1; if(pinNumber < 0 pinNumber > 7) return -1; if(command.substring(3,7) == "HIGH") state = 1; else if(command.substring(3,6) == "LOW") state = 0; else return -2; digitalWrite(pinNumber, state); return 1; }
Circuit diagrams
Circuit-Diagram-Particle-Photon-Based-IoT-Home-Automation-System- |