├── README.md ├── pytemp-dht22.py ├── pytemp-dallas.py └── pydisplay.py /README.md: -------------------------------------------------------------------------------- 1 | # raspberrypi 2 | 3 | Here is random code I've written for the Raspberry Pi. Apologies for the ugly coding and lack of comments. Since it's just for projects around the house, I do it quick and dirty without much thought to doing it right. 4 | 5 | None the less, may still be helpful to someone. 6 | -------------------------------------------------------------------------------- /pytemp-dht22.py: -------------------------------------------------------------------------------- 1 | # From Jan 18 2016 2 | # Reads temp and humidity for the DHT22 sensor and logs to Syslog 3 | # 4 | import os 5 | import glob 6 | import time 7 | import subprocess 8 | import syslog 9 | import sys 10 | import Adafruit_DHT 11 | 12 | #time.sleep(30) 13 | 14 | print('Beginning Temperature Monitoring - Raspberry Pi Display Living Room') 15 | syslog.syslog('Beginning Temperature Monitoring - Raspberry Pi Display Living Room') 16 | 17 | def read_temp(): 18 | # read from GPIO pin 23 using a DHT22 sensor 19 | try: 20 | humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 23) 21 | temperature = temperature * 9/5.0 + 32 22 | return temperature, humidity 23 | except: 24 | syslog.syslog('Error: Failed to read from sensor. Will try again.') 25 | #print('Error: Failed to read from sensor. Will try again.') 26 | return -99.0,-99.0 27 | 28 | while True: 29 | current_temp, current_humidity = read_temp() 30 | current_temp = format(current_temp, '.1f') 31 | current_humidity = format(float(current_humidity), '.1f') 32 | #print('lr_tempF=' + str(current_temp) +', lr_humidity='+ str(current_humidity)) 33 | syslog.syslog('lr_tempF=' + str(current_temp) +', lr_humidity='+ str(current_humidity)) 34 | time.sleep(59) 35 | -------------------------------------------------------------------------------- /pytemp-dallas.py: -------------------------------------------------------------------------------- 1 | # From Jan 18 2016 2 | # for the Dallas temp sensor DS18B20 and similar 3 | # Requires the OneWire (W1) modules be installed on your Pi 4 | # 5 | import os 6 | import glob 7 | import time 8 | import subprocess 9 | import syslog 10 | 11 | #time.sleep(30) 12 | 13 | os.system('modprobe w1-gpio') 14 | os.system('modprobe w1-therm') 15 | 16 | base_dir = '/sys/bus/w1/devices/' 17 | device_folder1 = glob.glob(base_dir + '10*')[0] 18 | device_file1 = device_folder1 + '/w1_slave' 19 | 20 | syslog.syslog('Beginning Temperature Monitoring - Raspberry Pi Office Sensor') 21 | 22 | def read_temp_raw(): 23 | catdata = subprocess.Popen(['cat',device_file1], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 24 | out,err = catdata.communicate() 25 | out_decode = out.decode('utf-8') 26 | lines = out_decode.split('\n') 27 | return lines 28 | 29 | def read_temp(): 30 | lines = read_temp_raw() 31 | while lines[0].strip()[-3:] != 'YES': 32 | time.sleep(0.2) 33 | lines = read_temp_raw() 34 | equals_pos = lines[1].find('t=') 35 | if equals_pos != -1: 36 | temp_string = lines[1][equals_pos+2:] 37 | temp_c = float(temp_string) / 1000.0 38 | temp_f = temp_c * 9.0 / 5.0 + 32.0 39 | return temp_f 40 | 41 | while True: 42 | current_temp = read_temp() 43 | #print('Office1=' + str(current_temp)) 44 | syslog.syslog('office1=' + str(current_temp)) 45 | time.sleep(59) 46 | -------------------------------------------------------------------------------- /pydisplay.py: -------------------------------------------------------------------------------- 1 | # Display stats for framebuffer1 LCD 2 | # Nov 22 2014 3 | # Updated: Jan 18 2016 4 | # Adding living room temp and humidity 5 | # Uses framebuffer 1 6 | # 7 | # Picture of how it looks: http://imgur.com/Hm9syVQ 8 | 9 | import pygame, sys, os, time, datetime, urllib, csv 10 | from pygame.locals import * 11 | os.environ["SDL_FBDEV"] = "/dev/fb1" 12 | 13 | ## Globals 14 | 15 | values = "NULL" 16 | labels = "NULL" 17 | timetopoll = True 18 | 19 | pygame.init() 20 | 21 | ## Set up the screen 22 | 23 | DISPLAYSURF = pygame.display.set_mode((320, 240), 0, 16) 24 | pygame.mouse.set_visible(0) 25 | pygame.display.set_caption('Stats') 26 | 27 | # set up the colors 28 | BLACK = ( 0, 0, 0) 29 | WHITE = (255, 255, 255) 30 | RED = (255, 0, 0) 31 | GREEN = ( 0, 255, 0) 32 | BLUE = ( 0, 0, 255) 33 | CYAN = ( 0, 255, 255) 34 | 35 | ## Main loop 36 | 37 | while True: 38 | for event in pygame.event.get(): 39 | if event.type == QUIT: 40 | pygame.quit() 41 | sys.exit() 42 | 43 | currentime = datetime.datetime.time(datetime.datetime.now()) 44 | 45 | 46 | ## Poll temperature date from splunk as CSV and parse it 47 | 48 | if timetopoll: 49 | try: 50 | tempdata = urllib.urlopen("http://10.0.0.14:3344/info/pitemp.cgi") 51 | if tempdata.getcode() <= 299: 52 | reader = csv.reader(tempdata, delimiter=',', quotechar='"') 53 | labels = reader.next() 54 | values = reader.next() 55 | timetopoll = False 56 | try: 57 | test = values[0] 58 | test = values[1] 59 | test = values[2] 60 | test = values[3] 61 | test = values[4] 62 | test = values[5] 63 | test = values[6] 64 | except: 65 | values = "--.-", "--.-", "--.-", "--.-", "--.-", "--.-", "--.-" 66 | else: 67 | values = "--.-", "--.-", "--.-", "--.-", "--.-", "--.-", "--.-" 68 | timetopoll = False 69 | except: 70 | values = "--.-", "--.-", "--.-", "--.-", "--.-", "--.-", "--.-" 71 | timetopoll = False 72 | 73 | # Relies on lighthttpd on Richmond to poll Splunk for this data 74 | 75 | else: 76 | #print "Not polling" 77 | timetopoll = True 78 | 79 | 80 | ## Draw the title 81 | 82 | ##graph = pygame.image.load("/root/lcd/background.png") 83 | #graphrect = graph.get_rect() 84 | #DISPLAYSURF.blit(graph, graphrect) 85 | 86 | black_square_that_is_the_size_of_the_screen = pygame.Surface(DISPLAYSURF.get_size()) 87 | black_square_that_is_the_size_of_the_screen.fill((0, 0, 0)) 88 | DISPLAYSURF.blit(black_square_that_is_the_size_of_the_screen, (0, 0)) 89 | 90 | font = pygame.font.Font(None, 40) 91 | text = font.render("Outside", 1, RED) 92 | textpos = text.get_rect(centerx=DISPLAYSURF.get_width()/4+13) 93 | DISPLAYSURF.blit(text, textpos) 94 | 95 | font = pygame.font.Font(None, 40) 96 | text = font.render("Inside", 1, RED) 97 | textpos = text.get_rect(centerx=DISPLAYSURF.get_width()/4+179) 98 | DISPLAYSURF.blit(text, textpos) 99 | 100 | 101 | 102 | ## Draw temperatures 103 | 104 | font = pygame.font.Font(None, 98) 105 | text = font.render(values[4], 1, WHITE) 106 | ## text = font.render("188.8", 1, WHITE) 107 | textpos = text.get_rect(center=(DISPLAYSURF.get_width()/4+10,80)) 108 | DISPLAYSURF.blit(text, textpos) 109 | 110 | font = pygame.font.Font(None, 20) 111 | textF = font.render(u'\u00b0' + "F", 1, WHITE) 112 | textposF = textpos[0] + textpos[2], textpos[1] + 10 113 | DISPLAYSURF.blit(textF, textposF) 114 | 115 | ## Draw Lines 116 | 117 | pygame.draw.line(DISPLAYSURF, GREEN, [5, 140], [DISPLAYSURF.get_width()-5,140], 1) 118 | 119 | pygame.draw.line(DISPLAYSURF, GREEN, [DISPLAYSURF.get_width()/2+30, 5], [DISPLAYSURF.get_width()/2+30,140], 1) 120 | 121 | 122 | ## Living room Temp 123 | 124 | font = pygame.font.Font(None, 75) 125 | text = font.render(values[6], 1, WHITE) 126 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/4+220,30)) 127 | DISPLAYSURF.blit(text, textpos) 128 | 129 | font = pygame.font.Font(None, 20) 130 | textF = font.render(u'\u00b0' + "F", 1, WHITE) 131 | textposF = textpos[0] + textpos[2], textpos[1] + 10 132 | DISPLAYSURF.blit(textF, textposF) 133 | 134 | 135 | ## Living room humidity 136 | 137 | font = pygame.font.Font(None, 75) 138 | text = font.render(values[5], 1, WHITE) 139 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/4+220,80)) 140 | DISPLAYSURF.blit(text, textpos) 141 | 142 | font = pygame.font.Font(None, 20) 143 | textF = font.render(" %", 1, WHITE) 144 | textposF = textpos[0] + textpos[2], textpos[1] + 10 145 | DISPLAYSURF.blit(textF, textposF) 146 | 147 | 148 | ## Min 149 | 150 | font = pygame.font.Font(None, 30) 151 | text = font.render("Min:", 1, WHITE) 152 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2-90,145)) 153 | DISPLAYSURF.blit(text, textpos) 154 | 155 | font = pygame.font.Font(None, 30) 156 | textF = font.render(values[2], 1, WHITE) 157 | ## textF = font.render("981.8", 1, WHITE) 158 | textposF = textpos[0] + textpos[2] + 10, textpos[1] 159 | DISPLAYSURF.blit(textF, textposF) 160 | 161 | 162 | ## Max 163 | 164 | font = pygame.font.Font(None, 30) 165 | text = font.render("Max:", 1, WHITE) 166 | ## textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2+90,145)) 167 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2-90,165)) 168 | DISPLAYSURF.blit(text, textpos) 169 | 170 | font = pygame.font.Font(None, 30) 171 | textF = font.render(values[3], 1, WHITE) 172 | ## textF = font.render("1.8", 1, WHITE) 173 | textposF = textpos[0] + textpos[2] + 10, textpos[1] 174 | DISPLAYSURF.blit(textF, textposF) 175 | 176 | 177 | 178 | ## Attic 179 | 180 | font = pygame.font.Font(None, 30) 181 | text = font.render("Attic:", 1, WHITE) 182 | ## textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2-90,165)) 183 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2+90,145)) 184 | DISPLAYSURF.blit(text, textpos) 185 | 186 | font = pygame.font.Font(None, 30) 187 | textF = font.render(values[0], 1, WHITE) 188 | ## textF = font.render("1.8", 1, WHITE) 189 | textposF = textpos[0] + textpos[2] + 10, textpos[1] 190 | DISPLAYSURF.blit(textF, textposF) 191 | 192 | ## Garage 193 | 194 | font = pygame.font.Font(None, 30) 195 | text = font.render("Garage:", 1, WHITE) 196 | textpos = text.get_rect(topright=(DISPLAYSURF.get_width()/2+90,165)) 197 | DISPLAYSURF.blit(text, textpos) 198 | 199 | font = pygame.font.Font(None, 30) 200 | textF = font.render(values[1], 1, WHITE) 201 | textposF = textpos[0] + textpos[2] + 10, textpos[1] 202 | DISPLAYSURF.blit(textF, textposF) 203 | 204 | ## Draw time 205 | 206 | font = pygame.font.Font(None, 75) 207 | text = font.render(currentime.strftime("%I:%M %p"), 1, CYAN) 208 | textpos = text.get_rect(center=(DISPLAYSURF.get_width()/2,215)) 209 | DISPLAYSURF.blit(text, textpos) 210 | 211 | 212 | ## Update the LCD 213 | 214 | pygame.display.update() 215 | 216 | 217 | ## Sleep time! 218 | 219 | time.sleep(60) 220 | --------------------------------------------------------------------------------