Ubiregi Advent Calendar 2019 Day 4 introduces easy IoT using Raspberry Pi and MESH.
When you press the "button" on the MESH block, a fixed message will be posted to Slack, which is very simple. As an application, I attach a monitor to the Raspberry Pi and use it as a doorphone, I think that I want to see it. If you live alone, there is a problem that you do not know if people are visiting while you are away, and if you place a MESH button instead, you can do something like notify your smartphone that a person has arrived.
The formula is very generous in this regard, so you should refer to it.
[Tell me how to install the "MESH Hub" app on my Raspberry Pi – MESH Support \ | Block-shaped electronic tags that connect to the app for playfulness](https://support.meshprj.com/hc/ ja / articles / 115002125554)
The Raspberry Pi I used this time is Zero, so it's very heavy when I launch the browser.
So, the part to access with the browser in the above procedure is done with a normal PC,
② How to log in to Raspberry Pi and execute the download command
I think it's easy to copy the command and download it via SSH.
Decide / create a notification channel in advance.
next, Use Slack's Incoming Webhooks -Qiita Let's add the Incoming Webhook and copy the URL by referring to. It is recommended to save the URL (we will use it next time) because you can use Notepad or anything.
I'm using Python simply because I use Python a lot, so it doesn't matter what language you use, as long as you can do the same.
By the way, Rasbian has Python installed by default, so you don't need to install Python itself.
The version of Python used this time is Python 2.7.13
.
Create a simple HTTP Server for hitting Raspberry Pi programs from MESH.
This time I used a lightweight micro web framework called bottle
.
Install the library used in the source code with pip
.
(Requests
is used when going to hit Slack)
$ pip install bottle requests
The location can be anywhere, so create a file in a suitable place and write the following source code.
The name can be anything, but this time I chose server.py
.
(Refer to "Making a chat robot operated by Apple Watch using the Raspberry Pi version MESH -Qiita")
server.py
# -*- coding: utf-8 -*-
from bottle import route, run, template
import json
import requests
slack_settings = {
# channel_Let's change the name arbitrarily
#This is because it is easy to increase the setting when you want to notify other channels as well.
'channel_name': {
'url' : '{URL obtained in the process of "Getting URL of Incoming Webhook"}',
'botname': 'test',
'icon' : ':muscle:',
'message': '<!here>\Notification from nMESH'
}
}
def push(setting):
datas = {
'username' : setting['botname'],
'icon_emoji' : setting['icon'],
'text' : setting['message'],
'contentType': 'application/json'
}
payload = json.dumps(datas)
result = requests.post(setting['url'], payload)
print(result)
@route('/post/slack/<command>')
def mesh_button(command):
if command == 'channel_name':
push(slack_settings['channel_name'])
else:
raise Error
run(host='localhost', port=8080, reloader=True)
Start the server with the following command. I don't think it's particularly difficult.
$ python server.py
If you start it well, you will see this display.
Bottle v0.12.17 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
Try hitting it with curl
. If you close the console running python server.py
, the server will stop, so let's open another console (even if you have an SSH connection).
$ curl http://localhost:8080/post/slack/channel_name
On the side running the server
<Response [200]>
127.0.0.1 - - [04/Dec/2019 13:46:15] "GET /post/slack/channel_name HTTP/1.1" 200 0
I think that a log will appear, so if it does, it is a success.
In MESH, processing is created by connecting standard processing called blocks with lines.
(Set later)
So, you need to create a block that hits the URL of server.py
created earlier. So, first make it.
Access SDK \ _TOP \ _JP. You can go to the custom block management screen from "Use MESH SDK".
Click Create New Block
to display the custom block creation screen, so set it.
The settings are as follows. Once you have set everything, click "Save" in the upper left to save.
Source code set in Code
var localhost = 'http://localhost:8080' + properties.path;
ajax({
url : localhost,
type : 'get',
timeout: 5000,
success: function (data) {
callbackSuccess({
resultType : 'continue'
});
},
error: function(request, errorMessage) {
log('ERROR: ' + errorMessage);
callbackSuccess({
resultType: 'continue'
});
}
});
return {
resultType: 'pause'
}
For the settings in this area, I referred to "Making a chat robot operated by Apple Watch using the Raspberry Pi version MESH -Qiita".
Connector This set only the Input Connector. As you will see later by touching the MESH app, there are Input and Output in the MESH block. This time, after pressing the button, the processing of this custom block is started and it feels like the end, so there is no setting in the Output Connector. If you want to do something else after this custom block works, the Output Connector also needs to be configured.
Property This will allow you to create properties that you can reference in your code. You can tweak the property values when placing custom blocks. So, for example, even if you want to notify another channel, you do not need to create a new block, you can just change the property value when arranging it and it will be OK.
Code This will be the JavaScript code that will be executed when the custom block is kicked. Once the code is kicked, I'm going to hit the server I wrote earlier in Python with Ajax.
To make a process using MESH, you need to make a "recipe" with the MESH app. ** This recipe can only be made from the MESH app. ** The recipe to run on Raspberry Pi will also be created from the MESH app.
As a preliminary preparation, let's turn on Bluetooth on your smartphone and Raspberry Pi. Launch the MESH app from your smartphone. There is a smartphone mark on the upper right, but if you tap this, you will see the following "Change to another terminal", so tap this.
Then, the "Select device" screen will appear. Sign in and tap "Add hub".
Tap "Add Hub" to display the next screen. Tap "Start Setup" to continue.
This screen also proceeds with "Next".
If the connection between the smartphone and Raspberry Pi is successful, the following screen will be displayed. If you can't wait a few minutes, check Bluetooth or see if the Raspberry Pi is ready to set up (tap the link on the screen and you'll see how to do it) ..
This screen is a screen as to whether or not to set the Wi-Fi of Raspberry Pi. Unless you have connected by wire, basically the Wi-Fi connection should be finished, so skip it.
Decide the name of the hub appropriately and proceed to the next.
If all goes well, you will end up with this screen, so select the Raspberry Pi MESH hub you just added and tap "Select" at the bottom of the screen.
Tap "New Recipe" on the top screen of the app to display this screen. First, let's add a MESH block. Turn on the MESH block and tap the "Add Block" plus mark at the bottom left. (The power can be turned on by pressing and holding the silicon material part of the MESH block. If it does not turn on, charge it.)
If you can add it successfully, a "button block" will be displayed in the lower left block, so tap it and drag it to place it. It should look like the screen below.
Then add a custom block. I just registered the JavaScript code on the web screen. Although it is reflected on the upper screen, you can go to the addition screen by tapping the plus mark of "Custom addition". Then, the custom block you registered earlier will be displayed as shown below, so tap it and tap the "Add" button.
I think I was able to add it safely, so place the custom block in the same way as the "button block" was placed earlier, and connect the end of the button block and the end of the custom block as shown below. You can connect with a line by dragging from the placed button block to the custom block.
If you tap the placed custom block, you will see a screen like this. You can change the value of the property here. If you change the server side program of Python, you can change the behavior by changing the Path here in the same custom block.
Now that the settings are complete, let's press the MESH button. If you post to Slack, you're successful.
This completes the notification (via Raspberry Pi) on Slack when you press the MESH button. There are quite a lot of steps, but as you may have noticed, the source code I wrote is very small. In the first place, there is a hurdle that you have to install Raspberry Pi, but if you exceed it, you can do it relatively quickly. In addition to buttons, MESH has various blocks such as LED, motion sensor, motion sensor, temperature / humidity sensor, brightness sensor, and GPIO. One of its strengths is that you can make your own IoT even if you can't do electronic work at all (except for GPIO). When it comes to electronic work, some knowledge of electricity and electronics is absolutely necessary, so for those who have been software engineers, the hurdles are a little high. However, MESH tags require almost no knowledge of electronic work and are recommended because most things can be done with software. By the way, if you want to do something physically but don't have knowledge of electronic work, you may want to try using the Eject command, which is very convenient (Reference: Eject command is Raspberry). Let's play with Pi ~ Easy work with CD \ -ROM drive ~).
Recommended Posts