Skip to content Skip to sidebar Skip to footer

How to.turn on/off Smary Socked From Gosund

Plug Load Devices have a major share in overall power consumption. The plug load devices are normal building equipments (like Lights, Bulbs, AC, Fans, Heaters etc) that draw power from an ordinary AC plug. In United States, the power consumption of plug load devices count around 20 percent of total power consumption. In India, it accounts to up to 40 percent of total power consumption in the country. With rapid urbanization in developing countries like India, the share of plug load devices in national power consumption is about to increase further. These devices dissipate lots of energy in the form of heat and lead to considerable loss of energy. In order to save electricity and minimize energy losses from plug load devices, smart energy sockets can be a solution. Such sockets can be further interfaced with smart electronics that keeps track of energy consumption and maintain a record of it online. This way, the electricity bills can also be controlled and reduced to best optimization.

In this project, a smart socket is designed which can be automatically switched using a relay. The socket is interfaced to a Particle Photon based IoT device which keeps track of energy consumption using ACS 712 Current sensor and help automatically disconnect socket connection with the Mains when the power consumption by a device exceeds a threshold value. The Photon also remains connected to a web server via Wi-Fi hotspot and keeps updating the energy consumption data to the server.

This smart socket is built on Particle Photon. The Photon is an Arduino Compatible IOT board developed and supplied by the Particle (Former Spark). The program code on Photon needs to be written on Web IDE provided at the official website of Particle. Before that a project engineer needs to create an account on Particle site and register the Photon board to his account. The developer can register multiple photon or other Particle boards to his user account. The developer needs to write code on Web IDE and select a board to transfer code to the selected board On the AIR i.e. through internet. If the selected IOT board is switched on and connected to Particle's Cloud service, the code is burnt to the board over internet and the board starts behaving according the transferred code.

The Photon connects to a Webpage from where the device connected to the smart socket can be controlled.  The webpage has the PHP and HTML code which allows switching the device connected to the smart socket ON or OFF by passing appropriate commands (string values) to the Photon. The energy consumption by the device connected to the smart socket is also updated on the same webpage.  By controlling the appliances connected to the smart socket from the webpage, the users can avoid energy wastage and keep track of energy consumption by plug load devices from anywhere and anytime.

Prototype of Photon based Smart IoT AC Socket

Fig. 1: Prototype of Photon based Smart IoT AC Socket

Components Required –

List of Components required for making Photon based Smart IoT AC Socket

Fig. 2: List of components required for making Photon based Smart IoT AC Socket

Block Diagram –

Block Diagram of Photon Based Smart IoT Socket and Energy Monitor

Fig. 3: Block Diagram of Photon Based Smart IoT Socket and Energy Monitor

Circuit Connections –

The smart socket designed in this project is an IoT device. It is built by interfacing a relay and ACS-712 current sensor to the Particle Photon. The relay is interfaced to the Photon by a relay driving circuit while current sensor is connected to one of the Analog Input pins of the board. The socket connects to the Mains via the relay. The Photon has on-board Wi-Fi modem so, there is no need to connect any shield or external modem.

This Photon based IoT device measure energy consumption by the appliance connected to the smart socket and publish that data to a webpage. The webpage has controls to switch the appliance ON or OFF to optimize energy consumption by the appliance connected to the smart socket.

Image showing Circuit Connections of Photon Based Smart IoT Socket and Energy Monitor

Fig. 4: Image showing circuit connections of Photon Based Smart IoT Socket and Energy Monitor

The smart IoT device designed here has the following circuit connections –

Particle Photon – Photon is a popular IOT board available from the Particle platform. The board houses STM32F205 120 MHz ARM Cortex M3 microcontroller and has 1 MB flash memory, 128 Kb RAM and 18 mixed signal general purpose input output (GPIO) pins with advanced peripherals. The module has on-board Cypress BCM43362 Wi-Fi chip for Wi-Fi connectivity and Single band 2.4GHz IEEE 802.11b/g/n for Bluetooth. The board comes equipped with 2 SPI, one I2S, one I2C, one CAN and one USB interface. The particle Photon has the following pin configuration –

Table Listing Pin Configuration of Particle Photon

Fig. 5: Table listing pin configuration of Particle Photon

Table Listing Pin Configuration of Particle Photon

Fig. 6: Table listing pin configuration of Particle Photon

It should be noted that 3V3 is a filtered output used for analog sensors. The 3V3 pin is used here to provide DC supply to the current sensor. This pin is the output of the on-board regulator and is internally connected to the VDD of the Wi-Fi module. When powering the Photon via VIN or the USB port, this pin will output a voltage of 3.3 V DC. This pin can also be used to power the Photon directly (max input 3.3 V DC). When used as an output, the max load on 3V3 is 100mA.

In the circuit, one of the analog input pin of the Photon is used to interface the current sensor and one of the GPIO pin is used to interface the relay driving circuit. The 3V3 and Ground pin of the board is utilized to power the current sensor.

For controlling board over the internet, a web page is designed which uses Ajax and Jquery to send data to the board using HTTP POST method. The web page identifies the board by a device ID and connects to the Particle's Cloud Service through an access token.

Relay Driver Circuit – The AC appliances cannot be directly controlled by the Particle Photon. There is need of a relay circuit to control the AC appliances through the Photon. A 12V 2A relays is used to control the smart socket and switch the AC appliances ON or OFF in this circuit. The  relay is connected to the pin D0 of the Particle Photon via BC547 transistor circuit connected in a common emitter configuration. The phase wire from the AC supply is provided at the COM terminal of the relay. When a HIGH logic is output by the Photon to the interfaced pin, the  COM point switches from NC to NO point so the relay short-circuits the phase with the neutral wire switching the supply to the appliance ON. An LED is connected parallel to the relay circuit with pull-up resistors in series. This LED gives a visual hint of the ON/OFF status of the socket.

Circuit Diagram of Relay Driver

Fig. 7: Circuit Diagram of Relay Driver

ACS-712 Current Sensor – The Allegro ACS-712 provides economical and precise solutions for AC or DC current sensing in industrial, commercial, and communications systems. The device package allows for easy implementation by the customer. Typical applications include motor control, load detection and management, switch mode power supplies, and over current fault protection. The device is not intended for automotive applications. The ACS-712 module is designed to be easily used with microcontrollers like the Arduino. This current sensor comes available in full scale current ratings of 5 A, 20 A and 30 A. In order to measure the current, the output pin of the sensor is connected to A0 pin of the particle photon. The current sensor module comes with two terminal to connect AC mains and three terminals for interfacing with the microcontrollers. One of the terminals for connecting to AC circuit is connected to live wire of the mains in socket and the other terminal is connected the NO point of the relay. The NC point of relay is connected to the neutral wire of the mains in the socket. There are three terminals for controller interfacing – VCC, GND and Out. The VCC terminal is used to provide DC supply to the ACS-712 IC. It is connected to 3V3 pin of the Photon. The GND pin is connected to the ground pin of the Photon (common ground). The Out pin of the sensor module is connected A0 Analog Input pin of the Photon.

Power Supply – In this circuit, Particle Photon and sensor modules need a 5V regulated DC while relay needs 12V regulated DC for its operation. The AC main is used as the primary source of power. The supply from the mains 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. The pin 1 of both the voltage regulator ICs is connected to the anode of the battery and pin 2 of both ICs is connected to ground. The respective voltage outputs are drawn from pin 3 of the respective voltage regulator ICs. An LED along with a 10K Ω pull-up resistor is also connected between common ground and output pin to get a visual hint of supply continuity.

How the circuit works –

When the Particle Photon is powered on, the relay circuit is initially set to keep the socket open circuited from the Mains. The Photon connects with the available Wi-Fi connection. The Wi-Fi settings are hard-coded in the code for the Photon. The photon connects with its IoT platform with the help of registered device ID and access token key. The user can now operate the smart socket with the help of a webpage.

This webpage is also connected to the IoT platform of the Particle and send data to the registered Photon board by the help of device ID and access token. The data is sent to the Photon by a form using POST method of the TCP-IP stack. The webpage is a simple HTML/PHP code having radio buttons to switch the device ON or OFF. By default the OFF radio button is selected. The user can select the ON radio button. The radio buttons allow to select either of the radio buttons at a time. When the user selects either ON or OFF radio button, the status is sent as a query of the HTTP POST method in the URL which also contains the device ID of the Photon. The same status is then passed from the IoT platform to the Photon. The photon reads the status by accessing the platform by connecting to a Wi-Fi hotspot. If a command 'ON' is read, the Photon switches the output at D0 to HIGH and so switching the smart socket ON. Otherwise, it keeps the output at D0 to LOW, thereby switching the smart socket OFF.

The photon also keeps reading the analog voltage output from the ACS-712 sensor. The sensor module has a voltage output which is linearly proportional to the current consumption.

Graph showing Output Voltage Respective Sensed Current

Fig. 8: Graph showing output voltage respective sensed current

 As it can be seen from the graph above (taken from the datasheet of ACS-712), the sensor outputs a voltage of 2.5 V for 0 A. The voltage output of the sensor remains between 2.5 V down to 0.5 V for current up to 20 A in the reverse direction while it remains between 2.5 V to 4.5 V for current up to 20 A in the positive direction. So, the direction of current as well as the current consumption can be detected by the analog output of the sensor.

In most cases, an expression of AC current will be in a value known as RMS. In order to use the ACS712 current sensor to measure AC current, it is important to understand how to calculate an RMS current value from the device readings. The ACS-712 reports current measurements with voltage output.  The RMS voltage need to calculated and some scale factor needs to be applied to determine actual current consumption. The conversion for a sine wave with a zero volt offset (like your mains or line power) can be done as follow –

1) Find the peak to peak voltage (Volts Peak to Peak).

2) Divide the peak to peak voltage by two to get peak voltage (Volts Peak).

3) Multiply the peak voltage by 0.707 to yield RMS volts (Volts RMS).

Having Calculated RMS voltage, is simply a matter of multiplying by the scale factor of the particular ACS-712 to yield the RMS value of the current being measured.

The analog voltage detected from the sensor is converted to a digital value by the in-built ADC channel of the Photon. The Photon has 12-bit long ADC channels, so the converted digital value ranges from 0 to 4096 for voltage between 0 to 5V. From the digitized value, the actual voltage output of the sensor can be derived from which RMS value can be calculated and so the RMS current consumption in amperes. The power consumption can be calculated by multiplying the current consumption in amperes to the Mains Voltage (220 V). The power consumption at the smart socket is detected and sent to the webpage. By the power consumption at the smart socket, a user can decide to switch OFF or keep the socket ON.

This way, the power consumption at the smart socket can be monitored online and the socket can be operated ON or OFF from the designed webpage. The webpage can be stored and loaded from any device like PC, Laptop or Smart Phone.

Programming Guide –

The Photon code begins with the declaration of variables for storing the sensor data reading and the variables representing the pin connections of the relay circuit and the current sensor with the Photon.

Screenshot of C Code used for Initialization in Photon Code for Smart Socket

Fig. 9: Screenshot of C Code used for Initialization in Photon Code for Smart Socket

Initialization in Photon Code for Smart Socket

 The setup() function is called in which the sensor pins connected to the particle photon are initialized as input pin and the pin connected to the relay is configured as the output pin. The setup() function runs only once during start of the code.

Screenshot of C Code used in Setup Function in Photon Code for Smart Socket

Fig. 10: Screenshot of C Code used in Setup Function in Photon Code for Smart Socket

Setup Function in Photon Code for Smart Socket

The loop() function is called which iterates infinitely. In the loop() function the getVPP function is called to read the value from the sensor. The value read from the sensor is converted to voltage that is returned to the main loop and converted to current (in Amps) according to the standard equations.

Screenshot of C Code used in Loop Function in Photon Code for Smart Socket

Fig. 11: Screenshot of C Code used in Loop Function in Photon Code for Smart Socket

Loop Function in Photon Code for Smart Socket

The code in loop() keeps track of the value received from the webpage via IoT platform to determine switching of the relay. Check out the complete code from code section and try it out.

This project is developed as an IoT application for smart energy. It is a low-cost device which can be easily assembled and installed in a smart home.

Project Source Code

              
                ###    //Program to   // -----------------------------------  // Smart Socket and Energy Monitoring Device  // -----------------------------------   //Input pin for current sensor  const int Current_ip = A0;    // output pin for Relay  int Relay = D0;   //Indication LED pin  int led = D7;   //Variable Initialize for storing values of various data  double Voltage = 0;  double VRMS = 0;  double AmpsRMS = 0;  double mVperAmp = 185;   void setup()  {     // Here's the pin configuration, which defines the mode     pinMode(Relay, OUTPUT);     pinMode(led, OUTPUT);     pinMode(Current_ip, INPUT);      // We are also going to declare a Particle.function so that we can turn the LED/Relay on and off from the cloud.     Particle.function("led",RelayToggle);     // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.      // For good measure, let's also make sure both Relay and lED are off when we start:     digitalWrite(Relay, LOW);     digitalWrite(led, LOW);   }   void loop()  {     // calling the function to read the voltage value      Voltage = getVPP();      //Formula to read the VRMS      VRMS = (Voltage/2.0) * 0.707;       //coverting the VRMS to Ampere       AmpsRMS = (VRMS * 1000)/mVperAmp;      //        Particle.publish("Ampere", String(AmpsRMS));             delay(2000);  }   // We're going to have a super cool function now that gets called when a matching API request is sent  // This is the ledToggle function we registered to the "led" Particle.function earlier.  int RelayToggle(String command)   {      /* Particle.functions always take a string as an argument and return an integer.      Since we can pass a string, it means that we can give the program commands on how the function should be used.      In this case, telling the function "on" will turn the Relay and LED on and telling it "off" will turn the Realy and LED off.      Then, the function returns a value to us to let us know what happened.      In this case, it will return 1 for the Relay and LED turning on, 0 for the Relay and LED turning off,      and -1 if we received a totally bogus command that didn't do anything to the Relay.      */       if (command=="on")       {          digitalWrite(Relay,HIGH);          digitalWrite(led,HIGH);          return 1;      }      else if (command=="off")      {          digitalWrite(Relay,LOW);          digitalWrite(led,LOW);          return 0;      }      else       {          return -1;      }  }   float getVPP()  {    float result;        int readValue;             //value read from the sensor    int maxValue = 0;          // store max value here    int minValue = 1024;          // store min value here         uint32_t start_time = millis();          while((millis()-start_time) < 1000) //sample for 1 Sec     {         readValue = analogRead(Current_ip)/4;          // see if you have a new maxValue         if (readValue > maxValue)          {             //record the maximum sensor value             maxValue = readValue;         }         if (readValue < minValue)          {             //record the maximum sensor value             minValue = readValue;         }     }     // Subtract min from max and multiply with the reference voltage of ADC     result = ((maxValue - minValue) * 3.3)/1024.0;             return result;   }  ###              

Circuit Diagrams


Project Datasheet

Project Video


Filed Under: Circuit Design, Electronic Projects, IoT
Tagged With: IoT

How to.turn on/off Smary Socked From Gosund

Source: https://www.engineersgarage.com/photon-based-smart-ac-socket-iot-project/

Post a Comment for "How to.turn on/off Smary Socked From Gosund"