3
3
# https://creativecommons.org/publicdomain/zero/1.0/
4
4
import logging
5
5
import os
6
+ import time
6
7
import sys
7
8
import asyncio
8
9
from arduino_iot_cloud import ArduinoCloudClient
10
+ from arduino_iot_cloud import Task
9
11
import argparse
10
12
11
13
@@ -20,6 +22,16 @@ def on_value_changed(client, value):
20
22
sys .exit (0 )
21
23
22
24
25
+ def wdt_task (client , ts = [None ]):
26
+ if ts [0 ] is None :
27
+ ts [0 ] = time .time ()
28
+ if time .time () - ts [0 ] > 5 :
29
+ loop = asyncio .get_event_loop ()
30
+ loop .set_exception_handler (exception_handler )
31
+ logging .error ("Timeout waiting for variable" )
32
+ sys .exit (1 )
33
+
34
+
23
35
if __name__ == "__main__" :
24
36
# Parse command line args.
25
37
parser = argparse .ArgumentParser (description = "arduino_iot_cloud.py" )
@@ -35,6 +47,9 @@ def on_value_changed(client, value):
35
47
parser .add_argument (
36
48
"-f" , "--file-auth" , action = "store_true" , help = "Use key/cert files"
37
49
)
50
+ parser .add_argument (
51
+ "-s" , "--sync" , action = "store_true" , help = "Run in synchronous mode"
52
+ )
38
53
args = parser .parse_args ()
39
54
40
55
# Configure the logger.
@@ -56,6 +71,7 @@ def on_value_changed(client, value):
56
71
device_id = os .getenv ("DEVICE_ID" ),
57
72
username = os .getenv ("DEVICE_ID" ),
58
73
password = os .getenv ("SECRET_KEY" ),
74
+ sync_mode = args .sync ,
59
75
)
60
76
elif args .file_auth :
61
77
import ssl
@@ -67,6 +83,7 @@ def on_value_changed(client, value):
67
83
"ca_certs" : "ca-root.pem" ,
68
84
"cert_reqs" : ssl .CERT_REQUIRED ,
69
85
},
86
+ sync_mode = args .sync ,
70
87
)
71
88
elif args .crypto_device :
72
89
import ssl
@@ -82,16 +99,17 @@ def on_value_changed(client, value):
82
99
"engine_path" : "/lib/x86_64-linux-gnu/engines-3/libpkcs11.so" ,
83
100
"module_path" : "/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so" ,
84
101
},
102
+ sync_mode = args .sync ,
85
103
)
86
104
else :
87
105
parser .print_help ()
88
106
sys .exit (1 )
89
107
90
108
# Register cloud objects.
91
- # Note: The following objects must be created first in the dashboard and linked to the device.
92
- # This cloud object is initialized with its last known value from the cloud. When this object is updated
93
- # from the dashboard, the on_switch_changed function is called with the client object and the new value.
109
+ # When this object gets initialized from the cloud the test is complete.
94
110
client .register ("answer" , value = None , on_write = on_value_changed )
111
+ # This task will exist with failure after a timeout.
112
+ client .register (Task ("wdt_task" , on_run = wdt_task , interval = 1.0 ))
95
113
96
114
# Start the Arduino IoT cloud client.
97
115
client .start ()
0 commit comments