-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
82 lines (68 loc) · 2.65 KB
/
main.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Minimal example of periodically sending data to WolkAbout IoT Platform."""
# Copyright 2020 WolkAbout Technology s.r.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import sys
import time
try:
import wolk # noqa
except ModuleNotFoundError:
print("Attempting to import WolkConnect from relative path")
try:
module_path = os.sep + ".." + os.sep + ".." + os.sep
sys.path.append(
os.path.dirname(os.path.realpath(__file__)) + module_path
)
import wolk
except Exception as e:
print(f"Failed to import WolkConnect: '{e}'")
raise e
# NOTE: Enable debug logging by uncommenting the following line
# Optionally, as a second argument pass a file name
# wolk.logging_config("debug")
def main() -> None:
"""Connect to WolkAbout IoT Platform and send a random feed reading."""
# Insert the device credentials received
# from WolkAbout IoT Platform when creating the device
device = wolk.Device(key="device_key", password="some_password")
# Create a WolkConnect object and pass your device
# NOTE: Change Platform instance with host:str, port:int, ca_cert:str
wolk_device = wolk.WolkConnect(
device, host="insert_host", port=80, ca_cert="PATH/TO/YOUR/CA.CRT/FILE"
)
# Establish a connection to the WolkAbout IoT Platform
wolk_device.connect()
publish_period_seconds = 5
i = 0
while True:
try:
# Generate a random value
temperature = random.randint(-20, 80)
# Add a feed reading to the message queue
temperature = ("T", temperature)
wolk_device.add_feed_value(temperature)
print(f"Adding feed value data {temperature}.")
i += 1
if i % 10 == 0:
time.sleep(publish_period_seconds)
print("Publishing the feed values.")
wolk_device.publish()
time.sleep(publish_period_seconds)
except KeyboardInterrupt:
print("\tReceived KeyboardInterrupt. Exiting script")
wolk_device.disconnect()
return
if __name__ == "__main__":
main()