Project tutorial
Enable Alexa Control to your Ceiling Fan

Enable Alexa Control to your Ceiling Fan © GPL3+

Control the speed of your ceiling fan using Alexa and interface device.

  • 3,585 views
  • 5 comments
  • 10 respects

Components and supplies

Pi 3 02
Raspberry Pi 3 Model B
×1
A000066 iso both
Arduino UNO & Genuino UNO
×1
Relay (generic)
either a 5V relay so that you can directly interface with arduino or a relay interface board would be enough
×1
Fan Regulator
you can use anytype, as ong as you can find terminal to supply power to the same. I would suggest older type regulators which used resistive blocks to reduce voltage supplied
×1

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

I will be detailing here on how I made my regular ceiling fan a smart one by integrating Amazon Alexa for speed control of the same. We will be using amazon alexa skill set to initiate the speed change, and will be ngrok tunneling service to route the request to our locally housed RPi server, which will tell our Arduino uno to turn on the related relay.

Setting up our local Raspberry Pi Server

we need the following services to make use for our server.

  • Python PIP : to install python packages, if you are using python 2.7 or 3 pip will be intalled by default, hence you can avoid this step
sudo apt-get install python-pip  
  • Flask : Flask is a sever package for python which will help you to implement python servers easily.
pip install flask 
  • flask ask : a flask extention used to create alexa frendly programs easily.
pip install flask-ask 
  • pyserial : serial communication module for python
sudo apt-get install pyserial
  • libpython2.7-dev
sudo apt-get intall libpython2.7-dev

all necessary modules are installed its time to set the server running. we need to enter the python script for the same

from flask import Flask
from flask_ask import Ask, statement
import requests
import json
import serial ser = serial.Serial("/dev/ttyAMA0",9600)
app = Flask(__name__)
ask = Ask(app, '/')
@ask.launch
@ask.intent("FanOn")
def on():    
    ser.write(b'1')    
    return statement("Ceiling Fan turned on.")
@ask.intent("FanOff")
def off():    
    ser.write(b'0')    
    return statement("Ceiling Fan turned off.")
@ask.intent("FanLevelUp")
def up():    
    ser.write(b'+')    
    return statement("Ceiling Fan speed up.")
@ask.intent("FanLevelDown")
def down():    
    ser.write(b'-')    
    return statement("Ceiling Fan speed down.")
if __name__ == "__main__":
    app.run(debug=True)

Run this python script, and enlist this script to the auto start list of your RPi.

3. Download Ngrok

Ngrok is secure tunneling platform to make your device online. With this platform you can make your web application or any application goes online with a very simple way. For download go to official site and download for ARM.

https://ngrok.com/

Unzip it and go to directory where you extract it. Run it using command

./ngrok http 5000 

Alexa Skill

Go to amazon developer page and sign up for your developer account

https://developer.amazon.com/

Enter the developer console after sign in and now enter select "Alexa skill kit"

now select "Add new skill"

Enter the name for your skill, and enter an invocation name,. ensure your invocation name meet the necessary conditions

save and proceed to next step

here enter the intents for invocation of the skill, and utternce examples, ie the word combinations to be initiate each intent.

intents are as follows

{ 
"intents": [ 
  { 
    "intent": "FanOn" 
  }, 
  { 
    "intent": "FanOff" 
  },
  { 
    "intent": "FanLevelUp" 
  },
  { 
    "intent": "FanLevelDown" 
  }
] 
}

sample utterances

FanOn say fan on
FanOn say on
FanOff say off
FanOff say fan off
FanLevelUp say fan level up
FanLevelUp say increase fan speed
FanLevelDown say fan level down
FanLevelDown say decrease fan speed

Save and proceed to the next stage. here you will have to enter the location to which you are expected to send the data once an intend is invoked.

since we are using ngrok, select HTTPS, and enter the secure link is generated in ngrok serve, save it and proceed to next step

choose second option for SSL Certificate, and proceed

To check whether our setup is working or not . Make sure your ngrok server is working and Python script also running otherwise it will not work.

we can see that servers are getting request and updating it.

Arduino Code

upload the attached sketch your arduino and connect it to raspberry pi as explained in the schematic.

int Fan[] = {13,12,11,10,9,8}; 
int n=0;
void reset();
void set(char);
void setup() { 
// put your setup code here, to run once: 
for(int i=0;i<6;i++)
{
 pinMode(Fan[i], OUTPUT); 
}
 Serial.begin(9600); 
} 
void loop() { 
 char x;
if (Serial.available())
{
 x=Serial.read();
 reset();
 set(x);
}
}
void reset()
{
 for(int i=0;i<6;i++)
 {
 digitalWrite(Fan[i], LOW); 
}
}
void set(char x)
{
 switch (x)
 {
   case '1':
     n=1;
     break;
   case '+':
     n++;
     break;
   case '-':
     n--;
     break;
   case '0':
     n=0;
     break;
 }
 digitalWrite(Fan[n],HIGH);
} 

Hardware

connect the hardware as shown in the schematic

Happy making. make any other appliance voice activated in similar way :)

Code

arduino sketchArduino
upload this code to your arduino and connect it to you raspberry pi server
int Fan[] = {13,12,11,10,9,8}; 
int n=0;
void reset();
void set(char);

void setup() { 
// put your setup code here, to run once: 
for(int i=0;i<6;i++)
{
 pinMode(Fan[i], OUTPUT); 
}
 Serial.begin(9600); 
} 

void loop() { 
 char x;
if (Serial.available())
{
 x=Serial.read();
 reset();
 set(x);
}
}
 
void reset()
{
 for(int i=0;i<6;i++)
 {
 digitalWrite(Fan[i], LOW); 
}
}

void set(char x)
{
 switch (x)
 {
   case '1':
     n=1;
     break;
   case '+':
     n++;
     break;
   case '-':
     n--;
     break;
   case '0':
     n=0;
     break;
 }
 digitalWrite(Fan[n],HIGH);
} 
Python Server codePython
un this code in your spython after ngrok is setup so as to receive data
from flask import Flask
from flask_ask import Ask, statement
import requests
import json
import serial ser = serial.Serial("/dev/ttyAMA0",9600)
app = Flask(__name__)
ask = Ask(app, '/')
@ask.launch
@ask.intent("FanOn")
def on():    
    ser.write(b'1')    
    return statement("Ceiling Fan turned on.")
@ask.intent("FanOff")
def off():    
    ser.write(b'0')    
    return statement("Ceiling Fan turned off.")
@ask.intent("FanLevelUp")
def up():    
    ser.write(b'+')    
    return statement("Ceiling Fan speed up.")
@ask.intent("FanLevelDown")
def down():    
    ser.write(b'-')    
    return statement("Ceiling Fan speed down.")
if __name__ == "__main__":
    app.run(debug=True)

Schematics

Connection Schematics
this is how you connect the devices together
Testing 2 bttdudidef

Comments

Similar projects you might like

Light Control Using Arduino and Amazon Alexa

Project tutorial by Rajesh

  • 15,636 views
  • 1 comment
  • 17 respects

Monocle: View & Control IP Cameras with Alexa & Arduino

Project tutorial by Team Monocle

  • 3,783 views
  • 2 comments
  • 11 respects

Alexa Based Smart Home Monitoring

Project tutorial by Adithya TG

  • 16,773 views
  • 19 comments
  • 47 respects

Use Alexa to Control Your TV!

Project tutorial by Team Grensom

  • 7,567 views
  • 7 comments
  • 53 respects

Alexa Doorman: Who Is at My Door?

Project tutorial by MD R. Islam

  • 9,187 views
  • 0 comments
  • 25 respects

Animated Smart Light with Alexa and Arduino

Project tutorial by Bruno Portaluri

  • 3,673 views
  • 9 comments
  • 23 respects
Add projectSign up / Login