[PYTHON] I tried connecting Raspberry Pi and conect + with Web API

The tool that visualizes IoT data called conect + seemed to be easy to use, so I decided to try it out. IMG_0554 - コピー.PNG

What is conect +

conect + is a visualization tool that allows you to easily create applications that display IoT data and control devices. There are conect + Studio for 9,980 yen per month and conect + Lite that can be used for free. This time I decided to visualize it with free conect +. (Please check the link for the difference between the services of conect + Studio and conect + Lite)

Constitution

cl2.PNG The configuration is as shown in the above figure. Connect the sensing data of DHT11 (temperature and humidity sensor) connected to Raspberry Pi to conect + Lite with WebAPI.

Settings with conect + Lite

Log in to conect + Lite and edit the project on the create screen. clcreate.PNG

Basic information settings

On the Basic Information tab, set the connection method (WebAPI) and product name (let's call it rasp_dht11). cl1.PNG

On the Image tab, set the icon image and thumbnail image. cl3.PNG

On the Sensor tab, set the sensor name (temperature, humidity) and key (temperature, humidity). cl4.PNG

App widget settings

Lay out the screen of the application from the prepared widget. widget.PNG

Web API settings

When you press the API key generation button, the API key will be generated. The API key is used in the Raspberry Pi script. key.PNG

Raspberry Pi settings

Connection between DHT11 and Raspberry Pi

First, connect the DHT temperature / humidity sensor (DHT11) and Raspberry Pi as follows.

DHT11 Raspberry Pi
VCC 3.3V
GND GND
DATA GPIO4

Script creation

Clone the Python library that gets the DHT11 sensor data from GitHub.

git clone https://github.com/szazo/DHT11_python.git

When the clone is completed, a folder called "DHT11_python" will be created. Get the temperature and humidity data with the sample script "dht11_example.py" in that folder. By default, the Pin number is 14, so modify it to 4.

dht11_example.py


import RPi.GPIO as GPIO
import dht11
import time
import datetime

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 4
instance = dht11.DHT11(pin=4) #pin number 4(GPIO4)Fixed to

while True:
    result = instance.read()
    if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: %d C" % result.temperature)
        print("Humidity: %d %%" % result.humidity)

    time.sleep(1)

Add a data save process to conect + to this sample script.

dht11_cp.py


import RPi.GPIO as GPIO
import dht11
import pprint
import json
import requests
from pytz import timezone
from datetime import datetime

# json.dumps
def post_data(api_key,device_id,data_key,sensing_at,value):
    url = "https://api.conect.plus/v1/(API key)/data" %{'api_key':api_key}
    response = requests.post(
        url,
        json.dumps({
            'deviceUuid' : device_id,
            'key' : data_key,
            'sensingAt' : sensing_at,
            'value1' : value}),
        headers = {'Content-Type' : 'application/json'})
    pprint.pprint(response.json())

# now
def now_utc_str():
    return datetime.now(timezone('UTC')).strftime("%Y-%m-%d %H:%M:%S")

# initialize GPIO
GPIO.setwarnings(False) #Ignore warnings
GPIO.setmode(GPIO.BCM) #Specify GPIO by role pin number
GPIO.cleanup() #Reset GPIO settings at the end of the script

API_KEY = '(API key)'
DEVICE_ID =  'SINWSSS'
DATA_KEY_TEMPERATURE = 'temperature'
DATA_KEY_HUMIDITY = 'humidity'

# read data using pin 4
instance = dht11.DHT11(pin=4) #Read GPIO4 data

while True:
    result = instance.read()
    if result.is_valid():
        break #Exit when valid data is obtained (repeat if invalid)

temp = result.temperature
hum = result.humidity
print(temp,hum)

# post data
now = now_utc_str()
post_data(API_KEY,DEVICE_ID,DATA_KEY_TEMPERATURE,now,temp)
post_data(API_KEY,DEVICE_ID,DATA_KEY_HUMIDITY,now,hum)

Execute dht11_cp.py, and if the following response is returned, it is successful.

$ python dht11_cp.py
23 36 
{'message': 'Success.', 'status': 'SUCCESS'}
{'message': 'Success.', 'status': 'SUCCESS'}

Confirm on the data screen of conect +

Let's check the execution result on the data screen of conect + whether the data is saved in the cloud. cldata.PNG

Scheduled execution settings

Use Raspbian's cron feature to run scripts on a regular basis without having to interact with the Raspberry Pi. Here, we will run dht11_cp.py once an hour.

Execute the following command in LX Terminal.

crontab -e

When "Select an editor" is displayed, select / bin / nano of "2". When "nano" opens, move the cursor to the bottom of the main and enter the following command.

00 * * * * /usr/bin/python3 /home/pi/DHT11_python/dht11_cp.py
crontab: installing new crontab

This will save the temperature and humidity data at 00:00 every hour.

App settings

Finally, let's set up the app.

Download and launch the app. ↓ Tap "+" at the top right of the screen IMG_0549 - コピー.PNG ↓ Select the project you want to display and add it IMG_0551.PNG ↓ Link the added device IMG_0552.PNG IMG_0553.PNG ↓ When the data is acquired, the sensed data is displayed. IMG_0554.PNG

After that, it will be updated every time new sensing data is received (every hour).

that's all.

Recommended Posts

I tried connecting Raspberry Pi and conect + with Web API
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried web scraping with python.
I tried running Movidius NCS with python of Raspberry Pi3
I tried follow management with Twitter API and Python (easy)
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
I tried to automate the watering of the planter with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
Get US stock price from Python with Web API with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
I tried using Twitter api and Line api
I tried to make a Web API
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried web scraping using python and selenium
I tried playing with PartiQL and MongoDB connected
I tried Jacobian and partial differential with python
I tried function synthesis and curry with python
MQTT RC car with Arduino and Raspberry Pi
I tried connecting AWS Lambda with other services
I tried "Receipt OCR" with Google Vision API
Get temperature and humidity with DHT11 and Raspberry Pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
I tried to automate internal operations with Docker, Python and Twitter API + bonus
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
I tried running Flask on Raspberry Pi 3 Model B + using Nginx and uWSGI
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I tried to make a simple image recognition API with Fast API and Tensorflow
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
I tried to implement and learn DCGAN with PyTorch
Detect mask wearing status with OpenCV and Raspberry Pi
I tried to automatically read and save with VOICEROID2
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
I tried to uncover our darkness with Chatwork API
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
I tried to implement Grad-CAM with keras and tensorflow
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
I tried hitting the API with echonest's python client
When I tried to do socket communication with Raspberry Pi, the protocol was different
Easy introduction to home hack with Raspberry Pi and discord.py
I tried fp-growth with python
I tried scraping with Python
Mutter plants with Raspberry Pi
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
I tried Learning-to-Rank with Elasticsearch!
Python beginner opens and closes interlocking camera with Raspberry Pi
I tried Google Sign-In with Spring Boot + Spring Security REST API
I tried clustering with PyCaret
I tried to get Web information using "Requests" and "lxml"
Create an LCD (16x2) game with Raspberry Pi and Python
I talked to Raspberry Pi