|
34 | 34 | from time import strftime
|
35 | 35 | else:
|
36 | 36 | from ulogging.ustrftime import strftime
|
37 |
| -from aiotcloud import AIOTClient |
38 |
| -from aiotcloud import Location |
39 |
| -from aiotcloud import Schedule |
40 |
| -from aiotcloud import ColoredLight |
| 37 | +from arduino_iot_cloud import AIOTClient |
| 38 | +from arduino_iot_cloud import Location |
| 39 | +from arduino_iot_cloud import Schedule |
| 40 | +from arduino_iot_cloud import ColoredLight |
41 | 41 | from random import randint, choice
|
42 | 42 |
|
43 | 43 | DEBUG_ENABLED = True
|
|
48 | 48 | DEVICE_ID = b"25deeda1-3fda-4d06-9c3c-dd31be382cd2"
|
49 | 49 |
|
50 | 50 |
|
51 |
| -async def user_main(aiot): |
| 51 | +async def user_main(client): |
52 | 52 | """
|
53 | 53 | Add your code here.
|
54 | 54 | NOTE: To allow other tasks to run, this function must yield
|
55 | 55 | execution periodically by calling asyncio.sleep(seconds).
|
56 | 56 | """
|
57 | 57 | while True:
|
58 | 58 | # The composite cloud object's fields can be assigned to individually:
|
59 |
| - aiot["clight"].hue = randint(0, 100) |
60 |
| - aiot["clight"].bri = randint(0, 100) |
61 |
| - aiot["user"] = choice(["=^.. ^=", "=^ ..^="]) |
| 59 | + client["clight"].hue = randint(0, 100) |
| 60 | + client["clight"].bri = randint(0, 100) |
| 61 | + client["user"] = choice(["=^.. ^=", "=^ ..^="]) |
62 | 62 | await asyncio.sleep(1.0)
|
63 | 63 |
|
64 | 64 |
|
65 |
| -def on_switch_changed(aiot, value): |
| 65 | +def on_switch_changed(client, value): |
66 | 66 | """
|
67 | 67 | This is a write callback for the switch that toggles the LED variable. The LED
|
68 |
| - variable can be accessed via the aiot cloud object passed in the first argument. |
| 68 | + variable can be accessed via the client object passed in the first argument. |
69 | 69 | """
|
70 | 70 | if value and not hasattr(on_switch_changed, "init"):
|
71 | 71 | on_switch_changed.init = True
|
72 | 72 | logging.info("Someone left the lights on!")
|
73 |
| - aiot["led"] = value |
| 73 | + client["led"] = value |
74 | 74 |
|
75 | 75 |
|
76 |
| -def on_clight_changed(aiot, clight): |
| 76 | +def on_clight_changed(client, clight): |
77 | 77 | logging.info(f"ColoredLight changed. Swi: {clight.swi} Bri: {clight.bri} Sat: {clight.sat} Hue: {clight.hue}")
|
78 | 78 |
|
79 | 79 |
|
80 | 80 | async def main():
|
81 |
| - aiot = AIOTClient( |
| 81 | + client = AIOTClient( |
82 | 82 | device_id=DEVICE_ID,
|
83 | 83 | ssl_params={"pin": "1234", "keyfile": KEY_URI, "certfile": CERT_URI, "ca_certs": CA_PATH},
|
84 | 84 | )
|
85 | 85 | # This cloud object is initialized with its last known value from the cloud.
|
86 |
| - aiot.register("sw1", value=None, on_write=on_switch_changed, interval=0.250) |
| 86 | + client.register("sw1", value=None, on_write=on_switch_changed, interval=0.250) |
87 | 87 |
|
88 | 88 | # This cloud object is initialized with its last known value from the cloud,
|
89 | 89 | # and gets manually updated from the switch's on_write_change callback.
|
90 |
| - aiot.register("led", value=None) |
| 90 | + client.register("led", value=None) |
91 | 91 |
|
92 | 92 | # This is a periodic cloud object that gets updated every 1 second.
|
93 |
| - aiot.register("pot", value=None, on_read=lambda x: randint(0, 1024), interval=1.0) |
| 93 | + client.register("pot", value=None, on_read=lambda x: randint(0, 1024), interval=1.0) |
94 | 94 |
|
95 | 95 | # This is a periodic cloud object that gets updated every 1 second,
|
96 | 96 | # with the formatted current time value.
|
97 |
| - aiot.register("clk", value=None, on_read=lambda x: strftime("%H:%M:%S", time.localtime()), interval=1.0) |
| 97 | + client.register("clk", value=None, on_read=lambda x: strftime("%H:%M:%S", time.localtime()), interval=1.0) |
98 | 98 |
|
99 | 99 | # This variable is an example for a composite object (a colored light object in this case),
|
100 | 100 | # which is composed of multiple variables. Once initialized, the object's variables can be
|
101 |
| - # accessed as normal attributes, using dot notation (e.g: aiot["clight"].swi = False) |
102 |
| - aiot.register(ColoredLight("clight", swi=True, hue=22, sat=75, bri=10, on_write=on_clight_changed)) |
| 101 | + # accessed as normal attributes, using dot notation (e.g: client["clight"].swi = False) |
| 102 | + client.register(ColoredLight("clight", swi=True, hue=22, sat=75, bri=10, on_write=on_clight_changed)) |
103 | 103 |
|
104 | 104 | # This variable is an example for a composite object (a map location).
|
105 |
| - aiot.register(Location("treasureisland", lat=31.264694, lon=29.979987)) |
| 105 | + client.register(Location("treasureisland", lat=31.264694, lon=29.979987)) |
106 | 106 |
|
107 | 107 | # This variable is updated manually from user_main.
|
108 |
| - aiot.register("user", value="") |
| 108 | + client.register("user", value="") |
109 | 109 |
|
110 | 110 | # This object allows scheduling recurring events from the cloud UI. On activation of the event, if
|
111 |
| - # on_active callback is provided, it gets called with the aiot object and the schedule object value. |
112 |
| - # The activation status of the object can also be polled using aiot["schedule"].active. |
113 |
| - aiot.register(Schedule("schedule", on_active=lambda aiot, value: logging.info(f"Schedule activated {value}!"))) |
| 111 | + # on_active callback is provided, it gets called with the client object and the schedule object value. |
| 112 | + # The activation status of the object can also be polled using client["schedule"].active. |
| 113 | + client.register(Schedule("schedule", on_active=lambda client, value: logging.info(f"Schedule activated {value}!"))) |
114 | 114 |
|
115 |
| - # Start the AIoT client. |
116 |
| - await aiot.run(user_main) |
| 115 | + # Start the Arduino IoT cloud client. |
| 116 | + await client.run(user_main) |
117 | 117 |
|
118 | 118 |
|
119 | 119 | if __name__ == "__main__":
|
|
0 commit comments