##RaspberryPiで取得したセンサ値をMongoDBに入れる
光センサから得られるアナログ値をMongoDB内に入れる
lux-db.py
#-*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import spidev
import time
import urllib
import urllib2
from pymongo import MongoClient
#con = MongoClient('10.0.2.15', 27017)
con = MongoClient('172.16.4.83', 27017)
db = con.sensordb
col = db.sensors
id = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
spi = spidev.SpiDev()
spi.open(0, 0)
starttime = time.time()
def readadc(adcnum):
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
while True:
print "event!"
t = int((time.time() - starttime) * 1000)
value = readadc(0)
print("%4d/1023" % (value))
col.insert({'ID':id, 'name': 'lux', 'value': value})
id += 1
実行してみる
$ sudo python lux.py
event!
177/1023
event!
176/1023
event!
180/1023
event!
184/1023
event!
175/1023
event!
186/1023
MongoDBに入れられました!
それでは,実際に入っているかどうか取り出してみます.
##MongoDB内の値をPythonで取り出す
MongoDBの起動
$ mongod --dbpath /usr/local/var/mongodb/
2015-05-29T13:03:43.146+0900 [initandlisten] MongoDB starting : pid=1263 port=27017 dbpath=/usr/local/var/mongodb/ 64-bit host=chunchun
2015-05-29T13:03:43.146+0900 [initandlisten]
2015-05-29T13:03:43.146+0900 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2015-05-29T13:03:43.146+0900 [initandlisten] db version v2.6.7
2015-05-29T13:03:43.146+0900 [initandlisten] git version: nogitversion
2015-05-29T13:03:43.146+0900 [initandlisten] build info: Darwin miniyosemite.local 14.1.0 Darwin Kernel Version 14.1.0: Fri Dec 5 06:49:27 PST 2014; root:xnu-2782.10.67~9/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-05-29T13:03:43.146+0900 [initandlisten] allocator: tcmalloc
2015-05-29T13:03:43.146+0900 [initandlisten] options: { storage: { dbPath: "/usr/local/var/mongodb/" } }
2015-05-29T13:03:43.149+0900 [initandlisten] journal dir=/usr/local/var/mongodb/journal
2015-05-29T13:03:43.149+0900 [initandlisten] recover begin
2015-05-29T13:03:43.150+0900 [initandlisten] recover lsn: 157305
2015-05-29T13:03:43.150+0900 [initandlisten] recover /usr/local/var/mongodb/journal/j._0
2015-05-29T13:03:43.151+0900 [initandlisten] recover skipping application of section seq:0 < lsn:157305
MongoDB内の値をPythonを用いて取り出して表示してみる
client.py
#/usr/bin/env python
#-*- coding:utf-8-*-
from pymongo import MongoClient
class getMongo():
con = MongoClient('172.16.4.83', 27017)
db = con["sensordb"]
col = db.sensors
for data in col.find({'name':'lux'}):
print data
実行する
$ python client.py
{u'_id': ObjectId('5567fbdc94e0343f0673444c'), u'data': 174, u'name': u'lux'}
{u'_id': ObjectId('5567fbdc94e0343f0673444d'), u'data': 176, u'name': u'lux'}
{u'_id': ObjectId('5567fbdd94e0343f0673444e'), u'data': 180, u'name': u'lux'}
{u'_id': ObjectId('5567fbdd94e0343f0673444f'), u'data': 176, u'name': u'lux'}
{u'_id': ObjectId('5567fbde94e0343f06734450'), u'data': 177, u'name': u'lux'}
{u'_id': ObjectId('5567fbdf94e0343f06734451'), u'data': 186, u'name': u'lux'}
{u'_id': ObjectId('5567fbdf94e0343f06734452'), u'data': 177, u'name': u'lux'}
{u'_id': ObjectId('5567fbe094e0343f06734453'), u'data': 174, u'name': u'lux'}
{u'_id': ObjectId('5567fbe094e0343f06734454'), u'data': 185, u'name': u'lux'}
{u'_id': ObjectId('5567fbe194e0343f06734455'), u'data': 175, u'name': u'lux'}
{u'_id': ObjectId('5567fbe194e0343f06734456'), u'data': 177, u'name': u'lux'}
こんな感じで取得できます.
次回は,MongoDB内のデータを教師データとして渡してみましょう