Monday 22 February 2016

My First Raspberry Pi IOT Project

The Source of this Project is from PubNub IOT 101 Tutorial.. Which is A Nice one and you can get it from here...

I Slightly modified that Code to add Some Extra Functionality..

Internet Of Things(IOT) is a Vast Concept(I always think it is a Ocean we can swim it all Our lives)..

You have to Know Web designing.. Internet Connectivity apart from the Basic electronics..

When I Attended the IOT Workshop in the India Electronics Week Organized by EFY(Electronics For You...) I am Clueless where I have to start..

i am just A Electronics Beginner Who is Struggling with arduino and Raspberry pi setup...

So Without Knowing Where to start i try to learn some basic HTML Concepts.. and at that time I came across this pubnub 101 tutorial.. So i start tinkering with it..

At My point of View.. You Don't need to master in web designing to start the IOT project..

You can see the Basic Setup in PubNub iot101 Tutorial...

So the HTML code is


 <!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Getting data from a sensor</title>

</head>

<body>
    <header>
        <h1>Control LED from Web Interface</h1>
        <h2>Publishing from web to control a LED</h2>
    </header>

    <section id="main" role="main">
            <button id = "ON">LED on!</button>
            <button id = "OFF">LED Off!</button>
    </section>


    <!-- including the latest PubNub JavaScript SDK -->
    <script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script>
    <script>
(function() {

    // DOM
    var buttonON = document.querySelector('#ON')
    var buttonOFF = document.querySelector('#OFF');

    // This is the channel name you are subscribing in remote-led.py
    var channel = 'disco';

    // Init - Get your own keys at admin.pubnub.com
    var p = PUBNUB.init({
        subscribe_key: 'sub-c-f762fb78-2724-11e4-a4df-02ee2ddab7fe',
        publish_key:   'pub-c-156a6d5f-22bd-4a13-848d-b5b4d4b36695'
    });

    // Sending data
    function disco() {
    p.publish({
      channel : channel,
      message : {led: 1}
    });
  }

     //Sending Off Command
function LEDOFF() {
    p.publish({
    channel : channel,
    message : {led: 2}
});
}

    // Click event
    buttonON.addEventListener('click', disco);
    buttonOFF.addEventListener('click',LEDOFF);

})();
    </script>
  
</body>
</html>



and the Python Code is

## Web-controlled LED

import RPi.GPIO as GPIO
import time
import sys
from pubnub import Pubnub

GPIO.setmode (GPIO.BCM)

LED_PIN = 4

GPIO.setup(LED_PIN,GPIO.OUT)


pubnub = Pubnub(publish_key='pub-c-156a6d5f-22bd-4a13-848d-b5b4d4b36695', subscribe_key='sub-c-f762fb78-2724-11e4-a4df-02ee2ddab7fe')

channel = 'disco'

def _callback(m, channel):
    print(m)
    if m['led'] == 1:
#        for i in range(6):
            GPIO.output(LED_PIN,True)
            print('blink')
    elif m['led'] == 2:
            GPIO.output(LED_PIN,False)
            print('Off')

def _error(m):
    print(m)

pubnub.subscribe(channels=channel, callback=_callback, error=_error)



You can Download the All the Source Code In my Github Repository Here

Output :

No comments:

Post a Comment