IoT devices (Python)

Follow

This documentation shows how to download and use different IoT device SDKs.

Installing and Using Countly IoT Raspberry Pi SDK

The Countly Raspberry Pi SDK is one of the Countly iOT SDK families which sends data to the Countly server. After this SDK sends data to Countly, you may visualize it on the dashboard. The server takes care of both storing data and visualizing it for you, so you can get more insights from your devices.

If a device doesn’t have an internet connection and cannot send data, the SDK stores this data and then sends it to the queue. Items in this queue are then sent one-by-one after an internet connection has been established.

You may add your Raspberry Pi Python SDK to your project in two different ways.

1st method: Getting the code from Github

The command below copies the SDK from Github directly to your computer. It needs the git command to be available on your operating system.

git clone https://github.com/Countly/countly-sdk-iot.git .

Now, you may copy the Countly.py command to your project folder:

cp raspberry_pi/Raspberry_SDK/Countly.py   your_project_folder

In order to import Countly in Python, use this line in your example py file:

from yourpackage.Countly import Countly

2nd method: Download using pip, easy Python installer

If you have pip command installed on your system, you may run the following command. This easily installs the Countly SDK into your system, so you may directly call it inside your app.

pip install Raspberry_SDK

In order to use the Countly SDK in your Python app, you may add it as follows:

from Raspberry_SDK.Countly import Countly

Using the Countly IoT Raspberry Pi SDK

To initiate the SDK without a timer,

countly = Countly("SERVER_URL", "APP_KEY", 0)

where server_URL is the IP or name of the server, and APP_KEY is the application key that you retrieved from the dashboard once you create the application.

To initiate the timer with a frequency, use the following:

countly = Countly("SERVER_URL", "APP_KEY", TIMER_SECOND)

In order to send a specific event, use this command:

countly.event(‘EVENT_NAME’, EVENT_VALUE)

Example scenario

The best method for retrieving data from the Raspberry Pi GPIO is to use the Grove interface. Grove is basically a ready-to-use toolset meant to ease the use of SPI, I2C, UART in analog and digital circuits. Prepared by Seeed Studio, it includes several sensors, I/O units, switches, lights, and joysticks.

Not only does Grove work with Raspberry Pi, it also works with several industrial, well-known development boards, such as Arduino, Intel Edison, and Beagle Bone Green. For more information about Grove, follow this link. To use Grove in Raspberry Pi, a shield-like card called Grove Pi is used.

In order to use Grove sensors and the Grove Pi shield, create an account on Countly. An App Key will automatically be created for you. We’ll use this key inside the SDK in places where it is relevant.

Raspberry Pi configuration

You may find the necessary configuration regarding how to use Python and Grove with Raspberry Pi. We will need this configuration, so start reading and implementing what is written here.

After installing Grove, you may install the Countly SDK:

pip install Raspberry_SDK

In our example scenario, we have a light sensor. This sensor is connected to the Grove input Analog 0. After configuring the Grove Pi and Grove light sensors, the following code may be used to send light sensor data to the Countly servers:

import threading
import grovepi 
from raspberry_pi.Raspberry_SDK.Countly import Countly
 
light_sensor = 0
 
countly = Countly("SERVER_URL", "APP_KEY", 0)
 
def grove():
      threading.Timer(30,grove).start()
       countly.event("Light", int(grovepi.analogRead(light_sensor)/10.24))
 
grove()

You have now finished sending data from Raspberry Pi to your server.

Looking for help?