Skip to content

Commit

Permalink
Added WittyPi 4 to webinterface and fixed timesync for WittyPi4
Browse files Browse the repository at this point in the history
  • Loading branch information
JavanXD committed Dec 19, 2023
1 parent 1100a02 commit d261524
Show file tree
Hide file tree
Showing 8 changed files with 13,182 additions and 62 deletions.
12 changes: 7 additions & 5 deletions backend/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function clean($string) {
$voltageStateFile = $GLOBALS['scriptsFolder']."/.isLowVoltage";
clear_file($voltageStateFile);

$wittyPi_version = (INT)$postJson["wittyPi"]["version"];

if ($postJson["wittyPi"]["enabled"] === true
&& isset($postJson["wittyPi"]["normal"]["enabled"])
&& $postJson["wittyPi"]["normal"]["enabled"] === true
Expand All @@ -104,24 +106,24 @@ function clean($string) {
file_put_contents($wittyPiFile, $postJson["wittyPi"]["normal"]["schedule"]);

// set WittyPi (dont wait exec to finish)
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 1 > /dev/null 2>&1 &");
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 1 $wittyPi_version > /dev/null 2>&1 &");
} else {

// clear schedule.wpi
emptyFile($wittyPiFile);
$postJson["wittyPi"]["normal"]["enabled"] = false;

// reset wittyPi schedule
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 0 > /dev/null 2>&1 &");
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 0 $wittyPi_version > /dev/null 2>&1 &");
}

// set WittyPi dummyload
if ($postJson["wittyPi"]["enabled"] == true && isset($postJson["wittyPi"]["version"]) && isset($postJson["wittyPi"]["dummyload"]) && $postJson["wittyPi"]["version"] === 3) {
if ($postJson["wittyPi"]["enabled"] == true && isset($postJson["wittyPi"]["version"]) && isset($postJson["wittyPi"]["dummyload"]) && $postJson["wittyPi"]["version"] >= 3) {
if ((INT)$postJson["wittyPi"]["dummyload"] > 0) {
$dummyload = (INT)$postJson["wittyPi"]["dummyload"];
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 2 $dummyload > /dev/null 2>&1 &");
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 2 $wittyPi_version $dummyload > /dev/null 2>&1 &");
} else {
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 3 > /dev/null 2>&1 &");
shell_exec("sudo sh ".$GLOBALS['shellDir']."/change_wittypi.sh 3 $wittyPi_version > /dev/null 2>&1 &");
}
}

Expand Down
39 changes: 27 additions & 12 deletions backend/shell-scripts/change_wittypi.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#! /bin/bash

if [ -z "$1" ] ; then
echo "Missing argument."
echo "Missing argument mode."
exit 1
elif [ -z "$2" ] ; then
echo "Missing argument wittyPi version."
exit 1
else
mode=$1
wittyPi_version=$2

# detect wittyPi version
# detect witty Pi install path
if [ -e /home/pi/wittyPi ] ; then
# WittyPi 2 or Mini
wittyPi=2
# Only WittyPi 2 or Mini use the capital letter in wittyPi
wittyPi_version=2 # user selected wrong WittyPi version?
path='/home/pi/wittyPi'
elif [ -e /home/pi/wittypi ] ; then
# WittyPi 3 or 3 Mini
wittyPi=3
path='/home/pi/wittypi'
else
echo "Error: No WittyPi installation found."
Expand All @@ -25,16 +27,24 @@ else

if [ $mode -eq 0 ] ; then
# mode=0: clear current schedule
if [ $wittyPi -eq 2 ] ; then
if [ $wittyPi_version -eq 2 ] ; then
(sleep 6; echo 7; echo 4; echo 8) | sudo ./wittyPi.sh
elif [ $wittyPi -eq 3 ] ; then
elif [ $wittyPi_version -eq 3 ] ; then
(sleep 6; echo 10; echo 6; echo 11) | sudo ./wittyPi.sh
elif [ $wittyPi_version -eq 4 ] ; then
# reset all startup and shutdown schedules as well as all other thresholds in Witty Pi 4
(sleep 3; echo 12; echo 8; echo 13) | sudo ./wittyPi.sh
fi
elif [ $mode -eq 1 ] ; then
# mode=1: transfer local schedule to the wittyPi module and run it
sudo cp /var/www/html/backend/schedule.wpi $path/schedule.wpi
# Sync time
sudo ./syncTime.sh
if [ $wittyPi_version -eq 4 ] ; then
# Synchronize with network time (only in Witty Pi 4)
(sleep 3; echo 3; echo 13) | sudo ./wittyPi.sh
else
# Sync time
sudo ./syncTime.sh
fi
# set schedule script
sudo ./runScript.sh
elif [ $mode -eq 2 ] ; then
Expand All @@ -43,8 +53,13 @@ else
echo "Missing dummyload argument."
exit 1
else
dummyload=$2 # default: 10-15
(sleep 6; echo 9; echo 5; echo $dummyload; echo 11) | sudo ./wittyPi.sh
dummyload=$3 # default: 10-15
if [ $wittyPi_version -eq 3 ] ; then
(sleep 6; echo 9; echo 5; echo $dummyload; echo 11) | sudo ./wittyPi.sh
else
# in Witty Pi 4 a bit hidden (11. View/change other settings...)
(sleep 3; echo 11; echo 5; echo $dummyload; sleep 1; echo 13) | sudo ./wittyPi.sh
fi
fi
elif [ $mode -eq 3 ] ; then
# reset dummyload to default
Expand Down
4 changes: 4 additions & 0 deletions backend/shell-scripts/installWittyPi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ else
sh /var/www/html/backend/shell-scripts/installWittyPi3.sh
# make sure the schedule script gets executed after the time synchronization is fully done
sed -i 's/sleep 3/sleep 17/g' /home/pi/wittypi/daemon.sh
elif [ $wittyPi -eq 4 ] ; then
sh /var/www/html/backend/shell-scripts/installWittyPi4.sh
# make sure the schedule script gets executed after the time synchronization is fully done
sed -i 's/sleep 3/sleep 17/g' /home/pi/wittypi/daemon.sh
fi

fi
Expand Down
147 changes: 147 additions & 0 deletions backend/shell-scripts/installWittyPi4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
[ -z $BASH ] && { exec bash "$0" "$@" || exit; }
#!/bin/bash
# file: install.sh
#
# This script will install required software for Witty Pi.
# It is recommended to run it in your home directory.
#

# check if sudo is used
if [ "$(id -u)" != 0 ]; then
echo 'Sorry, you need to run this script with sudo'
exit 1
fi

# target directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/wittypi"

# error counter
ERR=0

# config file
if [ "$(lsb_release -si)" == "Ubuntu" ]; then
# Ubuntu
BOOT_CONFIG_FILE="/boot/firmware/usercfg.txt"
else
# Raspberry Pi OS ("$(lsb_release -si)" == "Debian") and others
BOOT_CONFIG_FILE="/boot/config.txt"
fi

echo '================================================================================'
echo '| |'
echo '| Witty Pi Software Installation Script |'
echo '| |'
echo '================================================================================'

# enable I2C on Raspberry Pi
echo '>>> Enable I2C'
if grep -q 'i2c-bcm2708' /etc/modules; then
echo 'Seems i2c-bcm2708 module already exists, skip this step.'
else
echo 'i2c-bcm2708' >> /etc/modules
fi
if grep -q 'i2c-dev' /etc/modules; then
echo 'Seems i2c-dev module already exists, skip this step.'
else
echo 'i2c-dev' >> /etc/modules
fi

i2c1=$(grep 'dtparam=i2c1=on' ${BOOT_CONFIG_FILE})
i2c1=$(echo -e "$i2c1" | sed -e 's/^[[:space:]]*//')
if [[ -z "$i2c1" || "$i2c1" == "#"* ]]; then
echo 'dtparam=i2c1=on' >> ${BOOT_CONFIG_FILE}
else
echo 'Seems i2c1 parameter already set, skip this step.'
fi

i2c_arm=$(grep 'dtparam=i2c_arm=on' ${BOOT_CONFIG_FILE})
i2c_arm=$(echo -e "$i2c_arm" | sed -e 's/^[[:space:]]*//')
if [[ -z "$i2c_arm" || "$i2c_arm" == "#"* ]]; then
echo 'dtparam=i2c_arm=on' >> ${BOOT_CONFIG_FILE}
else
echo 'Seems i2c_arm parameter already set, skip this step.'
fi

miniuart=$(grep 'dtoverlay=pi3-miniuart-bt' ${BOOT_CONFIG_FILE})
miniuart=$(echo -e "$miniuart" | sed -e 's/^[[:space:]]*//')
if [[ -z "$miniuart" || "$miniuart" == "#"* ]]; then
echo 'dtoverlay=pi3-miniuart-bt' >> ${BOOT_CONFIG_FILE}
else
echo 'Seems setting Pi3 Bluetooth to use mini-UART is done already, skip this step.'
fi

miniuart=$(grep 'dtoverlay=miniuart-bt' ${BOOT_CONFIG_FILE})
miniuart=$(echo -e "$miniuart" | sed -e 's/^[[:space:]]*//')
if [[ -z "$miniuart" || "$miniuart" == "#"* ]]; then
echo 'dtoverlay=miniuart-bt' >> ${BOOT_CONFIG_FILE}
else
echo 'Seems setting Bluetooth to use mini-UART is done already, skip this step.'
fi

core_freq=$(grep 'core_freq=250' ${BOOT_CONFIG_FILE})
core_freq=$(echo -e "$core_freq" | sed -e 's/^[[:space:]]*//')
if [[ -z "$core_freq" || "$core_freq" == "#"* ]]; then
echo 'core_freq=250' >> ${BOOT_CONFIG_FILE}
else
echo 'Seems the frequency of GPU processor core is set to 250MHz already, skip this step.'
fi

if [ -f /etc/modprobe.d/raspi-blacklist.conf ]; then
sed -i 's/^blacklist spi-bcm2708/#blacklist spi-bcm2708/' /etc/modprobe.d/raspi-blacklist.conf
sed -i 's/^blacklist i2c-bcm2708/#blacklist i2c-bcm2708/' /etc/modprobe.d/raspi-blacklist.conf
else
echo 'File raspi-blacklist.conf does not exist, skip this step.'
fi

# install i2c-tools
echo '>>> Install i2c-tools'
if hash i2cget 2>/dev/null; then
echo 'Seems i2c-tools is installed already, skip this step.'
else
apt-get install -y i2c-tools || ((ERR++))
fi

# make sure en_GB.UTF-8 locale is installed
echo '>>> Make sure en_GB.UTF-8 locale is installed'
locale_commentout=$(sed -n 's/\(#\).*en_GB.UTF-8 UTF-8/1/p' /etc/locale.gen)
if [[ $locale_commentout -ne 1 ]]; then
echo 'Seems en_GB.UTF-8 locale has been installed, skip this step.'
else
sed -i.bak 's/^.*\(en_GB.UTF-8[[:blank:]]\+UTF-8\)/\1/' /etc/locale.gen
locale-gen
fi

# install wittyPi
if [ $ERR -eq 0 ]; then
echo '>>> Install wittypi'
if [ -d "wittypi" ]; then
echo 'Seems wittypi is installed already, skip this step.'
else
wget https://www.uugear.com/repo/WittyPi4/LATEST -O wittyPi.zip || ((ERR++))
unzip wittyPi.zip -d wittypi || ((ERR++))
cd wittypi
chmod +x wittyPi.sh
chmod +x daemon.sh
chmod +x runScript.sh
chmod +x beforeScript.sh
chmod +x afterStartup.sh
chmod +x beforeShutdown.sh
sed -e "s#/home/pi/wittypi#$DIR#g" init.sh >/etc/init.d/wittypi
chmod +x /etc/init.d/wittypi
update-rc.d wittypi defaults || ((ERR++))
touch wittyPi.log
touch schedule.log
cd ..
chown -R $SUDO_USER:$(id -g -n $SUDO_USER) wittypi || ((ERR++))
sleep 2
rm wittyPi.zip
fi
fi


echo
if [ $ERR -eq 0 ]; then
echo '>>> All done. Please reboot your Pi :-)'
else
echo '>>> Something went wrong. Please check the messages above :-('
fi
Loading

0 comments on commit d261524

Please sign in to comment.