-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCF8574OneShot.py
70 lines (56 loc) · 1.43 KB
/
PCF8574OneShot.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import datetime
import MySQLdb as mdb
import logging
import smbus
from heatingToAWS import sendHeatingData
#### Import dB Details from access.py
from access import hostname, username, password, dB
# PCF8574 commands
PCF8574 = 0x20
logging.basicConfig(filename='/home/pi/log/PCF8574_error.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# General read function, also updates a register
def write(address, value):
try:
bus.write_byte(address,value)
except IOError as io_err:
logger.error(io_err)
print(io_err)
return -1
def read(address):
try:
reading = bus.read_byte(address)
except IOError as io_err:
logger.error(io_err)
print(io_err)
return reading
################################################
#
# Start of "Main"
#
################################################
# Set up variables
# Get readings from sensors and store them in MySQL
bus = smbus.SMBus(1)
write(PCF8574, 0xFF)
try:
pins = read(PCF8574)
print ("%02x" % pins)
# print pins
# As the most significant two bits are unconnected,
# they should always be high. Hence, if the value of
# the pins is less than 192 (C0) we need to reset.
if (pins < 0xC0):
print ("Resetting to FF")
write(PCF8574, 0xFF)
else:
sendHeatingData(pins)
except IOError as io_error:
print("bus Read error", io_error)
print( "Clash")