Upload 5_st_live.py
Browse files- 5_st_live.py +100 -0
5_st_live.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Function to simulate real-time data update
|
| 7 |
+
def get_live_data():
|
| 8 |
+
try:
|
| 9 |
+
# Change the URL to the correct endpoint
|
| 10 |
+
response = requests.get('http://192.168.1.107/get_data')
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
data_list = response.json()
|
| 13 |
+
print("Fetched data list:", data_list)
|
| 14 |
+
# Extract values ensuring correct types
|
| 15 |
+
ky037 = int(data_list[1])
|
| 16 |
+
speaker = data_list[2]
|
| 17 |
+
light = int(data_list[6])
|
| 18 |
+
wet = data_list[5]
|
| 19 |
+
hum = float(data_list[3])
|
| 20 |
+
temp = float(data_list[4])
|
| 21 |
+
return ky037, speaker, light, wet, hum, temp
|
| 22 |
+
else:
|
| 23 |
+
print("Failed to fetch data list")
|
| 24 |
+
return None
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error fetching data: {e}")
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
st.markdown("<h1 style='text-align: center;'> گهوارهی هوشمند </h1>", unsafe_allow_html=True)
|
| 31 |
+
st.markdown("<p style='text-align: center; font-size: 30px;'>:وضعیت</p>", unsafe_allow_html=True)
|
| 32 |
+
|
| 33 |
+
# Create placeholders for each message and the table
|
| 34 |
+
message_placeholder = st.empty()
|
| 35 |
+
light_message_placeholder = st.empty()
|
| 36 |
+
wet_message_placeholder = st.empty()
|
| 37 |
+
temp_message_placeholder = st.empty()
|
| 38 |
+
table_placeholder = st.empty()
|
| 39 |
+
|
| 40 |
+
# Start the live update loop
|
| 41 |
+
while True:
|
| 42 |
+
result = get_live_data()
|
| 43 |
+
if result is not None:
|
| 44 |
+
ky037, speaker, light, wet, hum, temp = result
|
| 45 |
+
|
| 46 |
+
# Update messages
|
| 47 |
+
if light >= 980:
|
| 48 |
+
light_status = "تاریک"
|
| 49 |
+
else:
|
| 50 |
+
light_status = 'روشن'
|
| 51 |
+
|
| 52 |
+
if ky037 >= 3 and light_status == "روشن":
|
| 53 |
+
light_message_placeholder.markdown("<p style='text-align: center; font-size: 15px;'>.پیشنهاد میشود نور محیط را کم کنید</p>", unsafe_allow_html=True)
|
| 54 |
+
else:
|
| 55 |
+
light_message_placeholder.empty()
|
| 56 |
+
|
| 57 |
+
if ky037 >= 3:
|
| 58 |
+
ky037_status = ".کودک گریه میکند"
|
| 59 |
+
else:
|
| 60 |
+
ky037_status = ".کودک گریه نمیکند"
|
| 61 |
+
|
| 62 |
+
message_placeholder.markdown(f"<p style='text-align: center; font-size: 15px;'>{ky037_status}</p>", unsafe_allow_html=True)
|
| 63 |
+
|
| 64 |
+
if wet == "Wet":
|
| 65 |
+
wet_status = "Wet"
|
| 66 |
+
wet_message_placeholder.markdown("<p style='text-align: center; font-size: 15px;'>.زیرانداز کودک را عوض کنید</p>", unsafe_allow_html=True)
|
| 67 |
+
else:
|
| 68 |
+
wet_status = "Dry"
|
| 69 |
+
wet_message_placeholder.empty()
|
| 70 |
+
|
| 71 |
+
if temp < 20:
|
| 72 |
+
temp_message_placeholder.markdown("<p style='text-align: center; font-size: 15px;'>.اتاق سرد است</p>", unsafe_allow_html=True)
|
| 73 |
+
elif temp > 24:
|
| 74 |
+
temp_message_placeholder.markdown("<p style='text-align: center; font-size: 15px;'>.اتاق گرم است</p>", unsafe_allow_html=True)
|
| 75 |
+
else:
|
| 76 |
+
temp_message_placeholder.empty()
|
| 77 |
+
|
| 78 |
+
data = {
|
| 79 |
+
'وضعیت': [ky037_status, light_status, wet_status, hum, temp],
|
| 80 |
+
'سنسور': ['صدا', 'نور','تخت کودک','(%)رطوبت محیط','(^C)دمای محیط']
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
df = pd.DataFrame(data)
|
| 84 |
+
|
| 85 |
+
st.markdown("""
|
| 86 |
+
<style>
|
| 87 |
+
.stTable tr {
|
| 88 |
+
height: 50px; /* Adjust this value to change the row height */
|
| 89 |
+
}
|
| 90 |
+
</style>
|
| 91 |
+
""", unsafe_allow_html=True)
|
| 92 |
+
|
| 93 |
+
# Update the table in the placeholder
|
| 94 |
+
table_placeholder.table(df)
|
| 95 |
+
time. sleep(5)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|