This time, I wrote this article to reduce the troublesome part of Raspberry Pi that I had been thinking about for a long time, especially when developing IoT. Write instead of your own memorandum.
The intended target user is a person who bought a Raspberry Pi like himself because he wanted to develop an IoT device (it may be useful for general IoT device development such as taking the device home or outside the company)
--Understand the procedure for connecting and developing Raspberry Pi remotely --Reduces the number of devices to carry during remote development
What you need to develop a Raspberry Pi
However, this means that ** even if the device itself is small, an environment equivalent to desktop PC development is required **. I wanted to install the Raspberry Pi outdoors and use it for local testing and development depending on the situation, so I thought about the configuration necessary for local development while minimizing the equipment to be taken outdoors.
The configuration after work is as follows.
Trouble. ** If the IP does not switch properly when switching between smartphone and wifi **
This article starts with "Building a Raspberry Pi". There are many articles on the internet about "Raspberry Pi Construction", so please refer to them. The important thing this time is after building. If you build a development environment for Raspberry Pi and you can start the OS with Raspberry Pi, first of all, make the following settings in a hurry.
$ sudo apt-get update
$ sudo apt-get install xrdp
$ sudo apt install tightvncserver
$ sudo pip install slackweb
$ sudo pip install psutil
send_IPinfo.py
import slackweb
import psutil
import socket
import time
import subprocess
def is_ping(host):
# linux only
ping = subprocess.Popen(["ping", "-w", "3", "-c", "1", host], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
ping.communicate()
return ping.returncode == 0
time.sleep(180)
hostname = socket.gethostname()
slackurl = "WebHook url obtained by slack is listed here"
##
for name, addrs in psutil.net_if_addrs().items():
if name == "usb0" or name == "wlan0":
print("connect succeed")
flg = True
for addr in addrs:
if addr.family == socket.AF_INET:
message = hostname + " " + name + " is " + addr.address
slack = slackweb.Slack(url = slackurl)
slack.notify(text="%s" % message)
print(message)
You can use both iPhone and Android. Refer to the following, iOS is Blutooth, Android is wired connection with a cable, you will be able to connect to the network by tethering from Raspberry Pi. Reference: [Tethering by connecting to Belutooth to iPhone](http://make.bcde.jp/raspberry-pi/iphone%E3%81%ABbelutooth%E6%8E%A5%E7%B6%9A%E3%81% 97% E3% 81% A6% E3% 83% 86% E3% 82% B6% E3% 83% AA% E3% 83% B3% E3% 82% B0 /) Reference: USB tethering on Raspberry Pi and Android
This time, we will get the webhook URL of slack and set it to skip the information obtained by the python script to slack. Please check the following for details. If you do not want to use it, comment out the following lines 28 and 29 of the script and it will work without using slack, so please change to a method that can be linked externally such as webAPI or email transmission instead.
slack = slackweb.Slack(url = slackurl)
slack.notify(text="%s" % message)
Reference: Slack Webhook URL acquisition procedure
@reboot /usr/bin/python /home/pi/ipinfo_slack.py
At this point, the setting itself is complete. If you have successfully completed steps 1-7, you will be ready to develop without connecting anything other than the power supply to the Raspberry Pi.
This time, since we are focusing on "Raspberry Pi remote development", we need a network connection for remote connection. However, there is not always a network such as wifi outdoors, and the point is to utilize tethering on smartphones for that purpose.
Even if you don't have a laptop, you can easily set it by tethering your smartphone and installing the RDP software on your smartphone, so you can use it for other purposes such as setting up another wifi network.
If you have a network environment such as wifi, you can connect to Raspberry Pi remotely (even with smartphone tethering). This time, as a procedure, I chose RDP, which can be easily handled with GUI even in windows and is compatible with smartphones, but I think that there is no problem with other tools such as VNC.
If possible, it is desirable to be able to operate without connecting to Raspberry Pi except when development is necessary. This time it is assumed that you will connect remotely,
--I want to connect remotely --You must know the Raspberry Pi's IP to connect remotely ――The IP of Raspberry Pi has to connect to Raspberry Pi and check the IP, so do you want to connect remotely? ?? ??
The contradiction is born. Originally, I want to know the IP without connecting to Raspberry Pi, so I have a mechanism that "create a script that checks your own IP and notifies slack, and executes it with cron at startup".
(*) As a precaution, please make sure that you can connect to the tethering or wifi of your smartphone when you start it. Also, if you ping immediately, the program will start before network communication, so wait for 3 minutes appropriately, and notification will not be sent to slack immediately.
If you connect your smartphone to wifi in sequence, or connect to your smartphone from wifi, the IP will automatically switch according to the network, but there are times when the IP does not switch properly. There is. In that case, try restarting the xrdp service, or reinstalling if it doesn't work.
Restart service
sudo service xrdp restart
If it doesn't work, reinstall xrdp
sudo apt remove xrdp
sudo apt update
sudo apt install xrdp
With the above settings or ideas, I think that Raspberry Pi can be developed remotely more easily.