Giter VIP home page Giter VIP logo

c8ymqtt's Introduction

Cumulocity IoT Python3 Agent

c8yMQTT is a Python3 Cumulocity Agent for MQTT and Rasperry PI. The Cumulocity Python agent is divided into two Python modules. C8yMQTT is sort of a small SDK that wraps a lot of functionality into function calls. piAgent.py Module leverages C8yMQTT.py to implement device specific functions such as sending measurements, updating configurations and more.

To use this agent, you may sign up for free trial tenant at Cumulocity

The Raspberry Pi including the SensHat Sensor Array is a great starting point for rapid prototyping.

PI

Cumulocity IoT enables companies to to quickly and easily implement smart IoT solutions.

Dashboard


For more information you can Ask a Question in the TECHcommunity Forums.

You can find additional information in the Software AG TECHcommunity.


These tools are provided as-is and without warranty or support. They do not constitute part of the Software AG product suite. Users are free to use, fork and modify them, subject to the license agreement. While Software AG welcomes contributions, we cannot guarantee to include every contribution in the master project.

Contact us at TECHcommunity if you have any questions.

C8yMQTT Class

The class C8yMQTT lets you connect your device to the Cumulocity Cloud using mqtt. It provides basic methods to:

  • Connect to a cumulocity tenant via mqtt (tls supported)
  • Register a device, fetch credentials and store them on the device
  • Subscribe to MQTT Topics
  • Send and receive MQTT Messages

piAgent.py Module

To implement a Cumulocity Device Agent you have to cater the C8yMQTT class with device specific functionality (Send Measurements etc.). piAgent.py provides a sample implementation for the Raspberry PI 3. Though the piAgent.py module will look for a sense hat it will work also without it. Meaning you can also run the agent on all linux distributions that can fulfill the python requirements.

Prerequisites for Raspbian distribution

Enable SPI Interface

edit /boot/config.txt and enable dtparam=spi=on

Getting Started

Install the agent

checkout the repo and execute install.sh (sudo rights are needed). This will install all dependencies (python etc.) via apt and pip and create a systemd file "c8y.service" that is deployed to /etc/systemd/system/ the agent is now registered as a linux daemon. You can start it via service c8y start. When the agent starts the first time it creates the necessary smartrest template. To ensure that the newest version of the template is active you can delete it in the platform. The agent will create it on every startup anew.

If the requirements are installed, for debugging you can also start the agent simply by:

python3 piAgent.py

The agent is designed to run also without a sense hat extension. Then only CPU and Memory Measurements are transmitted. In general the agent can run on any python3 capable platform most critical dependency is psutils that is most hardware/OS dependent.

Configuration

The agent can be configured via the followin file:

pi.properties

the pi.properties file holds device specific parameters.

  • host -> Mqtt host (works for cumulocity.com do not change except you running on another instance of Cumulocity )
  • port -> Mqtt port it'll be set either to 1883 for tcp or to 8883 for Transport Encryption via TLS
  • tls -> Boolean which indicates whether to use tls or not. Must match the port settings
  • cert_auth -> When enabled certificate authentication is used
  • client_cert -> Client cert in pem format including the whole certificate chain
  • client_key -> Client private key in pem without password applied
  • cacert -> path to a certificate (pem format) that holds all trusted root certificates
  • operations -> all supported operations of the agent which are implemented in an agent module
  • subscribe -> Topics the agent will subscribe to.
  • deviceType -> speaks for itself
  • sendinterval -> Loop time of the RunAgent() function. Indicates in which time frame measurements etc. are read and send
  • requiredinterval -> Sets the time frame in which the device must contact the platform in order to be displayed connected.
  • Loglevel -> speaks for itself
  • reboot -> will be set by the restart device command to prevent from a infinite loop.
  • config_update -> same as reboot
  • bootstrap_pwd -> needed for auto registration process
  • serial -> optional, when configured the agent does not try to fetch the externalId from the PI /proc/cpuinfo but usese that value instead. Remove completely to use the cpu serial of a PI

Device Registration

To autoregister your pi go to Cumulocity -> Device Management. Create a new Device Registration by entering the serial (could be retrieved by cat /proc/cpuinfo) of your PI. After registration the c8y.properties file will be created automatically. For this the bootstrap_pwd in pi.properties must be set (default). If you run the Agent on plain linux you can set the serial using the optional parameter "serial" in pi.properties to configure it manually

!!!PLEASE MAKE SURE THAT THE DEVICE IS NOT ALREADY REGISTERED WITH IT'S SERIAL NUMBER!!! If that is the case either you have to delete the device and re-register or create the c8y.properties by hand and provide correct credentials and client id.

Certificate Authentication

When in pi.properties the cert_auth flag is enabled and the corresponding root cert is active in the tenant and auto registration is enabled for the root cert -> The device is automatically created in the platform. You have to make sure that the externalId of the device (e.g. /cat/proc/cpuinfo) matches the common name of the client_cert. If cert_auth is enabled the agent also pulls the bearere token from the platfrom to use it in rest requests. This is implementedd in the software download feature. You cannot mix the two authentication methods. If you wold like to switch you have to delete the device credentials in the platform first.

c8y.properties
After successful registration c8yMQTT will create and store the device credentials file c8y.properties in the same directory as the class. It can be created to provide manual credentials. Remove this file in order to initiate a new auto registration process.

[credentials]
user =
tenant =
password =
clientid =

If not present the initialized variable is false and the bootstrap method method of c8yMQTT module can be used to fetch new credentials.

Build in functions

The Agent supports the following functions:

  • Configuration -> The content of pi.properties will be displayed under Device Management and can be save to the device
  • Restart -> The Raspberry PI an be rebooted via the restart command.
  • Send Message to the Device -> Via the Send Message Widget in the Cockpit Application. Text can be send to the PI and will be displayed on the SensHats LED Matrix
  • Transmitted Measurements -> Temperature, Gyroscope, Acceleration, Pressure, Humidity
  • Joystick -> Events are created on pressing. If the joystick is pressed three times the PI will start a new registration process. This comes in handy if you have to move it to another tenant (works only if you dont use cert_auth).
  • Remote Access (thx Stefan W.)-> If the remote access microservice is subscribed to the cumulocity tenant and the user has remote access rights remote access can be configured within device management of the agents device
  • Experimental Software Update. You can create a zip file of the repo content and deploy that to the cumulocity software repository. It is recommended to leave the pi.properties out of the deployment since otherwise the local config would be overwritten.You should now be able to execute a software update in device management. Only Update is supported and the software name must start with piAgent. The agent will create a backup of the old installation in a backup folder. The Agent will also create a folder software_download where the downloaded versions are stored. Be sure to update the relese file in the new software zip, it will be reported as new software version to the platform.

c8ymqtt's People

Contributors

amag-sag avatar mstoffel-sag avatar peterlamar avatar techcommunity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

c8ymqtt's Issues

Connect sequence is not correct

The condition in file: c8yMQTT.py, line 115) is not correct:

Current code:

        count=0
        while self.connected==False and  count < 50: 
            time.sleep(.2)
            count+=1
        if self.connected!=False:
            self.logger.error('Could not connect to the MQTT Broker.')
            return False
        else:
            self.client.loop_start()
            for t in topics:
                self.client.subscribe(t, 2)

Suggested change:

        count=0
        self.client.loop_start()
        while self.connected==False and  count < 50: 
            time.sleep(.2)
            count+=1
        if self.connected==False:
            self.logger.error('Could not connect to the MQTT Broker.')
            self.client.loop_stop()
            return False
        else:
            for t in topics:
                self.client.subscribe(t, 2)

Python 3 or 2 ?

Hi

Trying to get it running, but ran in some problems.

piAgent.py is working, after outcommenting pressure (Init Failed)

Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "piAgent.py", line 136, in sendMeasurements sendPressure() File "piAgent.py", line 50, in sendPressure tempString = "994,," + str(sense.get_pressure()) File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 621, in get_pressure self._init_pressure() # Ensure pressure sensor is initialised File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 559, in _init_pressure raise OSError('Pressure Init Failed') OSError: Pressure Init Failed

sendMeasurements.py:
When running it in Python 3:

  • is missing brackets in print statements.
  • Changed ConfigParser to configparser

How to use sendMeasurements.py?

Thanks for helping

Johannes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.