Want to read sensor data (BME280) from serial interface (UART) and send this data to a webserver.
I have already micropython running to write from webserver to UART1.
Is there already some usable code available?
Actually I could use also a ESP32.
Hi.
I’m sorry but I didn’t understand your question.
Do you need to read data from UART 1 and place that data in a web server using MicroPython?
Do you already have some code on how to read data from UART?
Regards,
Sara
Hi Sara, thanks for your reply.
That’s right, I would like to read sensor data of a BME280 from UART1 and display them in a webserver page on a mobil device. On the webserver I have some touch buttons, which I use to send defined data strings to the UART1.
Here is my code “main.py” – I have already included the text for displaying the sensor data.
# ESP8266-01 with WIFI Manager, by Dieter Wingel
print(“ESP OK”)
def web_page():
html = “””<html><head> <meta name=”viewport” content=”width=device-width, initial-scale=1″>
<link rel=”icon” href=”data:,”> <title>Smart_Home_ESP</title> <style>body{font-family: Calibri; margin: 0px auto; text-align: center;}
.button {width: 80px; padding: 8px 20px; outline: none; font-size: 20px; color:#FFFFFF; background-color: #4CAF50; border: none; border-radius: 15px; box-shadow: 0 10px #999999; cursor: pointer;
-webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-tap-highlight-color: rgba(0,0,0,0);}
.button:active {background-color: #00FF00; box-shadow: 0 5px #666666; transform: translateY(2px);}
.button1 {background-color: #B40404;} .button1:active {background-color: #FF0000;}</style></head>
<body> <pre>
</pre>
<h2>WIFI – Fernbedienung</h2>
<h3>Eingaenge F16 bis F35
<pre> <a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste1=on’)\” ontouchend=\”toggleCheckbox(‘?taste1=off’)\”>EIN v</button></a> 16 Eingang 17 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste2=on’)\” ontouchend=\”toggleCheckbox(‘?taste2=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste3=on’)\” ontouchend=\”toggleCheckbox(‘?taste3=off’)\”>EIN v</button></a> 18 Eingang 19 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste4=on’)\” ontouchend=\”toggleCheckbox(‘?taste4=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste5=on’)\” ontouchend=\”toggleCheckbox(‘?taste5=off’)\”>EIN v</button></a> 20 Eingang 21 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste6=on’)\” ontouchend=\”toggleCheckbox(‘?taste6=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste7=on’)\” ontouchend=\”toggleCheckbox(‘?taste7=off’)\”>EIN v</button></a> 22 Eingang 23 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste8=on’)\” oontouchend=\”toggleCheckbox(‘?taste8=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste9=on’)\” ontouchend=\”toggleCheckbox(‘?taste9=off’)\”>EIN v</button></a> 24 Eingang 25 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste10=on’)\” ontouchend=\”toggleCheckbox(‘?taste10=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste11=on’)\” ontouchend=\”toggleCheckbox(‘?taste11=off’)\”>EIN v</button></a> 26 Eingang 27 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste12=on’)\” ontouchend=\”toggleCheckbox(‘?taste12=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste13=on’)\” ontouchend=\”toggleCheckbox(‘?taste13=off’)\”>EIN v</button></a> 28 Eingang 29 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste14=on’)\” ontouchend=\”toggleCheckbox(‘?taste14=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste15=on’)\” ontouchend=\”toggleCheckbox(‘?taste15=off’)\”>EIN v</button></a> 30 Eingang 31 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste16=on’)\” ontouchend=\”toggleCheckbox(‘?taste16=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste17=on’)\” ontouchend=\”toggleCheckbox(‘?taste17=off’)\”>EIN v</button></a> 32 Eingang 33 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste18=on’)\” ontouchend=\”toggleCheckbox(‘?taste18=off’)\”>AUS ^</button></a>
<a button class=\”button\” ontouchstart=\”toggleCheckbox(‘?taste19=on’)\” ontouchend=\”toggleCheckbox(‘?taste19=off’)\”>EIN v</button></a> 34 Eingang 35 <a button class=\”button button1\” ontouchstart=\”toggleCheckbox(‘?taste20=on’)\” ontouchend=\”toggleCheckbox(‘?taste20=off’)\”>AUS ^</button></a>
</pre></h3>
<h2>Sensordaten</h2>
<h3><pre>Aussentemperatur = C
Aussenfeuchtigkeit = %
Helligkeit = lux
Luftdruck = mBar
</pre></h3>
<script>
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open(“GET”, “/” + x, true);
xhr.send();
}
</script></body></html>”””
return html
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((”, 80))
s.listen(5)
except OSError as e:
machine.reset()
while True:
try:
if gc.mem_free() < 102000:
gc.collect()
conn, addr = s.accept()
conn.settimeout(3.0)
print(‘Got a connection from %s’ % str(addr))
request = conn.recv(1024)
conn.settimeout(None)
request = str(request)
print(‘Content = %s’ % request)
taste1_on = request.find(‘/?taste1=on’)
if taste1_on == 6:
uart.write(“202013”)
taste1_off = request.find(‘/?taste1=off’)
if taste1_off == 6:
uart.write(“202003”)
taste2_on = request.find(‘/?taste2=on’)
if taste2_on == 6:
uart.write(“202113”)
taste2_off = request.find(‘/?taste2=off’)
if taste2_off == 6:
uart.write(“202103”)
taste3_on = request.find(‘/?taste3=on’)
if taste3_on == 6:
uart.write(“202213”)
taste3_off = request.find(‘/?taste3=off’)
if taste3_off == 6:
uart.write(“202203”)
taste4_on = request.find(‘/?taste4=on’)
if taste4_on == 6:
uart.write(“202313”)
taste4_off = request.find(‘/?taste4=off’)
if taste4_off == 6:
uart.write(“202303”)
taste5_on = request.find(‘/?taste5=on’)
if taste5_on == 6:
uart.write(“202413”)
taste5_off = request.find(‘/?taste5=off’)
if taste5_off == 6:
uart.write(“202403”)
taste6_on = request.find(‘/?taste6=on’)
if taste6_on == 6:
uart.write(“202513”)
taste6_off = request.find(‘/?taste6=off’)
if taste6_off == 6:
uart.write(“202503”)
taste7_on = request.find(‘/?taste7=on’)
if taste7_on == 6:
uart.write(“202613”)
taste7_off = request.find(‘/?taste7=off’)
if taste7_off == 6:
uart.write(“202603”)
taste8_on = request.find(‘/?taste8=on’)
if taste8_on == 6:
uart.write(“202713”)
taste8_off = request.find(‘/?taste8=off’)
if taste8_off == 6:
uart.write(“202703”)
taste9_on = request.find(‘/?taste9=on’)
if taste9_on == 6:
uart.write(“203013”)
taste9_off = request.find(‘/?taste9=off’)
if taste9_off == 6:
uart.write(“203003”)
taste10_on = request.find(‘/?taste10=on’)
if taste10_on == 6:
uart.write(“203113”)
taste10_off = request.find(‘/?taste10=off’)
if taste10_off == 6:
uart.write(“203103”)
taste11_on = request.find(‘/?taste11=on’)
if taste11_on == 6:
uart.write(“203213”)
taste11_off = request.find(‘/?taste11=off’)
if taste11_off == 6:
uart.write(“203203”)
taste12_on = request.find(‘/?taste12=on’)
if taste12_on == 6:
uart.write(“203313”)
taste12_off = request.find(‘/?taste12=off’)
if taste12_off == 6:
uart.write(“203303”)
taste13_on = request.find(‘/?taste13=on’)
if taste13_on == 6:
uart.write(“203413”)
taste13_off = request.find(‘/?taste13=off’)
if taste13_off == 6:
uart.write(“203403”)
taste14_on = request.find(‘/?taste14=on’)
if taste14_on == 6:
uart.write(“203513”)
taste14_off = request.find(‘/?taste14=off’)
if taste14_off == 6:
uart.write(“203503”)
taste15_on = request.find(‘/?taste15=on’)
if taste15_on == 6:
uart.write(“203613”)
taste15_off = request.find(‘/?taste15=off’)
if taste15_off == 6:
uart.write(“203603”)
taste16_on = request.find(‘/?taste16=on’)
if taste16_on == 6:
uart.write(“203713”)
taste16_off = request.find(‘/?taste16=off’)
if taste16_off == 6:
uart.write(“203703”)
taste17_on = request.find(‘/?taste17=on’)
if taste17_on == 6:
uart.write(“204013”)
taste17_off = request.find(‘/?taste17=off’)
if taste17_off == 6:
uart.write(“204003”)
taste18_on = request.find(‘/?taste18=on’)
if taste18_on == 6:
uart.write(“204113”)
taste18_off = request.find(‘/?taste18=off’)
if taste18_off == 6:
uart.write(“204103”)
taste19_on = request.find(‘/?taste19=on’)
if taste19_on == 6:
uart.write(“204213”)
taste19_off = request.find(‘/?taste19=off’)
if taste19_off == 6:
uart.write(“204203”)
taste20_on = request.find(‘/?taste20=on’)
if taste20_on == 6:
uart.write(“204313”)
taste20_off = request.find(‘/?taste20=off’)
if taste20_off == 6:
uart.write(“204303”)
uart.read()
response = web_page()
conn.send(‘HTTP/1.1 200 OK\n’)
conn.send(‘Content-Type: text/html\n’)
conn.send(‘Connection: close\n\n’)
conn.sendall(response)
conn.close()
except OSError as e:
conn.close()
print(‘Connection closed’)
Thanks for your help.
Hi again.
Have you taken a look at other tutorials we have about displaying sensor readings on a web server using MicroPython?
In our examples, we concatenate the sensor readings as variables with the html string.
For example, in your case you have something like this:
<h2>Sensordaten</h2> <h3><pre>Aussentemperatur = C Aussenfeuchtigkeit = % Helligkeit = lux Luftdruck = mBar
It would be:
<h2>Sensordaten</h2> <h3><pre>Aussentemperatur = """+str(temp)+"""C Aussenfeuchtigkeit ="""+str(hum)+""" % Helligkeit = """+str(lux)+"""lux Luftdruck = """+str(pres)+"""mBar
In which temp, hum, lux and pres are variables that hold your sensor readings.
You need to get the updated sensor readings before sending the web page.Like you’re already doing in
uart.read()
However, you need to know in which format you receive the data via serial and split the information, so that you have different variables to hold your readings.
Let me know if this helps.
Regards,
Sara