Raspberry Pi on the Cloud (2): Uploading sensor data to AWS IoT and leveraging Kibana for presentation

Source: Internet
Author: User
Tags kibana aws documentation

Raspberry Pi on the Cloud (1): Environment preparation

Raspberry Pi on the Cloud (2): Uploading sensor data to AWS IoT and leveraging Kibana for presentation

1. Sensor installation and configuration 1.1 DHT22 installation

The DHT22 is a temperature and humidity sensor with 3 pins, the first pin on the left (#1) is the 3-5v power supply, the second pin (#2) is connected to the data input pin, and the rightmost pin (#4) is grounded.

The Raspberry Pi 3B has a total of 40 pins (gpio,general Purpose I/O Ports, universal input/Output port), only need to connect the sensor's 3 pins to the appropriate pin on the board. My connection is 1-01, 2-07, 4-09. Note the GPIO number for the data pin connection of the DHT22 is 07 names called GPIO04. This is used in the following code.

1.2 Simple test

To install the code base:

Git Clonehttps://github.com/adafruit/adafruit_python_dht.gitcd Adafruit_python_dhtsudo Apt -get  updatesudo apt-get install build-essential python-devsudo python setup.py Install

Simple python test code:

 as Dhtimport Time  while True:time.sleep (2.54'Temp =%.1f "C, humidity =%.1f%%rh  ' % (t, h)

Test results:

2. AWS IoT Implementation

The effect to be achieved is as follows:

2.1 AWS IoT Configuration

Do the following on the Https://ap-southeast-1.console.aws.amazon.com/iot/home?region=ap-southeast-1#/thinghub.

(1) Create thing

(2) Create a thing certificate and download

(3) Create policy

Policy is responsible for authorization, and the above configuration indicates that all AWS IoT operations are allowed for all resources.

(4) Attach the policy to the certificate created in the second step and attach the thing to the certificate as well.

(5) Create an AWS ElasticSearch domain

(6) Create a rule to import data from the Raspberry Pi into ElasticSearch

Specific configuration of the operation:

Several configuration items:

    • _id for each document in the Id:es, using the automatically generated UUID
    • Index: Saves the index of the Raspberry Pi document, named Homepi, which is a logical namespace
    • Type: A type below the index that represents the data for the Raspberry Pi
2.2 Configuration in the Raspberry Pi

(1) Installing the AWS IoT device python SDK

   git clone https://Github.com/aws/aws-iot-device-sdk-python   cd aws-iot-device-sdk-python/    python setup.py install

(2) Upload the four certificates downloaded in 2.1 to the Raspberry Pi

[Email protected]:/home/pi/awsiot# ls-Lttotal --rw-r--r--1Pi Pi1758The6 Ten: -verisign-class3-public-primary-certification-authority-G5.pem-rw-r--r--1Pi Pi1220The6  the: $aec2731afd-CERTIFICATE.PEM.CRT-rw-r--r--1Pi Pi1679The6  the: $aec2731afd-Private. Pem.key-rw-r--r--1Pi Pi451The6  the: $aec2731afd- Public. Pem.key

(3) Writing code

Import Rpi.gpio asGpioimport adafruit_dht asDHT fromawsiotpythonsdk.mqttlib Import awsiotmqttclient fromTime Import Sleep fromdatetime Import Date, DateTime # Initialize Gpiogpio.setwarnings (False) Gpio.setmode (GPIO. BCM) Gpio.cleanup () # AWS IoT certificate based connectionmymqttclient= Awsiotmqttclient ("123afhlss411") Mymqttclient.configureendpoint ("*************.iot.ap-southeast-1.amazonaws.com",8883) Mymqttclient.configurecredentials ("/home/pi/awsiot/verisign-class3-public-primary-certification-authority-g5.pem","/home/pi/awsiot/aec2731afd-private.pem.key","/HOME/PI/AWSIOT/AEC2731AFD-CERTIFICATE.PEM.CRT") mymqttclient.configureofflinepublishqueueing (-1) # Infinite offline Publish queueingmymqttclient.configuredrainingfrequency (2) # draining:2Hzmymqttclient.configureconnectdisconnecttimeout (Ten)  #TenSecmymqttclient.configuremqttoperationtimeout (5)  #5sec #connect and Publishmymqttclient.connect () Mymqttclient.publish ("homepi/dht22","Connected",0) #loop and publish sensor reading while 1: Now=Datetime.utcnow () now_str= Now.strftime ('%y-%m-%dt%h:%m:%sz') #e. G. .-Geneva-18t06: A: -. 877Zh,t= Dht.read_retry (DHT. DHT22,4the name of the #07 pin is GPIO04 print'Temp =%.1f "C, humidity =%.1f%%rh'%(t, h)Payload='{"timestamp": "'+ Now_str +'" ," Temperature ":'+"{:. 2f}". Format (t) +', "humidity":'+"{:. 2f}". Format (h) +' }'Print Payload Mymqttclient.publish ("homepi/dht22", payload,0) Sleep (Ten)

The code is simple, and a little explanation:

    • Use the AWS IoT Device SDK to create an MQTT connection to AWS IoT Core
    • Get temperature and humidity data from the sensor every 10 seconds using the DHT22 SDK
    • Use the AWS IoT device SDK to send data to MQTT topic

(4) Run the code and test it in AWS IoT to receive incoming Raspberry Pi sensor data

(5) Data statistics can also be seen in ElasticSearch

2.3 Configuring Kibana

AWS ElasticSearch has a Kibana built in by default and can see its links on the ES interface. Open the link, and then make the following configuration:

(1) Configure index pattern

The purpose of the configuration is to have Kibana navigate to one or several index in ES. Here is the index with Homepi.

(2) Configure the virsualize, then you can see the diagram.

The recent home temperature has been at more than 30 degrees, originally thought the evening will be low. There are some changes in humidity.

3. A little Feeling
    • GitHub is a great place to share something that avoids a lot of repetitive wheel-building work.
    • AWS free tier is a good thing to do, and doing some POC testing is basically not a cost, but pay attention to the content of the resources that are provided at no cost, otherwise the money is accidentally spent, and the regular check of the bill
    • The AWS documentation is so rich that a file is two hundred or three hundred pages long
    • The AWS IoT platform should be categorized as an application support platform AEP (Application enablement Platform), providing device access (MQTT), interfacing with AWS services, data analysis, security, and more.
    • The AWS ElasticSearch feature is not too comprehensive, there are few things that can be done on the interface, and permissions management is complicated
    • It's a pretty interesting thing to do and then share.

Reference Links:

    • http://techblog.calvinboey.com/raspberrypi-aws-iot-python/
    • https://cloudncode.blog/2017/11/07/make-your-first-iot-device-via-aws-iot-service-and-raspberry-pi/
    • https://www.jianshu.com/p/f31b90cc756f

Raspberry Pi on the Cloud (2): Uploading sensor data to AWS IoT and leveraging Kibana for presentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.