| #!/usr/bin/env python
# -*- coding: utf-8 -*-
# 2018/02/25 (C)Dr.Ishikawa 
import time
import datetime
import smbus
I2C = smbus.SMBus(1)
# VEML6070 constants
IT = 0x01 #Integration time  0x01:112.5ms 
SD = 0x00 #Shutdown mode disabled
ADD_LOW = 0x38 # VEML6070 low address 0b0111000
ADD_HIGH = 0x39 # VEML6070 high address 0b111001
SENSITIVITY = 0.05  #W/(m*m)/step
command = (IT<<2) + 0x02 + SD
def mapfloat( x,  in_min,  in_max,  out_min,  out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
while True:
	try:
		I2C.write_byte(ADD_LOW, command)
		time.sleep(1.0)
		VEML6070_MSB = I2C.read_byte(ADD_HIGH) #UV Data MSB
		VEML6070_LSB = I2C.read_byte(ADD_LOW) #UV Data LSB
		
		VEML6070_Raw = VEML6070_MSB * 256 + VEML6070_LSB
		VEML6070_Intensity =  SENSITIVITY * VEML6070_Raw #W/(m*m)
		VEML6070_Index = mapfloat(VEML6070_Raw, 0.0, 2055.0, 0.0, 11.0)
		# Output data 
		now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S") 
		data = now + ","  + str(VEML6070_Intensity) + "," + str(VEML6070_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)
	except Exception as e:#エラーでもメッセージをだして処理続行
		print str(e)
		time.sleep(59)
 |