-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
215 lines (163 loc) · 6.53 KB
/
setup.sh
File metadata and controls
215 lines (163 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# TODO: Setup variables for sys name, server address, user name and group name
user="pycam-user"
system="epsilon"
sys_name=scope-$system
# Update and upgrade the system
sudo apt-get update && sudo apt-get -y upgrade
# Install the following packages:
# - libhdf5-dev: Development files for the Hierarchical Data Format 5 (HDF5) library
# - nginx: A high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
# - libgl1-mesa-glx: A free implementation of the OpenGL API, which provides graphics rendering capabilities.
sudo apt-get install -y nginx libgl1-mesa-glx python3-dev git acl hostapd dnsmasq iptables
# Get the repo and create a virtual environment
cd ~/
git clone https://github.com/arrancurran/pyCam.git
python3 -m venv pyCam-venv
# Set up the virtual environment to activate on boot
echo "source ~/pyCam-venv/bin/activate" >> ~/.bashrc
source ~/.bashrc
# Install necessary Python packages into our pyCam-venv virtual environment
pip install pypylon flask flask-socketio opencv-python Pillow
# Download and install the Pylon software
mkdir -p ~/Downloads && cd ~/Downloads
# wget https://www2.baslerweb.com/media/downloads/software/pylon_software/pylon-7.5.0.15658-linux-aarch64_debs.tar.gz
wget https://dullenslab.com/pycam/pylon_7.5.0.15658-deb0_arm64.deb
# tar -xzf pylon-7.5.0.15658-linux-aarch64_debs.tar.gz
sudo dpkg -i pylon_7.5.0.15658-deb0_arm64.deb
cd ~/ && rm -rf ~/Downloads
# Set environment variables for Pylon
export PYLON_ROOT=/opt/pylon
export GENICAM_ROOT_V3_1=$PYLON_ROOT/genicam
export GENICAM_CACHE=$HOME/.GenICam_XMLCache
export LD_LIBRARY_PATH=$PYLON_ROOT/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$PYLON_ROOT/lib:$PYTHONPATH
export PATH=$PYLON_ROOT/bin:$PATH
source ~/.bashrc
# Configure Nginx
sudo mkdir -p /var/www/pyCam
# Create Nginx configuration file
pyCam_nginx_config="server {
listen 80;
server_name $sys_name.local;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location /static {
alias /home/$user/pyCam/static;
}
}"
echo "$pyCam_nginx_config" | sudo tee /etc/nginx/sites-available/pyCam > /dev/null
# Enable the new site and restart Nginx
sudo ln -s /etc/nginx/sites-available/pyCam /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo chown -R www-data:www-data /var/www/pyCam
sudo chmod -R 755 /var/www/pyCam
# Enable the camera app to run at start/reboot by creating a service file
SERVICE_FILE_CONTENT="[Unit]
Description=pyCam
After=network.target
[Service]
ExecStart=/home/$user/pyCam-venv/bin/python /home/$user/pyCam/main.py
WorkingDirectory=/home/$user/pyCam
StandardOutput=inherit
StandardError=inherit
Restart=always
User=$user
Environment="PATH=/home/$user/pyCam-venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
[Install]
WantedBy=multi-user.target"
# Create a service file
echo "$SERVICE_FILE_CONTENT" | sudo tee /etc/systemd/system/pyCam.service > /dev/null
# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Setting up auto mount for external USB drive
# Create a directory to mount the USB drive
sudo mkdir -p /mnt/usb
# Add the USB drive to /etc/fstab for auto-mounting
# Replace 'UUID=your-uuid' with the actual UUID of your USB drive
echo "UUID=6790-F7FC /mnt/usb exfat defaults,auto,users,rw,nofail,noatime,uid=$user,gid=$user 0 0" | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
# Mount the USB drive
sudo mount -a
# Ensure the user running the script has write permissions
sudo chown -R $user:$user /mnt/usb
sudo chmod -R 775 /mnt/usb
#sudo blkid /dev/sda1
# Setting up the wifi access point, note there is a min character length for the passphrase.
echo "interface=wlan0
driver=nl80211
ssid=$sys_name
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=pass-$system
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP" | sudo tee /etc/hostapd/hostapd.conf > /dev/null
# Configure dnsmasq
sudo bash -c 'cat > /etc/dnsmasq.conf <<EOF
interface=wlan0
# Set the DHCP range
dhcp-range=192.168.10.100,192.168.10.150,12h
EOF'
echo "# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source /etc/network/interfaces.d/*
auto wlan0
iface wlan0 inet static
address 192.168.10.1
netmask 255.255.255.0" | sudo tee /etc/network/interfaces > /dev/null
echo "[Match]
Name=wlan0
[Network]
Address=192.168.10.1/24" | sudo tee /etc/systemd/network/10-wlan0.network > /dev/null
sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd
# In summary, the command sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" enables IP forwarding on a Linux system by writing the value 1 to the appropriate kernel parameter file. This allows the system to forward network packets between interfaces, effectively turning it into a router.
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo nano /etc/sysctl.conf # Uncomment the line net.ipv4.ip_forward=1
# In summary, the command sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" enables IP forwarding on a Linux system by writing the value 1 to the appropriate kernel parameter file. This allows the system to forward network packets between interfaces, effectively turning it into a router.
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
echo '#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
iptables-restore < /etc/iptables.ipv4.nat
exit 0' | sudo tee /etc/rc.local > /dev/null
sudo systemctl unmask hostapd
# Note country must be set in raspi-config
sudo systemctl enable hostapd
sudo ifconfig wlan0 192.168.10.1 netmask 255.255.255.0
# Enable the service to start on boot
sudo systemctl enable pyCam.service
# Start the service immediately
sudo systemctl start pyCam.service
# TODO: Check the service is running
# Check the status of the service
# sudo systemctl status pyCam.service
# if we need to reduce the wifi signal power
# sudo iwconfig wlan0 txpower 20
# and add the following to /etc/rc.local iwconfig wlan0 txpower 20