1+ """Climate station example using WSEN-PADS, HTS221 and SSD1327 OLED.
2+
3+ Reads temperature and pressure from the WSEN-PADS sensor and humidity
4+ from the HTS221 sensor. Includes a startup calibration phase to compensate
5+ sensor offsets. Displays live temperature, humidity and pressure readings
6+ with horizontal progress bars and a comfort index on a round 128x128 OLED.
7+ """
18from machine import I2C , SPI , Pin
29from time import sleep_ms
310import ssd1327
613
714# === Ecran ===
815spi = SPI (1 )
9- dc = Pin ("DATA_COMMAND_DISPLAY" )
16+ dc = Pin ("DATA_COMMAND_DISPLAY" )
1017res = Pin ("RST_DISPLAY" )
11- cs = Pin ("CS_DISPLAY" )
18+ cs = Pin ("CS_DISPLAY" )
1219display = ssd1327 .WS_OLED_128X128_SPI (spi , dc , res , cs )
1320
1421# === Capteurs ===
1522i2c = I2C (1 )
16- pads = WSEN_PADS (i2c ) # pression + temperature
17- hts = HTS221 (i2c ) # humidite
23+ pads = WSEN_PADS (i2c )
24+ hts = HTS221 (i2c )
1825
1926# === Calibration ===
2027print ("Calibration en cours..." )
2128temps = []
22- hums = []
29+ hums = []
2330press = []
2431for _ in range (10 ):
2532 temps .append (pads .temperature ())
2633 hums .append (hts .humidity ())
2734 press .append (pads .pressure_hpa ())
2835 sleep_ms (200 )
2936
30- OFFSET_TEMP = 20.0 - (sum (temps ) / len (temps ))
31- OFFSET_HUM = 50.0 - (sum (hums ) / len (hums ))
37+ OFFSET_TEMP = 20.0 - (sum (temps ) / len (temps ))
38+ OFFSET_HUM = 50.0 - (sum (hums ) / len (hums ))
3239OFFSET_PRES = 1013.0 - (sum (press ) / len (press ))
3340print (f"Offset T:{ OFFSET_TEMP :.2f} H:{ OFFSET_HUM :.2f} P:{ OFFSET_PRES :.2f} " )
3441print ("Calibration OK" )
3542
43+
3644# === Dessin ===
37- def draw_hline (x , y , w , color = 255 ):
45+ def draw_hline (x , y , w , color = 15 ):
3846 for i in range (w ):
3947 display .pixel (x + i , y , color )
4048
41- def draw_vline (x , y , h , color = 255 ):
49+
50+ def draw_vline (x , y , h , color = 15 ):
4251 for i in range (h ):
4352 display .pixel (x , y + i , color )
4453
45- def draw_rect (x , y , w , h , color = 255 ):
54+
55+ def draw_rect (x , y , w , h , color = 15 ):
4656 draw_hline (x , y , w , color )
4757 draw_hline (x , y + h - 1 , w , color )
4858 draw_vline (x , y , h , color )
4959 draw_vline (x + w - 1 , y , h , color )
5060
51- def draw_fill_rect (x , y , w , h , color = 255 ):
61+
62+ def draw_fill_rect (x , y , w , h , color = 15 ):
5263 for i in range (h ):
5364 draw_hline (x , y + i , w , color )
5465
55- def draw_circle (cx , cy , r , color = 255 ):
66+
67+ def draw_circle (cx , cy , r , color = 15 ):
5668 x = r
5769 y = 0
5870 err = 0
@@ -71,86 +83,90 @@ def draw_circle(cx, cy, r, color=255):
7183 x -= 1
7284 err += 1 - 2 * x
7385
74- def draw_bar_h (x , y , w , h , value , min_val , max_val , color = 200 ):
75- draw_rect (x , y , w , h , 80 )
86+
87+ def draw_bar_h (x , y , w , h , value , min_val , max_val , color = 13 ):
88+ draw_rect (x , y , w , h , 5 )
7689 ratio = (value - min_val ) / (max_val - min_val )
7790 ratio = max (0.0 , min (1.0 , ratio ))
7891 filled = int ((w - 2 ) * ratio )
7992 if filled > 0 :
8093 draw_fill_rect (x + 1 , y + 1 , filled , h - 2 , color )
8194
95+
8296def comfort_label (temp , hum , pres ):
8397 if 18 <= temp <= 26 and 40 <= hum <= 60 and 1000 <= pres <= 1025 :
84- return "IDEAL" , 255
98+ return "IDEAL" , 15
8599 elif temp > 30 or hum > 75 :
86- return "CHAUD" , 180
100+ return "CHAUD" , 11
87101 elif temp < 15 or hum < 25 :
88- return "FROID" , 150
102+ return "FROID" , 9
89103 elif pres < 1000 :
90- return "BASSE P" , 150
104+ return "BASSE P" , 9
91105 elif pres > 1025 :
92- return "HAUTE P" , 200
106+ return "HAUTE P" , 13
93107 else :
94- return "CORRECT" , 200
108+ return "CORRECT" , 12
109+
95110
96111def draw_screen (temp , hum , pres ):
97112 display .fill (0 )
98113
99- # Bordure circulaire decorative
100- draw_circle (64 , 64 , 62 , 60 )
101- draw_circle (64 , 64 , 60 , 40 )
114+ # Bordure circulaire
115+ draw_circle (64 , 64 , 62 , 4 )
116+ draw_circle (64 , 64 , 60 , 2 )
102117
103118 # === TITRE ===
104- display .text ("CLIMAT" , 35 , 20 , 255 )
105- draw_hline (19 , 29 , 90 , 100 )
119+ display .text ("CLIMAT" , 35 , 20 , 15 )
120+ draw_hline (19 , 29 , 90 , 6 )
106121
107122 # === TEMPERATURE ===
108- display .text ("T" , 19 , 35 , 120 )
123+ display .text ("T" , 19 , 35 , 7 )
109124 temp_str = f"{ temp :.1f} C"
110- display .text (temp_str , 29 , 35 , 255 )
111- draw_bar_h (19 , 44 , 90 , 5 , temp , 0 , 50 , 220 )
125+ display .text (temp_str , 29 , 35 , 15 )
126+ draw_bar_h (19 , 44 , 90 , 5 , temp , 0 , 50 , 13 )
112127
113128 # === HUMIDITE ===
114- display .text ("H" , 19 , 52 , 120 )
129+ display .text ("H" , 19 , 52 , 7 )
115130 hum_str = f"{ hum :.1f} %"
116- display .text (hum_str , 29 , 52 , 255 )
117- draw_bar_h (19 , 61 , 90 , 5 , hum , 0 , 100 , 180 )
131+ display .text (hum_str , 29 , 52 , 15 )
132+ draw_bar_h (19 , 61 , 90 , 5 , hum , 0 , 100 , 11 )
118133
119134 # === PRESSION ===
120- display .text ("P" , 19 , 69 , 120 )
135+ display .text ("P" , 19 , 69 , 7 )
121136 pres_str = f"{ pres :.0f} hPa"
122- display .text (pres_str , 29 , 69 , 255 )
123- draw_bar_h (19 , 78 , 90 , 5 , pres , 950 , 1050 , 160 )
137+ display .text (pres_str , 29 , 69 , 15 )
138+ draw_bar_h (19 , 78 , 90 , 5 , pres , 950 , 1050 , 10 )
124139
125140 # === SEPARATEUR ===
126- draw_hline (19 , 86 , 90 , 70 )
141+ draw_hline (19 , 86 , 90 , 4 )
127142
128143 # === CONFORT ===
129144 label , c_color = comfort_label (temp , hum , pres )
130145 cx = 64 - len (label ) * 4
131146 display .text (label , cx , 92 , c_color )
132147
133148 # === CAPTEURS ===
134- draw_hline (19 , 102 , 90 , 50 )
135- display .text ("PADS+HTS221" , 22 , 106 , 60 )
149+ draw_hline (19 , 102 , 90 , 3 )
150+ display .text ("PADS+HTS221" , 22 , 106 , 4 )
136151
137152 display .show ()
138153
154+
139155# === Boucle principale ===
140156print ("Station climatique demarree" )
141157while True :
142158 try :
143159 temp = pads .temperature () + OFFSET_TEMP
144- hum = hts .humidity () + OFFSET_HUM
145- pres = pads .pressure_hpa ()+ OFFSET_PRES
146- hum = max (0.0 , min (100.0 , hum ))
160+ hum = hts .humidity () + OFFSET_HUM
161+ pres = pads .pressure_hpa () + OFFSET_PRES
162+ hum = max (0.0 , min (100.0 , hum ))
147163 print (f"T:{ temp :.2f} C H:{ hum :.2f} % P:{ pres :.1f} hPa" )
148164 draw_screen (temp , hum , pres )
149165 except Exception as e :
150166 print ("Erreur:" , e )
151167 display .fill (0 )
152- display .text ("ERREUR" , 35 , 55 , 255 )
153- display .text (str (e )[:16 ], 0 , 70 , 150 )
168+ display .text ("ERREUR" , 35 , 55 , 15 )
169+ display .text (str (e )[:16 ], 0 , 70 , 9 )
154170 display .show ()
155171 sleep_ms (1000 )
156172
0 commit comments