Self-style Raspeye Remote Development Settings Best Practices

at first

What is this?

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.

Target user

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)

To be able to

--Understand the procedure for connecting and developing Raspberry Pi remotely --Reduces the number of devices to carry during remote development

Since Raspberry Pi is a basic PC, there are many devices required for development, and it is troublesome to use it outside.

What you need to develop a Raspberry Pi

  1. ** "Raspberry Pi body" **
  2. ** "Battery power supply and cable" **
  3. ** "Display and Cable" **
  4. ** "Devices such as wifi" **
  5. ** "Keyboard" **
  6. ** "Mouse if necessary" **

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.

  1. ** "Raspberry Pi main unit (confirmed model is Raspberry Pi 4, OS is Raspbian Buster)" **
  2. ** "Notebook PC (confirmation OS is Windows 10)" (display, keyboard, mouse replacement, even when the power supply can be replaced by USB) **
  3. ** "Smartphone (confirmed model is Pixel 3a OS is Android)" (alternative to wifi) **

Self-styled rasp pie remote development principle

  1. ** Immediately set remote after building Raspberry Pi **
  2. ** The initial setting is to play with the Raspberry Pi "on your smartphone" **
  3. ** Remote connection with RDP / VNC etc. when there is a wifi environment **
  4. ** It is convenient to notify logs etc. with a tool that can be linked externally such as slack **

Trouble. ** If the IP does not switch properly when switching between smartphone and wifi **

1. Immediately set remote after building Raspberry Pi

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.

  1. Install xrdp for RDP connection on the terminal screen Reference: Installing and connecting xrdp
$ sudo apt-get update
$ sudo apt-get install xrdp
$ sudo apt install tightvncserver
  1. On the terminal screen of Raspberry Pi, install slackweb, pustil used in the script for slack communication Reference: Connect to Raspberry Pi with Remote Desktop
$ sudo pip install slackweb
$ sudo pip install psutil
  1. Place the following program for slack communication on Raspberry Pi Open the screen with an editor such as vi, nano, and paste the following python program Reference: Notify Slack of Raspberry Pi's Wi-Fi address Reference: Machine judgment of ping communication confirmation result

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)
  1. Put your smartphone in tethering mode and connect it with a cable

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

  1. Check if you can connect to the Internet, and if possible, register the slack API.

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

  1. Check if send_IPinfo.py works, and if you can check it, register it in cron. Reference: How to use cron on Raspberry Pi to automatically execute commands at startup or repeatedly execute them at n-minute intervals
@reboot /usr/bin/python /home/pi/ipinfo_slack.py 
  1. Install the remote desktop application on your PC and smartphone, and check if you can connect with the IP that appears on slack and the console.

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.

2. The initial setting is to play with the Raspberry Pi "on your smartphone"

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.

3. Remote connection with RDP / VNC etc. when there is a wifi environment

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.

4. It is convenient to notify logs etc. with a tool that can be linked externally such as slack

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.

Trouble. If the IP does not switch properly when switching between smartphone and wifi

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 

At the end

With the above settings or ideas, I think that Raspberry Pi can be developed remotely more easily.

Recommended Posts

Self-style Raspeye Remote Development Settings Best Practices
AWS Lambda Development My Best Practices
Personal best practices for VS Code-fronted Python development environments