DEPENDENCY:
- Before installation Adafruit IO first check all the dependency are readily installed or not!!
- If not go through following commands just copy and paste in RPi terminal
PYTHON 2:
sudo apt-get update
sudo apt-get install python-pip
sudo python -m pip install --upgrade pip setuptools wheel
PYTHON 3:
sudo apt-get update
sudo apt-get install python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel
INSTALL ADAFRUIT-IO IN RPI
PYTHON 2:
sudo pip install adafruit-io
sudo pip install requests
PYTHON 3:
sudo pip3 install adafruit-io
sudo pip3 install requests
How To SetUp Adafruit IO
Step 1: Open Adafruit IO in browser by this link: https://io.adafruit.com/
SignUp by gmail account if you have already an acc on Adafruit IO then just login to it.
Step 2: Click on Action and Create New Dashboard
Step 3: Give name and Create a New Dashboard
Step 4: Click on “Create a new block”
Step 5:Click on toggle button what is mark on below pic
Step 6: Create a “feed”
Step 7: Do Check mark on and goto “Next step”
Step 8: Give a block title and click on “create block” as shown below
Step 9: Now we can find a toggle button below
CIRCUIT:
NOTE: In this case it uses Led to ON/ OFF for the demo purpose but if you want to control AC (Alternative Current) loads just replace led with Relay circuit. If You unable to do the circuit connection, please let me know on the comment session.
CODE:
# Tested code by Robogenesis
import RPi.GPIO as board
import time
from Adafruit_IO import MQTTClient
import requests
light = 3
board.setwarnings(False)
board.setmode(board.BOARD)
board.setup(light,board.OUT)
ADAFRUIT_IO_USERNAME = "enter username here"
ADAFRUIT_IO_KEY = "enter key here"
def connected(client):
print ('Connected to Adafruit IO! Listening for feed changes...')
client.subscribe('enter feed name')
def disconnected(client):
print ('Disconnected from Adafruit IO!')
sys.exit(1)
def message(client, feed_id, status):
if feed_id == 'enter feed name':
#print(status,type(status))
if status == 'ON':
print("Light 1 ON")
board.output(light,True)
elif status == 'OFF':
print("Light 1 OFF")
board.output(light,False)
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message
client.connect()
client.loop_blocking()
Code Description:
light = 3
This means the led is connected to Pin No. 3
if you are not familiar with Pin Configuration just go through this link: https://pinout.xyz/
ADAFRUIT_IO_USERNAME = “enter username here”
ADAFRUIT_IO_KEY = “enter key here”
Here u have to give your Adafruit IO username and Key; where to get this key- go to the “key” Symbol on the top right corner
And here is the Username and Active key just copy and paste within the double quote without any spacing ” ” .
def connected(client):
print (‘Connected to Adafruit IO! Listening for feed changes…’)
client.subscribe(‘enter feed name’)
def disconnected(client):
print (‘Disconnected from Adafruit IO!’)
sys.exit(1)
def message(client, feed_id, status):
if feed_id == ‘enter feed name’:
In the above code just enter the feed name in my case it was ‘Light’ you can find in Step 6
def connected(client):
print (‘Connected to Adafruit IO! Listening for feed changes…’)
client.subscribe(‘Light’)
def disconnected(client):
print (‘Disconnected from Adafruit IO!’)
sys.exit(1)
def message(client, feed_id, status):
if feed_id == ‘Light’: