#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2018/02/25 (C)Dr.Ishikawa
import time
import datetime
import Adafruit_ADS1x15
# ADS1115
ADS1115 = Adafruit_ADS1x15.ADS1115()
GAIN = 1
# ML8511 constants
ML8511_OFFSET = -0.01 #真っ暗の時に出力電圧1.0Vとなるように
ML8511_SCALEFACTOR = 1.0 #微調整の必要はなかった
while True:
try:
# ML8511の出力をADS1115のA0端子に接続した場合
ML8511_Raw = 4.096 * ADS1115.read_adc(0, gain=GAIN) / 32768.0
ML8511_output = ML8511_Raw + ML8511_OFFSET
#出力電圧からUVインデックスを求める。本文参照
ML8511_Index = ML8511_SCALEFACTOR * (12.49 * ML8511_output -12.49)
# Output data
now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
data = now + "," + str(ML8511_Index) + "\n"
filename = 'UV_data/' + datetime.datetime.now().strftime("%Y%m") + 'UV_data.txt'
file_data = open(filename , "a" )
file_data.write(data)
print (data)
file_data.close()
time.sleep(59.5)
except Exception as e:#I2Cの伝送エラーが起きることを想定
print str(e)
time.sleep(59.5)
|