Sunday, December 9, 2012

Script for uploading DHT11 readings to Cosm using Curl/JSON on Raspberry Pi running Arch Linux

Earlier, I have written on how to get the Vodafone 3G USB Modem (K3770-Z) working on Raspberry Pi here. After I was done doing that, I decided to create a shell script to upload sensor data from DHT11 to cosm.com over the internet connection provided by the Vodafone 3G USB Modem.




Here are is how I got that done:
NOTE:  Execute all commands on Raspberry Pi unless otherwise specified. I referred to the following two Adafruit tutorials while doing this: DHT Humidity Sensing on Raspberry Pi with GDocs Logging and end Raspberry Pi Data to COSM

  1. Assemble the setup as shown in the photo above and follow the instructions here (Getting Vodafone 3G to work on Raspberry Pi in India using K3770-Z) to get the Vodafone 3G USB Modem working on your Raspberry Pi. I use the Arch Linux distribution. Make sure to create sakis3g.conf to automate the Sakis3G script. Also ensure that the sakis3g script is in your root's home directory (/root) and has the execute bit set before your proceed. Your Raspberry Pi will need internet access to download some of the packages and files in the following steps.
  2. Install the development packages (includes compiler, libraries and related utilities to compile programs on Raspberry Pi for Raspberry Pi:
    pacman -S base-devel
  3. Next we need to install a library which will allow us to control GPIO pins of Raspberry Pi from our C programs: bcm2835 library. The latest version of this library at the time of writing this was 1.14. Execute the following commands to download the source, compile and install the library. Do this while logged in as root and while in the root's home folder (/root):
    wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.14.tar.gz
    tar -zxvf bcm2835-1.14.tar.gz
    cd bcm2835-1.14
    ./configure
    make
    make install
  4. Now we need the C program source and it Makefile which will read the temperature and humidity values from DHT11 sensor (these files are modified versions of sources taken from Adafruit's repository on github):
    cd ~

    wget 'https://raw.github.com/lithiumhead/RPi-cosm-curl-DHT11/master/DHT.c'

    wget 'https://raw.github.com/lithiumhead/RPi-cosm-curl-DHT11/master/Makefile'
  5. Compile the C program:
    make
  6. And execute it (assuming you too are using DHT11 connected to pin 4 of Raspberry Pi with a 4.7K pull-up):
    ./DHT.out 11 4
  7. If the program executed successfully, you will see the output similar to:
    OK 28 36
    where OK specifies that the communication with DHT11 was successful (if not, there will be no output displayed on the screen) and the two numbers after that: 28 and 36 are temperature (in Celsius) and relative humidity (in %) readings respectively.
  8. Next step is to install curl:
    pacman -S curl
  9. Next download the bash script which will upload the readings to cosm.com:
    wget 'https://raw.github.com/lithiumhead/RPi-cosm-curl-DHT11/master/cosm-curl-DHT11.sh'
  10. Edit this script and change the values of the API_KEY and the FEED_ID to match those of your own feed. You may also want to update the various parameters of the JSON structure in the script for your own specific settings: title, website, lat, lon etc..:
    nano cosm-curl-DHT11.sh
  11. Ctrl+O to save the file 
  12. Ctrl+X to exit
  13. Set the execute bit on the script:
    chmod +x cosm-curl-DHT11.sh
  14. And execute the script:
    bash cosm-curl-DHT11.sh
  15. The script connect to the internet using the Vodafone 3G USB Modem and start uploading the readings to cosm.com every 10 seconds.
  16. You can stop the script by pressing Ctrl+C
Output of the cosm-curl-DHT11.sh script executing on Raspberry Pi as observed over ssh terminal


NOTES:
  • DHT11 doesn't always reply with valid temperature and humidity data and so sometimes the C program which tries to read data from it will not respond with any temperature and humidity values. In such cases, the script will simply wait for 10 more seconds before trying to execute the program (DHT11.out) again
  • For instructions on how to create a new feed on cosm.com, refer to this section from Adafruit's tutorial on Sending Raspberry Pi Data to COSM. It is essential that you select the device/feed as Arduino if you want to see the feed ID and the API key for the new feed that you are creating.

0 comments:

Post a Comment