6. MoonShineP: Software setup
Contents
6. MoonShineP: Software setup#
Setting up a new Raspberry Pi#
On a computer go to the Raspberry Pi website https://www.raspberrypi.com/software/ to download and install the Raspberry Pi Imager.
Insert the micro SD card into the comptuer.
Select your Pi model. MoonShineP has been tested and working on Pi model 4 and 5.
Select the “Raspberry Pi OS (Legacy, 32-bit)” for installation.
Raspberry Pi OS (Legacy, 32-bit)#
You must enter “pi” as the username, so that all the filepaths are correct. You can choose your own password.
Put “pi” as username.#
Ignore all other configurations by pressing Next.
One the initialization of the SD card is complete, put it in the Pi.
Connect power, monitor, mouse, and keyboard to the Raspberry Pi and boot it up.
Install Python 3.9.9.
Attention
MoonShineP DOES NOT SUPPORT OTHER VERSIONS OF PYTHON
Reference: How to Update Python on Raspberry Pi
Enter the following sequence of commands in the terminal:
```
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz
```
```{attention}
If you recieve `The certificate of 'www\.python.org' is not trusted` after running the above line, resolve it by running:
`sudo apt update`
`sudo apt install --reinstall ca-certificates`
`sudo update-ca-certificates`
```
```
tar -zxvf Python-3.9.9.tgz
```
```
cd Python-3.9.9
```
```
./configure --enable-optimizations
```
```
sudo make altinstall
```
```
cd /usr/bin
```
```
sudo rm python
```
```
sudo ln -s /usr/local/bin/python3.9 python
```
Check Python version is version (3.9.9) by entering:
python --version
Attention
It must be Python 3.9.9
Reboot the Raspberry Pi.
Modify the Raspberry Pi to run the ws2811 library.
Reference: Connect and Control WS2812 RGB LED Strips via Raspberry Pi
In terminal, enter the following sequence of commands:
sudo apt-get update
sudo apt-get install gcc make build-essential python3-dev git scons swig
Confirm with Y
This following command line will open a file editor. To deactivate audio output, edit the
.conffile by entering the following command:sudo nano /etc/modprobe.d/snd-blacklist.conf
A new terminal window will open. In this window add the following line text:
blacklist snd_bcm2835
Then save file with CTRL + O.
And close the editor with CTRL + X.
We also need to edit another file. To do so, enter:
sudo nano /boot/firmware/config.txt
When the editor is opened, comment out the following line with a # at the beginning:
dtparam=audio=on
i.e.,
#dtparam=audio=onThen save file with CTRL + O.
And close the editor with CTRL + X.
Reboot the Raspberry Pi by entering:
sudo reboot
Download rpi_ws281x library by entering:
cdgit clone https://github.com/jgarff/rpi_ws281x
Next, compile the library for Python.
In terminal, enter the following commands:
cdcd rpi_ws281x/
sudo apt install python3-pip
sudo /usr/bin/python3 -m pip install --break-system-packages rpi-ws281x
Now the Raspberry Pi should be ready to control the SK6812 LED strips.
Setup real time clock (RTC) and time#
A real-time clock (RTC) module is recommended for MoonShineP, especially when operating without internet access. The RTC maintains time during power outages. We recommend disabling automatic internet time synchronization and using a fixed timezone without daylight saving time (DST) to ensure that the Raspberry Pi’s clock remains consistent with the MoonShineR-generated schedules.
Install RTC module DS3231 as described in Assemble Raspberry Pi and LED strips.
On the task bar of the main Linux window of Raspberry Pi, go to the Start menu (top left button) and enable the RTC module by selecting the following options: > Preferences > Raspberry Pi Configuration > Interfaces > I2C Enable > OK
Open the Raspberry Pi configuration file, in terminal enter:
sudo nano /boot/firmware/config.txt
Add the following lines under
[all], and save the file by pressingCtrl+O, Enter, thenCtrl+X:dtparam=rtc=off #This line is only required for Pi 5. Older models do not need this line. dtoverlay=i2c-rtc,ds3231
Reboot Pi:
sudo reboot
Disables automatic network time synchronization when connected to the internet:
sudo timedatectl set-ntp false
Set a fixed EST timezone without DST:
sudo timedatectl set-timezone Etc/GMT+5
Set the Raspberry Pi’s system clock to the current local standard time (without daylight saving time, even if DST is currently in effect) by entering the following command in the terminal:
sudo date -s 'YYYY-MM-DD hh:mm:ss'
Change the above to the user’s current time, for example ‘2022-01-01 01:01:00’.
Copy the time from the Raspberry Pi system to the Hardware RTC by entering:
sudo hwclock -w
To check if RTC is working, enter:
sudo hwclock -rv timedatectl cat /sys/class/rtc/rtc0/name
The RTC time will be reported.
Test whether the RTC maintains time after a power outage:
Disconnect the Raspberry Pi from the internet and shut it down.
Once the Raspberry Pi has shut down, disconnect its power supply for at least 5 minutes.
Reconnect the power supply and allow the Raspberry Pi to boot up, without connecting to the internet.
Check the system time and RTC time by entering:
timedatectl sudo hwclock -rvs
Desktop folders#
Download the control_moon and control_sun folders (download in Download files).
control_moon contains
moonshine_moon.pycontrol_sun contains
moonshine_sun.py
Move the two folders to the Raspberry Pi Desktop.
Note
In
moonshine_moon.py, the line of LED_PIN = 18 specifies the communication with the moonlight LED strip through the GPIO 18. Inmoonshine_sun.py, the line of LED_PIN = 21 controls the sunlight/twilight LED strip through GPIO 21 instead.In additional, download
clear_moon.pyandclear_sun.pyput them directly on the Raspberry Pi Desktop.
Setting up systemd service#
Running MoonShineP using the Linux systemd service manager allows it to recover and resume light re-creation automatically, in the unlikely event of a crash.
Create a new service file.
In terminal, enter:
sudo nano /etc/systemd/system/moonshine_moon.service
The file editor will open up a blank file, paste in the following lines:
[Unit] Description=relaunch moonshine_moon.py when crashed [Service] User=root Group=root Type=simple ExecStart=/usr/bin/python3 /home/pi/Desktop/control_moon/moonshine_moon.py Restart=always RestartSec=3 [Install] WantedBy=default.target
Note
The line of “ExecStart=…” instructs system service to locate the MoonShineP python script in the correct directory. Restart=always and RestartSec=3 are configured to restart
moonshine_moon.pywithin 3 seconds upon closing (i.e., the script crashing).Then save file with CTRL + O. CTRL + X to close the editor.
When prompted for the file name, confirm that it is
moonshine_moon.serviceand save.Refresh the system service files. It may ask for the username and password (if so enter the username and password). Enter in terminal:
sudo systemctl daemon-reload
(Optional) If the user is recreating sunlight and twilight as well, repeat the above steps, from 1 to 4, to make another service for control_sun/moonshine_sun.py
For step 1, use a different file name:
sudo nano /etc/systemd/system/moonshine_sun.service
For step 2, replace the line of
ExecStart=...withExecStart=sudo /usr/bin/python3 /home/pi/Desktop/control_sun/moonshine_sun.py
For step 3, save the file with name
moonshine_sun.serviceNote
Every time a
.servicefile is edited, it requires a refresh (see Step 4).
Setting LED numbers in moonshine_moon.py and moonshine_sun.py#
Recall that in 2. MoonShineR: Moonlight scheduler and 3. MoonShineR: Sunlight/twilight scheduler, the user was required to enter the specifications of the LED array (diode_per_strip and strip_count) into the R program. This was essential for generating the schedule
.csv. It is important that LED_COUNT (below) in MoonShineP python script represents the product of diode_per_strip (from MoonShineR) and strip_count (from MoonShineR).
To calculate the total number of LEDs, diode_per_strip should be multiplied by strip_count. E.g., 144 x 4 = 576.
Edit the line of “LED_COUNT” in
moonshine_moon.pyandmoonshine_sun.pywith the respective total number of LEDs for each array (seeled_count).
Specify the line of “LED_COUNT” in
moonshine_sun.pyto 576 when using four daisy-chained 144 LED strips.#Save the file.
Note
When changing the number of LEDs number in an array, change the corresponding settings in MoonShineR schedulers and the MoonShineP python file. This will, of course, also require a recalibration of the illuminance.