-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_sensor_example.py
More file actions
64 lines (52 loc) · 1.64 KB
/
simple_sensor_example.py
File metadata and controls
64 lines (52 loc) · 1.64 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Simple Sensor Example with CyberFly SDK
This example shows basic sensor integration:
- Adding a CPU temperature sensor
- Reading sensor data
- Publishing sensor data to the IoT platform
"""
from cyberflySdk import CyberflyClient
import time
# Device credentials
kp = {
"publicKey": "d04bbd8f403e583248aa461896bd7518113f89b85c98f3d9596bbfbf30df0bcb",
"secretKey": "a0ec3175c6c80e60bc8ef18bd7b73a631c507b9f0a42c973036c7f96d21b047a"
}
# Initialize client
client = CyberflyClient(
node_url="https://node.cyberfly.io",
device_id="simple-sensor-device",
key_pair=kp,
network_id="testnet04"
)
# Add a CPU temperature sensor (works on Raspberry Pi)
client.add_sensor(
sensor_id="cpu_temp",
sensor_type="vcgen",
inputs={},
alias="CPU Temperature Monitor"
)
print("Added CPU temperature sensor")
# Set up message handler for commands from IoT platform
@client.on_message()
def handle_commands(data):
print(f"Received command: {data}")
# Sensor commands are handled automatically by the SDK
# Main loop
print("Starting sensor monitoring...")
print("Send commands from IoT platform with format:")
print('{"sensor_command": {"action": "read", "sensor_id": "cpu_temp"}}')
while True:
try:
# Read and publish sensor data every 10 seconds
reading = client.read_sensor("cpu_temp")
print(f"CPU Temperature: {reading['data']}")
# Publish to platform
client.publish_sensor_reading("cpu_temp")
time.sleep(10)
except KeyboardInterrupt:
print("\nStopping...")
break
except Exception as e:
print(f"Error: {e}")
time.sleep(5)