# Run this to connect to the designated ClearBlade system as a user or device import sys import time import random import argparse from collections import deque # Add path to clearblade module if needed #path_to_clearblade_module = "" #sys.path.append(path_to_clearblade_module) from clearblade.ClearBladeCore import System # Command line arguments: device number, lower temperature and upper temperature, number of points system_key = sys.argv[1] system_secret = sys.argv[2] devNum = sys.argv[3] device_key = sys.argv[4] loTemp = float(sys.argv[5]) hiTemp = float(sys.argv[6]) numPoints = int(sys.argv[7]) system = System(system_key, system_secret, url="http://127.0.0.1:9000") device_name = f"device-{devNum}" # Device created in Devices table in console device = system.Device(device_name, device_key) token = device.token # Can be stored to use to connect without authentication in future as shown below # Group C: Uncomment next two lines to treat device as device and connect using previously acquired token #token = "" #device = system.Device(device_name, authToken=token) # Create Messaging object mqtt = system.Messaging(device, port=1883, use_tls=False, url="127.0.0.1") # MQTT Topics subscribe_topic = f"device/requests/_edge/FirstEdge" ack_topic = "device/ack/_platform" publish_topic = f"device/{devNum}/temperatures" # Subscribe to device/1/requests topic on connection def on_connect(client, userdata, flags, rc): global subscribe_topic client.subscribe(subscribe_topic) # Initialize incoming_request incoming_request = None # Handle incoming request messages def on_message(client, userdata, msg): global incoming_request incoming_request = (msg.payload).decode('utf-8') mqtt.publish(ack_topic, f"FirstEdge received '{incoming_request}'.") # Add event handlers to mqtt object and connect mqtt.on_connect = on_connect mqtt.on_message = on_message mqtt.connect() # Set up window for sliding window average sliding_window = 10 # Set up deque for holding samples used to calculate sliding window average. # Initialize the deque such that every element's value is the mean temperature. samples_to_avg = deque([(hiTemp + loTemp)/2] * sliding_window) for i in range (0, numPoints): try: # Simulate a raw temperature that varies randomly between the two limits temperature = random.uniform(loTemp, hiTemp) # Append raw temperature to deque; pop oldest one off if window is full samples_to_avg.append(temperature) if len(samples_to_avg) > sliding_window: samples_to_avg.popleft() # If incoming_request indicates as such, smooth (average() contents of sliding # window, print and publish. Otherwise print and publish the raw temperature. if incoming_request == "smooth": smoothed_temperature = sum(samples_to_avg) / sliding_window print(f"smoothed_temperature: {smoothed_temperature}") publish_data = str(smoothed_temperature) else: print(f"temperature: {temperature}") publish_data = str(temperature) # Publish temperature data to publish_topic mqtt.publish(publish_topic, publish_data) time.sleep(1) except KeyboardInterrupt: mqtt.unsubscribe("request") mqtt.disconnect() time.sleep(3) break