tym razem na łamach mojego bloga znajdzie się konstrukcja prosta, lecz na codzień potrzebna – pogodynka z prognozą pogody oraz pomiarem temperatury wilgotności i ciśnienia w pomieszczeniu.
Projekt rozpoczął się z powodu leżącego ponad rok na półce Raspberry pi zero oraz wyświetlacza e-papier – waweshare 2,7 cala. Dodatkowo znalazł się tam również czujnik BME280.
Cały kod został napisany w języku python. Prognoza pogoda oparta jest o dane pobierane przez wifi ze strony openwheatermap. Wystarczy się zalogować i za darmo można pobierać 60x na minutę prognozę pogody. Do tego twórcy strony dodają instrukcję jak szybko zaimplementować api do swojego programu.

Skrypt wyzwalam co 1 godzinę. Przez to że wyświetlacz nie emituje światła, nadaje się do sypialni i nie trzeba go wyłączać w nocy. Jedynym minusem jest to że wyświetlacz odświeża się dosyć długo – 2 sekundy – przy czym miga na czarno i biało.

Oczywiście takie wykorzystanie Raspberry to przerost formy nad treścią, lecz w obudowie można ukryś parę innych czujników, podłączyć drukarkę i zrobić z pogodynki printserwer czy też zlecić kopanie kryptowalut
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
#!/usr/bin/python # -*- coding:utf-8 -*- import epd2in7b import time import commands import sys import os import smbus import socket from ctypes import c_short from ctypes import c_byte from ctypes import c_ubyte picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'P/pic') from PIL import Image,ImageDraw,ImageFont import traceback #IP def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # doesn't even have to be reachable s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except: IP = '127.0.0.1' finally: s.close() return IP print "\n IP :", get_ip() #BME280 DEVICE = 0x76 # Default device I2C address bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1 # Rev 1 Pi uses bus 0 def getShort(data, index): # return two bytes from data as a signed 16-bit value return c_short((data[index+1] << 8) + data[index]).value def getUShort(data, index): # return two bytes from data as an unsigned 16-bit value return (data[index+1] << 8) + data[index] def getChar(data,index): # return one byte from data as a signed char result = data[index] if result > 127: result -= 256 return result def getUChar(data,index): # return one byte from data as an unsigned char result = data[index] & 0xFF return result def readBME280ID(addr=DEVICE): # Chip ID Register Address REG_ID = 0xD0 (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2) return (chip_id, chip_version) def readBME280All(addr=DEVICE): # Register Addresses REG_DATA = 0xF7 REG_CONTROL = 0xF4 REG_CONFIG = 0xF5 REG_CONTROL_HUM = 0xF2 REG_HUM_MSB = 0xFD REG_HUM_LSB = 0xFE # Oversample setting - page 27 OVERSAMPLE_TEMP = 2 OVERSAMPLE_PRES = 2 MODE = 1 # Oversample setting for humidity register - page 26 OVERSAMPLE_HUM = 2 bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM) control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE bus.write_byte_data(addr, REG_CONTROL, control) # Read blocks of calibration data from EEPROM # See Page 22 data sheet cal1 = bus.read_i2c_block_data(addr, 0x88, 24) cal2 = bus.read_i2c_block_data(addr, 0xA1, 1) cal3 = bus.read_i2c_block_data(addr, 0xE1, 7) # Convert byte data to word values dig_T1 = getUShort(cal1, 0) dig_T2 = getShort(cal1, 2) dig_T3 = getShort(cal1, 4) dig_P1 = getUShort(cal1, 6) dig_P2 = getShort(cal1, 8) dig_P3 = getShort(cal1, 10) dig_P4 = getShort(cal1, 12) dig_P5 = getShort(cal1, 14) dig_P6 = getShort(cal1, 16) dig_P7 = getShort(cal1, 18) dig_P8 = getShort(cal1, 20) dig_P9 = getShort(cal1, 22) dig_H1 = getUChar(cal2, 0) dig_H2 = getShort(cal3, 0) dig_H3 = getUChar(cal3, 2) dig_H4 = getChar(cal3, 3) dig_H4 = (dig_H4 << 24) >> 20 dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F) dig_H5 = getChar(cal3, 5) dig_H5 = (dig_H5 << 24) >> 20 dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F) dig_H6 = getChar(cal3, 6) # Wait in ms (Datasheet Appendix B: Measurement time and current calculation) wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575) time.sleep(wait_time/1000) # Wait the required time # Read temperature/pressure/humidity data = bus.read_i2c_block_data(addr, REG_DATA, 8) pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4) temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4) hum_raw = (data[6] << 8) | data[7] #Refine temperature var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11 var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14 t_fine = var1+var2 temperature = float(((t_fine * 5) + 128) >> 8); # Refine pressure and adjust for temperature var1 = t_fine / 2.0 - 64000.0 var2 = var1 * var1 * dig_P6 / 32768.0 var2 = var2 + var1 * dig_P5 * 2.0 var2 = var2 / 4.0 + dig_P4 * 65536.0 var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0 var1 = (1.0 + var1 / 32768.0) * dig_P1 if var1 == 0: pressure=0 else: pressure = 1048576.0 - pres_raw pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1 var1 = dig_P9 * pressure * pressure / 2147483648.0 var2 = pressure * dig_P8 / 32768.0 pressure = pressure + (var1 + var2 + dig_P7) / 16.0 # Refine humidity humidity = t_fine - 76800.0 humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity))) humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0) if humidity > 100: humidity = 100 elif humidity < 0: humidity = 0 return temperature/100.0,pressure/100.0,humidity def main(): (chip_id, chip_version) = readBME280ID() print "===Temperature internal sensor===" print " Chip ID :", chip_id print " Version :", chip_version temperature,pressure,humidity = readBME280All() print " Temperature : ", temperature, "C" print " Pressure : ", pressure, "hPa" print " Humidity : ", humidity, "%" if __name__=="__main__": main() #BME280 END #API # Python program to find current # weather details of any city # using openweathermap api # import required modules import requests, json # Enter your API key here api_key = "94115424xxxxxxxxxxxxxx" # base_url variable to store url base_url = "http://api.openweathermap.org/data/2.5/weather?" # Give city name city_name = ("Krakow,pl") # complete_url variable to store # complete url address complete_url = base_url + "q=" + city_name + "&appid=" + api_key # get method of requests module # return response object response = requests.get(complete_url) # json method of response object # convert json format data into # python format data x = response.json() #print(x) # Now x contains list of nested dictionaries # Check the value of "cod" key is equal to # "404", means city is found otherwise, # city is not found if x["cod"] != "404": # store the value of "main" # key in variable y y = x["main"] # store the value corresponding # to the "temp" key of y current_temperature = y["temp"] ctemp = (float(current_temperature)-273.25) ctemp = round(ctemp,1) min_temperature = y["temp_min"] mintemp = (float(min_temperature)-273.25) mintemp = round(mintemp,1) max_temperature = y["temp_max"] maxtemp = (float(max_temperature)-273.25) maxtemp = round(maxtemp,1) f = x["wind"] current_wind = f["speed"] wind = float(current_wind) # store the value corresponding # to the "pressure" key of y current_pressure = y["pressure"] # store the value corresponding # to the "humidity" key of y current_humidiy = y["humidity"] # store the value of "weather" # key in variable z z = x["weather"] # store the value corresponding # to the "description" key at # the 0th index of z weather_description = z[0]["description"] t = time.localtime() current_time = time.strftime("%H:%M", t) # print following values print("\n===Forecast==="+ "\n Temperature (deg C) = " + str(ctemp) + "\n Atmospheric pressure (in hPa unit) = " + str(current_pressure) + "\n Humidity (in percentage) = " + str(current_humidiy) + "\n Description = " + str(weather_description)+ "\n Time = " + current_time + "\n ") else: print(" City Not Found ") #API END #DISPLAY try: epd = epd2in7b.EPD() epd.init() print("Clear Display...") epd.Clear(0xFF) # Drawing on the Horizontal image HBlackimage = Image.new('1', (epd2in7b.EPD_HEIGHT, epd2in7b.EPD_WIDTH), 255) # 298*126 HRedimage = Image.new('1', (epd2in7b.EPD_HEIGHT, epd2in7b.EPD_WIDTH), 255) # 298*126 # Horizontal print("...Drawing Frame...") drawblack = ImageDraw.Draw(HBlackimage) drawred = ImageDraw.Draw(HRedimage) HBlackimage = Image.open(os.path.join(picdir, '2in77.bmp')) #font h1 = ImageFont.truetype('/usr/share/fonts/truetype/piboto/Piboto-Bold.ttf', 36) h2 = ImageFont.truetype('/usr/share/fonts/truetype/piboto/Piboto-Bold.ttf', 24) h3 = ImageFont.truetype('/usr/share/fonts/truetype/piboto/Piboto-Bold.ttf', 18) h4 = ImageFont.truetype('/usr/share/fonts/truetype/piboto/Piboto-Bold.ttf', 16) #text print("...Drawing text...") # X Y # Column X=20 column = 20 #drawred.text((column, 0), (str("Forecast")), font = h4, fill = 0) drawred.text((column, 20), (str(ctemp)), font = h1, fill = 0) drawred.text((column, 60), (str(current_humidiy)), font = h2, fill = 0) drawred.text((column, 85), (str(current_pressure)), font = h2, fill = 0) drawred.text((column, 110), (str(wind)), font = h2, fill = 0) drawred.text((column, 135), (str(weather_description)), font = h3, fill = 0) drawred.text((column, 155), (""+ current_time+" "+get_ip()), font = h3, fill = 0) # Column X=150 column = 150 temperature,pressure,humidity = readBME280All() #drawred.text((column, 0), (str("Home temperature")), font = h4, fill = 0) drawred.text((column, 20), (str(round(temperature,1))),font = h1, fill = 0) drawred.text((column, 60), (str(round(humidity))), font = h2, fill = 0) drawred.text((column, 85), (str(round(pressure))), font = h2, fill = 0) # drawred.text((60, 5),(str(maxtemp)), font = font19, fill = 0) # drawred.text((20, 20), (str(ctemp)), font = font36, fill = 0) # drawred.text((60, 60),(str(mintemp)), font = font19, fill = 0) # drawred.text((150, 20), (str(current_pressure)), font = font24, fill = 0) # drawred.text((170, 70), (str(current_humidiy)), font = font24, fill = 0) # drawred.text((170, 130),(str(wind)), font = font24, fill = 0) # drawred.text((5, 90), ( str(weather_description)), font = font18, fill = 0) # drawred.text((5, 140),("Update: "+ current_time), font = font18, fill = 0) epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRedimage)) time.sleep(2) epd.sleep() print("...Done...") except : print ('traceback.format_exc():\n%s',traceback.format_exc()) exit() |

Obudowa została wydrukowana w dwóch kawałkach z materiału PLA. Dodatkowo dodane zostały nóżki gumowe firmy 3M. Projekt natomiast powstał w oprogramowaniu FreeCAD. Ogólnie polecam program, w dwa popołudnia od zera i bez podstaw można go opanować.

Jak na pierwszą obudowę 3D mojego autorstwa, uważam za udaną i bardzo mi się podoba.


obrazek jak i icony zostały wykonane przeze mnie w programie graficznym – Paint – proszę się nie śmiać zbyt głośno 🙂

Moduł z czujnikiem został ukryty w gnieździe DB9 – tak by w środku obudowy nie był regularnie podgrzewany za pomocą raspberry 🙂
Pobór prądu w czasie pracy nie jest większy niż 180mA przy 5V.