IOT機器に接続しているセンサーは、温湿度センサー、気圧センサー、照度センサーの3種類となります。これはテストです

MasS(Measurement as a Service)

BeagleBone Blackのハードウエア
接続センサーの種別
①温湿度センサー
②気圧センサー(BeagleBone内に実装)
③照度センサー
センサーのBeagleBone BlackのインターフェースはI2Cとする。

IOT機器の動作としては、以下のソースをcronで実行している。
1分おきに起動し、センサーデータを取得後、メール送信,Fluent用log記録を行う。
 

# Cwlogger ver1.02 
# 2014/11/24 start
# 2014/12/29 adding message.ini
# for BeagleBone Black

#import --------------------
import time
import datetime
import os
import smtplib
import sys
import smbus

import Adafruit_DHT
import Adafruit_BMP.BMP085 as BMP085
import Adafruit_GPIO.MCP230xx as MCP

from email.mime.text import MIMEText
from email import utils
from email.Utils import formatdate
nowDate = datetime.datetime.today()


#groval variable------------
LogInterval = 60
ReadInterval = 20

ToMailAddress = "beagle1@****************"
FromMailAddress = "beagle1@**************"
smtpServer = "192.168.**.**"


OperationMode = 0
PowerCharge = 0
DataComment = "CwLogger V2.00"
Logfilename = "/media/A459-740B/CWLLogdata.csv"
Fluentfilename = "/var/log/pcless.log"

CurMeasDatetime = ""
CurMeasDatetime2 = ""
CurMeasData = ["--", "--", "--", "--", "--", "--"]
presec = -1

#Adafruit_DHT.AM2302 -------------------
TempSensor = Adafruit_DHT.AM2302
TempSensorPin = 'P8_11'
TempSensorPin2 = 'P8_13'

# Define MCP pins connected to the LCD. -----------------
#lcd_rs = 0
#lcd_en = 2
#lcd_d4 = 4
#lcd_d5 = 5
#lcd_d6 = 6
#lcd_d7 = 7
#lcd_backlight = 3
#lcd_green = 8
#lcd_blue = 3

# Define LCD column and row size for 16x2 LCD.
#lcd_columns = 16
#lcd_rows = 2

# Alternatively specify a 20x4 LCD.
# lcd_columns = 20
# lcd_rows = 4

# Initialize MCP23017 device using its default 0x20 I2C address.
#gpio = MCP.MCP23017()

# Alternatively you can initialize the MCP device on another I2C address or bus.
##gpio = MCP.MCP23017(0x27, busnum=1)

# BH1750 light sensor
ltbus = smbus.SMBus(1)


#-------------------------------------------------------
humidity1 = None
temperature1 = None
humidity2 = None
temperature2 = None
pressure = None
ltdata = None

MeasureOn = 0
#nowDate = datetime.datetime.today()


#module --------------------
def get_mes():

f = open('/home/debian/CWLogger/message.ini')
lines2 = f.readlines()
f.close()

for line in lines2:
temp = line.split('=')
if temp[-2] == "OperationMode":
OperationMode = int(temp[-1])
if temp[-2] == "PowerCharge":
PowerCharge = int(temp[-1])
if temp[-2] == "Comment":
DataComment = temp[-1]
return [OperationMode,PowerCharge,DataComment]

def saveMeasDataLog():
try:
flg = os.path.exists(Logfilename)
if not flg:
try:
fh = open(Logfilename, "w")
fh.write("datetime,ch1-temperature(C),ch1-humidity(%),ch2-temperature(C),ch2-humidity(%),ch3-absolute pressure(hPa),ch4-illumination(lux)\r\n")
except:
return
fh.close()
fh = open(Logfilename, "a")
str = '%s,%s,%s,%s,%s,%s,%s\r\n' % (CurMeasDatetime2, CurMeasData[0], CurMeasData[1], CurMeasData[2], CurMeasData[3], CurMeasData[4], CurMeasData[5])
fh.write(str)
except:
return
fh.close()

def saveMeasDataFluent():
def saveMeasDataLog():
try:
flg = os.path.exists(Logfilename)
if not flg:
try:
fh = open(Logfilename, "w")
fh.write("datetime,ch1-temperature(C),ch1-humidity(%),ch2-temperature(C),ch2-humidity(%),ch3-absolute pressure(hPa),ch4-illumination(lux)\r\n")
except:
return
fh.close()
fh = open(Logfilename, "a")
str = '%s,%s,%s,%s,%s,%s,%s\r\n' % (CurMeasDatetime2, CurMeasData[0], CurMeasData[1], CurMeasData[2], CurMeasData[3], CurMeasData[4], CurMeasData[5])
fh.write(str)
except:
return
fh.close()

def saveMeasDataFluent():
try:

OperationMode,PowerCharge,DataComment = get_mes()
str = '{"Date":"'+ CurMeasDatetime2 + '",'
str2 = '"Mode":"%d",' % (OperationMode)
str3 = '"Be_InsideTemp":"' + CurMeasData[0] + '","Be_InsideFumi":"' + CurMeasData[1] + '","Be_OutTemp":"' + CurMeasData[2] + '","Be_OutFumi":"' + CurMeasData[3]
str4 = '","Be_AbsPress":"' + CurMeasData[4] + '","Be_Illumi":"' + CurMeasData[5] + '",'
str5 = '"Charge":"%d"' % (PowerCharge)
DataComment = DataComment.replace('\n','')
str6 = ',"Comment":"' + DataComment + '"}'
msg = str + str2 + str3 + str4 + str5 + str6
fh = open(Fluentfilename, "a")
msg = '%s\r\n' % (msg)
fh.write(msg)
except:
return
fh.close()


def sendEmail():
OperationMode,PowerCharge,DataComment = get_mes()
str = '{"Date":"'+ CurMeasDatetime2 + '",'
str2 = '"Mode":"%d",' % (OperationMode)
str3 = '"Be_InsideTemp":"' + CurMeasData[0] + '","Be_InsideFumi":"' + CurMeasData[1] + '","Be_OutTemp":"' + CurMeasData[2] + '","Be_OutFumi":"' + CurMeasData[3]
str4 = '","Be_AbsPress":"' + CurMeasData[4] + '","Be_Illumi":"' + CurMeasData[5] + '",'
str5 = '"Charge":"%d"' % (PowerCharge)
str6 = ',"Comment":"' + DataComment + '"}'

# msg = MIMEText(str + str2 + str3 + str4 + str5 + str6)
msg = MIMEText(str + str2 + str3 + str4 + str5 + str6, 'plain', 'utf-8')
msg['To'] = ToMailAddress
msg['From'] = FromMailAddress
msg['Subject'] = 'Maas'
msg['Date'] = utils.formatdate(localtime=1)

try:
# ssvr = smtplib.SMTP(smtpServer,587)
ssvr = smtplib.SMTP(smtpServer,25)
cset = 'utf-8'
# ssvr.login(smtpUser, smtpPassword)
ssvr.sendmail(FromMailAddress, ToMailAddress, msg.as_string())
return
except:
return

ssvr.quit()


#start ---------------------------
#nowDate = datetime.datetime.today()
BMPsensor = BMP085.BMP085()

#lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
# lcd_columns, lcd_rows, backlight=None,
# invert_polarity=True, initial_backlight=0, gpio=gpio)

#lcd.message('Cwlogger Ver1.01')
#lcd.set_cursor(0,1)
#lcd.message(' ')


#nowDate = datetime.datetime.today()
cursec = nowDate.minute*60 + nowDate.second

if presec != cursec:
#12/12 13:59:59
CurMeasDatetime = '%02d/%02d %02d:%02d:%02d ' % (nowDate.month, nowDate.day, nowDate.hour, nowDate.minute, nowDate.second)
CurMeasDatetime2 = '%04d/%02d/%02d %02d:%02d:%02d' % (nowDate.year, nowDate.month, nowDate.day, nowDate.hour, nowDate.minute, nowDate.second)

if cursec%12 < 3 :
mess = "CH1:" + CurMeasData[0] + "C " + CurMeasData[1] + "% "
elif cursec%12 < 6:
mess = "CH2:" + CurMeasData[2] + "C " + CurMeasData[3] + "% "
elif cursec%12 < 9:
mess = "CH3:" + CurMeasData[4] + "hPa "
else:
mess = "CH4:" + CurMeasData[5] + " lux "

# lcd.set_cursor(0,0)
# lcd.message(CurMeasDatetime)

# lcd.set_cursor(0,1)
# lcd.message(mess)

print CurMeasDatetime

curint = cursec%ReadInterval
curint = 0
if curint == 0:
humidity1, temperature1 = Adafruit_DHT.read_retry(TempSensor, TempSensorPin)
humidity2, temperature2 = Adafruit_DHT.read_retry(TempSensor, TempSensorPin2)
pressure = BMPsensor.read_pressure()
pressure = pressure/100
ltdata = ltbus.read_i2c_block_data(0x23, 0x11)
MeasureOn = 1
if MeasureOn == 1:
if temperature1 is not None:
if -40 <= temperature1 and temperature1 <= 80:
CurMeasData[0] = '%0.1f' % temperature1
if humidity1 is not None:
if 0 <= humidity1 and humidity1 <= 100:
CurMeasData[1] = '%0.1f' % humidity1

if temperature2 is not None:
if -40 <= temperature2 and temperature2 <= 80:
CurMeasData[2] = '%0.1f' % temperature2
if humidity2 is not None:
if 0 <= humidity2 and humidity2 <= 100:
CurMeasData[3] = '%0.1f' % humidity2
if pressure is not None:
CurMeasData[4] = '%0.1f' % pressure
if ltdata is not None:
lux = (ltdata[1] + ltdata[0]*256)/1.2
CurMeasData[5] = '%0.0f' % lux

print "temperature1:" + CurMeasData[0] + "C"
print "humidity1:" + CurMeasData[1] + "%"
print "temperature2:" + CurMeasData[2] + "C"
print "humidity2:" + CurMeasData[3] + "%"
print "pressure:" + CurMeasData[4] + "hPa"
print "illumination:" + CurMeasData[5] + " lux"
else:
MeasureOn = 1

curint = cursec%LogInterval
curint = 0
if curint == 0:
if CurMeasData[0] != "--":
print "Save and send cur data"
saveMeasDataLog()
saveMeasDataFluent()
sendEmail()
sys.exit(1)
presec = cursec