File tree 4 files changed +37
-36
lines changed
4 files changed +37
-36
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,13 @@ Cloud4RPi Examples for [Raspberry Pi](https://www.raspberrypi.org/products/)
3
3
4
4
[ ![ Build Status] ( https://travis-ci.org/cloud4rpi/cloud4rpi-raspberrypi-python.svg?branch=master )] ( https://travis-ci.org/cloud4rpi/cloud4rpi-raspberrypi-python )
5
5
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
+
6
13
## Running the Sample Code
7
14
8
15
1 . Update your system and make sure you have the latest versions of all required software:
Original file line number Diff line number Diff line change 1
1
# -*- 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
+
26
3
import sys
27
4
import time
28
5
import random
29
- import RPi .GPIO as GPIO # pylint: disable=F0401
30
6
import cloud4rpi
31
7
import ds18b20
32
8
import rpi
9
+ import RPi .GPIO as GPIO # pylint: disable=F0401
33
10
34
11
# Put your device token here. To get the token,
35
12
# sign up at https://cloud4rpi.io and create a device.
@@ -99,8 +76,8 @@ def main():
99
76
diagnostics = {
100
77
'CPU Temp' : rpi .cpu_temp ,
101
78
'IP Address' : rpi .ip_address ,
102
- 'Host' : gethostname () ,
103
- 'Operating System' : " " . join ( uname ())
79
+ 'Host' : rpi . host_name ,
80
+ 'Operating System' : rpi . os_name
104
81
}
105
82
106
83
device = cloud4rpi .connect (DEVICE_TOKEN )
Original file line number Diff line number Diff line change 1
1
# -*- coding: utf-8 -*-
2
2
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
+
3
12
import os
4
13
import re
5
14
import subprocess
Original file line number Diff line number Diff line change 1
1
# -*- coding: utf-8 -*-
2
2
3
+ from os import uname
4
+ from socket import gethostname
3
5
import subprocess
4
6
import re
5
7
6
8
7
- def parse_output (pattern , default , args ):
9
+ def parse_output (pattern , args ):
8
10
try :
9
11
out_str = subprocess .check_output (args )
10
12
if isinstance (out_str , bytes ):
11
13
out_str = out_str .decode ()
12
14
except Exception :
13
- out_str = 'error'
15
+ out_str = ''
16
+
14
17
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
20
19
21
20
22
21
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
24
24
25
25
26
26
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 ())
You can’t perform that action at this time.
0 commit comments