loading

It is the age of the IoT.Each day more and more people are connecting their devices to the cloud. So it seemed like doing injustice to the temperature/pressure sensors and a raspberry pi B+ (an almost 1GHz pumped up beast) that were lying around the computer table. They needed to be put to good use , didn't they ? So the hardware was arranged . But in the Internet Scheme of things, both hardware and software need to collaborate,so it was time to select a software cloud platform. I for one, found Thingspeak platform extremely convenient, owing to it's simple methodology.And then the journey started.....

Step 1: Parts Needed to Work the Magic

1) Raspberry Pi B+(or RPi2) with latest Raspbian image installed

2) BMP180 (Temperature and Pressure sensor)

3) 4 male-to-male headers,an ethernet cable (or WiFi dongle) and power cable for the Raspberry Pi

4) A linux-based PC/Laptop to remotely connect to Raspberry Pi (or Putty on Windows)

Step 2: A Little Bit Theory Wouldn't Hurt(this Part Can Be Skipped by the Pure Action Lovers)...

Now the raspberry pi is the intermediary that connects the pressure sensing device( BMP180 ) to the cloud (i.e is Thingspeak) . So the connections are as follows:-

1) Raspberry Pi to BMP180 - The communication protocol used in this connection is the standard I2C protocol (Two Wire Interface) i.e a master-slave configuration essentially . This includes two main pins namely

  • SDA(Serial Data) - This pin actually does the talking between the two devices.
  • SCL(Serial Clock) - This pin decides the baudrate(speed of data transfer) between the two components( RPi and BMP180)

The I2C protocol follows a 7-bit addressing mode and sometimes 10-bit addressing (BMP has address 0x77), working at around standard speed of 100KHz (standard mode). Philips( they designed this protocol ) allows for different modes of working of up to 400Khz(fast mode),3.4MHz(hi-speed mode) and even up to 5MHz(ultra-high mode). What we will be making use of is "System Management Bus" or 'SMBus' (an Intel variant of I2C).

To start communication,the master pulls SDA low (leaving SCL high) which alerts all the slaves connected.It then starts transmitting data.

To stop communication,the master toggles SCL (0 to 1) and leaves it high, this is followed by SDA toggle condition (0 to 1).

2) Raspberry Pi to Thingspeak - The RPi upload temperature/pressure values (from step1) to the thingspeak channel (that we will be creating in a minute) by POSTing values to an custom API key that they provide.

Now the question that turns out is what "POST" really is . So I would like to go back in the networking basics a little bit . We all know that computers use the TCP/IP model to communicate over the internet. The topmost layer of this model is the Application layer that tells if we are requesting for some information or providing the same.Many protocols like HTTP,OMAP,SMTP,POP3 and many more are available at this layer. To get/send a page (or page info) we stick to the HTTP protocol. So POST is the HTTP method of submitting data to a specified source (here submitting sensor data to thingspeak )

Step 3: The Connections

Again the connections are made with respect to the Raspberry Pi. They are

1) Raspberry Pi to BMP180 - The four connections that need to be made between the master (Raspi ) and slave(BMP180) are -

  1. Raspberry Pi SDA --------------> BMP180 SDA
  2. Raspberry Pi SCL --------------> BMP180 SCL
  3. Raspberry 3.3V ----------------->BMP180 Vin (Input pin of BMP180 must be connected to 3.3V not 5V)
  4. Raspberry Gnd ----------------->BMP180 Gnd

2) Raspberry Pi to Thingspeak - An internet connection is needed for this part.I do suppose you have one as you are this :P. The raspberry pi can connect to your home router via either ethernet or wifi. You will need to check with the wifi adapters that raspberry pi supports in order to get your device online.

Step 4: The Account Creation

After the wiring is all done with, we proceed to Thingspeak site to create our channel (first we will need to create an account).Following are the steps needed to complete the channel creation.

  1. Sign Up at Thingspeak.
  2. Create a channel by clicking on "Create Channel"
  3. Check the fields 1 and 2 and provide them with a name we will use as an identifier later('temperature' and 'pressure' for example without the inverted commas).
  4. Before clicking 'Save Channel', you can select 'Make Public' checkbox if you wish to make your data publicly visible (I didn't).
  5. Click the API tab, here you will find your write API key to be used while sending data to the Thingspeak Channel (copy the write API key).
  6. Click on the Private tab (else public tab if you selected 'Make Public' checkbox in step 4).
  7. Again click on "Add Visualizations" and select both the fields (fields 1 and 2 ) and we are done here (atleast !).

Step 5: Enable the I2C Bus

To enable the Raspberry Pi communication with the BMP180,you must follow the instructions below:-

  1. Type "sudo raspi-config" on the cmdline
  2. Goto Advanced options--->I2C
  3. It will ask to add the i2c module , click yes
  4. It will again ask for default loading of I2C kernel module, again click yes

An excellent tutorial is available here.

You can find more on enabling i2c by directly searching online.

Step 6: The Code

Now with Account and Channel all setup. We move to the final part of writing code on the RasPi to send regular updates on the Thingspeak channel.The code reads temperature and pressure from the BMP180 and sends to Thingspeak channel making use of the Custom write API key.

You can download the file or follow below instructions-

  • You must log in Raspberry Pi via SSH

  • on command line make a folder (choose a name for it ) and cd (change directory) into it.
  • write the following command "git init" (install git if not present) followed by "git clone https://bitbucket.org/snippets/aslan_ali/bKKkM"
  • cd into the newly created folder "bKKkM" and check for "Thingspeak_bmp180.py" file
  • edit file using nano editor to add your write API key in params inside the loop of main() function (as superuser)
  • save and run the python script using "sudo python Thingspeak_bmp180,py"
  • go to thingspeak channel and check inside private view tab, you will see the graphs updating accordingly(with some delay I suppose around 10 seconds)

The code is written in Python on the Raspberry Pi (as a matter-of-factly) . The code snippets do mainly all the heavy-lifting .

  • RPi gets temperature/pressure values from BMP sensor - The python script main() contains a readBMP180() function to read both temperature and pressure values.

bus=smbus.SMBus(1) #RPi rev2 uses 1
 #Read Temperature
bus.write_byte_data(addr,REG_MEAS,CRV_TEMP)#sending temperature control command to BMP180 inquiring temperature 
time.sleep(0.005)#wait for 5ms
(msb,lsb) = bus.read_i2c_block_data(addr,REG_MSB,2)
UT = (msb<<8) + lsb # unrefined 16-byte temperature is generated by adding lsb and 8bit left-shift msb

#Read Pressure
bus.write_data_byte(addr,REG_MEAS,CRV_PRES + (OVERSAMPLE<<6))
time.sleep(0.04)
(msb,lsb,xsb) = bus.read_i2c_block_data(addr,REG_MSB,3)
UP = ((msb<<16) + (lsb<<8) + xsb ) >> (8-OVERSAMPLE) #unrefined pressure value
<br>

More code is needed inside main() for offsets and refining of each value.

  • RPi sends this data to Thingspeak channel - The httplib module in python is used to POST values to the Thingspeak channel.
conn = httplib.HTTPConnection("api.thingspeak.com:80")
headers = {"Content-type":"application/x-www-form-uelencoded","Accept":"text/plain"}
(temperature,pressure)=readBMP180()
params = urllib.encode({"field1" : temperature,'field2':pressure,'key':"Your Write API key"})
conn.request("POST","/update",params,headers)
response=conn.getresponse()
print response.status,response.reason
conn.close()

<br>

Note - Please add your write API key inside "params" by replacing "Your Write API key". Also you can clear or delete channel inside the Settings tab

Hope You enjoyed!!

Step 7: Conclusion

So Finally we are connected to the cloud.There are a few Thingspeak mobile applications as well , available on the playstore. You just need to provide your Channel ID and API key to get started .Thank You and... All hail the tinkerers!!

<p>Hi, it success run 3 min then have error show up: (Raspberry pi3 with python3)</p><p>File &quot;bmp180.py&quot;, line 123, in &lt;module&gt;</p><p> main()</p><p> File &quot;bmp180.py&quot;, line 118, in main</p><p> response=conn.getresponse()</p><p> File &quot;/usr/lib/python2.7/httplib.py&quot;, line 1111, in getresponse</p><p> response.begin()</p><p> File &quot;/usr/lib/python2.7/httplib.py&quot;, line 444, in begin</p><p> version, status, reason = self._read_status()</p><p> File &quot;/usr/lib/python2.7/httplib.py&quot;, line 408, in _read_status</p><p> raise BadStatusLine(line)</p><p>httplib.BadStatusLine: ''</p>
<p>Cool Raspberry Pi monitor. </p>
Thnq!!

About This Instructable

4,120views

28favorites

License:

Bio: The eternal noob DIYer is something I would call myself. Understanding everything in the most layman terms and trying to explain the same to others ... More »
More by AslanA1:NodeMCU onboard LED access via web server Home Temperature monitoring using Raspberry Pi and Thingspeak(using BMP180) 
Add instructable to: