UPDATED Feb 27, 2017


The base station is the central place where data is logged and served out to different places. The Pi will will be connected to the LAN and Internet via the USB WiFi adapter. This is how the base station operates:


Lets gather some tools:

This will make it easy to transfer files from our 'puter to the Pi.

We'll need to SSH into the Pi and do some CLI kungfu!

IMHO the best text editor out there. We'll use this to write our PHP and Python scripts.

Get a new Gmail account.
This email address will be used by the Pi to send out alerts. For security reasons, I recommend you setup a new email address just to be used for the Plant Friends system.


Raspian OS
Download and write the OS onto your SD card. As of this writing (Feb 27, 2017), the latest version is Raspian Jessie.

The Raspberry Pi website already have amazing guides written up on installing Raspian:

After you have written the Raspian image onto the SD card,

For Raspberry Pi 2: Plug in the Ethernet cable AND the USB WiFi adapter to the Pi. The Raspian OS has SSH enabled by default and we will setup everything via the shell.

Go in your router and find your Pi's IP address. Log into it using PuTTY using the SSH setting.

Lets run the config utility. In the shell, type:

sudo raspi-config

NOTE: If you prefer, go ahead and plug in a monitor and keyboard to the Pi. When you first boot up your Pi, it will bring you to the raspi-config utility.

Make sure you set the following items (if you get dropped into the shell, type the above command to get back into the utility).

Set the following options in the utility:

1. Expand Filesystem
Select this option so we can utilize the whole SD card.

2. Change User Password
Change the default password. Pick something secure!

3. Timezone
This is very important as the database will need the date/time to time-stamp our sensor node data. The Pi will automatically sync the clock to the public time servers. Change it under “Internationalization Options”.

4. Hostname
Give your Pi a new hostname like “plantfriends”. This is under “Advanced Options”.

5. Memory
Since we are using the Pi as a server, we can divert memory away from the GPU and use it for system memory. Under “Advanced Options” choose “Memory Split”. Type in 16.

6. Overclocking
There is a nice speed improvement when the Pi is overclocked. I won't be covering this here but you should do some research before playing with this.

After you are done with the config utility, reboot the system:

sudo reboot

Log back into the system and update the system:

sudo apt-get update

sudo apt-get upgrade

Lets enable the WIFI adapter. Open up the network configuration file:

sudo nano /etc/network/interfaces

You should see something like this:


Modify it to look like this:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

auto wlan0
iface wlan0 inet static
wireless-power off

The # sign comments out the lines. Modify the relevant fields.

WPA-SSID is your WiFI router's name. Insert it there.

WPA-PSK is your WiFi password.

ADDRESS is the static IP address you want the Pi to have.

GATEWAY is your router's IP address.

Press CTRL+X and save your changes. The version of Raspian as of this writing (Raspian June 2014 release) does some weird stuff to the WIFI connection. To fix, do the following:

sudo rm /etc/udev/rules.d/70-persistent-net.rules

On the next bootup, your Pi will connect to your WiFi network automatically. It will have an IP address of the one you specified earlier. Reboot so our changes take effect:

sudo reboot

Log in to the Pi using the WIFI IP address you specified earlier. Does it work? Yes? Awesome! Unplug that Ethernet cable.

Test the WIFI connection to make sure it is connected to the Internet:


You should get a response back from google.

To ensure the Pi's WiFi connection remains stable, we'll make it reconnect if the connection drops. Type in these commands:

cd /etc/ifplugd/action.d/

sudo mv ifupdown ifupdown.original

sudo cp /etc/wpa_supplicant/ ./ifupdown

sudo reboot

WARNING: This will render the Ethernet port unusable! But since we are using the WIFI only, it doesn't matter.


Lets install some software onto the Pi.


Lighttpd is a light weight web server and ideal for the Raspberry Pi. This will be used to run our PHP scripts for managing the database and serve data to the Plant Friends Android app.

sudo apt-get -y install lighttpd

Check the server is installed and running correctly:

ps -u www-data u

Change the permissions on the www directory to allow pi user to write to it. Change the directory owner and group:

sudo chown www-data:www-data /var/www

Allow the group to write to the directory:

sudo chmod 775 /var/www

Add the pi user to the www-data group:

sudo usermod -a -G www-data pi

By default web servers communicate over port 80. If you want to change the port number that Lighttpd uses you can easily change it by editing the lighttpd.conf file :

sudo nano /etc/lighttpd/lighttpd.conf

Then change the “server port” line and pick a port number:

server.port = 6200

Exit nano and restart the server:

sudo /etc/init.d/lighttpd restart

You can now access your web server using: You should be able to see a lighttpd place holder page!


We will be logging all of our sensor node data into a MySQL database so lets install that.

sudo apt-get -y install mysql-server

During the install, it will ask you to create a password for the mySQL root user. Pick something secure and note it down because we'll need this later on!


PHP will enable us to write PHP scripts for the webserver. We'll install the mySQL module so we can query the database via PHP.

sudo apt-get -y install php5-cgi php5-mysql


We'll be using Python scripts to insert sensor node data to the database and send out out alerts.

sudo apt-get -y install python-mysqldb

sudo apt-get -y install python-serial

sudo apt-get -y install python-setuptools


This is a webgui written in PHP will allow us to administer the database via a web gui.

sudo apt-get -y install phpmyadmin

The installation script will ask you if you want to auto configure for apache or lighttpd. Choose lighttpd.


During the install, it will ask you about configuring the database for phpmyadmin with dbconfig-common. Choose yes.


After that, it will also ask your for the password to the mySQL server. This is the password you created when you installed mySQL earlier!

This is asking you to create a password for phpmyadmin. Pick something secure and note it down!


After the installation, you might encounter an error. To fix it, type:

sudo apt-get upgrade

Another phpmyadmin configuration dialogue will come up. (weird.). After this, phpmyadmin will be done configuring itself. To check phpmyadmin is working, point your browser to:


Login is “root”. Password is the one you created for mySQL during install.


Supervisor will enable us to run our Python scripts as a daemon.

sudo easy_install supervisor

Generate a configuration file for supervisord:

sudo echo_supervisord_conf > supervisord.conf

Then move it to /etc:

sudo mv supervisord.conf /etc

We'll need to make supervisord start on boot. In my source files, find supervisord startup script and use WinSCP to transfer the file to /home/pi on the Pi. Go to your console. Make sure you are at /home/pi.

Set permission on the file we just uploaded.

sudo chmod a+x supervisord

Move it to the right place:

sudo mv supervisord /etc/init.d/

This will tell the system to start supervisord on startup.

sudo update-rc.d supervisord defaults


Raspian enables the serial console by default. In order for us to use the serial port to talk to the Moteino, we'll need to disable the console. Luckily, someone wrote a script to make the whole process convenient.


Follow the instructions on how to install and use the script!


All the required software is now installed. Lets setup the Plant Friends scripts. MySQL is a powerful relational database used by many web applications. Plant Friends uses it to store information about each sensor node and all the sensor data. Lets set that up: In the shell type:

mysql -u root -p

It will ask you for your 'root' password. It is the password we created during install. You should now be in the mysql shell. Lets see what's in our database:


Lets create our database:

CREATE DATABASE plantfriendsdb;

Look at what databases we have again. You should see the name of the database we just created. Create a new user that can access this database:

CREATE USER 'plantuser'@'localhost' IDENTIFIED BY 'passwordhere';

You should come up with a secure password for this user! Note this down because we'll need it later. Select the plantfriendsdb database:

USE plantfriendsdb;

Grant all privileges to 'plantuser;:

GRANT ALL ON plantfriendsdb.* TO 'plantuser'@'localhost';

Type exit to exit. :)


Now we have an empty database ready for use. Lets create the tables where we are going to store the data. We'll be using a Python script to do that.

Load up in your favorite text editor. I recommend notepad++. Change the user name and password to what you used to create the plantfriendsdb database earlier. Save the file and upload it to the Pi to your home folder via WinSCP.

Execute the script in the shell:


This will create the node index table with the relevant columns. Fire up your web browser and point it to


You should will see the plantfriendsdb database. Click on it and you will see one table called NodeIndex. Inside you will see the columns. The Plant Friends system utilizes a single python script to handle all the incoming sensor data and the data insertion into the database as well as sending out alerts.

Open and edit the database user name and password. Enter the new Plant Friends Gmail account info you created earlier. Don't forget to change the recipient email address.

In order for the script to run continuously, we will set it up to run as a daemon. On the pi, in your home dir, create a new directory called “plantfriends”. Upload into that dir.

To daemonize the script, we'll use supervisord. Open the supervisor config file:

sudo nano /etc/supervisord.conf

At the bottom of the file, add an entry to point to the Plant Friends python script:

command=/usr/bin/python /home/pi/plantfriends/

On the next reboot, supervisord will start the script automatically in the background. It will continuously check the serial port for any incoming data. To allow a sensor node to enter its' data into the database, we will need to register it manually into the NodeIndex table.


We'll use PHP script to help us with that. In my source under /www dir, open up config.php. Edit the database user name and password accordingly. When you are done, upload all the files to /var/www on the Pi. You should delete the lighttpd.default.html file while you are at it.

Point your browser to http://YOUR_RASPI_ADDRESS/admin.php. Add a new sensor node. The newly added sensor node should show up on the top of the page. The NodeID is sorted in ascending order. Remember ID 1 is reserved for the gateway.


The Pi is all setup ready to receive data from the serial port. The only thing missing now is the gateway Moteino that is responsible for receiving the wireless data and passing it to the Pi. Take your gateway Moteino and solder the straight male headers on to it like what we did with the sensor node. Solder a LED with a resistor to D5. Open up gateway.ino and upload it to the Moteino.

Hook up the gateway Moteino to the Pi. You'll need 4 wires.

Moteino => RPI

VIN => 5v PIN2



TX => RX PIN10

Here is a diagram and picture for the Raspberry Pi GPIO header:


We are stealing 5v from the Pi to power the Moteino. The TX pin on the Pi should go into the RX pin on the Moteino and vice versa.

Lets reboot the Pi so the master python script loads up.

sudo reboot


You can now upload the sensor_node.ino final into the sensor node. This is has the same as the test file but with the serial print code commented out and the sleep cycle at 450 (1 reading per hour). For testing purposes you can set it to something low like 8 ( 64 seconds ).

Put some batteries into the sensor node. It should now blink and start transmitting data to the base station. The LED connected to the gateway should blink when wireless data is received.

Point your browser to http://YOUR_RASPI_ADDRESS/phpmyadmin and go into the plantfriends database. You should now see a new table has been automatically created derived from the node name and node ID. Click on the table and you will see the first set of sensor data have been inserted!



The gateway Moteino receives data from the sensor nodes and passes it straight to the serial port unaltered. There is a lot of potential for the gateway Moteino to do other stuff as there are many IO pins still available. In my original Plant Friends, I have the gateway Moteino drive three cascading shift registers which in turn drives 24 LEDs allowing me to creating different lighting sequences.


The NodeIndex table does exactly what it sounds like; An index of all the sensor nodes. The admin.php file is used to add entries into this table.

NodeID – The node id in numerical value. This has to match what you programmed into the sensor node Moteino!

Alias – Since our NodeID will be a numerical value, we can put a more meaningful (to us humans!) name here.

Location – Location of the sensor mode.

Plant – The type of plant that the sensor node is monitoring.

Comments – Any comment / description you want to input here.

The most important columns in this table are the NodeID and Alias. As mentioned earlier, the python script checks against this table to see if the incoming data tagged with the corresponding Node ID exists. Each sensor node gets its' own table to store the sensor data. Each entry is timestamped the instant the data gets inserted into the table.

The table name for the respective sensor node is derived from the Alias plus the NodeID. This might seem a little weird but it is done this way because it's a bad idea to have only numbers as table names in MySQL.

The columns for the sensor node table follows the Plant Friends custom data format:

DateTime : ErrorLvl : SoilMoist : TempC : Humid : Voltage

As you can see, this can be expanded easily by adding more columns.


The script is responsible for reading the data from the serial buffer. When the script grabs data from the serial port, it sees the data in the colon deliminated format:

NodeID : Error Level : Soil Moisture : Temperature : Humidity : Battery

The script will check to see if the node ID is in the NodeIndex table. If it is, it will look for the respective table for that node. If a table for that node doesn't exist, it will create one. After which, it splits the data and inserts each datatype in to the appropriate fields in the table. If the node ID doesn't exist, the data is ignored.

During the data splitting process, the script checks the error level in the sensor node data received. If a the error level is great than 0, an alert/message is generated and the type of alert is dependent on the error type:

Error Level 0 = everything awesome.

Error Level 1 = soil moisture low.

Error Level 2 and 3 = Temperature / humidity error.

Error Level 4 = low battery.

Once the message is created, the script spawns a thread to send the email out so it doesn't block the rest of the program. As for SMS, most (if not all) mobile networks have an email address associated with your mobile number where you can send and email to and receive it via SMS. Each provider is different so a little research will be needed.


Admin.php allows you to add new sensor nodes into the database. The interface is a little rough right now but it works. If you are changing the information for the sensor nodes, only update one node at a time!


This is reserved for the Plant Friends app. If you point your browser to http://MYRASPIADDRESS/ You will see the information that is in the NodeIndex table.

When the app needs to request data for a specific node, for example node ID 23, it will request the URL:


This will dump 6 lines of sensor data in the colon deliminated format. The first line represents the sensor data of the latest known entry for the respective node. Each subsequent line after that represents the previous days' data. IE, the 2nd line is data for 1 day ago, the 2nd line is 2 days ago etc, up till a maximum of 5 days. The Plant Friends app takes each line, splits up the data and displays it accordingly.


1. Intro & BOM

2. Sensor Node Hardware

3. Sensor Node Software

4. Sensor Node Enclosure

5. Basestation Hardware & Software

6. Basestation Enclosure

7. The Mobile App

8. Notes & Thoughts

PreviousNext Page