树莓派 + 传感器 + Jubatus 1(包含MongoDB和传感器)

将从树莓派获取的传感器数值存入MongoDB数据库中。

将由光传感器获取的模拟数值输入到MongoDB中。

#-*- 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!现在,我将尝试提取数据以确认是否成功存入。

用Python从MongoDB中提取值。

在中文中启动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

使用Python从MongoDB中提取值并进行显示试试看。

#/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中的数据作为师傅数据传递。

广告
将在 10 秒后关闭
bannerAds