# Send Raspberry Pi Data to COSM

## Overview

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/001/350/medium800/raspberry_pi_pi-with-temp-sensor.jpg?1410964052)

The combination of connecting a Raspberry Pi to COSM makes creating a internet of things much easier than it has been in the past. The Pi with it's easy access to ethernet / WiFi and COSM's drop dead simple usability will graph all sensor data you send to it.   
  
This tutorial explains how to connect a analog temperature sensor to the Pi and use a small python script to upload that data for storage and graphing on COSM.

# To follow this tutorial you will need

- [MCP3008 DIP-package ADC converter chip](https://www.adafruit.com/products/856 "Link: https://www.adafruit.com/products/856")  
- [Analog Temperature Sensor TMP-36](http://www.adafruit.com/products/165 "Link: http://www.adafruit.com/products/165")
- [Adafruit Pi Cobbler](https://www.adafruit.com/products/914 "Link: https://www.adafruit.com/products/914") - follow the tutorial to assemble it
- [Half](https://www.adafruit.com/products/64 "Link: https://www.adafruit.com/products/64") or [Full-size breadboard](https://www.adafruit.com/products/239 "Link: https://www.adafruit.com/products/239")
- [Breadboarding wires](https://www.adafruit.com/category/82 "Link: https://www.adafruit.com/category/82")
- Raspberry Pi with a internet connection

_Hey, that photo up there has the GPIO cable in backwards - so when you wire it up don't follow that pic!_  

# Send Raspberry Pi Data to COSM

## Connecting the Cobbler to the MCP3008 and TMP36

Info: 

# Why we need an ADC
  
The Raspberry Pi computer does not have a way to read analog inputs. It's a digital-only computer. Compare this to the Arduino, AVR or PIC microcontrollers that often have 6 or more analog inputs! Analog inputs are handy because many sensors are analog outputs, so we need a way to make the Pi analog-friendly.  
  
We'll do that by wiring up an&nbsp;[MCP3008 chip](https://www.adafruit.com/products/856)&nbsp;to it. The&nbsp;[MCP3008](https://www.adafruit.com/products/856)&nbsp;acts like a 'bridge' between digital and analog. It has 8 analog inputs and the Pi can query it using 4 digital pins. That makes it a perfect addition to the Pi for integrating simple sensors like&nbsp;[photocells](http://learn.adafruit.com/photocells),&nbsp;[FSRs](http://learn.adafruit.com/force-sensitive-resistor-fsr)&nbsp;or&nbsp;potentiometers,&nbsp;[thermistors](http://learn.adafruit.com/thermistor), etc.!  
  
 [Lets check the datasheet of the MCP3008 chip.](http://www.adafruit.com/datasheets/MCP3008.pdf)&nbsp;On the first page in the lower right corner there's a pinout diagram showing the names of the pins.![](https://cdn-learn.adafruit.com/assets/assets/000/001/353/medium800/raspberry_pi_cosm-mcp-3008.gif?1447977183)

# Wiring Diagram
  
In order to read analog data we need to use the following pins: **VDD** (power), **DGND** (digital ground) to power the MCP3008 chip. We also need four 'SPI' data pins: **DOUT** (Data Out from MCP3008), **CLK** (Clock pin), **DIN** (Data In from Raspberry Pi), and / **CS** (Chip Select). Finally of course, a source of analog data, we'll be using the TMP36 temperature sensor  
  
The MCP3008 has a few more pins we need to connect: **AGND** (analog ground, used sometimes in precision circuitry, which this is not) connects to **GND** , and **VREF** (analog voltage reference, used for changing the 'scale' - we want the full scale so tie it to **3.3V** )  
  
Below is a wiring diagram. Connect the 3.3V cobbler pin to the left + rail and the GND pin to the right - rail. Connect the following pins for the MCP chip  

- MCP3008 VDD -\> 3.3V (red)
- MCP3008 VREF -\> 3.3V (red)  
- MCP3008 AGND -\> GND (green)  
- MCP3008 CLK -\> #18   
- MCP3008 DOUT -\> #23 
- MCP3008 DIN -\> #24
- MCP3008 CS -\> #25 
- MCP3008 DGND -\> GND (green)

Advanced users may note that the Raspberry Pi does have a hardware SPI interface (the cobbler pins are labeled MISO/MOSI/SCLK/CE0/CE1). The hardware SPI interface is super fast but not included in all distributions. For that reason we are using a bit banged SPI implementation so the SPI pins can be any of the raspberry pi's GPIOs (assuming you update the script). Once you get this project working with the above pinout, feel free to edit the python code to change the pins as you'd like to have them!  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/354/medium800/raspberry_pi_temp-sensor-wiring.jpg?1410964091)

# TMP36
Finally the TMP36 has three pins that need to be connected. They are numbered from left to right in ascending order when the text of the sensor is facing you.  

- pin1: 3.3v&nbsp;
- pin2: analog out --\> channel0 on mcp3008 (pin1)
- pin3: gnd

  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/355/medium800/raspberry_pi_tmp36pinout.jpeg?1410964095)

# Send Raspberry Pi Data to COSM

## Necessary Packages

Info: 

This guide is based on Debian's "Wheezy" release for Raspberry Pi. It was made available in&nbsp;Mid July 2012. The following items must be installed in order to utilize the Raspberry Pi's GPIO pins and to upload&nbsp;data to COSM.&nbsp;  
  
Add the latest dev packages for Python (2.x)  
```auto
sudo apt-get install python-dev
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/360/medium800/raspberry_pi_cosm-python-dev.gif?1447977197)

Upgrade distribute (required for RPi.GPIO 0.3.1a) - [No image for this one]

```auto
sudo easy_install -U distribute
```

Install python-pip (Pip Installs Packages, python packages)

```auto
sudo apt-get install python-pip
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/362/medium800/raspberry_pi_cosm-python-pip.gif?1447977212)

Install rpi.gpio (0.3.1a) or later

```auto
sudo pip install rpi.gpio
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/361/medium800/raspberry_pi_cosm-rpi-gpio.gif?1447977204)

Download EEML - markup language COSM accepts

```auto
wget -O geekman-python-eeml.tar.gz https://github.com/geekman/python-eeml/tarball/master
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/359/medium800/raspberry_pi_cosm-eeml-dl.gif?1447977190)

Extract the EEML tarball

```auto
tar zxvf geekman-python-eeml.tar.gz
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/363/medium800/raspberry_pi_cosm-eeml-extract.gif?1447977219)

Change into the directory and install the EEML python package

```auto
cd geekman-python-eeml*
sudo python setup.py install
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/364/medium800/raspberry_pi_cosm-eeml-install.gif?1447977227)

# Send Raspberry Pi Data to COSM

## COSM Account and Feed

Info: 

COSM (used to be Pachube) helps connect little devices like the raspberry pi to the internet. You will need to do the following to use COSM.

- Setup a Account
- Create a Feed
- Save the API\_KEY
- Save the FEED ID

  
# Setup a Account
You will need to create a COSM account. Click on the blue "Get Started" circle to create a new account. It's your typical e-mail/password followed by password verification. You will need to check your e-mail and click the verification link. ![](https://cdn-learn.adafruit.com/assets/assets/000/019/695/medium800/raspberry_pi_cosm-signup.jpg?1410968098)

# Add a Feed
Click the blue plus to add a feed.&nbsp;  
![](https://cdn-learn.adafruit.com/assets/assets/000/019/696/medium800/raspberry_pi_cosm-add-feed.jpg?1410968150)

Select Arduino

![](https://cdn-learn.adafruit.com/assets/assets/000/019/697/medium800/raspberry_pi_cosm-something-else.jpg?1410968188)

Give your new&nbsp;feed a title and tags.  
  
Title: "Raspberry Pi Temperature" _(or whatever you like)_  
Tags: raspberry pi, temperature, adc _(or make up your own)_  
&nbsp;  
Select the "Create" button.

![](https://cdn-learn.adafruit.com/assets/assets/000/019/698/medium800/raspberry_pi_cosm-configure-arduino.jpg?1410968231)

You need to extract the API\_KEY and FEEDID from the code sample that COSM provides. These will go into the python script that we setup on the next page. The API\_KEY lets&nbsp;COSM knows who is connecting and to which feed they want to send data.  
  
In this example the API\_KEY is:&nbsp;5RNOO3ShYJxYiq2V2sgSRtz3112SAKxFQjNDQmNXc0RScz0g  
The FEEDID is: 68872  
  
Do not use those numbers, use your own!

![](https://cdn-learn.adafruit.com/assets/assets/000/019/699/medium800/raspberry_pi_cosm-api-key-feedid.jpg?1410968272)

# Send Raspberry Pi Data to COSM

## Python Script

Info: 

# The Code
This 100+ line python script can be pasted into a editor and saved on your raspberry pi.  
  
The script is fairly simple. Half of the code (the **readadc** function) is a function that will 'talk' to the MCP3008 chip using four digital pins to 'bit bang' the SPI interface (this is because not all Raspberry Pi's have the hardware SPI function).  
  
The MCP3008 is a 10-bit ADC. That means it will read a value from 0 to 1023 (2^^10 = 1024 values) where 0 is the same as 'ground' and '1023' is the same as '3.3 volts'. We don't convert the number to voltage although its easy to do that by multiplying the number by (3.3 / 1023).  
  
Every 30 seconds we:  

- read the adc value on channel 0 (temperature sensor)  
- convert the adc value to millivolts: **millivolts = read\_adc0 \* ( 3300.0 / 1023.0 )**  
- convert the millivolts value to a celsius temperature: **temp\_C = ((millivolts - 100.0) / 10.0) -**  **40.0**
- convert the celsius temperature to a fahrenheit temperature: **temp\_F = ( temp\_C \* 9.0 / 5.0 ) + 32 )**
- then send the data up to pachube to be saved and graphed

http://gist.github.com/3249416

# Feeds and Keys
Update the API\_KEY and FEED values to the ones that COSM provided you.

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/001/370/medium800/raspberry_pi_cosm-code-update-feedid-api-key.gif?1447977284)

# Run it!
Now that you have the code modified with your keys, go ahead and make the file executable.

```auto
$ chmod +x adafruit-cosm-temp.py
```

Run the script. With DEBUG = 1 (default) you will see of the adc0 value, millivolts, celsius and fahrenheit on sent to your terminals STDOUT. These same values are also being sent up to COSM.&nbsp;

```auto
$ sudo ./adafruit-cosm-temp.py
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/371/medium800/raspberry_pi_cosm-run-cmd.gif?1447977291)

Info: 

# Send Raspberry Pi Data to COSM

## COSM Graph View

Info: 

This is how COSM [displays the temperature we are sending it](https://cosm.com/feeds/68872).&nbsp;We can see both celsius and fahrenheit temperature graphs.&nbsp;The graphs have independent sliders&nbsp;so it can easily be adjusted from minutes to weeks to months. There are a lot of fun settings for viewing the graph data.   
  
A really cool feature is that you can have triggers go off based on the data values. COSM will alert you via HTTP POST or Twitter so that you can setup alarms if things go bad. If we connected up more sensors the MCP3008 we could easily have more graphs appear.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/019/694/medium800/raspberry_pi_cosm-pi-graph.jpg?1410966489)


## Related Guides

- [Pi Box](https://learn.adafruit.com/pi-box.md)
- [MCP3008 - 8-Channel 10-Bit ADC With SPI Interface](https://learn.adafruit.com/mcp3008-spi-adc.md)
- [PyPortal IoT Weather Station](https://learn.adafruit.com/pyportal-iot-weather-station.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-python-and-adafruit-io.md)
- [Ladyada's Toolkit](https://learn.adafruit.com/ladyadas-toolkit.md)
- [CLUE Metal Detector in CircuitPython](https://learn.adafruit.com/clue-metal-detector-circuitpython.md)
- [Piper Make With the Raspberry Pi Pico](https://learn.adafruit.com/piper-make-with-the-raspberry-pi-pico.md)
- [Esenciales para CircuitPython](https://learn.adafruit.com/esenciales-para-circuitpython.md)
- [Trellis Feather DSP-G1 Synthesizer](https://learn.adafruit.com/feather-trellis-dsp-g1-synthesizer.md)
- [Adafruit HUZZAH32 - ESP32 Feather](https://learn.adafruit.com/adafruit-huzzah32-esp32-feather.md)
- [Quickstart - Raspberry Pi RP2040 with BLE and CircuitPython](https://learn.adafruit.com/quickstart-raspberry-pi-rp2040-with-ble-and-circuitpython.md)
- [Using LoraWAN and The Things Network with Feather](https://learn.adafruit.com/the-things-network-for-feather.md)
- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [Scrambled Number Security Keypad](https://learn.adafruit.com/scrambled-number-security-keypad.md)
- [Adafruit MLX90640 IR Thermal Camera](https://learn.adafruit.com/adafruit-mlx90640-ir-thermal-camera.md)
- [Schluff - The Sleep Monitor](https://learn.adafruit.com/schluff-the-oshw-sleep-monitor.md)
- [Make an AI Freezer Monitor](https://learn.adafruit.com/ai-freezer-monitor.md)
