Menu |
Prof /
Cosqmv2-RaspbianCoSQM - a color hack to the SQM-LE for Light At Night SensingSoftware package for CoSQM v2 Creative Commons Attribution-ShareAlike 4.0 International License IntroductionThe aim of this project is to develop a system capable of remotely controlling a light pollution monitoring system consisting in a 5 slot filters wheel aiming to add multispectral detection capabilities to the well known Sky Quality Meter (SQM-LE). We chose the SQM as the light sensor to allow a full backward compatibility to the impressive historical SQM database. Four of the slots are filled with color filers (Red, Green, Blue, Yellow). The fift slot is empty and therefore allow the standard SQM measurement. The SQM-LE is communicating with a raspberry pi 4b (RPI). The linux distribution used for this system is Raspberry Pi OS (32-bit). We chose a development philosophy based on a complete control from the Linux command line. The system can be controlled simply with a low speed remote connection provided by a SSH client installed on the user workstation. The RPI connected to the system act as a server. Also to promote the implementation of measuring network, the data acquired by the CoSQM is stored on a web server installed on the RPI. Thanks to that web server, the data from multiple CoSQM units all around the world can be downloaded by a central server. Locally, the web server can be accessed with any portable device through a proprietary wifi network. SSH can also be accessed through this wifi network. How to access the CoSQMFrom internet: ssh -p 2022 sand@router_public_ip or inside a private network ssh sand@servers_local_ip From the internet, port 2022 is used instead of standard port 22 to reduce risk of attacks by hackers. A password and user name is required. The standard username is sand. The system administrator should be able to provide you the password for sand. On the local network, we typically reserve the IP 192.168.0.100 for the raspberry pi while using 192.168.0.101 or 192.168.0.102 for the SQM-LE. Installing and configuring subsystemsBasic system installations and dependancies
sudo useradd sand sudo passwd sand sudo mkdir /home/sand sudo cp /home/pi/.bashrc /home/sand/ sudo chown -R sand /home/sand sudo chgrp -R sand /home/sand sudo sh -c 'echo "sand ALL=(ALL:ALL) ALL" >> /etc/sudoers'
sudo raspi-config
sudo apt update && sudo apt-get upgrade sudo apt install vim sudo apt install vim-common sudo apt install apache2 sudo apt install gpsd sudo apt install gpsd-clients sudo apt install lm-sensors sudo apt install imagemagick sudo apt install ntp sudo apt install openssh-client sudo apt install openssh-server sudo apt install netpbm sudo apt install git sudo apt install python3-serial sudo apt install python3-dev sudo apt install python3-rpi.gpio sudo apt install python3-gpiozero sudo apt install bc sudo apt install iptables
sudo mkdir /home/sand/backup Mount the usb stick on /home/sand/backupThe usb stick will be the support to backup all the data. The backup will be done at 9 AM UT. That will be managed by the crontab.
sudo lsblk You should see the usb drive on the sda1 drive
sudo umount /dev/sda1 sudo su echo "/dev/sda1 /home/sand/backup vfat rw,user,exec,umask=000 0 0" >> /etc/fstab exit sudo reboot Notes:
SSHSSH server allows any remote SSH client to connect to the server to remotely control system programs from the command line or to download (upstream / downstream) data.
sudo raspi-config
sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT sudo iptables-save Activating the pi camera module
sudo raspi-config
Add sand user to the video groupsudo usermod -a -G video sand Installing CoSQM applicationsLatest available package release on github repository
cd mkdir git cd git git clone https://github.com/aubema/cosqm.git
sudo cp -f /home/sand/git/cosqm/* /usr/local/bin/ sudo chmod a+x /usr/local/bin/*
cp -f /home/sand/git/cosqm/localconfig /home/sand Automatic startup of observe-sqm-stepper.bash codecd /home/sand/git/cosqm chmod a+x * sudo cp * /usr/local/bin sudo cp observe.service /etc/systemd/system sudo systemctl enable observe.service sudo systemctl start observe.service Apache serverWe are using the Apache2 web server. This server allows browsing the database and look at the log file. To activate the web server, we must:
sudo a2enmod userdir sudo service apache2 restart
sudo mkdir /var/www/html/data sudo chmod a+rx /var/www/html/data
sudo vim /etc/apache2/apache2.conf
# added for cosqm <Directory /var/www/html/data> Options Indexes FollowSymLinks MultiViews AllowOverride None Require all granted </Directory>
sudo service apache2 restart crontabThe crond daemon is scheduling repetitive tasks on linux systems. Users who have the right to use crond can schedule their own tasks to crond. We are using crond to schedule a camera picture acquisition every 15 min. Add program Camera.bash to the root crond scheduleThis scripts include an automatic integration time algorithm so that the sky must be visible no matter the level of radiance.
sudo su crontab -e
*/15 * * * * /usr/local/bin/Camera.bash 00 12 * * * /sbin/shutdown -r now 00 09 * * * /usr/bin/rsync -a /var/www/html/data/* /home/sand/backup
N.B. the time for daily shutdown above was set to local noon (which is 00 12 in Greenwich). Please adapt to your time zone so that it will be executed around noon local time.
exit USB GPS Stratux Vk-162 Remote Mount USB GPS (U-blox)The GPS code in no more compatible with the new version of gpsd. So that we recommend not to use the GPS module until that bug will be corrected. The latitude/longitude of your site can simply be written ii the /home/sand/localconfig file The GPS is controlled by the gpsd server. The GPS startup may easily take 45 seconds (time to connect to available satellites). Sometimes, the GPS does not succeed to connect to satellites. Most of the time this occur when satellites are masked by obstacles like mountains or buildings. The observe-sqm-stepper.bash is able to extract latitude and longitude from the GPS. To be sure that the gpsd server will work properly, do the following steps.
sudo vim /etc/default/gpsd
# Default settings for the gpsd init script and the hotplug wrapper. # Start the gpsd daemon automatically at boot time START_DAEMON="true" # Use USB hotplugging to add new USB devices automatically to the daemon USBAUTO="true" # Devices gpsd should collect to at boot time. # They need to be read/writeable, either by user gpsd or the group dialout. DEVICES="/dev/ttyACM0" # Other options you want to pass to gpsd GPSD_OPTIONS="-F /var/run/gpsd.sock -D 5 -N -n"
Shutdown/reboot button
cd /home/sand/git/cosqm chmod a+x shutdown_button.py sudo cp shutdown_button.py /usr/local/bin sudo cp shutdown_button.service /etc/systemd/system sudo systemctl enable shutdown_button.service sudo systemctl start shutdown_button.service Real time clockWe are using the DS3231 RTC module to keep the date and time after powering off the system. In CoSQMv2, the RTC module is included with the UPS hat. Configure the I2C interfacesudo raspi-config This command will bring up the configuration tool; this tool is an easy way to make a variety of changes to your Raspberry Pi’s configuration. Today, however, we will only by exploring how to enable the I2C interface. Use the arrow keys to go down and select “3 Interfacing Options“. Once this option has been selected, you can press Enter.
sudo apt-get install python3-smbus i2c-tools
sudo i2cdetect -y 1 If you have successfully wired up your RTC circuit, you should see the ID #68 appear. This id is the address of the DS3231 RTC Chips. Once we have the Kernel driver up and running the tool will start to display UU instead, this is an indication that it is working as intended. Setting up the Raspberry Pi RTC TimeWith I2C successfully setup and verified that we could see our RTC circuit then we can begin the process of configuring the Raspberry Pi to use our RTC Chip for its time.
sudo vim /boot/config.txt
dtoverlay=i2c-rtc,ds3231 Once you have added the correct line for your device to the bottom of the file you can save and quit out of it by pressing Esc, then :wq and then Enter. With that change made we need to restart the Raspberry Pi, so it loads in the latest configuration changes.
sudo reboot
sudo i2cdetect -y 1 You should see a wall of text appear, if UU appears instead of 68 then we have successfully loaded in the Kernel driver for our RTC circuit. Now that we have successfully got the kernel driver activated for the RTC Chip and we know it’s communicating with the Raspberry Pi, we need to remove the fake hwclock package. This package acts as a placeholder for the real hardware clock when you don’t have one.
sudo apt-get -y remove fake-hwclock sudo update-rc.d -f fake-hwclock remove Now that we have disabled the fake-hwclock package we can proceed with getting the original hardware clock script that is included in Raspbian up and running again by commenting out a section of code.
sudo vim /lib/udev/hwclock-set If you upgrade the operating system, you may need to redo the following step.
Syncing time from the Pi to the RTC moduleNow that we have our RTC module all hooked up and Raspbian and the Raspberry Pi configured correctly we need to synchronize the time with our RTC Module. The reason for this is that the time provided by a new RTC module will be incorrect.
sudo hwclock -r
date
sudo hwclock -w
sudo hwclock -r You should hopefully now have a fully operational RTC module that is actively keeping your Raspberry Pi’s time correct even when it loses power or loses an internet connection. Set up the wifi access point
sudo apt install hostapd
sudo systemctl unmask hostapd sudo systemctl enable hostapd
sudo apt install dnsmasq
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent Set up the network routerThe Raspberry Pi will run and manage a standalone wireless network. It will also route between the wireless and Ethernet network, providing internet access to wireless clients when the wired network interface is connected to the web. Define the wireless interface IP configurationThe Raspberry Pi runs a DHCP server for the wireless network; this requires static IP configuration for the wireless interface (wlan0) in the Raspberry Pi. The Raspberry Pi also acts as the router on the wireless network. We give the router the first IP address in the network: 192.168.5.1.
sudo vim /etc/dhcpcd.conf
interface wlan0 static ip_address=192.168.5.1/24 nohook wpa_supplicant Enable routing and IP masqueradingThis section configures the Raspberry Pi to let wireless clients access computers on the main (Ethernet) network, and from there, to access the internet (if the wired interface is connected to the web). NOTE: If you prefer to block wireless clients from accessing the Ethernet network and the internet, skip this section.
sudo vim /etc/sysctl.d/routed-ap.conf
# https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md # Enable IPv4 routing net.ipv4.ip_forward=1 Enabling routing will allow hosts from network 192.168.5.0/24 to reach the LAN and the main router towards the internet. In order to allow traffic between clients on this foreign wireless network and the internet without changing the configuration of the main router, the Raspberry Pi can substitute the IP address of wireless clients with its own IP address on the LAN using a "masquerade" firewall rule. The main router will see all outgoing traffic from wireless clients as coming from the Raspberry Pi, allowing communication with the internet. The Raspberry Pi will receive all incoming traffic, substitute the IP addresses back, and forward traffic to the original wireless client.
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo netfilter-persistent save Filtering rules are saved to the directory /etc/iptables/. If in the future you change the configuration of your firewall, make sure to save the configuration before rebooting. Configure the DHCP and DNS services for the wireless networkThe DHCP and DNS services are provided by dnsmasq. The default configuration file serves as a template for all possible configuration options, whereas we only need a few. It is easier to start from an empty file.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig sudo vim /etc/dnsmasq.conf
interface=wlan0 # Listening interface dhcp-range=192.168.5.2,192.168.5.20,255.255.255.0,24h # Pool of IP addresses served via DHCP domain=wlan # Local wireless DNS domain address=/cosqm/192.168.5.1 # Alias for this router The Raspberry Pi will deliver IP addresses between 192.168.5.2 and 192.168.5.20, with a lease time of 24 hours, to wireless DHCP clients. There are many more options for dnsmasq; see the default configuration file (/etc/dnsmasq.conf) or the online documentation for details. Ensure wireless operationCountries around the world regulate the use of telecommunication radio frequency bands to ensure interference-free operation. The Linux OS helps users comply with these rules by allowing applications to be configured with a two-letter "WiFi country code", e.g. US for a computer used in the United States. In the Raspberry Pi OS, 5 GHz wireless networking is disabled until a WiFi country code has been configured by the user, usually as part of the initial installation process.
sudo rfkill unblock wlan This setting will be automatically restored at boot time. We will define an appropriate country code in the access point software configuration, next. Configure the access point
sudo vim /etc/hostapd/hostapd.conf
country_code=CA interface=wlan0 ssid=cosqm hw_mode=g channel=7 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=cosqmwifi wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP Note the line country_code=CA: it configures the computer to use the correct wireless frequencies in Canada. Adapt this line and specify the two-letter ISO code of your country. See Wikipedia for a list of two-letter ISO 3166-1 country codes. To use the 5 GHz band, you can change the operations mode from hw_mode=g to hw_mode=a. Possible values for hw_mode are: a = IEEE 802.11a (5 GHz) b = IEEE 802.11b (2.4 GHz) g = IEEE 802.11g (2.4 GHz) ad = IEEE 802.11ad (60 GHz) Run your new wireless access point
sudo systemctl reboot Once your Raspberry Pi has restarted, search for wireless networks with your wireless client. The network SSID cosqm should now be present, and it should be accessible with the password cosqmwifi. If SSH is enabled on the Raspberry Pi, it should be possible to connect to it from your wireless client as follows, assuming the pi account is present: ssh sand@192.168.5.1 or ssh sand@cosqm Configuring the wired IP address of the SQMThe SQM-LE does not come shipped with a fixed IP address, so it may be necessary to fix that address before installing the unit into such a direct connection system without DHCP server software running.
IP Address: 192.168.0.101 Subnet Mask: 255.255.255.0 Default Gateway: field may be left blank.
Configure the network parameters of the raspberry pi
sudo vim /etc/dhcpcd.conf
# Example static IP configuration: interface eth0 static ip_address=192.168.0.100/24 static ip6_address=fd51:42f8:caae:d92e::ff/64 static routers=192.168.0.1 static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1 UPS hatThe UPS is a power battery backup. It will protect the system against power fluctuations and will allow the system to continue its ongoing tasks during short power failure.
sudo vim /etc/rc.local
bash /usr/local/bin/ups.sh & System rebootNow all the relevant software are installed, in order to activate all newly added functionalities, restart the computer. Optional stuffD-LINK router configurationThe router is useful when CoSQM is connected to the Internet and that the appropriate ports are open to take control of the instrument remotely. If the system is not connected to the internet, a simple network switch or hub is sufficient.
|