forked from jalmeroth/homie-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature_raspi.py
More file actions
executable file
·39 lines (30 loc) · 993 Bytes
/
temperature_raspi.py
File metadata and controls
executable file
·39 lines (30 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import homie
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
TEMPERATURE_INTERVAL = 60
config = homie.loadConfigFile("homie-python.json")
Homie = homie.Homie(config)
temperatureNode = Homie.Node("temperature", "temperature")
def getCpuTemperature():
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
cpu_temp = tempFile.read()
tempFile.close()
return (float(cpu_temp) / 1000)
def main():
Homie.setFirmware("raspi-temperature", "1.0.0")
temperatureNode.advertise("degrees")
Homie.setup()
while True:
temperature = getCpuTemperature()
logger.info("Temperature: {:0.2f} °C".format(temperature))
temperatureNode.setProperty("degrees").send(temperature)
time.sleep(TEMPERATURE_INTERVAL)
if __name__ == '__main__':
try:
main()
except (KeyboardInterrupt, SystemExit):
logger.info("Quitting.")