Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Last active October 18, 2016 13:10
Show Gist options
  • Save MarcScott/a843c4dd4dfa3934b3de7b1fc0beadf8 to your computer and use it in GitHub Desktop.
Save MarcScott/a843c4dd4dfa3934b3de7b1fc0beadf8 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo 'Please ensure your Weather Station HAT is connected to you Raspberry Pi, with the battery installed.'
echo 'Please ensure your Raspberry Pi is connected to the Internet'
## Check ready to start
echo 'Press any key to continue'
read -n 1 -s
## Update and upgrade - especially important for old NOOBS installs and I2C integration
sudo apt-get update && sudo apt-get upgrade -y
##E nable I2C
sudo raspi-config nonint do_i2c 0
## Update config files.
echo "dtoverlay=w1-gpio" | sudo tee -a /boot/config.txt
echo "dtoverlay=pcf8523-rtc" | sudo tee -a /boot/config.txt
echo "i2c-dev" | sudo tee -a /etc/modules
echo "w1-therm" | sudo tee -a /etc/modules
## Check the RTC exists
if ls /dev/rtc** 1> /dev/null 2>&1; then
echo "RTC found"
else
echo "No RTC found - please follow manual setup to Troubleshoot."
exit 1
fi
## Initialise RTC with correct time
echo "The current date and time set is:"
date
read -r -p "Is this correct [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]; then
sudo hwclock -w
else
read -p "Enter todays date and time (yyyy-mm-dd hh:mm:ss): " user_date
sudo hwclock --set --date="$user_date" --utc #set hardware clock
fi
#update system clock
sudo hwclock -s
#Update hwclock config
sudo perl -pi -e 's/systz/hctosys/g' /lib/udev/hwclock-set
#Remove hwc package
sudo update-rc.d fake-hwclock remove
sudo apt-get remove fake-hwclock -y
## Install com tools
sudo apt-get install i2c-tools python-smbus telnet -y
## Set password for mysql-server
echo 'You need to choose a password for your database'
echo 'Please remember your password'
read -p "Choose a database password: " pass
## install mysql-server without prompting for password, and pass in password chosen earlier
sudo debconf-set-selections <<EOT
mysql-server mysql-server/root_password password $pass
mysql-server mysql-server/root_password_again password $pass
EOT
DEBIAN_FRONTEND=noninteractive sudo apt-get install mysql-server -y
## Install additional database software
sudo apt-get install apache2 python-mysqldb php5 libapache2-mod-php5 php5-mysql -y
## Create a database and weather table
echo 'Creating Database'
mysql --user=root --password=$pass <<EOT
CREATE DATABASE weather;
USE weather;
CREATE TABLE WEATHER_MEASUREMENT(
ID BIGINT NOT NULL AUTO_INCREMENT,
REMOTE_ID BIGINT,
AMBIENT_TEMPERATURE DECIMAL(6,2) NOT NULL,
GROUND_TEMPERATURE DECIMAL(6,2) NOT NULL,
AIR_QUALITY DECIMAL(6,2) NOT NULL,
AIR_PRESSURE DECIMAL(6,2) NOT NULL,
HUMIDITY DECIMAL(6,2) NOT NULL,
WIND_DIRECTION DECIMAL(6,2) NULL,
WIND_SPEED DECIMAL(6,2) NOT NULL,
WIND_GUST_SPEED DECIMAL(6,2) NOT NULL,
RAINFALL DECIMAL (6,2) NOT NULL,
CREATED TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ( ID )
);
EOT
## Get the weather station python files
cd ~
git clone https://github.com/raspberrypi/weather-station.git
## Setup rc.local to start weatherstaion daemon
sudo sed -i '/exit 0/d' /etc/rc.local
echo 'echo "Starting Weather Station daemon..."' | sudo tee -a /etc/rc.local
echo '/home/pi/weather-station/interrupt_daemon.py start' | sudo tee -a /etc/rc.local
echo 'exit 0' | sudo tee -a /etc/rc.local
## Add in correct sql credentials
cd weather-station
cat <<EOT > credentials.mysql
{
"HOST": "localhost",
"USERNAME": "root",
"PASSWORD": "$pass",
"DATABASE": "weather"
}
EOT
## Alter crontab for periodic uploads
crontab < crontab.save
## Add credentials for weather station
echo 'You should have registered you weather station at'
echo 'https://apex.oracle.com/pls/apex/f?p=81290:LOGIN_DESKTOP:0:::::&tz=1:00'
echo 'You should have a Weather Station Name'
echo 'You should have a Weather Station Key'
read -p "Please type in your Weather Station Name: " name
read -p "Please type in your Weather Station Key: " key
cat <<EOT > credentials.oracle
{
"WEATHER_STN_NAME": "$name",
"WEATHER_STN_PASS": "$key"
}
EOT
sudo reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment