Skip to content

Python program that collects sensor data from a DHT 22 temperature sensor and creates a CoAP endpoint to share this data.

Notifications You must be signed in to change notification settings

Silver292/rpi-coap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raspberry Pi Sensor to ThingsBoard using Constrained Application Protocol

Table of Contents

Introduction

This script is designed to be run on Raspberry Pi 3 Model B hardware running the Raspbian 9.6 operating system. It is also assumed that a DHT22 temperature and humidity sensor is connected to the Raspberry Pi (RPi) using GPIO 4 pin. The process of connecting the sensor is described here.

The script creates a Constrained Application Protocol (CoAP) client on the RPi and repeatedly polls the attached DHT22 sensor for temperature and humidity data. This data is then formatted to JavaScript Object Notation (JSON) and is sent to a CoAP endpoint provided by the ThingsBoard Cloud Platform. This data is then displayed on the ThingsBoard dashboard that shows the current temperature and humidity as well as historical readings in a graph form.

Installation

Connecting the sensor

Diagram showing sensor connection to RPi

To connect the DHT22 sensor to the RPi:

  1. Connect the - output on the sensor the pin 6 on the RPi.
  2. Connect the + input on the sensor to pin 2 on the RPi.
  3. Connect the output pin on the sensor to pin 7 on the RPi.

Downloading the script

Clone the repository:

$ git clone https://github.com/Silver292/rpi-coap.git
$ cd rpi-coap/

Create a virtual environment if needed:

$ virtualenv .env
$ source .env/bin/activate

Install the application:

$ pip install .

Application is run using:

$ rpicoap

Configuration

Settings are kept in the config.ini file. You can copy config.ini.example to create your own or this file will be created the first time you run the script.

The easiest way to edit the configuration file is to run the program passing the edit flag -e or --edit i.e. rpicoap -e or rpicoap --edit. This will open the configuration file with your default editor.

Most of the default settings should be fine, unless you are using a different GPIO port on the Raspberry Pi.

You will need to update the host and device_auth_token settings in the config.ini. These can be retrieved from the Thingsboard you wish to send the sensor data to.

Where to get auth token

The auth token can be obtained by clicking the button highlighted in yellow above.

Config.ini

SLEEP_INTERVAL = 5 - Amount of time between each sensor reading and sending of data.

GPIO_PIN = 4 - GPIO pin on the RPi used to connect to the DHT22 sensor. Defaults to 4, shown in installation.

PORT = 5683 - Port for CoAP endpoint, defaults to 5683.

HOST = demo.thingsboard.io - Host of the Thingsboard. Defaults to using the Thingsboard live demo.

DEVICE_AUTH_TOKEN = - Auth token for device. This is retrieved from the Thingsboard devices section.

Usage

To start recording and sending data to the Thingsboard platform run:

$ rpi-coap

This will start the CoAP client and begin reading data from the sensor and sending it to the ThingsBoard dashboard.

Command line options

--help - Running the application with this will display the help text.

-e --edit - This will open the configuration file with your default editor.

-f --file filename.csv - This allows a csv file to be passed to the program. The data contained in the csv sent to the ThingsBoard platform. This is useful for testing configurations. Data in the csv file must be in the format:

humidity,temperature
10,24
10.5,24.2
...etc

Where the header row is optional.

-v --verbose - This option will display the data being sent with a timestamp.

Stopping

To stop the script press Ctrl + C.

About

Python program that collects sensor data from a DHT 22 temperature sensor and creates a CoAP endpoint to share this data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages