Created
June 11, 2020 15:01
-
-
Save ringo156/cd1dec5f838763c2eeac9c54136b0418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import time | |
import datetime | |
import os | |
path = "./log/" | |
import bme280 | |
# import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
if __name__ == '__main__': | |
# Raspberry Pi pin configuration: | |
RST = None # on the PiOLED this pin isnt used | |
# Note you can change the I2C address by passing an i2c_address parameter like: | |
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C) | |
# Initialize library. | |
disp.begin() | |
# Clear display. | |
disp.clear() | |
disp.display() | |
# Create blank image for drawing. | |
# Make sure to create image with mode '1' for 1-bit color. | |
width = disp.width | |
height = disp.height | |
image = Image.new('1', (width, height)) | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
# Draw some shapes. | |
# First define some constants to allow easy resizing of shapes. | |
padding = -2 | |
top = padding | |
bottom = height-padding | |
# Move left to right keeping track of the current x position for drawing shapes. | |
x = 5 | |
# Load default font. | |
font = ImageFont.load_default() | |
while True: | |
# clear display | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
sensor_data = bme280.readData() | |
temp = "%6.2f" % (sensor_data[0]) | |
draw.text((x, top), "Temp =" + temp + " °C", font = font, fill = 255) | |
press = "%7.2f" % (sensor_data[1]) | |
draw.text((x, top + 8), "Press = " + press + " hPa", font = font, fill = 255) | |
hum = "%6.2f" % (sensor_data[2]) | |
draw.text((x, top + 16), "Hum =" + hum + " %", font = font, fill = 255) | |
disp.image(image) | |
disp.display() | |
# ファイル書き込み処理 | |
date = str(datetime.date.today()) | |
nowtime = datetime.datetime.now() | |
# ファイルがあったら上書きする | |
if(os.path.exists(path + date +".csv")): | |
f = open(path + date +".csv", 'a') | |
f.write(str(nowtime.hour) + ':' + str(nowtime.minute) + ':' + str(nowtime.second) + ',') | |
f.write(temp + ',' + press + ',' + hum + ' \n') | |
f.close() | |
# なかったら生成する | |
else: | |
f = open(path + date +".csv", 'w') | |
f.write("date,Temp,Press,Hum\n") | |
f.write(str(nowtime.hour) + ':' + str(nowtime.minute) + ':' + str(nowtime.second) + ',') | |
f.write(temp + ',' + press + ',' + hum + ' \n') | |
f.close() | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment