Skip to content

Commit b54e4e8

Browse files
sky3dconst314
authored andcommitted
cpu temp as float, extend rpi (#5)
* cpu temp as float, extend rpi * replace defaults with None value
1 parent ab20e3c commit b54e4e8

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Cloud4RPi Examples for [Raspberry Pi](https://www.raspberrypi.org/products/)
33

44
[![Build Status](https://travis-ci.org/cloud4rpi/cloud4rpi-raspberrypi-python.svg?branch=master)](https://travis-ci.org/cloud4rpi/cloud4rpi-raspberrypi-python)
55

6+
This example demonstrates different scenarios of using Cloud4RPi service on Raspberry Pi:
7+
- Monitoring events
8+
- Controling a GPIO pin
9+
- Monitoring temperature with the DS18B20 sensor
10+
11+
For complete instructions on how to run this example, refer to the [How To](https://cloud4rpi.github.io/docs/howto/rpi) article.
12+
613
## Running the Sample Code
714

815
1. Update your system and make sure you have the latest versions of all required software:

Diff for: control.py

+4-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
#
3-
# Cloud4RPi Example for Raspberry Pi
4-
# ==================================
5-
#
6-
# This example demonstrates different scenarios of using Cloud4RPi service
7-
# on Raspberry Pi:
8-
#
9-
# - Monitoring events
10-
# - Controling a GPIO pin
11-
# - Monitoring temperature with the DS18B20 sensor
12-
#
13-
# For complete instructions on how to run this example, refer
14-
# to the [How To](https://cloud4rpi.github.io/docs/howto/) article.
15-
#
16-
# The DS18B20 sensor should be connected as follows:
17-
#
18-
# / GND |────────────> GND
19-
# | DATA |─────────┬──> GPIO4
20-
# \ VCC |─┬─[4k7]─┘
21-
# └──────────> 5V
22-
# DS18B20 (bottom view)
23-
24-
from os import uname
25-
from socket import gethostname
2+
263
import sys
274
import time
285
import random
29-
import RPi.GPIO as GPIO # pylint: disable=F0401
306
import cloud4rpi
317
import ds18b20
328
import rpi
9+
import RPi.GPIO as GPIO # pylint: disable=F0401
3310

3411
# Put your device token here. To get the token,
3512
# sign up at https://cloud4rpi.io and create a device.
@@ -99,8 +76,8 @@ def main():
9976
diagnostics = {
10077
'CPU Temp': rpi.cpu_temp,
10178
'IP Address': rpi.ip_address,
102-
'Host': gethostname(),
103-
'Operating System': " ".join(uname())
79+
'Host': rpi.host_name,
80+
'Operating System': rpi.os_name
10481
}
10582

10683
device = cloud4rpi.connect(DEVICE_TOKEN)

Diff for: ds18b20.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3+
# The DS18B20 sensor should be connected as follows:
4+
#
5+
# / GND |────────────> GND
6+
# | DATA |─────────┬──> GPIO4
7+
# \ VCC |─┬─[4k7]─┘
8+
# └──────────> 5V
9+
# DS18B20 (bottom view)
10+
#
11+
312
import os
413
import re
514
import subprocess

Diff for: rpi.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
# -*- coding: utf-8 -*-
22

3+
from os import uname
4+
from socket import gethostname
35
import subprocess
46
import re
57

68

7-
def parse_output(pattern, default, args):
9+
def parse_output(pattern, args):
810
try:
911
out_str = subprocess.check_output(args)
1012
if isinstance(out_str, bytes):
1113
out_str = out_str.decode()
1214
except Exception:
13-
out_str = 'error'
15+
out_str = ''
16+
1417
match = re.search(pattern, out_str)
15-
if match:
16-
result = match.group(1)
17-
else:
18-
result = default
19-
return result
18+
return match.group(1) if match else None
2019

2120

2221
def cpu_temp():
23-
return parse_output(r'temp=(\S*)\'C', '0', ['vcgencmd', 'measure_temp'])
22+
t_str = parse_output(r'temp=(\S*)\'C', ['vcgencmd', 'measure_temp'])
23+
return float(t_str) if t_str else None
2424

2525

2626
def ip_address():
27-
return parse_output(r'(\S*)', '?', ['hostname', '-I'])
27+
return parse_output(r'(\S*)', ['hostname', '-I'])
28+
29+
30+
def host_name():
31+
return gethostname()
32+
33+
34+
def os_name():
35+
return " ".join(uname())

0 commit comments

Comments
 (0)