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 Wi–Fi 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")
- This code is using the Quarky library to draw a pattern depending on the status of the Wi–Fi connection. If the Wi–Fi 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.
- Additionally, it imports the iot module and connects to a Wi-Fi network with the name specified name and the password.
- 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
- If the Green Light is displayed, your Wi-Fi is connected.
- If the Red Light is displayed, your Wi-Fi is not connected. Change the Wi-Fi and the password and try again.
- If the Red Cross sign is displayed, a Python error has occurred in Quarky. Check the serial monitor and try to reset the Quarky.