[IOT Tutorial]Control Your Fan using Pubnub API


Hi All,

Today I’m going to show you a nice tutorial about controlling things remotely from Internet.target audience for this tutorial are engineers interested in the IOT trend .

Things i will be using in this tutorial

  • breadboard
  • Raspberry pi 2
  • Infrared TX and RX
  • Resistors
  • Jumper female to male wires
  • a remote controlled fan

on the software side we will use the following.

  • Python
  • HTML,CSS and Javascript

I think all the above is pretty clear for most people.

one thing i want to stress on is pubnub API.

Pubnub API

PubNub is a cloud-based, real-time messaging service that functions on mobile phones and web browsers. It is a Push technology intended specifically for high-message-volume applications and games. The API allows users to publish and subscribe to messages. in other words it allows you to control things from the Internet using persistent socket.

before going forward in this tutorial i recommend to visit the below link for pubnub tutorial,


during the tutorial we will utilize pubnub API using python and Javascript.

for people who are lazy enough to go through the tutorial, this photo best describes the API.


the cloud is capable of sending messages by Publisher and the corresponding subscriber can receive it. the corresponding subscriber can know that the message is intended to him using publisher and subscriber keys so he receives them and drop other unrelated messages. thats enough for pubnub, let’s continue.


So for all electronics geeks, IR TX and RX are not a rocket science. they are used to transmit and receive signals with certain frequency and pattern, I’m not going in to details but all you need to know that Receiver can only receive signal from transmitter by having a line of sight. and transmitter should send unique signals to receiver in order to differentiate between for example LG from Sony TV.

Hardware Setting up receiver

First thing required is to setup the raspberry pi with the IR receiver. as in below picture IR receiver has 3 pins

  1. Output
  2. GND
  3. VCC



below is a physical photo of the IR receiver with Raspberry pi.


LIRC Library

LIRC is a mature and stable open source library that provides the ability to send and receive IR commands.LIRC support is now included by default in the latest version of Raspbian OS. before going through follow the instructions in this link to install Lirc on your raspberry pi 2.


The target now is to make the IR TX receive buttons from the remote control of the fan, record them and then send the same pattern using the IR TX.


to capture the button and record it ,a perfect utility come with LIRC is called irrecord is suitable for the job. before using the utitlity one note you have to consider, during configuring LIRC you have to determine which pin in the Raspberry pi will be the Input Pin that receives the IR signals from the remote, in our case it’s GPIO18 as in previous screenshots, hence in the LIRC configuration files edit /boot/config.txt with the input and output pin used in your hardware setup, in my case it was


now let’s use IRRECORD. for example let’s record the ON/OFF button of the fan.

run the below command

irrecord -d /dev/lirc0 ONOFFButton.conf

this will record the button signal and store them in ONOFFButton.conf, after running the command just follow the instructions written until the configuration file is generated.

begin remote

  name  tornado
  flags RAW_CODES
  eps            30
  aeps          100

  gap          8979

      begin raw_codes

          name KEY_STOP
             8989    4500     557    1734     506     597
              536     647     486     648     460     597
              536     601     532     601     507     578
              555     580     553    1686     555    1712
              528    1714     553    1685     556    1713
              527    1714     554    1685     555    1714
              527    1712     555     580     527     578
              555     600     507     627     507     578
              555     602     504     628     507     578
              563    1705     527    1714     553    1685
              556    1713     527    1721     546    1686

      end raw_codes

end remote

Two things are important in this file

  1. name tornado (this is the name of the remote)
  2. name KEY_ONOFF (this is the name of the Button)

the same way you can create a configuration file for any button on your remote control,Now we are finished with the receiver, let’s go through the transmitter

Hardware Setting up Transmitter

The transmitter is Two Pins

  1. GPIO
  2. GND


Noting that the output pin has to be  wired to GPIO22 as we configured in LIRC configuration  file


Sending IR Signal

Next to send the IR signal a perfect utility that comes with raspberry pi is called irsend.

before using irsend, the output of the ONOFFButton.conf is placed in the following file


Next simply run the command with your IR TX pointed and close to the fan.

irsend send_once tornado KEY_ONOFF

tornado and KEY_ONOFF are the names of the remote and button defined in the configuration file.

Running the command will power on the fan, running it again will close it.

Pubnub Publisher and Subscriber

our target is to give the raspberry pi a remote signal using java script by utilizing the pubnub API to the subscriber(raspberry pi) which will be utilizing pubnub API using Python.

Publisher Code

First the publisher will be our simple Website which will be used to push messages remotely to the raspberry pi to toggle the fan ON/OFF. i will remove CSS part from the code as it’s unnecessary beside I’m not a developer so don’t get scared from the Website 😀 i will only mention the important parts of the code.

Before going through the code make sure you have read the PUBNUB tutorial link previously

<meta charset="utf-8"/>
<!--Creating a Button-->
<button id="ToggleFan"><span>FAN Control!!!!</span></button>   
<!--loading the pubnub API-->
var button = document.querySelector('button');
var channel = 'sakr';

<!--Initializing the Publisher-->
    var p = PUBNUB.init({
        subscribe_key: 'sub-c-be3145da-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        publish_key:   'pub-c-12cbec68-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

<!--Sending Message to subscriber(FanoNorOff)-->
function ToggleMyFan() {
      channel : channel, 
      message : {Action: "FanoNorOff"}

<!--Adding Event Handler to the ToggleFan button-->
  document.getElementById("ToggleFan").addEventListener('click', ToggleMyFan);

Now my website looks like this Selection_138.png

Subscriber Code

Now let’s go through the subscriber code which will be Python running on our raspberry pi.

import sys
##Importing the PUBNUB Module###
from pubnub import Pubnub
import os
import RPi.GPIO as GPIO
GPIO.setup(22, GPIO.OUT) 
#######Publisher and subscriber Keys######
pubnub = Pubnub(publish_key='pub-c-12cbec68-xxxx-xxxx-xxxx-xxxxxxxxxxx'
, subscribe_key='sub-c-be3145da-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

######Command used to Toggle FAN#########
ToggleFan='irsend send_once tornado KEY_ONOFF'
#######Function that will be used when receiving msg from publisher#####
###When the Subscriber receives message FanONorOff it will use irsend###
def _callback(m, channel):
    if m['Action']=="FanoNorOff":

def _error(m):
#########Initialize Subscriber#############
pubnub.subscribe(channels=channel, callback=_callback, error=_error)

Running The Project

After running the subscriber first using python. Then pushing on the Toggle Fan button on our website, our Raspberry pi will show this output, and at the same time if your wiring is correct and your IT TX has a line of sight with the fan, Your fan will be toggled to ON and if you press the button again it will be turned OFF


below is a 30 seconds video that shows the implementation

Hope you liked My tutorial Don’t hesitate to write back if you have any comments or questions.

Good Bye 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *