Developer Support Iotic Labs

Complete follow script

 

Copy this basic Python script for your Iotic Things, and save it as a .py file.

 

It will perform some of the basic functions you may want your Iotic Thing to perform, including receiving data and printing essential details. You can find more information about these functions in the following sections.

 

Simply run the script from a terminal using python3 and you will begin to receive data from the Things you follow. The data is event-driven, so as soon as it’s published, you’ll receive it.

 

Illustration of the Iotic folder-file structure

 

NOTE: This script references a Thing with the Local ID First_Iotic_Thing, and the file Iotic_Agent.ini. You will need to update these to match your own Thing Local ID and the name of your .ini file.

 

Python logo

# PYTHON2 COMPATIBILITY -----------------------------------------------------------------------------------------------
from __future__ import unicode_literals, print_function  # pylint: disable=unused-import

# LOGGING -------------------------------------------------------------------------------------------------------------
# Logging set to only CRITICAL messages by default.  To see more, use logging.INFO, or to see everything, use logging.DEBUG
import logging
logging.basicConfig(format='%(asctime)s,%(msecs)03d %(levelname)s [%(name)s] {%(threadName)s} %(message)s',
                    level=logging.CRITICAL)

# IMPORTS -------------------------------------------------------------------------------------------------------------
import time

# IOTIC AGENT IMPORTS -------------------------------------------------------------------------------------------------
from IoticAgent import IOT
from IoticAgent.Core.Const import R_FEED

# ---------------------------------------------------------------------------------------------------------------------


def connect_thing(client):
    print("Connecting First Iotic Thing")

    # CREATE THING -----------------------------------------------------------------------------------------------------
    # Note: Calling 'create_thing' connects your script to a virtual Thing if the Local ID (lid) is already in use. If not it creates a new Thing with this lid.
    my_thing = client.create_thing('First_Iotic_Thing') # ** Make sure the Local ID matches your Thing **

    # Print some information about your Thing
    print("ABOUT MY THING")
    print("Local ID:", my_thing.lid)
    print("Global unique ID (GUID):", my_thing.guid)

    return my_thing


def show_metadata(my_thing):
    print("METADATA (simplified)")

    # Get and print the metadata you set in the UI
    with my_thing.get_meta() as my_metadata:

        my_labels = my_metadata.get_labels()
        print("Labels:")
        print(my_labels)

        my_descriptions = my_metadata.get_descriptions()
        print("Descriptions:")
        print(my_descriptions)

        my_lat, my_lon = my_metadata.get_location()
        if my_lat is not None:
            print("Location lat = %f & lon = %f" % (my_lat, my_lon))
        else:
            print("No location set")

        print("Tags:")
        tag_list = my_thing.list_tag()
        print(tag_list)


def connect_subscriptions(my_thing):
    print("Printing received data")

    # List any feeds or controls you're following
    # Note: 'id' is the feed/control's global point ID (gpid)
    sub_list = my_thing.list_connections()

    for key in sub_list:
        gpid = sub_list[key]['id']
        if sub_list[key]['type'] == R_FEED:
            my_thing.follow(gpid, follow_feed_callback)
        print("Global point ID (GPID):", gpid)


def follow_feed_callback(args):

    # Print data recieved from followed feeds
    print("Received data:", args['data'])


# MAIN -------------------------------------------------------------------------------------------------------------
def main():
    with IOT.Client(config='Iotic_Agent.ini') as client:  # ** Make sure this matches your .ini file **
        my_thing = connect_thing(client)
        show_metadata(my_thing)

        while True:
            try:
                print("Main running. Press ctrl+c to quit.")
                connect_subscriptions(my_thing)
                time.sleep(10)
            except KeyboardInterrupt:
                break


# RUN --------------------------------------------------------------------------------------------------

if __name__ == '__main__':
    main()

# END --------------------------------------------------------------------------------------------------

 

 

Remember, before you can hook your Thing up to code you also need to:

 

  1. Create a new Iotic Agent in the Space app, and download an .ini file
  2. Assign Things to an Agent
  3. Organise your file directory

By continuing to use the site, you agree to the use of cookies. more information

This website uses cookies via Google Analytics to give you the best browsing experience possible. No personally-identifiable information is collected about you unless you explicitly submit that information on this website. Click "Accept" to continue.

Close