Tuesday, 28 October 2014

IoT Data Logging using Beaglebone Black and Thingspeak

Did you ever imagine that you could implement Internet of Things aka IoT with just 21 lines of coding? Yes that's true. In this post I will guide you on getting started with IoT and using cloud based services like Thingspeak for online data logging. Lot's of talk so let's get started.



What you need

  • Beaglebone Black
  • USB  cable
  • Bread board
  • Light Dependent Resistor(LDR)
  • 10K Resistor
  • PC with internet connection and Putty installed.
  • Latest Debian distribution of Linux installed.

Setting up your system

  • If you are new to Beaglebone Black(BBB) then I recommend that you visit the learning resources page here.
  • After you have successfully updated your BBB with the latest Debian Image follow the instruction on connecting the BBB to the internet here.
  • If you are not using the latest Debian image then you need to install the Adafruit_BBIO library. You can get the instructions here.
  • You can also follow my previous posts on getting some tutorials on BBB.

Setting up Thingspeak

Thing speak is a free online cloud service that lets it's users to create data channels which they can connect with their devices with the unique API Key generated with each channel. The connected device just need to send an HTTP POST request along with the API key and the data to update the data channels. Don't worry if it looks too complicated. Let's make it simple.
  • Create a Thingspeak account by clicking on getting started. Now you will be asked to register yourself with thingspeak. 

  • After you are registered you will be with a screen like this.
  • Now click on the "New Channel" button. You will have a form like this. 

  • After filling up the Name you can save the channel by clicking on "Save Channel". I have given some extra parameters for understanding but it is not mandatory.
  • Now you will get a window like this. In this window you ca visualize your channel feed. If you want to tweak your window or add some more charts you are free to check out the documentations. For now we will proceed with the basic window.

  • Now Click on the "API Key" tab to open a window like this.

  • Note down the API Key. You will need this in order to connect your device to the newly created channel. I have blotted out my API Key so that it doesn't become public.
  • Now follow the wiring diagram as shown below.

  • You can also try out this experiment here. I have modified the code here for this project.
  • Now start up your Beaglebone Black and Log in with your ID and password. Connect your BBB to the internet.
  • Now open the text editor and copy the following python script.

import Adafruit_BBIO.ADC as ADC  
 import time  
 import httplib, urllib  
 sensor_pin = 'P9_40'  
 ADC.setup()  
 print('Reading\t\tVolts')  
 while True:  
     reading = ADC.read(sensor_pin)  
     volts = reading * 1.800  
     params = urllib.urlencode({'field1': volts,'key':'YOUR API KEY'})  
     headers = {"Content-type": "application/x-www-form-urlencoded","Accept":"text/plain"}  
     conn = httplib.HTTPConnection("api.thingspeak.com:80")  
     conn.request("POST", "/update", params, headers)  
     print('%f\t%f' % (reading, volts))  
     res = conn.getresponse()  
     print res.status, res.reason  
     time.sleep(16)  
  • Replace 'YOUR API KEY' with the API key you noted down previously.
  • Now save the file with .py extension. I have named my file ldr.py
  • Now in the shell type
 python <filename>.py 
  • Replace <filename> with the name you have given.
  • That's it you are done. Now check the chart on your channel Private window where the data gets updated every 15 seconds.
  • This is my chart getting updated here.
If you have any querry regarding this project please feel free to comment. If you like my project please +1 me and share it. Check out my blog for more cool stuff and more IoT projects.


3 comments:

  1. You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!. rfid wristbands

    ReplyDelete
  2. hello how are you
    I am trying to visualize the information transmitted by my beaglebone black, I have already created the api in thinkspeak, but I do not visualize anything.
    I want to ask if at the time of creating the new channel the fields are filled:
    Name myIoTChannel
    Description Room light Monitor
    Tags BeagleBone Black

    Faithful 1 Voltage level
    Only these four spices, or that more must be filled .. pq I have not been able to visualize the inofrmation.
    Garcias for the attention paid.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete