2
2
# https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
3
3
# https://randomnerdtutorials.com/micropython-bme280-esp32-esp8266/
4
4
5
+ import _thread # to use two infinite loops we need to use two threads of our CPU (ESP32 has 2)
5
6
from time import sleep
6
7
from web_pages import web_page_weather
7
8
10
11
s .bind (('' , 80 )) # bind the socket to an IP address ('' or 'localhost') and port (80, but normally use >3000)
11
12
s .listen (5 ) # enable to accept connections with maximum 5 queued connections.
12
13
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
17
20
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
30
28
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 ()
46
44
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
51
49
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
57
55
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
71
69
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
83
82
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()
84
117
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 , ())
0 commit comments