def main(): """Connect to WolkAbout IoT Platform and send a random sensor reading.""" # Insert the device credentials received # from WolkAbout IoT Platform when creating the device device = wolk.Device(key="92dececc-71eb-4174-b339-a487b8e8e0f4", password="******") wolk_device = wolk.WolkConnect(device) # Establish a connection to the WolkAbout IoT Platform print("Connecting to WolkAbout IoT Platform") wolk_device.connect() publish_period_seconds = 5 while True: try: temperature = random.randint(-20, 80) wolk_device.add_sensor_reading("T", temperature) print('Publishing "T": ' + str(temperature)) wolk_device.publish() time.sleep(publish_period_seconds) except KeyboardInterrupt: print("\tReceived KeyboardInterrupt. Exiting script") wolk_device.disconnect() sys.exit()
def main(): """Connect to WolkAbout IoT Platform and send a random sensor reading.""" # Insert the device credentials received # from WolkAbout IoT Platform when creating the device device = wolk.Device(key="device_key", password="******") wolk_device = wolk.WolkConnect(device) # Establish a connection to the WolkAbout IoT Platform print("Connecting to WolkAbout IoT Platform") try: wolk_device.connect() except RuntimeError as e: print(str(e)) sys.exit(1) publish_period_seconds = 5 while True: try: temperature = random.randint(-20, 80) wolk_device.add_sensor_reading("T", temperature) print('Publishing "T": ' + str(temperature)) wolk_device.publish() time.sleep(publish_period_seconds) except KeyboardInterrupt: print("Received KeyboardInterrupt, quitting") os._exit(0)
def main(): global _i2c # Define device which will be connected to colud # key and password defined by WolkAbout platform device = wolk.Device(key="05e67f85-1f9c-49f8-82b6-5f2860f775ec", password="******") # Asign our deviced to connecting to WolkAbout platform wolk_device = wolk.WolkConnect(device) # Start program print("Connecting to WolkAbout IoT Platform") # Connect to device to WolkAbout Platform, in case that is not possible, exit system try: wolk_device.connect() except RuntimeError as e: print(str(e)) sys.exit(1) while True: try: _i2c.i2c_reading() # sensor need 20ms to measure values time.sleep(0.2) temp_var = _i2c.get_temp() humi_var = _i2c.get_humi() temperature = C1 + C2 * (temp_var / C3) humidity = 100.0 * humi_var / C3 # upload measured values to WolkAbout platform wolk_device.add_sensor_reading("Temperature", temperature) wolk_device.add_sensor_reading("Humidity", humidity) wolk_device.publish() # show measured values print("Temperature in Celsius: %.2f C " % temperature) print("Humidity: %.2f %% " % humidity) # wait until next cycle of measurement time.sleep(MEASUREMENT_PERIOD) # if program execution is interrupted, then disconnect device from WolkAbout platform except KeyboardInterrupt: print("Received KeyboardInterrupt, quitting") wolk_device.disconnect() sys.exit()
import speech_recognition as sr import requests from getch import getch import os import sys module_path = os.sep + ".." + os.sep + ".." + os.sep sys.path.append(os.path.dirname(os.path.realpath(__file__)) + module_path) import wolk if __name__ == "__main__": total = 0 node_ip = input("Enter node IP:") r = sr.Recognizer() list_microphones() device = wolk.Device(key="4upvzcqmjdoatxw9", password="******") wolk_device = wolk.WolkConnect(device, host="demo.wolkabout.com") # Establish a connection to the WolkAbout IoT Platform print("Connecting to WolkAbout IoT Platform") try: wolk_device.connect() except RuntimeError as e: print(str(e)) sys.exit(1) wolk_device.add_sensor_reading("S1", " ") wolk_device.add_sensor_reading("S2", " ") wolk_device.add_sensor_reading("S3", " ") wolk_device.add_sensor_reading("S4", " ")
def main(): """ Demonstrate all functionality of wolk module. Create actuation handler and actuator status provider for switch and slider actuators. Pass all of these to a WolkConnect class and start a loop to send different types of random readings. """ # Insert the device credentials received # from WolkAbout IoT Platform when creating the device # List actuator references included on your device actuator_references = [SWITCH1_REF, SWITCH2_REF] device = wolk.Device( key="some-key", password="******", actuator_references=actuator_references, ) class Actuator: def __init__( self, inital_value: Optional[Union[bool, int, float, str]] ): self.value = inital_value switch1 = Actuator(False) switch2 = Actuator(False) # Provide a way to read actuator status if your device has actuators def actuator_status_provider( reference: str, ) -> Tuple[wolk.State, Optional[Union[bool, int, float, str]]]: if reference == actuator_references[0]: return wolk.State.READY, switch1.value elif reference == actuator_references[1]: return wolk.State.READY, switch2.value return wolk.State.ERROR, None # Provide an actuation handler if your device has actuators def actuation_handler( reference: str, value: Union[bool, int, float, str] ) -> None: logging.info(f"Setting actuator '{reference}' to value: {value}") if reference == actuator_references[0]: if sonoff_switch(SWITCH1_ADD, value): switch1.value = value else: # Set switch in inactive state switch1.value = 0 elif reference == actuator_references[1]: if sonoff_switch(SWITCH2_ADD, value): switch2.value = value else: # Set switch in inactive state switch2.value = 0 # Pass device and optionally connection details # Provided connection details are the default value # Provide actuation handler and actuator status provider via with_actuators wolk_device = ( wolk.WolkConnect( device=device, host="api-demo.wolkabout.com", port=8883, ca_cert="utility/ca.crt", ) .with_actuators( actuation_handler=actuation_handler, actuator_status_provider=actuator_status_provider, ) ) # Establish a connection to the WolkAbout IoT Platform logging.info("Connecting to WolkAbout IoT Platform") try: wolk_device.connect() except RuntimeError as e: logging.error(str(e)) sys.exit(1) # Successfully connecting to the platform will publish actuator status wolk_device.publish_actuator_status(SWITCH1_REF) wolk_device.publish_actuator_status(SWITCH2_REF)
from sys import platform #support libraries for image packing import struct import pickle #arguments parser for IP address enter import argparse #wolkabout iot cloud service try: import wolk device_name = "RaspberryPi_Kaca" device_key = "pmrnavhc53n3gcby" device_password = "******" device = wolk.Device(key=device_key, password=device_password) wolk_device = wolk.WolkConnect( device=device, #protocol=wolk.Protocol.JSON_SINGLE, host="iot-elektronika.ftn.uns.ac.rs", port=1883) wolk_device.connect() print("#1 Wolk Connection successful.") except ModuleNotFoundError as e: print(e) sys.exit(-1) except RuntimeError as e: print("#1 Wolk Connection unsuccessful.")
def main(): """ Demonstrate all functionality of wolk module. Create actuation handler and actuator status provider for switch and slider actuators. Create configuration handler and configuration provider for 4 types of configuration options. Create a custom queue to store messages on disk before sending them to the platform. Create a firmware installer and handler for enabling firmware update. Pass all of these to a WolkConnect class and start a loop to send different types of random readings. """ # Insert the device credentials received # from WolkAbout IoT Platform when creating the device # List actuator references included on your device device = wolk.Device(key="device_key", password="******", actuator_references=["SW", "SL"]) class ActuatorSimulator: def __init__(self, inital_value): self.value = inital_value switch = ActuatorSimulator(False) slider = ActuatorSimulator(0) class ConfigurationSimulator: def __init__(self, inital_value): self.value = inital_value configuration_1 = ConfigurationSimulator(0) configuration_2 = ConfigurationSimulator(False) configuration_3 = ConfigurationSimulator("configuration_3") configuration_4 = ConfigurationSimulator( ("configuration_4a", "configuration_4b", "configuration_4c")) # Provide a way to read actuator status if your device has actuators class ActuatorStatusProviderImpl(wolk.ActuatorStatusProvider): def get_actuator_status(self, reference): if reference == "SW": return wolk.ActuatorState.READY, switch.value elif reference == "SL": return wolk.ActuatorState.READY, slider.value # Provide an actuation handler if your device has actuators class ActuationHandlerImpl(wolk.ActuationHandler): def handle_actuation(self, reference, value): print("Setting actuator " + reference + " to value: " + str(value)) if reference == "SW": switch.value = value elif reference == "SL": slider.value = value # Provide a configuration handler if your device has configuration options class ConfigurationHandlerImpl(wolk.ConfigurationHandler): def handle_configuration(self, configuration): for key, value in configuration.items(): if key == "config_1": configuration_1.value = value elif key == "config_2": configuration_2.value = value elif key == "config_3": configuration_3.value = value elif key == "config_4": configuration_4.value = value # Provide a way to read current device configuration class ConfigurationProviderImpl(wolk.ConfigurationProvider): def get_configuration(self): configuration = dict() configuration["config_1"] = configuration_1.value configuration["config_2"] = configuration_2.value configuration["config_3"] = configuration_3.value configuration["config_4"] = configuration_4.value return configuration # Custom queue example class FilesystemOutboundMessageQueue(wolk.OutboundMessageQueue): def __init__(self, path="."): if path == ".": self.queue = PersistentQueue("FileOutboundMessageQueue") else: self.queue = PersistentQueue("FileOutboundMessageQueue", path) def put(self, message): self.queue.push(message) def get(self): message = self.queue.pop() self.queue.flush() return message def peek(self): if not self.queue.peek(): self.queue.clear() return None else: return self.queue.peek() filesystemOutboundMessageQueue = FilesystemOutboundMessageQueue() # Extend this class to handle the installing of the firmware file class MyFirmwareInstaller(wolk.FirmwareInstaller): def __init__(self): pass def install_firmware(self, firmware_file_path): """Handle the installing of the firmware file here.""" print("Installing firmware from path: " + firmware_file_path) os._exit(0) # Enable firmware update on your device # Implement wolk.FirmwareURLDownloadHandler to enable URL download firmware_handler = wolk.FileSystemFirmwareHandler( version="1.0", chunk_size=1024 * 1024, max_file_size=100 * 1024 * 1024, download_location="", firmware_installer=MyFirmwareInstaller(), firmware_url_download_handler=None, ) # Pass your device, actuation handler and actuator status provider # Pass configuration handler and provider # Pass custom outbound message queue implementation # Enable firmware update by passing a firmware handler try: wolk_device = wolk.WolkConnect( device=device, protocol=wolk.Protocol.JSON_SINGLE, actuation_handler=ActuationHandlerImpl(), actuator_status_provider=ActuatorStatusProviderImpl(), configuration_handler=ConfigurationHandlerImpl(), configuration_provider=ConfigurationProviderImpl(), outbound_message_queue=filesystemOutboundMessageQueue, firmware_handler=firmware_handler, host="api-demo.wolkabout.com", port=8883, ca_cert=".." + os.sep + ".." + os.sep + "wolk" + os.sep + "ca.crt", ) except RuntimeError as e: print(str(e)) sys.exit(1) # Establish a connection to the WolkAbout IoT Platform print("Connecting to WolkAbout IoT Platform") try: wolk_device.connect() except RuntimeError as e: print(str(e)) sys.exit(1) wolk_device.publish_configuration() wolk_device.publish_actuator_status("SW") wolk_device.publish_actuator_status("SL") publish_period_seconds = 5 while True: try: timestamp = int(round(time.time() * 1000)) temperature = random.uniform(15, 30) humidity = random.uniform(10, 55) pressure = random.uniform(975, 1030) accelerometer = ( random.uniform(0, 100), random.uniform(0, 100), random.uniform(0, 100), ) if humidity > 50: # Adds an alarm event to the queue wolk_device.add_alarm("HH", True) else: wolk_device.add_alarm("HH", False) # Adds a sensor reading to the queue wolk_device.add_sensor_reading("T", temperature, timestamp) wolk_device.add_sensor_reading("H", humidity, timestamp) wolk_device.add_sensor_reading("P", pressure, timestamp) wolk_device.add_sensor_reading("ACL", accelerometer, timestamp) # Publishes all sensor readings and alarms from the queue # to the WolkAbout IoT Platform print("Publishing buffered messages") wolk_device.publish() time.sleep(publish_period_seconds) except KeyboardInterrupt: print("Received KeyboardInterrupt, quitting") os._exit(0)
import pandas col_list = ["Key"] col_list2 = ["Password"] os.chdir("key/") for file in glob.glob("*.csv"): df = pandas.read_csv(file, usecols=col_list) device_key = df["Key"] for file in glob.glob("*.csv"): df = pandas.read_csv(file, usecols=col_list2) device_password = df["Password"] return (device_key[0], device_password[0]) wolk_credentials = readWolkCredentials() if wolk_credentials is not None: device = wolk.Device(key=wolk_credentials[0], password=wolk_credentials[1]) else: print("Error reading credentials") sys.exit() try: wolk_device = wolk.WolkConnect(device, host="iot-elektronika.ftn.uns.ac.rs", port=1883) wolk_device.connect() print("#1 Wolk Connection successful.") except RuntimeError as e: print("#1 Wolk Connection unsuccessful.") print(str(e)) sys.exit(-1)