-
Notifications
You must be signed in to change notification settings - Fork 0
/
VOC_log.py.orig
executable file
·131 lines (96 loc) · 2.97 KB
/
VOC_log.py.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
###########################################
#
# VOC monitor
# we are using a SGP40 gas sensor
# This is fed from a SHT31D sensor
#
###########################################
import sys
import os
import time
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
from Adafruit_IO import Client, Feed
import json
import datetime
import adafruit_ssd1306 # the display
from Sensirion_SGP40 import Sensirion_SGP40 # gas sensor
from Sensirion_SHT30 import Sensirion_SHT30 # temp/humidity sensor
######
# Variables
######
LOG_FILE = '/home/pi/VOC_monitor/VOC_log.txt'
FONT_FILE = '/home/pi/VOC_monitor/PixelOperator.ttf'
i2c = board.I2C() # uses board.SCL and board.SDA
## IO adafruit setup
# VOC_log.py "USERNAME" "KEY"
USERNAME = sys.argv[1]
KEY = sys.argv[2]
print (sys.argv[1] + " " + sys.argv[2] )
aio = Client(USERNAME, KEY)
# gather data from sensors
sht30=Sensirion_SHT30()
sht30.sht30_read()
#set IICbus elativeHumidity(0-100%RH) temperature(-10~50 centigrade)
sgp40=Sensirion_SGP40(bus = 1, relative_humidity = 50, temperature_c = 25)
sgp40.set_envparams(sht30.humidity, sht30.temperature)
#set Warm-up time
#print('Please wait 5 seconds...')
sgp40.begin(5)
# process it
voc_index = int(sgp40.get_voc_index())
voc_raw = int(sgp40.measure_raw())
temp = float(sht30.temperature)
humidity = float(sht30.humidity)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
entry = now+" T:%2.2fC, H:%2.2f, I:%3.0d, R:%5.0d" % (temp, humidity, voc_index, voc_raw)
#print (entry)
# write to the log file
file = open(LOG_FILE, "a")
file.write (entry+"\n")
file.close ()
# send it to the AIO feed
aio.send_data(aio.feeds('enviro-humidity').key, humidity)
aio.send_data(aio.feeds('enviro-temp').key, temp)
aio.send_data(aio.feeds('voc-index').key, voc_index)
aio.send_data(aio.feeds('voc-raw').key, voc_raw)
##### display section
# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)
# Display settings
WIDTH = 128
HEIGHT = 64
BORDER = 0
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
# Load font for the display
font = ImageFont.truetype(FONT_FILE, 40)
# Clear display.
oled.fill(0)
oled.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a white background
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
# Draw a smaller inner rectangle
draw.rectangle(
(BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
outline=0,
fill=0,
)
# set the voc index to the display, dont care about temp/humidity.
text = "VOC: " + str(voc_index)
(font_width, font_height) = font.getsize(text)
draw.text(
(oled.width // 2 - font_width // 2, oled.height // 2 - (font_height + font_height//2) // 2),
text,
font=font,
fill=255,
)
# Display image
oled.image(image)
oled.show()