Repository navigation
Expand file tree
/
Copy pathozono.py
More file actions
executable file
·155 lines (123 loc) · 4.78 KB
/
Copy pathozono.py
File metadata and controls
executable file
·155 lines (123 loc) · 4.78 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python3
# I2C Ozone Sensor
# Device: https://is.gd/ai1Qdi & C library: https://github.com/DFRobot/DFRobot_OzoneSensor
# Rpi i2c smbus info:
# https://raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2
# https://www.electronicwings.com/raspberry-pi/python-based-i2c-functions-for-raspberry-pi
# http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc
# https://embeddedmicro.weebly.com/raspberry-pi.html
import smbus
import time
# I2C channel 1 is connected to the GPIO pins
channel = 1
# Ozone Sensor - 0x70 - 0x71 - 0x72 - 0x73 ( default )
address = 0x73
# Mode
measure_mode_auto = 0x00
measure_mode_passive = 0x01
# register address
auto_read_data = 0x00
passive_read_data = 0x01
mode_register = 0x03
read_ozone_data_register = 0x04
AUTO_data_high_eight_bits = 0x09
AUTO_data_low_eight_bits = 0x0A
PASS_data_low_eight_bits = 0x08
PASS_data_high_eight_bits = 0x07
# variables
DEBUG = 0
OCOUNT = 100
m_flag = 0
collect_number = 20 # 1-100
OzoneData = [0x00] * OCOUNT
########################################
### FUNCTS ####
########################################
# SetModes
def setModes(mode, address):
global measure_mode_auto, bus, measure_mode_passive, m_flag, mode_register
if mode == measure_mode_auto:
# Bus.write_byte_data(Device Address, Register Address, Value)
bus.write_byte_data(address, mode_register, measure_mode_auto)
m_flag = 0
if mode == measure_mode_passive:
# Bus.write_byte_data(Device Address, Register Address, Value)
bus.write_byte_data(address, mode_register, measure_mode_passive)
m_flag = 1
else:
m_flag = -1
# ReadOzoneData
def ReadOzoneData(CollectNum, address):
global bus, m_flag, auto_read_data, read_ozone_data_register, AUTO_data_high_eight_bits, OzoneData, passive_read_data, PASS_data_high_eight_bits, DEBUG
i = 0
j = 0
if CollectNum > 0:
#for(j = CollectNum - 1; j > 0; j--):
for j in range(CollectNum - 1, j > 0, -1):
OzoneData[j] = OzoneData[j-1]
print(OzoneData[j])
if m_flag == 0:
# read active data in active mode, first request once, then read the data
bus.write_byte_data(address, read_ozone_data_register, auto_read_data)
if DEBUG:
time.sleep(0.1);
read = bus.read_byte_data(address, read_ozone_data_register)
print("i wrote: {} and i read: {}".format(hex(auto_read_data),hex(read)))
time.sleep(0.1);
OzoneData[0] = i2cReadOzoneData(address, AUTO_data_high_eight_bits)
if DEBUG:
print("Read active data in active mod")
print("Word Ozone Data: ", OzoneData[0])
if m_flag == 1:
# read passive data in passive mode, first request once, then read the data
bus.write_byte_data(address, read_ozone_data_register, passive_read_data)
if DEBUG:
time.sleep(0.1);
read = bus.read_byte_data(address, read_ozone_data_register)
print("i wrote: {} and i read: {}".format(hex(passive_read_data),hex(read)))
time.sleep(0.1);
OzoneData[0] = i2cReadOzoneData(address, PASS_data_high_eight_bits)
if DEBUG:
print("Read passive data in passive mod")
print("Word Ozone Data: ", OzoneData[0])
if i < CollectNum:
i = i + 1
return getAverageNum(OzoneData, i)
if CollectNum <= 0 or CollectNum > 100:
return -1
# getAverageNum
def getAverageNum(bArray, iFilterLen):
bTemp = 0
#for(i = 0; i < iFilterLen; i++):
for i in range(iFilterLen):
bTemp = bTemp + bArray[i]
return bTemp / iFilterLen
# i2cReadOzoneData
def i2cReadOzoneData(address, reg):
global bus, DEBUG
bus.write_byte(address, reg)
time.sleep(0.1)
# read 2 bytes from the sensor
rxbuf = bus.read_i2c_block_data(address, reg, 2)
# convert byte in a word
O3_ppb = (rxbuf[0] << 8) + rxbuf[1]
if DEBUG:
print(O3_ppb)
# send back the converted ppb to ppm
return O3_ppb/1000
########################################
### MAIN ####
########################################
# Initialize I2C (SMBus)
print("Open i2c bus on channel: {}".format(channel))
bus = smbus.SMBus(channel)
# SetModes
print("Set the mode to: {} for the address: {}".format(measure_mode_passive, hex(address)))
setModes(measure_mode_passive, address)
print("Read data from the sensor")
# ReadOzoneData
while True:
time.sleep(1.0)
ozoneConcentration = ReadOzoneData(collect_number, address)
print("Ozone concentration is {} PPB.".format(ozoneConcentration))
print("##########################################################################")