Skip to content

Commit c38de41

Browse files
Experiment: _thread for Server and Sensors - WIP - Errors
1 parent 98eba75 commit c38de41

File tree

3 files changed

+125
-112
lines changed

3 files changed

+125
-112
lines changed

BME280 Sensor - online weather station/boot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# import sensor library and set Pins for I2C
3030
import BME280
3131
i2c_sensor = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)
32+
bme = BME280.BME280(i2c=i2c_sensor)
3233

3334
# import OLED display library, set Pins for I2C and activate a screen
3435
import ssd1306

BME280 Sensor - online weather station/main.py

Lines changed: 99 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
33
# https://randomnerdtutorials.com/micropython-bme280-esp32-esp8266/
44

5+
import _thread # to use two infinite loops we need to use two threads of our CPU (ESP32 has 2)
56
from time import sleep
67
from web_pages import web_page_weather
78

@@ -10,106 +11,109 @@
1011
s.bind(('', 80)) # bind the socket to an IP address ('' or 'localhost') and port (80, but normally use >3000)
1112
s.listen(5) # enable to accept connections with maximum 5 queued connections.
1213

13-
counter = 0
14-
temp_sum = 0
15-
hum_sum = 0
16-
pres_sum = 0
14+
def sensorsThread():
15+
global oled
16+
counter = 0
17+
temp_sum = 0
18+
hum_sum = 0
19+
pres_sum = 0
1720

18-
while True:
19-
try:
20-
# Free memory if it is too low
21-
if gc.mem_free() < 102000:
22-
gc.collect()
23-
24-
# GET DATA FROM SENSOR AS A STRING
25-
# We dont have to import BME280 here, it's already imported in boot.py which runs before main.py
26-
bme = BME280.BME280(i2c=i2c_sensor)
27-
temp_string = bme.temperature
28-
hum_string = bme.humidity
29-
pres_string = bme.pressure
21+
while True:
22+
try:
23+
# GET DATA FROM SENSOR AS A STRING
24+
# We dont have to import BME280 here, it's already imported in boot.py which runs before main.py
25+
temp_string = bme.temperature
26+
hum_string = bme.humidity
27+
pres_string = bme.pressure
3028

31-
# SHOW ON OLED SCREEN - oled is activated in boot.py
32-
oled.fill(0)
33-
if temp_string != '0.00C':
34-
oled.text('Temp: ' + temp_string, 0, 10, 1)
35-
else:
36-
oled.text('Temp: ---', 0, 10, 1)
37-
if hum_string != '0.00%':
38-
oled.text('Hum: ' + hum_string, 0, 25, 1)
39-
else:
40-
oled.text('Hum: ---', 0, 25, 1)
41-
if pres_string != '0.00hPa':
42-
oled.text('Pres: ' + pres_string, 0, 40, 1)
43-
else:
44-
oled.text('Pres: ---', 0, 40, 1)
45-
oled.show()
29+
# SHOW ON OLED SCREEN - oled is activated in boot.py
30+
oled.fill(0)
31+
if temp_string != '0.00C':
32+
oled.text('Temp: ' + temp_string, 0, 10, 1)
33+
else:
34+
oled.text('Temp: ---', 0, 10, 1)
35+
if hum_string != '0.00%':
36+
oled.text('Hum: ' + hum_string, 0, 25, 1)
37+
else:
38+
oled.text('Hum: ---', 0, 25, 1)
39+
if pres_string != '0.00hPa':
40+
oled.text('Pres: ' + pres_string, 0, 40, 1)
41+
else:
42+
oled.text('Pres: ---', 0, 40, 1)
43+
oled.show()
4644

47-
# GET DATA FROM SENSOR AS FLOATS (SHOULD I SEND RAW INTEGERS TO A SERVER?)
48-
temp = bme.read_temperature()/100
49-
hum = bme.read_humidity()/1024
50-
pres = bme.read_pressure()/256/100
45+
# GET DATA FROM SENSOR AS FLOATS (SHOULD I SEND RAW INTEGERS TO A SERVER?)
46+
temp = bme.read_temperature()/100
47+
hum = bme.read_humidity()/1024
48+
pres = bme.read_pressure()/256/100
5149

52-
# COUNTER AND SUMS TO CALCULATE AVERAGE
53-
counter += 1
54-
temp_sum += temp
55-
hum_sum += hum
56-
pres_sum += pres
50+
# COUNTER AND SUMS TO CALCULATE AVERAGE
51+
counter += 1
52+
temp_sum += temp
53+
hum_sum += hum
54+
pres_sum += pres
5755

58-
# EVERY X times send avarage data to an API (TODO, print for now)
59-
if counter == 10:
60-
temp_average = round(temp_sum/counter, 2)
61-
hum_average = round(hum_sum/counter, 2)
62-
pres_average = round(pres_sum/counter, 2)
63-
print('Temperature: ', temp_average)
64-
print('Humidity: ', hum_average)
65-
print('Pressure: ', pres_average)
66-
67-
counter = 0
68-
temp_sum = 0
69-
hum_sum = 0
70-
pres_sum = 0
56+
# EVERY X times send avarage data to an API (TODO, print for now)
57+
if counter == 10:
58+
temp_average = round(temp_sum/counter, 2)
59+
hum_average = round(hum_sum/counter, 2)
60+
pres_average = round(pres_sum/counter, 2)
61+
print('Temperature: ', temp_average)
62+
print('Humidity: ', hum_average)
63+
print('Pressure: ', pres_average)
64+
65+
counter = 0
66+
temp_sum = 0
67+
hum_sum = 0
68+
pres_sum = 0
7169

72-
except OSError as e:
73-
print('Error:')
74-
print(e)
75-
oled.fill(0)
76-
oled.text('Sensor Error', 0, 10, 1)
77-
oled.show()
78-
except:
79-
print('Some Error')
80-
oled.fill(0)
81-
oled.text('Some Error', 0, 10, 1)
82-
oled.show()
70+
except OSError as e:
71+
print('---- Error: ', e)
72+
oled.fill(0)
73+
oled.text('Sensor Error', 0, 10, 1)
74+
oled.show()
75+
except:
76+
print('Some Sensor Error')
77+
# oled.fill(0)
78+
# oled.text('Some Error', 0, 10, 1)
79+
# oled.show()
80+
81+
sleep(5) # whait for 5s and repeat a while loop
8382

83+
def serverThread():
84+
while True:
85+
try:
86+
# Free memory if it is too low
87+
if gc.mem_free() < 102000:
88+
gc.collect()
89+
90+
# ACCEPT NETWORK CONNECTIONS AND SEND WEB PAGES:
91+
conn, addr = s.accept()
92+
conn.settimeout(3.0)
93+
print('Got a connection from %s' % str(addr))
94+
request = conn.recv(1024)
95+
conn.settimeout(None)
96+
request = str(request)
97+
print('Content = %s' % request)
98+
response = web_page_weather()
99+
conn.send('HTTP/1.1 200 OK\n')
100+
conn.send('Content-Type: text/html\n')
101+
conn.send('Connection: close\n\n')
102+
conn.sendall(response)
103+
conn.close()
104+
105+
except OSError as e:
106+
print('---- Error: ', e)
107+
conn.close()
108+
print('Connection closed')
109+
oled.fill(0)
110+
oled.text('Connection Error', 0, 10, 1)
111+
oled.show()
112+
except:
113+
print('Some Connection Error')
114+
# oled.fill(0)
115+
# oled.text('Some Error', 0, 10, 1)
116+
# oled.show()
84117

85-
try:
86-
# ACCEPT NETWORK CONNECTIONS AND SEND WEB PAGES:
87-
conn, addr = s.accept()
88-
conn.settimeout(3.0)
89-
print('Got a connection from %s' % str(addr))
90-
request = conn.recv(1024)
91-
conn.settimeout(None)
92-
request = str(request)
93-
print('Content = %s' % request)
94-
response = web_page_weather()
95-
conn.send('HTTP/1.1 200 OK\n')
96-
conn.send('Content-Type: text/html\n')
97-
conn.send('Connection: close\n\n')
98-
conn.sendall(response)
99-
conn.close()
100-
101-
except OSError as e:
102-
print('Error:')
103-
print(e)
104-
conn.close()
105-
print('Connection closed')
106-
oled.fill(0)
107-
oled.text('Connection Error', 0, 10, 1)
108-
oled.show()
109-
except:
110-
print('Some Error')
111-
oled.fill(0)
112-
oled.text('Some Error', 0, 10, 1)
113-
oled.show()
114-
115-
sleep(5)
118+
_thread.start_new_thread(sensorsThread, ())
119+
_thread.start_new_thread(serverThread, ())
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
def web_page_weather():
2-
# bme = BME280.BME280(i2c=i2c_sensor) # BME should be activated in main.py befor we use this function
3-
4-
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1">
5-
<link rel="icon" href="data:,"><style>body { text-align: center; font-family: "Trebuchet MS", Arial;}
6-
table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }
7-
th { padding: 12px; background-color: #0043af; color: white; }
8-
tr { border: 1px solid #ddd; padding: 12px; }
9-
tr:hover { background-color: #bcbcbc; }
10-
td { border: none; padding: 12px; }
11-
.sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px;
12-
</style></head><body><h1>ESP with BME280</h1>
13-
<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>
14-
<tr><td>Temp. Celsius</td><td><span class="sensor">""" + str(bme.temperature) + """</span></td></tr>
15-
<tr><td>Temp. Fahrenheit</td><td><span class="sensor">""" + str(round((bme.read_temperature()/100.0) * (9/5) + 32, 2)) + """F</span></td></tr>
16-
<tr><td>Pressure</td><td><span class="sensor">""" + str(bme.pressure) + """</span></td></tr>
17-
<tr><td>Humidity</td><td><span class="sensor">""" + str(bme.humidity) + """</span></td></tr></body></html>"""
18-
return html
2+
try:
3+
import BME280
4+
bme2 = BME280.BME280(i2c=i2c_sensor) # BME should be activated in main.py befor we use this function
5+
6+
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="icon" href="data:,"><style>body { text-align: center; font-family: "Trebuchet MS", Arial;}
8+
table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }
9+
th { padding: 12px; background-color: #0043af; color: white; }
10+
tr { border: 1px solid #ddd; padding: 12px; }
11+
tr:hover { background-color: #bcbcbc; }
12+
td { border: none; padding: 12px; }
13+
.sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px;
14+
</style></head><body><h1>ESP with BME280</h1>
15+
<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>
16+
<tr><td>Temp. Celsius</td><td><span class="sensor">""" + str(bme2.temperature) + """</span></td></tr>
17+
<tr><td>Temp. Fahrenheit</td><td><span class="sensor">""" + str(round((bme2.read_temperature()/100.0) * (9/5) + 32, 2)) + """F</span></td></tr>
18+
<tr><td>Pressure</td><td><span class="sensor">""" + str(bme2.pressure) + """</span></td></tr>
19+
<tr><td>Humidity</td><td><span class="sensor">""" + str(bme2.humidity) + """</span></td></tr></body></html>"""
20+
return html
21+
except OSError as e:
22+
print('Error: ', e)
23+
print(e)
24+
print('Webpage error')
25+
except:
26+
print('Some Webpage Error', bme2)

0 commit comments

Comments
 (0)