The tool that visualizes IoT data called conect + seemed to be easy to use, so I decided to try it out.
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)
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.
Log in to conect + Lite and edit the project on the create screen.
On the Basic Information tab, set the connection method (WebAPI) and product name (let's call it rasp_dht11).
On the Image tab, set the icon image and thumbnail image.
On the Sensor tab, set the sensor name (temperature, humidity) and key (temperature, humidity).
Lay out the screen of the application from the prepared widget.
When you press the API key generation button, the API key will be generated. The API key is used in the Raspberry Pi script.
First, connect the DHT temperature / humidity sensor (DHT11) and Raspberry Pi as follows.
DHT11 | Raspberry Pi |
---|---|
VCC | 3.3V |
GND | GND |
DATA | GPIO4 |
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'}
Let's check the execution result on the data screen of conect + whether the data is saved in the cloud.
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.
Finally, let's set up the app.
Download and launch the app. ↓ Tap "+" at the top right of the screen ↓ Select the project you want to display and add it ↓ Link the added device ↓ When the data is acquired, the sensed data is displayed.
After that, it will be updated every time new sensing data is received (every hour).
that's all.