IoT-Enabled Smart Plug with Python Code

Example Description
Learn how to create a switch on Adafruit IO with Python code and an IoT-enabled Smart Plug. This project demonstrates how to control a plug and retrieve information from the cloud with the help of a Quarky Expansion Board, Adafruit IO, and an IoT house.

The project demonstrates how to create a smart plug that can be controlled by an IoT device and that can retrieve information from the cloud. The smart plug can be used to turn lights ON and OFF.

Creating a Switch on Adafruit IO

We will be using Adafruit IO for creating a switch on the cloud. Follow the instructions:

  1. Create a new Feed named Light.
  2. Create a new Dashboard named Light Control.
  3. Edit the Dashboard and add a Toggle Block.
  4. Connect the Light feed to the block and click on Next Step.
  5. Edit the Block Setting and click on Create Block.
  6. Block is added. You can try to toggle the switch.
  7. Go to the Light feed. You will observe the value of the feed changing as we click on the switch on the Dashboard.

Adafruit IO Key

You can find the information about your account once you log in from here:

Note: Make sure you are login on Adafruit IO: https://io.adafruit.com/

Circuit For the Lamp

The bulb is connected to the smart plug which is controlled with a relay.

Note:  A relay is an electromechanical switch that is used to turn on or turn off a circuit by using a small amount of power to operate an electromagnet to open or close a switch.

If the relay is ON, the smart switch gets ON, turning on the light. The relay has the following connections:

  1. GND Pin connected to GND of the Quarky Expansion Board.
  2. VCC Pin connected to VCC of the Quarky Expansion Board.
  3. Signal Pin connected to Servo 4 of the Quarky Expansion Board.

Python Code for Stage Mode

This Python code connects to Adafruit IO using the given credentials and checks if the light is ON or OFF. If the light is ON, then the code sets the relay to 0 (ON) and sets the LED to white with a brightness of 100. If the light is OFF, then the code sets the relay to 1 (OFF) and sets the LED to black with a brightness of 100. The code runs in an infinite loop to continuously check the status of the light.

#This code is used to control the light in an IOT house. 
#The code connects to Adafruit IO using the given credentials and checks if the light is "ON" or "OFF". 
#If the light is "ON" then the code sets the relay to 0 (ON) and sets the LED to white with a brightness of 100. 
#If the light is "OFF" then the code sets the relay to 1 (OFF) and sets the LED to black with a brightness of 100. 

quarky = Quarky() #Creating an object for the Quarky class

adaio = AdaIO() #Creating an object for the AdaIO class
house = IoTHouse() #Creating an object for the IoTHouse class

adaio.connecttoadafruitio("STEMNerd", "aio_UZBB56f7VTIDWyIyHX1BCEO1kWEd") # Connecting to Adafruit IO using the given credentials 

while True: # Loop that runs forever
  if (adaio.getdata("Light") == "ON"): #Checking if the light is "ON"
    house.setrelay(0, "pwm4") # Setting the relay to 0
    quarky.setled(1, 1, [255, 255, 255], 100) #Setting the LED to white with a brightness of 100

  if (adaio.getdata("Light") == "OFF"): #Checking if the light is "OFF"
    house.setrelay(1, "pwm4") # Setting the relay to 1
    quarky.setled(1, 1, [0, 0, 0], 100) #Setting the LED to black with a brightness of 100

 

Output

IoT-Enabled Smart Plug Upload Mode

You can also make the IoT Enabled Smart Plug work independently of PictoBlox using the Upload Mode. For that switch to upload mode and replace the when green flag clicked block with when Quarky starts up the block.

This code connects to a wifi network and an Adafruit IO account, creates an object for the IoTHouse class, and then sets a relay and LED based on the data from the Adafruit IO account. The loop runs forever and will always check if the wifi is connected and if the light isON orOFF“. If the wifi is not connected, it will set the LED to red.

from quarky import *

# Connect to a wifi network
import iot
import iothouse
wifi = iot.wifi()
wifi.connecttowifi("IoT", "12345678")

# Connect to an adafruit IO account
adaio = iot.AdaIO()
adaio.connecttoadafruitio("STEMNerd", "aio_UZBB56f7VTIDWyIyHX1BCEO1kWEd")

#Creating an object for the IoTHouse class
house = iothouse.iothouse()

while True:  # Loop that runs forever
  # Check if the wifi is connected
  if wifi.iswificonnected():
    if (adaio.getdata("Light") == "ON"):  #Checking if the light is "ON"
      house.setrelay(0, "pwm4")  # Setting the relay to 0
      quarky.setled(1, 1, [255, 255, 255], 100)  #Setting the LED to white with a brightness of 100

    if (adaio.getdata("Light") == "OFF"):  #Checking if the light is "OFF"
      house.setrelay(1, "pwm4")  # Setting the relay to 1
      quarky.setled(1, 1, [0, 0, 0], 100)  #Setting the LED to black with a brightness of 100

  else:
    # Set LED 1 to red
    quarky.setled(1, 1, [255, 0, 0], 100)

 

Table of Contents