Table of Contents

wifi.iswificonnected()

Function Definition: wifi.iswificonnected()

Parameters

Description

The function reports if the Wi-Fi is connected to the Quarky or ESP32 or not. This block is only available in Upload Mode.

Example

Learn how to create a light switch in Adafruit IO with Python code. Step-by-step guide on how to connect Quarky to Adafruit IO and create a dashboard with a toggle block.

In this example, we are going to understand how to make a feed on Adafruit IO. Later we will write the Python code that will retrieve the information from the cloud and make the Quarky lights ON and OFF.

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/

Creating a Light Switch in Adafruit IO

  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.

Python Code for Stage Mode

This Python code creates two instances, one of the Quarky class and one of the AdaIO class. It sets the brightness of the Quarky instance to 10 and connects to Adafruit IO using the specified username and key. It then creates a loop that checks to see if the value of theLight feed from Adafruit IO isON“. If the data is “ON” then the white light is turned on on the display, otherwise, the display is cleared.

# Create a new instance of the Quarky class and call it quarky
quarky = Quarky()

# Create a new instance of the AdaIO class and call it adaio
adaio = AdaIO()

# Set the brightness of Quarky to 10
quarky.setbrightness(10)

# Connect to Adafruit IO using the specified username and key
adaio.connecttoadafruitio("STEMNerd", "aio_UZBB56f7VTIDWyIyHX1BCEO1kWEd")

# Create an loop
while True:

  # Check to see if the value of the "Light" feed from Adafruit IO is "ON"
  if (adaio.getdata("Light") == "ON"):
    # If the data is "ON" draw the specified pattern
    quarky.drawpattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

  # Otherwise, clear the display
  else:
    quarky.cleardisplay()

Output

Python Code for Upload Mode

You can also make the script work independently of PictoBlox using the Upload Mode. For that switch to upload mode.

Note:  In the upload mode your Quarky needs to connect to the WiFi network. You can learn how to do it here: https://ai.thestempedia.com/example/wi-fi-connect-for-quarky-in-upload-mode-python/
# This code connects to a wifi network and an adafruit IO account.
# It also uses the module "quarky" to set the brightness and draw a pattern.
# The code will check if the wifi is connected and if the value of "Light" in the adafruit IO account is "ON",
# it will draw a pattern on the display. If not, it will clear the display.
# If the wifi is not connected, it will set LED 1 to red.

from quarky import *

# imported module
import iot

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

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

# Set the brightness
quarky.setbrightness(10)

while True:
  # Check if the wifi is connected
  if wifi.iswificonnected():
    # Check if the value of "Light" in the adafruit IO account is "ON"
    if (adaio.getdata("Light") == "ON"):
      # Draw a while LED pattern on the display
      quarky.drawpattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    else:
      # Clear the display
      quarky.cleardisplay()
  else:
    # Set LED 1 to red
    quarky.setled(1, 1, [255, 0, 0], 100)

Troubleshooting:

  1. If the Green Light comes, your Wi-Fi is connected.
  2. If the Red Light comes, your Wi-Fi is not connected. Change the Wi-Fi and the password and try again.
  3. If the Red Cross sign comes, Quarky, Python error has occurred. Check the serial monitor and try to reset the Quarky.
Read More
Learn how to create a Color Switch on Adafruit IO and the Python code needed to retrieve the color information from the cloud and make the Quarky lights ON and OFF with the selected color.

In this example, we are going to make a colored switch in Adafruit IO. Then we will make Python code for Quarky that will retrieve the color information from the cloud and make the Quarky lights ON and OFF with the selected color on the cloud.

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/

Creating the Dashboard for the Color Switch

  1. Create two new Feeds named Light and Color.
  2. Create a new Dashboard named RGB Light.
  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. Next, create another block. This time select Color Picker.
  8. Connect to Color Feed.
  9. You will find the blocks on the Dashboard. Edit the layout as per your liking.

Python Code for Stage Mode

This code creates an instance of the Quarky and AdaIO classes and connects to Adafruit IO using credentials. It then creates a loop that checks if the light is on and, if so, sets the LED on the Quarky to the color from the Adafruit IO data. If the light is not on, the display on the Quarky is cleared. The loop pauses for 1 second before repeating.

# Create an instance of the quarky class
quarky = Quarky()

# Import the time library
import time

# Create an instance of the AdaIO class
adaio = AdaIO()

# Clear the display on the quarky
quarky.cleardisplay()

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

# Loop forever
while True:
  # Get the data from Adafruit IO to see if the light is on
  if (adaio.getdata("Light") == "ON"):
    # Get the color data from Adafruit IO
    adaio.getcolordata("Color")
    # Get the RGB values from the color data
    Color = [adaio.getRGB(1), adaio.getRGB(2), adaio.getRGB(3)]
    
    # Loop through the 5 rows and 7 columns on the quarky
    for i in range(1, 6):
      for j in range(1, 8):
        # Set the LED on the quarky to the Color with a brightness of 20
        quarky.setled(j, i, Color, 20)

  # If the light is not on, clear the display on the quarky
  else:
    quarky.cleardisplay()

  # Pause the program for 1 second
  time.sleep(1)

 

Output

Python Code for Upload Mode

You can also make the script work independently of PictoBlox using the Upload Mode. For that switch to upload mode.

Note:  In the upload mode your Quarky needs to connect to the WiFi network. You can learn how to do it here: https://ai.thestempedia.com/example/wi-fi-connect-for-quarky-in-upload-mode-python/
# Create an instance of the quarky class
from quarky import *

# Import the library
import time
import iot

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

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

# Clear the display on the quarky
quarky.cleardisplay()

# Loop forever
while True:
  # Check if the wifi is connected
  if wifi.iswificonnected():
    
    # Get the data from Adafruit IO to see if the light is on
    if (adaio.getdata("Light") == "ON"):
      # Get the color data from Adafruit IO
      adaio.getcolordata("Color")
      # Get the RGB values from the color data
      Color = [adaio.getRGB(1), adaio.getRGB(2), adaio.getRGB(3)]

      # Loop through the 5 rows and 7 columns on the quarky
      for i in range(1, 6):
        for j in range(1, 8):
          # Set the LED on the quarky to the Color with a brightness of 20
          quarky.setled(j, i, Color, 20)

    # If the light is not on, clear the display on the quarky
    else:
      quarky.cleardisplay()

    # Pause the program for 1 second
    time.sleep(1)
    
  else:
    # Set LED 1 to red
    quarky.setled(1, 1, [255, 0, 0], 100)

Troubleshooting:

  1. If the Red Cross sign comes, Quarky, Python error has occurred. Check the serial monitor and try to reset the Quarky. Also, ensure that the WiFi you are trying to connect to is available.
Read More
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)

 

Read More
Learn how to connect your Quarky to a WiFi network and troubleshoot any connection issues. Follow the guide to ensure a successful connection.

In this example, we look at how to establish and see if the Wi-Fi is connected to Quarky or not. The code will connect the Quarky to a specified WiFi network and show a green status if the connection is successful, or a red status if the connection fails.

Alert: Quarky’s WiFi connection ability is only available when using the Upload Mode in Pictoblox. This mode allows users to write scripts and upload them to the board so they can be used while the board is not connected to a computer. This is useful for running scripts without a computer connection.

Code

# imported modules
from quarky import *
import iot

# Create a Wi-Fi object
wifi = iot.wifi()

# Change the Wi-Fi Name and Password
wifi.connecttowifi("IoT", "12345678")

# Run the loop to check if the Wi-Fi is connected
while True:
  # Check if the Wi-Fi is connected
  if wifi.iswificonnected():
    # Draw a pattern on the quarky
    quarky.drawpattern("ccccccccccccccccccccccccccccccccccc")

  else:
    # Draw a different pattern on the quarky
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
  1. This code is using the Quarky library to draw a pattern depending on the status of the WiFi connection. If the WiFi connection is successful, it will draw the green light on the Quarky LEDs. If the Wi-Fi connection is unsuccessful, it will draw a red light on the Quarky LEDs.
  2. Additionally, it imports the iot module and connects to a Wi-Fi network with the name specified name and the password.
  3. It also has a while loop that checks to see if the Wi-Fi is connected and will draw the appropriate pattern accordingly.

Output

Alert: If you do not yet have the IoT House assembled, please refer to this document to guide you through the assembly process: https://ai.thestempedia.com/docs/iot-house-quarky-addon-kit-documentation/iot-house-assembly-guide/.

Troubleshooting

  1. If the Green Light is displayed, your Wi-Fi is connected.
  2. If the Red Light is displayed, your Wi-Fi is not connected. Change the Wi-Fi and the password and try again.
  3. If the Red Cross sign is displayed, a Python error has occurred in Quarky. Check the serial monitor and try to reset the Quarky.
Read More
All articles loaded
No more articles to load