Skip to content

vikassingh281/RPi-DeviceHive-Tutorial-Python

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RPi-DeviceHive-Tutorial-Python

Tutorial that shows how to read from a temperature sensor and control a light using DeviceHive cloud platform

Tutorial is based on example located at http://www.devicehive.com/samples/python-and-raspberry-pi-temperature-sensor.

================ Assemble Circuit

  1. Please see diagram at the top of http://www.devicehive.com/samples/python-and-raspberry-pi-temperature-sensor
====================== Configure Raspberry Pi ======================
  1. Make sure you have the latest packages installed on the Pi
  2. sudo apt-get update
    sudo apt-get upgrade
    
  3. Load 1-wire kernel modules that come pre-installed but not loaded:
  4. sudo modprobe w1-gpio
    sudo modprobe w1_therm
    
  5. Add lines w1-gpio and w1_therm into /etc/modules using the following code so they get loaded automatically the next time you restart.
  6. sudo nano /etc/modules
    

    Below is the the contents of the file /etc/modules/ with the two lines added.

    # /etc/modules: kernel modules to load at boot time.
    #
    # This file contains the names of kernel modules that should be loaded
    # at boot time, one per line. Lines beginning with "#" are ignored.
    # Parameters can be specified after the module name.
    
    snd-bcm2835
    w1-gpio
    w1_therm
    
  7. Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a
  8. Test the sensor, by printing out it’s output: cat /sys/bus/w1/devices/28-00000393268a/w1_slave
===================================== Installing DeviceHive on Raspberry Pi =====================================
  1. Go to home/pi directory
  2. Git the latest code from this repository
  3. git clone https://github.com/mvartani76/RPi-DeviceHive-Tutorial-Python
    
  4. Go to devicehive.com/playground and create yourself an account for a playground if you don’t have one.
  5. On your RasPi edit ~/devicehive/examples/raspi_led_thermo.py and change _W1_FILENAME and _API_URL url (usually http://nnXXX.pg.devicehive.com/api if created using playground) to match the environment, using nano editor.
  6. Install Twisted and DeviceHive device libraries. The DeviceHive python library is built using twisted engine. A very powerful tool to build protocol and connectivity libraries.
  7. sudo apt-get install python-twisted
    ~/devicehive run sudo python setup.py install
    
  8. Run the script:
  9. sudo python ~/devicehive/examples/raspi_led_thermo.py
    
================================ Access your device from Admin UI ================================
  1. After a first run of the script you can see the device and the readings from the sensor in Admin UI (http://nnXXX.pg.devicehive.com/admin, login as admin / your_playground_password). Go to Devices tab, and click “detail” for your device), then go to “notifications”.
  2. You can also send on/off messages to the LED: go to “commands” for your device, click “enter new command”, set name as UpdateState and parameters as {"equipment":"LED","state":"1"}, then click “push”. The LED will turn on. Sending the parameters as {"equipment":"LED","state":"0"} will turn the LED off.
================= Create Client App =================
  1. Login to the DeviceHive administrative console. Go to the Users tab and create new user of the Client role. Click on the Networks button for this user and grant access to the your device network.
  2. Now, it’s time for the actual client app. Let’s use JavaScript. The client sample code to match your installation of playground. For example go to ~/examples/RasPiClient.
  3. Open index.html file. Find the line where new DeviceHive instance is created and insert your assigned DeviceHive service URL; login and password of the client user you created on step 1. As a result, it should look like the following:
  4. var deviceHive = new DeviceHive("http://yourinstance.pg.devicehive.com/api", "myclientuser", "mypassword");
    app.start(deviceHive, "9f33566e-1f8f-11e2-8979-c42c030dd6a5");
    
    The GUID specifies unique identifier of the device we will be interacting with, it does not have to be changed if you use sample devices from DeviceHive sources.
  5. Open index.html in your browser. The application should now work, displaying current LED state, and allowing you to switch on/off the LED and get temperature readings.

About

Tutorial that shows how to read from a temperature sensor and control a light using DeviceHive cloud platform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published