I wanted to get sensing data from Raspberry Pi from Node.js as well as Arduino Firmata, but it seems difficult, so I decided to go back to Python. Since GPIO of Raspberry Pi can only input digitally, TMP36 and LM35DZ AD converters such as MCP3008 and PCF8591 when using analog sensors will become necessary. Let's write a Python program that measures temperature using TMP36.
[SPI (Serial Peripheral Interface)](http://ja.wikipedia.org/wiki/%E3%82%B7%E3%83%AA%E3%82%A2%E3%83%AB%E3%83% BB% E3% 83% 9A% E3% 83% AA% E3% 83% 95% E3% 82% A7% E3% 83% A9% E3% 83% AB% E3% 83% BB% E3% 82% A4% E3% 83% B3% E3% 82% BF% E3% 83% 95% E3% 82% A7% E3% 83% BC% E3% 82% B9) is one of the serial communication standards. It seems that the method of enabling SPI on Raspberry Pi has changed from 3.18 of the kernel. Raspi-config in Enabling The SPI Interface On The Raspberry Pi It describes how to use and how to enable it manually. This time we will do it manually.
If the kernel is not 3.18.x, update the firmware. The package has also been updated.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-update
Check the kernel version.
$ cat /proc/version
Linux version 3.18.11+ (dc4@dc4-XPS13-9333) (gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) ) #777 PREEMPT Sat Apr 11 17:24:23 BST 2015
Edit /boot/config.txt
.
$ sudo vi /boot/config.txt
dtparam=spi=on
reboot.
$ sudo reboot
SPI is now enabled.
$ lsmod | grep spi
spi_bcm2708 6018 0
Wire the MCP3008, Raspberry Pi and FT232RL on a breadboard.
Wire with MCP3008 and Raspberry Pi.
Wire the TMP36.
Install the Python development environment for Raspberry Pi.
$ sudo apt-get update
$ sudo apt-get install python-dev
$ python -V
Python 2.7.3
Install ez_setup and pip.
$ curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python
$ curl https://bootstrap.pypa.io/get-pip.py -o - | sudo python
$ pip -V
pip 6.1.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
py-spidev
Install py-spidev to operate SPI devices from Python.
$ cd
$ git clone git://github.com/doceme/py-spidev
$ cd py-spidev
$ sudo python setup.py install
...
Writing /usr/local/lib/python2.7/dist-packages/spidev-3.0.egg-info
Create a project.
$ mkdir -p ~/python_apps/spidev-spike
$ cd !$
Write a Python sample program by referring to the following site.
~/python_apps/spidev-spike/spi_tmp36.py
#!/usr/bin/env python
import time
import sys
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
def readAdc(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
def convertVolts(data):
volts = (data * 3.3) / float(1023)
volts = round(volts,4)
return volts
def convertTemp(volts):
temp = (100 * volts) - 50.0
temp = round(temp,4)
return temp
if __name__ == '__main__':
try:
while True:
data = readAdc(0)
print("adc : {:8} ".format(data))
volts = convertVolts(data)
temp = convertTemp(volts)
print("volts: {:8.2f}".format(volts))
print("temp : {:8.2f}".format(temp))
time.sleep(5)
except KeyboardInterrupt:
spi.close()
sys.exit(0)
Run a Python program. Displays the temperature in degrees Celsius at 5-second intervals.
$ python spi_tmp36.py
adc : 232
volts: 0.75
temp : 24.84
adc : 233
volts: 0.75
temp : 25.16
adc : 233
volts: 0.75
temp : 25.16
Recommended Posts