写一个农业大棚环境监测的代码
时间: 2023-12-01 07:01:50 浏览: 29
以下是一个简单的农业大棚环境监测的代码:
```python
import time
import random
class Greenhouse:
def __init__(self):
self.temperature = 0
self.humidity = 0
self.light = 0
def read_temperature(self):
# 模拟读取温度传感器的数值
self.temperature = round(random.uniform(20, 30), 1)
def read_humidity(self):
# 模拟读取湿度传感器的数值
self.humidity = round(random.uniform(50, 70), 1)
def read_light(self):
# 模拟读取光照传感器的数值
self.light = round(random.uniform(0, 100), 1)
if __name__ == '__main__':
greenhouse = Greenhouse()
while True:
greenhouse.read_temperature()
greenhouse.read_humidity()
greenhouse.read_light()
print('温度:{}℃,湿度:{}%,光照强度:{}lux'.format(greenhouse.temperature, greenhouse.humidity, greenhouse.light))
time.sleep(5) # 5秒钟读取一次传感器数值
```
这个代码会不断地模拟读取温度、湿度和光照强度传感器的数值,并将这些数值打印出来。你可以根据实际需要对代码进行修改,比如将读取到的数值存储到数据库中,或者根据读取到的数值控制灌溉系统、通风系统等等。