November 23, 2016

Internet of Things with Thingspeak #8

Hai everybody....long time no see...

Let's begin the topic of connecting our Raspberrypi to Internet


Let's cook some IoT 

Here is a easy way to connect your raspberry pi with Internet; also say it as IoT

Now let's get started with thingspeak.....(opensource- free tool )


Step 1: Install these packages to start your first IoT


~ thingspeak
~ psutil package : it is a cross-platform library for retrieving information onrunning processes and system utilization (CPU, memory, disks, network)  - install this only if needed

just type these in to your terminal


sudo pip install thingspeak

<when it is done installing, go on with the below>

sudo pip install psutil

Step 2: After Installing this you need to setup thinkspeak


so visit the thingspeak website  https://thingspeak.com   and sign up

then get started by creating a New channel 

Name your channel such as "MyfirstIoT" or something


select the number of fields you needed ; for example i need to create a program to note distance measured from ultrasonic sensor.


          In-order to store the distance only one field is enough as shown below, which you can rename it as you wish

also you can find the Make Public option, which you have to enable it, (to make it seen by everyone)
finally save the channel

the below image is the confirmation window that your channel is set

   


















Here you should make a note of Channel ID (Highlighted)

And open the API Keys 

 Note down the Write API as shown

Step 3: After Making note of it, we can move on to programming again i.e same old ultrasonic we are taking about before but with some little modification

import RPi.GPIO as GPIO        #Imporing  GPIO module
import subprocess                     #Importing Sub-process Modules, which will be discussed later
import time                                #Importing time modules
import thingspeak                # importing thingspeak libraries


channel_id = "190090"
write_key  = "W0BY8W15LGUEEC0F"    # Here i used mine, instead use your own channel id and API
channel = thingspeak.Channel(id=channel_id,write_key=write_key)

# Here the channel information and Write API is taken to proceed the process

GPIO.setmode(GPIO.BCM)    #  Setting up a mode of operation

TRIG = 23                                #  Assigning a pin no. for Trigger
ECHO = 24                              #  Assigning a pin no. for Echo

GPIO.setup(TRIG,GPIO.OUT)   #  Setting up a trigger pin
GPIO.setup(ECHO,GPIO.IN)     #  Setting up a echo pin
GPIO.output(TRIG, False)

print "Waiting For Sensor To Settle"   
time.sleep(2)                              #  Wait for 2 seconds

while(True):                                #  Start a loop to process this continuously
    GPIO.output(TRIG, True)      #  Start a trigger 
    time.sleep(0.00001)                 #  To make the trigger i frequency
    GPIO.output(TRIG, False)     #  Stop a trigger
    while GPIO.input(ECHO)==0:  # loop to check the status of  echo pin
        pulse_start = time.time()         # Note the initial pulse release time
    while GPIO.input(ECHO)==1:    # loop to check the status of  echo pin again
        pulse_end = time.time()           # Note the final  pulse reach time
    pulse_duration = pulse_end - pulse_start  # finding the travel time of a pulse by taking difference

    distance = pulse_duration*17150   # calculating distance
    distance = round(distance, 2)          # rounding off 
    print "Distance:",distance,"cm"       # printing Output

    time.sleep(1)      # to avoid rapid changes...you can increase it if needed.
    try:
        response = channel.update({distance})
        print response
    except:
        print "connection failed"

# Sometimes the channel may not be connected due to network traffic, or failed internet connections, Since the try-exception methodology is always recommended to be used with thingspeak interface programming as shown above. warning: be conscious about the  intent space

GPIO.cleanup()


Now, run this program and see the magic; open up your thingspeak anywhere in the world ..you can see your sensor data stored in there......

you can find more examples here.

try everything

for any doubts comment me...


bye
--------
Arun

No comments:

Post a Comment