-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnickPiScript01.py
More file actions
159 lines (127 loc) · 4.62 KB
/
nickPiScript01.py
File metadata and controls
159 lines (127 loc) · 4.62 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
154
155
156
157
158
# Need to install dependencies
# sudo apt-get install python3-pip
# sudo pip3 install adafruit-blinka
# sudo pip3 install adafruit-circuitpython-charlcd
# sudo pip3 install w1thermsensor
# sudo python3 -m pip install influxdb (influxDb should be installed and configured)
# DS18B20 temperature sensor connected to GPIO4 (pin 7), 3.3v and gnd
# LCD PIN Rasp GPIO Rasp V.B pin
#------------------------------------------------------
# LCD Pin #4 (RS) #22 (pin 13)
# LCD Pin #6 (EN) #17 (pin 11)
# LCD Pin #11 (D4) #25 (pin 22)
# LCD Pin #12 (D5) #24 (pin 18)
# LCD Pin #13 (D6) #23 (pin 16)
# LCD Pin #14 (D7) #18 (pin 12)
# LCD Pin #1 (GND) GND (pin 6 )
# LCD Pin #2 (5V) 5V (pin 2 )
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
from time import sleep
import datetime
from w1thermsensor import W1ThermSensor
from influxdb import InfluxDBClient
import board
import digitalio
import adafruit_character_lcd.character_lcd as characterlcd
import asyncio
# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2
# compatible with all versions of RPI as of Jan. 2019
# v1 - v3B+
lcd_rs = digitalio.DigitalInOut(board.D22)
lcd_en = digitalio.DigitalInOut(board.D17)
lcd_d4 = digitalio.DigitalInOut(board.D25)
lcd_d5 = digitalio.DigitalInOut(board.D24)
lcd_d6 = digitalio.DigitalInOut(board.D23)
lcd_d7 = digitalio.DigitalInOut(board.D18)
# Initialise the lcd class
lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows)
# Set required InfluxDB parameters.
# (this could be added to the program args instead of beeing hard coded...)
host = "localhost" #Could also use local ip address like "192.168.1.136"
port = 8086
user = "pi"
password = "pi"
dbname = "temperature_db"
# Initialize the Influxdb client
client = InfluxDBClient(host, port, user, password, dbname)
# Initialize DS18B20 temperature prob
sensor = W1ThermSensor()
# Only set the precision one time for the sensor. writing is limited
# sensor.set_precision(9, persist=True)
# looking for an active Ethernet or WiFi device
def find_interface():
device_name = "null"
find_device = "ip addr show"
interface_parse = run_cmd(find_device)
for line in interface_parse.splitlines():
if "state UP" in line:
device_name = line.split(':')[1]
return device_name
# find an active IP on the first LIVE network device
def parse_ip(interface):
ip = "not found"
find_ip = "ip addr show %s" % interface
find_ip = "ip addr show %s" % interface
ip_parse = run_cmd(find_ip)
for line in ip_parse.splitlines():
if "inet " in line:
ip = line.split(' ')[5]
ip = ip.split('/')[0]
return ip
def getIp():
return parse_ip(find_interface())
# run unix shell command, return as ASCII
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output.decode('ascii')
def saveToDb(temperature):
timestamp=datetime.datetime.utcnow().isoformat()
datapoints = [
{
"measurement": "temperature",
"time": timestamp,
"fields": {
"value": temperature
}
}
]
return client.write_points(datapoints)
# wipe LCD screen before we start
lcd.clear()
lcd.message = "nickPiScript01\n " + "By nicgravel"
sleep(2)
temperature = sensor.get_temperature()
loopCounter = 60
line2 = ""
ip = "not found"
try:
while True:
temperature = sensor.get_temperature()
temperatureText = str(temperature) + " " + chr(223) + "C"
line1 = datetime.datetime.now().strftime('%b %d %H:%M:%S\n')
# Save only each 60 seconds
if loopCounter == 60:
lcd.clear()
line2 = "IP " + getIp()
elif loopCounter < 58 and loopCounter > 0:
if loopCounter == 57:
lcd.clear()
line2 = temperatureText + " " + "{:02d}".format(loopCounter) #Display number with leading zeros
elif loopCounter == 0:
saveToDb(temperature)
line2 = temperatureText + " Saving..."
print("Saving " + temperatureText)
elif loopCounter == -1:
loopCounter = 61
print(line1 + line2 + "\n")
lcd.message = line1 + line2
loopCounter -= 1
# Getting the temperature take about one second so we don't have to sleep
# sleep(1)
except KeyboardInterrupt:
lcd.clear()