Skip to content

Instantly share code, notes, and snippets.

@charlie2951
Created June 24, 2022 18:27
Show Gist options
  • Save charlie2951/828365022f3edf91d995d3b5f61f4168 to your computer and use it in GitHub Desktop.
Save charlie2951/828365022f3edf91d995d3b5f61f4168 to your computer and use it in GitHub Desktop.
#Python program to read temp and humidity data from serial port
#and to send them to a webpage
#DHT11 sensor is used
#Version 1.0.4
import serial
import time
file_name = "index.html"
ser = serial.Serial('COM5', 112500, timeout=1)
print("Reading data from serial port.....");
time.sleep(2)
title = "Pico sensor data";
ser.reset_input_buffer() # Delete any stale data.
def write_data(data):
fo = open(file_name,"w+")
# Start of HTML page.
fo.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>")
fo.write("<html><head><title>"+title+"</title>") # Page & Head begin.
fo.write("<meta http-equiv='refresh' content='20'>")
fo.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>")
fo.write("<link rel='shortcut icon' href='favicon.ico' />")
fo.write("<link rel='icon' type='image/x-icon' href='favicon.ico' />")
fo.write("<link rel='icon' type='image/png' href='favicon.png' />")
fo.write("</head><body><center><p style='text-align:center'>&nbsp;</p>") # Head end, body begin.
#fo.write("</head><body><center><p>"Temperature"</p>")
fo.write('<p style="text-align:center"><span style="color:#d35400"><span style="font-family:Comic Sans MS,cursive"><span style="font-size:36px">Raspberry Pi Pico Real time Sensor Data display using Python</span></span></span></p>')
fo.write('<p style="text-align:center">&nbsp;</p><p style="text-align:center">&nbsp;</p>')
fo.write('<table align="center" border="1" cellpadding="1" cellspacing="1" style="width:500px">')
fo.write('<caption><p><span style="font-size:36px"><span style="font-family:Lucida Sans Unicode,Lucida Grande,sans-serif"><span style="color:#2980b9">Dashboard</span></span></span></p>')
fo.write('<p>&nbsp;</p></caption><tbody><tr><td style="text-align:center"><span style="color:#27ae60"><span style="font-size:26px"><span style="font-family:Trebuchet MS,Helvetica,sans-serif">Temperature</span></span></span></td>')
fo.write('<td style="text-align:center"><span style="color:#27ae60"><span style="font-size:28px"><span style="font-family:Trebuchet MS,Helvetica,sans-serif">Humidity</span></span></span></td></tr>')
fo.write('</tr><tr><td style="text-align:center"><span style="font-size:26px">'+data[0]+'</span></td>')
fo.write('<td style="text-align:center"><span style="font-size:26px">'+data[1]+'</span></td></tr></tbody></table><p>&nbsp;</p>')
fo.write("</body>") # Body end.
fo.write("</html>") # Page end.
# Done, close file.
fo.close()
for i in range(200):
# Reading all bytes available bytes till EOL
line = ser.readline()
if line:
# Converting Byte Strings into unicode strings
rxdata = line.decode()
# Converting Unicode String into integer
#num = int(rxdata)
#print(num)
tmp = rxdata.split(',')
#write into webpage
write_data(tmp)
print(tmp)
#print(tmp[1])
del tmp #clear received data
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment