PiPlanter 2 | DIY Lite Version Release!

Since I returned to college the PiPlanter has been running without me having to do any maintenance on it at all. The plants are still alive and growing and all processes associated with the PiPlanter are still going. I figure now is a good a time as any to bring together all of the work I’ve done to till this point in one concise post.

This does NOT mean I’m done working on future versions of the PiPlanter. I’ll hopefully write another post stating goals for the future sometime soon. Now onto the build tutorial.


 

The Hardware

 

First, the hardware of the project. A good place to start would be the parts list:

In the previous version of the PiPlanter, I didn’t have a concrete parts list for the project. Hopefully I’ll be able to keep this spreadsheet updated if the project changes. A lot of these components are mix and match, you could use pretty much any pump (The math for volumetric pumping is done with this pump) or any tubing or any power supply that can do 12v and 5v. A computer PSU would work great as well.

This is the hookup guide for the system:

(Thanks to tamps for the help!)

The two sets of header blocks are to be replaced by the moisture sensors, and the motor replaced with the pump.

For a physical configuration, I’ve found through multiple times doing this that mounting it on a wire rack works the best as seen here:

Edit (10/19/2014) Here is the same group of plants two months later without any direct human interaction. They grew from the light in the window and used up all of the water in the reservoir which was totally filled before I left.

To distribute the water to the plants, attach the vinyl tubing to the outflow of the pump and seal off the other end of the outflow tube. Run the tubing along the plants and drill holes wherever you’d like the water to exit.

You’ll also need to install the camera module in the Pi and point it wherever you’d like the frame of the photo to be.

 

The Software

As a preface, I’d like to at first say that this software was written entirely by me. I’ve never had any formal training in programming of any kind, so if there are obvious flaws with my code please let me know. That being said, I’ve found that this system is very effective and has worked for me and kept my plants alive for months.

All of this runs off of a base install of raspian on a raspberry pi model b.

There three major parts to the software. First, the prerequisites:


apt-get install python-imaging python-imaging-tk python-pip mencoder python-dev git apache2 mysql-server php5 php5-mysql python-mysqldb python-serial php5-gd

pip install tweepy apscheduler spidev wiringpi

wget https://gdata-python-client.googlecode.com/files/gdata-2.0.18.tar.gz
tar -xzvf gdata-2.0.18.tar.gz
cd gdata-2.0.18/
sudo python setup.py install

wget https://youtube-upload.googlecode.com/files/youtube-upload-0.7.2.tgz
tar -xzvf youtube-upload-0.7.2.tgz
cd youtube-upload-0.7.2/
sudo python setup.py install

mkdir /srv/www/lib/
cd /srv/www/lib/
wget http://www.pchart.net/release/pChart2.1.3.tar.gz
tar -xzvf pChart2.1.3.tar.gz
mv pChart2.1.3 pChart

You’ll need to enable SPI on your Pi in order to use the MCP3008 ADC. Do this by running the following commands:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Comment out the spi-bcm2708 line so it looks like this:

#blacklist spi-bcm2708

Then run this to make it more permanent.

sudo modprobe spi-bcm2708

And finally reboot your Pi with:

sudo reboot

Then the php code that renders the pChart graph. More details for installing pChart here and officially here.

<?php

/* Include all the classes */
include("/srv/www/lib/pChart/class/pData.class.php");
include("/srv/www/lib/pChart/class/pDraw.class.php");
include("/srv/www/lib/pChart/class/pImage.class.php");

$myData = new pData(); /* Create your dataset object */

$db = mysql_connect("localhost", "piplanter", "password"); //location of server, db username, db pass
mysql_select_db("PiPlanter_DB", $db);

$Requete = "SELECT * FROM PiPlanter_DB." .$argv[1];

$Result = mysql_query($Requete, $db);

if (!$Result) { // add this check.
die('Invalid query: ' . mysql_error());
}

/*This fetches the data from the mysql database, and adds it to pchart as points*/
while($row = mysql_fetch_array($Result))
{
$Time = $row["Time"];
$myData->addPoints($Time,"Time");

$P_MST0 = $row["P_MST0"];
$myData->addPoints($P_MST0,"P_MST0");
$P_MST1 = $row["P_MST1"];
$myData->addPoints($P_MST1,"P_MST1");

$A_TMP0 = $row["A_TMP0"];
$myData->addPoints($A_TMP0,"A_TMP0");

$A_LDR0 = $row["A_LDR0"];
$myData->addPoints($A_LDR0,"A_LDR0");
}

$myData-> setSerieOnAxis("P_MST0", 2);
$myData-> setSerieOnAxis("P_MST1", 2);
$myData-> setAxisName(2, "Relative Moisture [%]");

$myData-> setSerieOnAxis("A_TMP0", 0); //assigns the data to the first axis
$myData-> setAxisName(0, "Degrees [F]"); //adds the label to the first axis

$myData-> setSerieOnAxis("A_LDR0", 1);
$myData-> setAxisName(1, "Ambient Light Level [%]");

$myData->setAbscissa("Time"); //sets the time data set as the x axis label

$myData-> setSerieWeight("P_MST0",1); //draws the line thickness

$myData->setPalette("P_MST0",array("R"=>58,"G"=>95,"B"=>205,"Alpha"=>80)); //sets the line color
$myData-> setSerieWeight("P_MST1",1);
$myData->setPalette("P_MST1",array("R"=>39,"G"=>64,"B"=>139,"Alpha"=>80));

$myData-> setSerieWeight("A_LDR0",2);
$myData->setPalette("A_LDR0",array("R"=>255,"G"=>255,"B"=>0,"Alpha"=>80));
$myData-> setSerieTicks("A_LDR0", 4);

$myData-> setSerieWeight("A_TMP0",2);
$myData->setPalette("A_TMP0",array("R"=>255,"G"=>0,"B"=>0,"Alpha"=>80));
$myData-> setSerieTicks("A_TMP0", 4);

// $myPicture = new pImage(1000,700,$myData); /* Create a pChart object and associate your dataset */
// $myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/pf_arma_five.ttf","FontSize"=>6)); /* Choose a nice font */
// $myPicture->setGraphArea(210,40,900,600); /* Define the boundaries of the graph area */
// $myPicture->drawScale(array("LabelRotation"=>320)); /* Draw the scale, keep everything automatic */

$myPicture = new pImage(1300,700,$myData); /* Create a pChart object and associate your dataset */
$myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/pf_arma_five.ttf","FontSize"=>6)); /* Choose a nice font */
$myPicture->setGraphArea(210,40,1200,600); /* Define the boundaries of the graph area */
$myPicture->drawScale(array("LabelRotation"=>320)); /* Draw the scale, keep everything automatic */

$Settings = array("R"=>250, "G"=>250, "B"=>250, "Dash"=>1, "DashR"=>0, "DashG"=>0, "DashB"=>0);

/*The combination makes a cool looking graph*/
$myPicture->drawPlotChart(array("DisplayValues"=>TRUE,"LabelRotation"=>320));
$myPicture->drawLineChart();
$myPicture->drawLegend(30,30); //adds the legend

$time = date("d-M-Y_H:i:s");
$myPicture->render($argv[2].$time.".png");
print($argv[2].$time.".png");

And now the star of the show, the python script:

import MySQLdb
from datetime import datetime
import time
from time import sleep
import os
import sys
import spidev
import RPi.GPIO as GPIO
import logging
logging.basicConfig()
from apscheduler.schedulers.blocking import BlockingScheduler
import subprocess

def ConsoleDebug(input):
	debug = '[' + datetime.now().strftime("%m-%d-%Y_%I-%M-%S-%p") + '] Debug: ' + input
	print debug
	file = open(str(os.getcwd()) + "/log.txt", "a")
	file.write(debug + "\n")
	file.close()

def FirstTimeSetup():
	global cycle

	ConsoleDebug('---------------- NEW INSTANCE OF PIPLANTER ----------------')

	MySQL_Commands = {1 : 'CREATE DATABASE IF NOT EXISTS PiPlanter_DB', 2: "GRANT ALL ON `PiPlanter_DB`.* TO 'piplanter'@'localhost' IDENTIFIED BY 'password'", 3: 'USE PiPlanter_DB' }
	for i in MySQL_Commands.itervalues():
		ConsoleDebug('MYSQL COMMAND: ' + i)
		cursor.execute(i)

	MySQLTableSetup(False,'Daily',True)
	VisualLocationSetup(True,'dontcare')
	ConsoleDebug('Setup Complete')
	cycle = 0

def MySQLTableSetup(full,kind,first):

	global MySQL_Tables

	now = datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")

	if first == True:
		MySQL_Tables = { 'MySQLTable_Daily' : 'DailyTable' + now, 'MySQLTable_Weekly' : 'WeeklyTable' + now, 'MySQLTable_Monthly' : 'MonthlyTable' + now}
		if full == False:
			CreateTables = {0: "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))", 1 : "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))" , 2 : "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"}
			for i in CreateTables.itervalues():
				ConsoleDebug('MYSQL COMMAND: ' + i)
				cursor.execute(i)
		if full == True:
			CreateTables = {0: "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))", 1:"CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))" , 2:"CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))" }
			for i in CreateTables.itervalues():
				ConsoleDebug('MYSQL COMMAND: ' + i)
				cursor.execute(i)

	elif first == False:
		if kind == 'Daily':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Daily'] = 'DailyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Daily'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"
		elif kind == 'Weekly':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Weekly'] = 'WeeklyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Weekly'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"
		elif kind == 'Monthly':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Monthly'] = 'MonthlyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Monthly'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"		

		ConsoleDebug('MYSQL: ' + CreateTable)
		cursor.execute(CreateTable)

	ConsoleDebug('Current Daily: ' + MySQL_Tables['MySQLTable_Daily'])
	ConsoleDebug('Current Weekly: ' + MySQL_Tables['MySQLTable_Weekly'])
	ConsoleDebug('Current Monthly: ' + MySQL_Tables['MySQLTable_Monthly'])

def VisualLocationSetup(first,kind):
	global VisualLocation

	now = datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")

	runninglocation = str(os.getcwd())
	if first == True:

		ConsoleDebug('Creating Video Directory')
		if not os.path.exists(runninglocation + '/videos/'):
			os.makedirs(runninglocation + '/videos/')
			ConsoleDebug('Video Directory Created')
		else:
			ConsoleDebug('Video Directory Already Exists')

		ConsoleDebug('Creating Daily Video Directory')
		if not os.path.exists(runninglocation + '/videos/dailys/'):
			os.makedirs(runninglocation + '/videos/dailys/')
		else:
			ConsoleDebug('Daily Video Directory Already Exists')

		ConsoleDebug('Creating Image Directory')
		if not os.path.exists(runninglocation + '/images/'):
			os.makedirs(runninglocation + '/images/')
		else:
			ConsoleDebug('Image Directory Already Exists')

		ConsoleDebug('Creating Daily Image Directory')
		if not os.path.exists(runninglocation + '/images/dailys/'):
			os.makedirs(runninglocation + '/images/dailys/')
		else:
			ConsoleDebug('Daily Image Directory Already Exists')

		ConsoleDebug('Creating Graph Directory')
		if not os.path.exists(runninglocation + '/graphs/'):
			os.makedirs(runninglocation + '/graphs/')
		else:
			ConsoleDebug('Graph Directory Already Exists')

		ConsoleDebug('Updating All Current Visual Directories')
		VisualLocation = {'CurrentImageDirectory' : runninglocation + '/images/dailys/' + 'CurrentImageDirectory_' + now + '/' , 'CurrentVideoDirectory' : runninglocation + '/videos/dailys/' + 'CurrentVideoDirectory_' + now + '/' , 'CurrentGraphDirectory' : runninglocation + '/graphs/' + 'CurrentGraphDirectory_' + now + '/' }

		for i in VisualLocation.itervalues():
			ConsoleDebug('Making Directory: ' + i)
			os.makedirs(i)

	if first == False:
		ConsoleDebug('Updating Location Of ' + kind + ' Directory')
		if kind == 'Image':
			VisualLocation['CurrentImageDirectory'] = runninglocation + '/images/dailys/' + 'CurrentImageDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentImageDirectory'])
			os.makedirs(VisualLocation['CurrentImageDirectory'])

		elif kind == 'Video':
			VisualLocation['CurrentVideoDirectory'] = runninglocation + '/videos/dailys/' + 'CurrentVideoDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentVideoDirectory'])
			os.makedirs(VisualLocation['CurrentVideoDirectory'])

		elif kind == 'Graph':
			VisualLocation['CurrentGraphDirectory'] = runninglocation + '/graphs/' + 'CurrentGraphDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentGraphDirectory'])
			os.makedirs(VisualLocation['CurrentGraphDirectory'])

	ConsoleDebug('Current Image Directory' + VisualLocation['CurrentImageDirectory'])
	ConsoleDebug('Current Video Directory' + VisualLocation['CurrentVideoDirectory'])
	ConsoleDebug('Current Graph Directory' + VisualLocation['CurrentGraphDirectory'])

	return 0

#this function can be used to find out the ADC value on ADC 0
def ReadADC0(adcnum_0):
    if adcnum_0 > 7 or adcnum_0 < 0:
        return -1
    r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
    adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
    return adcout_0

#this function can be used to find out the ADC value on ADC 1
def ReadADC1(adcnum_1):
    if adcnum_1 > 7 or adcnum_1 < 0:
        return -1
    r_1 = spi_1.xfer2([1, 8 + adcnum_1 << 4, 0])
    adcout_1 = ((r_1[1] & 3) << 8) + r_1[2]
    return adcout_1

#this function converts a given value from the ADC and turns it into usable data
def ConvertADC(adcinput,unit):
	millivolts = adcinput*(3300.0/1024.0) #converts the ADC value to milivolts
	temp_c = ((millivolts - 100.0)/10)-40.0
	percent = (adcinput/1024.0)*100
	if unit == 'c' : #used for a temperature sensor to return Celsius
		return temp_c
	elif unit == 'f' :  #used for a temperature sensor to return Fahrenheit
		temp_f = (temp_c * 9.0 / 5.0) + 32
		return temp_f
	elif unit == 'mV':
		return millivolts
	elif unit == '%':
		return percent
	else:
		print "ConvertADC input error"
		return 0
	return 0

#returns a usable numerical value from the ADC
def PollSensor(sensor,unit,precision,samples):
	GPIO.output(pins['MST_Enable'], True)
	if PiPlanterFull == True:
		#Full PiPlanter
		sensors = {\
		'P_TMP0' : ConvertADC(ReadADC0(0),unit),\
		'P_MST0' : ConvertADC(ReadADC0(1),unit),\
		'P_TMP1' : ConvertADC(ReadADC0(2),unit),\
		'P_MST1' : ConvertADC(ReadADC0(3),unit),\
		'P_TMP2' : ConvertADC(ReadADC0(4),unit),\
		'P_MST2' : ConvertADC(ReadADC0(5),unit),\
		'P_TMP3' : ConvertADC(ReadADC0(6),unit),\
		'P_MST3' : ConvertADC(ReadADC0(7),unit),\

		'A_TMP0' : ConvertADC(ReadADC1(0),unit),\
		'A_LDR0' : ConvertADC(ReadADC1(1),unit),\
		'A_LDR1' : ConvertADC(ReadADC1(2),unit),\
		'A_MST0' : ConvertADC(ReadADC1(3),unit)}
	else:
		#Simple PiPlanter
		sensors = {\
		'P_MST0' : ConvertADC(ReadADC0(0),unit),\
		'P_MST1' : ConvertADC(ReadADC0(1),unit),\
		'A_TMP0' : ConvertADC(ReadADC0(2),unit),\
		'A_LDR0' : ConvertADC(ReadADC0(3),unit)}

	outputsum = 0
	for x in range(0,samples): #An averaging algorithm that creates a more precise reading
		outputsum = outputsum + sensors[sensor]
	output = round(outputsum/samples, precision)
	GPIO.output(pins['MST_Enable'], False)
	return output

#samples all sensors, outputs different formats of the data to be used in other places in the program
def SampleAllSensors(sensor_precision,sensor_samples,form,Full):
	global MySQL_Tables
	if Full == True:
		#Full PiPlanter
		current_sensors = {\
		'P_TMP0' : PollSensor('P_TMP0' , 'f', sensor_precision, sensor_samples),\
		'P_MST0' : PollSensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_TMP1' : PollSensor('P_TMP1' , 'f', sensor_precision, sensor_samples),\
		'P_MST1' : PollSensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'P_TMP2' : PollSensor('P_TMP2' , 'f', sensor_precision, sensor_samples),\
		'P_MST2' : PollSensor('P_MST2' , '%', sensor_precision, sensor_samples),\
		'P_TMP3' : PollSensor('P_TMP3' , 'f', sensor_precision, sensor_samples),\
		'P_MST3' : PollSensor('P_MST3' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : PollSensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : PollSensor('A_LDR0' , '%', sensor_precision, sensor_samples),\
		'A_LDR1' : PollSensor('A_LDR1' , '%', sensor_precision, sensor_samples),\
		'A_MST0' : PollSensor('A_MST0' , '%', sensor_precision, sensor_samples)}
	else:
		#Simple PiPlanter
		current_sensors = {\
		'P_MST0' : PollSensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_MST1' : PollSensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : PollSensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : PollSensor('A_LDR0' , '%', sensor_precision, sensor_samples)}

	if form == 'MySQL':
		if Full == True:
			#Full PiPlanter
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Weekly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			ConsoleDebug('MySQL Tables Updated Full')
			output = "INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")"
		else:
			#Simple PiPlanter
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Weekly'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			ConsoleDebug('MySQL Tables Updated Simple')
			output = "INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"

	elif form == 'Console':
		if Full == True:
			#Full PiPlanter
			output = 'Debug Update:' + ' P_TMP0: ' + str(str(current_sensors['P_TMP0'])) + ',' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_TMP1: ' + str(str(current_sensors['P_TMP1'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ','+ ' P_TMP2: ' + str(str(current_sensors['P_TMP2'])) + ','+ ' P_MST2: ' + str(str(current_sensors['P_MST2'])) + ','+ ' P_TMP3: ' + str(str(current_sensors['P_TMP3'])) + ','+ ' P_MST3: ' + str(str(current_sensors['P_MST3'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0'])) + ','+ ' A_LDR1: ' + str(str(current_sensors['A_LDR1'])) + ','+ ' A_MST0: ' + str(str(current_sensors['A_MST0']))
		else:
			#Simple PiPlanter
			output = 'Debug Update:' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0']))
	elif form == 'Twitter':
		if Full == True:
			#Full PiPlanter
			output = 'Ambient LDR: ' + str(round(((current_sensors['A_LDR0'] + current_sensors['A_LDR1'])/2),1) ) + '%, ' + 'Ambient Tmp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Tmp: ' + str(round( (current_sensors['P_TMP0'] + current_sensors['P_TMP1'] + current_sensors['P_TMP2'] + current_sensors['P_TMP3'] )/4, sensor_precision-2)) + 'DF, ' + 'Ambient Mst: ' + str(round(current_sensors['A_MST0'],2)) + '%, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1']+ current_sensors['P_MST2']+ current_sensors['P_MST3'] )/4 ,1)) + '%'
		else:
			#Simple PiPlanter
			output = 'Ambient Light: ' + str(round((current_sensors['A_LDR0']),1)) + '%, ' + 'Ambient Temp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1'])/2 ,1)) + '%'
	else:
		print "ConvertADC input SampleAllSensors"
		return 0
	return output

#pumps a given amount of water from a given pump
def PumpWater(pump,volume):
	LPM = 4.00 #L per minute
	ontime = volume*(60/LPM)
	ConsoleDebug('PUMP ON')
	GPIO.output(pumps[pump],True)
	time.sleep(ontime)
	GPIO.output(pumps[pump],False)
	ConsoleDebug('PUMP OFF')
	output = 'Pumped ' + str(volume) + ' L Of Water Into Plants In ' + str(ontime) + ' Seconds'
	ConsoleDebug(output)
	return output

def Image(dir,cycle,high_quality):
	image = dir + str(cycle).zfill(4) + '.jpg'
	if high_quality == False:
		picture_command = 'raspistill -q 10 -o ' + image
	if high_quality == True:
		picture_command = 'raspistill -q 100 -o ' + image
	os.system(picture_command)
	ConsoleDebug('Image Captured, High Quality = ' + str(high_quality) + ', Image: ' + str(image))
	return image	

def RenderGraph(table,location):
	ConsoleDebug('Rendering Graph')
	rendercommand = 'php ' + str(os.getcwd()) + '/pChartRender_1_0_5.php ' + table + ' ' + location
	ConsoleDebug('Running Command: ' + rendercommand)
	proc = subprocess.Popen(rendercommand, shell=True, stdout=subprocess.PIPE)
	script_response = proc.stdout.read()
	ConsoleDebug('Output File: ' + script_response)
	ConsoleDebug('Rendering Complete')
	return script_response

def TryTweet(image, imagelocation, text):
	for i in range(10):
		try:
			import tweepy
			consumer_key=""
			consumer_secret=""
			access_token=""
			access_token_secret=""
			auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
			auth.set_access_token(access_token, access_token_secret)
			api = tweepy.API(auth)

			ConsoleDebug('Attempt [' + str(i) + '] To Tweet: ' + text + ' , image = ' + str(image))
			if image == True:
				output = api.update_with_media(imagelocation, text)
			if image == False:
				output = api.update_status(text)
			break

		except tweepy.error.TweepError as e:
			ConsoleDebug('Tweet Failed, Retrying')
			ConsoleDebug('Twitter Error: ' + str(e))
			i = i + 1
			time.sleep(15)

	if i < 10:
		ConsoleDebug('Tweet Sent After ' + str(i) + ' Attempts, Details: Actual Text [ ' + str(output.text) + '] URL: https://twitter.com/piplanter_bot/status/' + str(output.id) )

	if i == 10:
		ConsoleDebug('Tweet Was a Failure')

def RenderVideo(infolder,outfolder):
	outputfile = outfolder + str(datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")) + '_VIDEO.avi'
	ConsoleDebug('Attempting To Render: ' + outputfile)
	render_command = 'sudo mencoder mf://' + str(infolder) + '*.jpg -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -mf type=jpeg:fps=15 -o ' + outputfile
	os.system(render_command)
	ConsoleDebug('Render Complete, File: ' + outputfile)
	return outputfile

def UploadVideo(video,email,password):
	humantime = str(datetime.now().strftime("%m/%d/%Y"))
	title = 'Time Lapse of Tomato Plants of the Three Days Prior To ' + str(humantime)
	description = 'Confused? https://esologic.com/?page_id=1042'
	category = 'Tech'
	keywords = 'piplanter'
	uploadcommand = 'youtube-upload --email=' + email + ' --password=' + password + ' --title="' + title +'"'+ ' --description="' + description + '"' + ' --category=' + category + ' --keywords=' + keywords + ' ' + os.path.normpath(video)
	ConsoleDebug('Upload Command: ' + uploadcommand)

	for i in range(10):
		try:
			ConsoleDebug('Attempt [' + str(i) + '] To Upload: ' + str(video))
			proc = subprocess.Popen(uploadcommand, shell=True, stdout=subprocess.PIPE)
			output = proc.stdout.read()
			break

		except:
			ConsoleDebug('Upload Failed, Retrying')
			ConsoleDebug('Upload Error: ' + str(output))
			i = i + 1
			time.sleep(15)

	if i < 10:
		ConsoleDebug('Uploaded After ' + str(i) + ' Attempts, Details: URL [ ' + str(output) + ']' )
		return output

	if i == 10:
		ConsoleDebug('Upload Was a Failure')
		return 'Upload was a Failure'

def TwentyMinutes():
	global cycle

	SampleAllSensors(1,1000,'MySQL',False)
	ConsoleDebug(SampleAllSensors(5,1000,'Console',False))
	image = Image(VisualLocation['CurrentImageDirectory'],cycle,False)
	tweet = SampleAllSensors(3,20,'Twitter',False) + " https://esologic.com/?page_id=1042"

	tweet = tweet = SampleAllSensors(3,20,'Twitter',False) + " https://esologic.com/?page_id=1042"

	TryTweet(True,image,tweet)
	cycle = cycle + 1

def ThreeHours():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Daily'],VisualLocation['CurrentGraphDirectory'])
	tweet = 'Graph of day so far: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	TryTweet(True,graphlocation,tweet)
	ConsoleDebug('ThreeHours Complete')

def Daily():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Daily'],VisualLocation['CurrentGraphDirectory'])
	MySQLTableSetup(False,'Daily',False)
	tweet = 'Graph of Previous 24 Hours: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	TryTweet(True,graphlocation,tweet)
	tweet2 = PumpWater('PUMP0',2.5)
	TryTweet(False,'',tweet2)
	ConsoleDebug('Daily Complete')

def ThreeDays():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Weekly'],VisualLocation['CurrentGraphDirectory'])
	video = RenderVideo(VisualLocation['CurrentImageDirectory'],VisualLocation['CurrentVideoDirectory'])

	yt_url = UploadVideo(video,"YOUREMAIL@gmail.com","YOURPASSWORD")

	tweet1 = 'Graph of Week So Far: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	tweet2 = 'Time Lapse Video of Previous Three Days: ' + yt_url

	TryTweet(True,graphlocation,tweet1)
	TryTweet(False,'',tweet2)

	VisualLocationSetup(False,'Image')
	VisualLocationSetup(False,'Video')

	ConsoleDebug('ThreeDays Complete')

def Weekly():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Weekly'],VisualLocation['CurrentGraphDirectory'])
	tweet = 'Graph of Previous Week: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	TryTweet(True,graphlocation,tweet)
	MySQLTableSetup(False,'Weekly',False)
	ConsoleDebug('Weekly Complete')

if __name__ == '__main__':
	global MySQL_Tables
	global VisualLocation

	GPIO.setmode(GPIO.BOARD)

	pins = {'MST_Enable' : 8} #assign names to GPIO pins
	for d in pins.itervalues():
		GPIO.setup(d,GPIO.OUT)

	pumps = {'PUMP0' : 7, 'PUMP1' : 11, 'PUMP2' : 13, 'PUMP3' : 16} #assign names to GPIO pins
	for k in pumps.itervalues():
		GPIO.setup(k,GPIO.OUT)

	#first ADC setup on SPI port 1
	spi_1 = spidev.SpiDev()
	spi_1.open(0, 1)

	#first ADC setup on SPI port 0
	spi_0 = spidev.SpiDev()
	spi_0.open(0, 0)

	PiPlanterFull = False

	user = MySQLdb.connect(host="localhost",user="root",passwd="YOURMYSQL PASSWORD")
	cursor = user.cursor()

	scheduler = BlockingScheduler()
	scheduler.add_job(TwentyMinutes, 'interval', minutes = 20)
	scheduler.add_job(ThreeHours,'interval', hours = 3)
	scheduler.add_job(Daily,'interval', days=1)
	scheduler.add_job(ThreeDays,'interval', days = 3)
	scheduler.add_job(Weekly,'interval', weeks=1)

	try:
		FirstTimeSetup()

		scheduler.start()

	except (KeyboardInterrupt, SystemExit):
		pass

Before running, make sure you make the following changes to the script:

You’ll need set up access to twitter API’s, seen here. You’ll need to input your information about your twitter app into into 331-334 of this script.

You’ll need to input information about your YouTube account on line 429

On line 473 you’ll need to input your mysql information.

 

Output Demos

The PiPlanter is very connected. It renders graphs of data, takes images and renders timelapse videos.

Here’s a standard tweet showing the plants:

Here’s a tweet showing a day’s worth of data in  a  graph render:

Here’s a tweet showing a week’s worth of data in a graph render:

Here’s a timelapse video of three days:

Follow @PiPlanter_Bot for updates on my plants.

That’s pretty much it! Please feel free to modify this code for any use you’d like.

All of my research on this project can be found here.

Thanks for reading, and please leave a comment if you like my work!

PiPlanter 2 | Updating Dependencies

In addition to the directions in this post on getting the ADC working, the following must be run to get the current version of the PiPlanter up and running.


sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git apache2 mysql-server php5 php5-mysql python-mysqldb sudo python-serial

sudo pip install tweepy apscheduler spidev wiringpi

wget https://gdata-python-client.googlecode.com/files/gdata-2.0.18.tar.gz
tar -xzvf gdata-2.0.18.tar.gz
cd gdata-2.0.18/
sudo python setup.py install

wget https://youtube-upload.googlecode.com/files/youtube-upload-0.7.2.tgz
tar -xzvf youtube-upload-0.7.2.tgz
cd youtube-upload-0.7.2/
sudo python setup.py install

PiPlanter 2 | Adding Youtube Upload Functionality

In order to keep things moving quickly, I’ve decided to take a shortcut when it comes to uploading timelapse videos to youtube. I’ve decided to basically create a function that passes data to youtube-upload, a command line utility for linux that can upload videos very simply.

Here’s the function:

def UploadVideo(video,email,password):
	humantime = str(datetime.now().strftime("%m/%d/%Y"))
	title = 'Time Lapse of Tomato Plants of the Three Days Prior To ' + str(humantime)
	description = 'Confused? https://esologic.com/?page_id=1042' 
	category = 'Tech'
	keywords = 'piplanter'
	uploadcommand = 'youtube-upload --email=' + email + ' --password=' + password + ' --title="' + title +'"'+ ' --description="' + description + '"' + ' --category=' + category + ' --keywords=' + keywords + ' ' + os.path.normpath(video)
	ConsoleDebug('Upload Command: ' + uploadcommand)
	
	for i in range(10):
		try:
			ConsoleDebug('Attempt [' + str(i) + '] To Upload: ' + str(video)) 
			proc = subprocess.Popen(uploadcommand, shell=True, stdout=subprocess.PIPE)
			output = proc.stdout.read()
			break
		
		except:
			ConsoleDebug('Upload Failed, Retrying')
			ConsoleDebug('Upload Error: ' + str(output))
			i = i + 1
			time.sleep(15)

	if i < 10:
		ConsoleDebug('Uploaded After ' + str(i) + ' Attempts, Details: URL [ ' + str(output) + ']' )
		return output

	if i == 10:
		ConsoleDebug('Upload Was a Failure')
		return 'Upload was a Failure'

It should remind you a lot of “TryTweet” from the main version of the PiPlanter.

 

Getting a Raspberry Pi on Worcester Polytechnic Institute (WPI) WiFi (WPA-EAP)

The following is a very specific guide and like all guides of this nature written by me it is mostly for my benefit so I can come back to it later. It is a modification of this guide written by Campus IT.  If you have any suggestions to improve anything, PLEASE shoot me an email or leave me a comment below.


I will be connecting my Raspberry Pi Model B+ running the latest build of Raspbian using the Edimax EW-7811Un WiFi dongle to this kind of network (From Campus IT):

Specifically, WPI requires 802.1x EAP-TLS certificate based authentication. This is sometimes referred to as WPA Enterprise

Having an internet connection will make doing this much much easier. In fact, if all you need to do is share your laptops WiFi with the Pi over the Ethernet port on your laptop that is quite easy (For WPI people please note that this is a violation of the networks’ acceptable use policy). For windows 8.1:

First, we will have to enable sharing our Wi-Fi through the Ethernet ports of our computer.

Open the Network and Sharing center on your computer. It is found under Control Panel->Network and Internet->Network and Sharing Center.
Next, click on “change adapter settings.”
Right click on your Wi-Fi, and select “Properties.” You will most likely need to be an administrator for this step.
Click on the “Sharing” tab.
Check the “Allow other network users to connect through this computer’s Internet connection” checkbox.
Hit OK to close this window.
Next, we will connect to the raspberry Pi over our Ethernet cable.

Open up cmd. Type “ping raspberrypi.mshome.net” into the command line. Do not use any quotes when you type in this command.
Take note of this IP address. You can connect to the Pi through Putty using that IP address.

If you’re using a fresh install, make sure you set the Pi’s internal time to the proper time using raspi-config. It’s under internationalization options.

sudo raspi-config

You will then need to register the MAC address

Next we need to acquire the proper certificates.

Campus IT has already created a good tutorial for doing this found here. You’ll want two get two certificates seen here:

Move those two documents onto the Pi as well. I’m using

/home/pi/certs/

as the location for my certificates for the sake of this tutorial.

From there you’ll have to convert the ‘certificate.p12’ document to a .pem format with OpenSSL. OpenSSL is installed by default in Raspian. Do this with the following command:

openssl pkcs12 -in /home/pi/certs/certificate.p12 -out /home/pi/certs/certificate.pem -nodes

Enter the password for the NETWORK when prompted. We now have 3 certificate files. The CA-.pem, certificate.p12 and certificate.pem all located in the /home/pi/certs directory on the pi.

Next we have to disable all the default wifi settings that come with Raspian. Do this by changing your /etc/network/interfaces file to the following:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

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

Doing this stops the Pi from trying to use the wlan0 device at boot and will allow us to use it directly.

Now we must configure wpa_supplicants. It doesn’t really matter where you put the configuration file, but the raspberry pi places it by default here:

/etc/wpa_supplicant/wpa_supplicant.conf

Edit the file to look like the following. Note that things you WILL have to change are marked with []’s. Also note that this config places all 3 certs in that directory I’ve mentioned a few times.

#ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
#update_config=1

network={
 ssid="WPI-Wireless"
 key_mgmt=WPA-EAP
 proto=WPA2
 pairwise=CCMP
 group=CCMP
 eap=TLS

 identity="[YOUR_WPI_EMAIL]@wpi.edu"

 ca_cert="/home/pi/certs/CA-[A_BUNCH_OF_NUMBERS].pem"
 client_cert="/home/pi/certs/certificate.pem"
 private_key="/home/pi/certs/certificate.p12"
 private_key_passwd="[YOUR_WPI_EMAIL_PASSWORD]"

 priority=1
}

I found that in an example configuration of wpa_supplicant.conf specifically notes the need of a .pem file for the client cert, thus the conversion.

We’re pretty much done, all we need to do is add a few steps to the boot process to start the whole process each time the device boots. We can use crontab or /etc/rc.local (thanks Greg Tighe) to accomplish this.

With Crontab:

crontab -e

Add the two lines to the file:

@reboot sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlan0
@reboot sudo /sbin/dhclient wlan0

or edit /etc/rc.local to contain:

@reboot sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlan0

And reboot your pi! Everything should connect and work.

PiPlanter 2 | Moving again

I bought a wire rack from walmart and put the PiPlanter back inside my house. Hopefully the plants will live longer than they did last year so while I’m at college I can continue to develop on this model. Images below.

PiPlanter 2 | Solving Broken Pipe Errors [Errno 32] in Tweepy

If I haven’t mentioned it already, https://twitter.com/piplanter_bot IS the new twitter account for PiPlanter. Like last time, I’m using the tweepy library for python to handle all things twitter for the project. What I’m NOT using this time is Flickr. From a design point of view, it wasn’t worth it. It was too complicated and had too many things that could go wrong for me to continue using it. Twitter is more than capable of hosting images, and tweepy has a very simple method of passing these images to twitter. Recently I moved the whole setup indoors and mounted it all onto a shelf seen here and it came with a set of strange problems.

Long story short, what I think happened was that since I moved them to a different location, the complexity of the images increased, causing an increase in the size of the images themselves. A broken pipe error implies that the entirety of the package sent to twitter wasn’t sent, causing the tweet not to go through. I first started to suspect this problem after seeing this:

 

The graphs were going through just fine, but images were seeming to have a hard time. You can’t tell from this photo, but those tweets are hours apart as opposed to the 20 minutes they are supposed to be. Once I started having this problem, I bit the bullet and integrated logging into my project which produced this log:

[08-12-2014_11-21-20-PM] Debug: MySQL Tables Updated Simple
[08-12-2014_11-21-20-PM] Debug: Debug Update: P_MST0: 87.10938, P_MST1: 89.64844, A_TMP0: 69.03711, A_LDR0: 70.99609
[08-12-2014_11-21-26-PM] Debug: Image Captured, High Quality = False, Image: /home/pi/PiPlanter2/images/dailys/CurrentImageDirectory_08_11_2014__06_58_32PM/0098.jpg
[08-12-2014_11-21-26-PM] Debug: Attempt [0] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-21-57-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-21-57-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-22-12-PM] Debug: Attempt [1] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-22-43-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-22-43-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-22-58-PM] Debug: Attempt [2] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-23-30-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-23-30-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-23-45-PM] Debug: Attempt [3] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-24-16-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-24-16-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-24-31-PM] Debug: Attempt [4] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-25-03-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-25-03-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-25-18-PM] Debug: Attempt [5] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-25-49-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-25-49-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-26-04-PM] Debug: Attempt [6] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-26-36-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-26-36-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-26-51-PM] Debug: Attempt [7] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-27-22-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-27-22-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-27-37-PM] Debug: Attempt [8] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-29-07-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-29-07-PM] Debug: Twitter Error: Failed to send request: The write operation timed out
[08-12-2014_11-29-22-PM] Debug: Attempt [9] To Tweet: Ambient Light: 70.6%, Ambient Temp: 69.0DF, Average Plant Mst: 88.5% https://esologic.com/?page_id=1042 , image = True
[08-12-2014_11-29-54-PM] Debug: Tweet Failed, Retrying
[08-12-2014_11-29-54-PM] Debug: Twitter Error: Failed to send request: [Errno 32] Broken pipe
[08-12-2014_11-30-09-PM] Debug: Tweet Was a Failure

Hours and hours of failed tweets due to “[Errno 32] Broken pipe”. I tried a lot of things, I figured out that it was the size of the images after seeing this:

Photos that were simple in nature had no problem being sent. After scaling the image size down, I’ve had absolutely no problem sending tweets.


If you are tweeting images with tweepy in python and getting intermediate Broken pipe errors, decrease the size of your image.
Thanks for reading.

PiPlanter 2 | Progress Update

I’m almost done with a very stable version of the Python code running the PiPlanter. There are many specific differences between this version of the python code and the version I wrote and implemented last summer, but the main one is that I tried to write functions for pretty much every task I wanted to do, and made each routine much more modular instead of one long line after line block to do each day. This took significantly longer to do (thus the lack of updates, sorry) but is much more expandable going forward. Below is the new version of the code, but by no means am I an expert programmer. The following code seems to work very well for what I want it to do.

import MySQLdb
from datetime import datetime
import time
from time import sleep
import os
import sys
import spidev
import RPi.GPIO as GPIO
import tweepy
import logging
logging.basicConfig()
from apscheduler.scheduler import Scheduler
import subprocess

def ConsoleDebug(input):
	debug = '[' + datetime.now().strftime("%m-%d-%Y_%I-%M-%S-%p") + '] Debug: ' + input
	print debug
	file = open(str(os.getcwd()) + "/log.txt", "a")
	file.write(debug + "\n")
	file.close()

def FirstTimeSetup():	
	global cycle
	
	ConsoleDebug('---------------- NEW INSTANCE OF PIPLANTER ----------------')
	
	MySQL_Commands = {1 : 'CREATE DATABASE IF NOT EXISTS PiPlanter_DB', 2: "GRANT ALL ON `PiPlanter_DB`.* TO 'piplanter'@'localhost' IDENTIFIED BY 'password'", 3: 'USE PiPlanter_DB' }
	for i in MySQL_Commands.itervalues():
		ConsoleDebug('MYSQL COMMAND: ' + i)
		cursor.execute(i)

	MySQLTableSetup(False,'Daily',True)
	VisualLocationSetup(True,'dontcare')
	ConsoleDebug('Setup Complete')
	cycle = 0

def MySQLTableSetup(full,kind,first):

	global MySQL_Tables
		
	now = datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")
	
	if first == True:
		MySQL_Tables = { 'MySQLTable_Daily' : 'DailyTable' + now, 'MySQLTable_Weekly' : 'WeeklyTable' + now, 'MySQLTable_Monthly' : 'MonthlyTable' + now}		
		if full == False:
			CreateTables = {0: "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))", 1 : "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))" , 2 : "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"}
			for i in CreateTables.itervalues():
				ConsoleDebug('MYSQL COMMAND: ' + i)
				cursor.execute(i)
		if full == True:
			CreateTables = {0: "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))", 1:"CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))" , 2:"CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))" }
			for i in CreateTables.itervalues():
				ConsoleDebug('MYSQL COMMAND: ' + i)
				cursor.execute(i)
				
	elif first == False:
		if kind == 'Daily':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Daily'] = 'DailyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Daily'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Daily'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"
		elif kind == 'Weekly':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Weekly'] = 'WeeklyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Weekly'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Weekly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"
		elif kind == 'Monthly':
			ConsoleDebug('Daily Database Name Has Been Updated')
			MySQL_Tables['MySQLTable_Monthly'] = 'MonthlyTable_' + now
			ConsoleDebug(MySQL_Tables['MySQLTable_Monthly'])
			if full == False:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"
			elif full  == True:
				CreateTable = "CREATE TABLE " + MySQL_Tables['MySQLTable_Monthly'] + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"		
		
		ConsoleDebug('MYSQL: ' + CreateTable)
		cursor.execute(CreateTable)
	
	ConsoleDebug('Current Daily: ' + MySQL_Tables['MySQLTable_Daily'])
	ConsoleDebug('Current Weekly: ' + MySQL_Tables['MySQLTable_Weekly'])
	ConsoleDebug('Current Monthly: ' + MySQL_Tables['MySQLTable_Monthly'])

def VisualLocationSetup(first,kind):
	global VisualLocation
	
	now = datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")
	
	runninglocation = str(os.getcwd())
	if first == True:
		
		ConsoleDebug('Creating Video Directory')
		if not os.path.exists(runninglocation + '/videos/'):
			os.makedirs(runninglocation + '/videos/')
			ConsoleDebug('Video Directory Created')
		else:
			ConsoleDebug('Video Directory Already Exists')

		ConsoleDebug('Creating Daily Video Directory')
		if not os.path.exists(runninglocation + '/videos/dailys/'):
			os.makedirs(runninglocation + '/videos/dailys/')
		else:
			ConsoleDebug('Daily Video Directory Already Exists')
			
		ConsoleDebug('Creating Image Directory')
		if not os.path.exists(runninglocation + '/images/'):
			os.makedirs(runninglocation + '/images/')
		else:
			ConsoleDebug('Image Directory Already Exists')
		
		ConsoleDebug('Creating Daily Image Directory')
		if not os.path.exists(runninglocation + '/images/dailys/'):
			os.makedirs(runninglocation + '/images/dailys/')
		else:
			ConsoleDebug('Daily Image Directory Already Exists')
			
		ConsoleDebug('Creating Graph Directory')
		if not os.path.exists(runninglocation + '/graphs/'):
			os.makedirs(runninglocation + '/graphs/')
		else:
			ConsoleDebug('Graph Directory Already Exists')
		
		ConsoleDebug('Updating All Current Visual Directories')
		VisualLocation = {'CurrentImageDirectory' : runninglocation + '/images/dailys/' + 'CurrentImageDirectory_' + now + '/' , 'CurrentVideoDirectory' : runninglocation + '/videos/dailys/' + 'CurrentVideoDirectory_' + now + '/' , 'CurrentGraphDirectory' : runninglocation + '/graphs/' + 'CurrentGraphDirectory_' + now + '/' }
				
		for i in VisualLocation.itervalues():
			ConsoleDebug('Making Directory: ' + i)
			os.makedirs(i)
	
	if first == False:
		ConsoleDebug('Updating Location Of ' + kind + ' Directory')
		if kind == 'Image':
			VisualLocation['CurrentImageDirectory'] = runninglocation + '/images/dailys/' + 'CurrentImageDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentImageDirectory'])
			os.makedirs(VisualLocation['CurrentImageDirectory'])
			
		elif kind == 'Video':
			VisualLocation['CurrentVideoDirectory'] = runninglocation + '/videos/dailys/' + 'CurrentVideoDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentVideoDirectory'])
			os.makedirs(VisualLocation['CurrentVideoDirectory'])
			
		elif kind == 'Graph':
			VisualLocation['CurrentGraphDirectory'] = runninglocation + '/graphs/' + 'CurrentGraphDirectory_' + now + '/'
			ConsoleDebug('Making Directory: ' + VisualLocation['CurrentGraphDirectory'])
			os.makedirs(VisualLocation['CurrentGraphDirectory'])
			
	
	ConsoleDebug('Current Image Directory' + VisualLocation['CurrentImageDirectory'])
	ConsoleDebug('Current Video Directory' + VisualLocation['CurrentVideoDirectory'])
	ConsoleDebug('Current Graph Directory' + VisualLocation['CurrentGraphDirectory'])
	
	return 0

#this function can be used to find out the ADC value on ADC 0
def ReadADC0(adcnum_0): 
    if adcnum_0 > 7 or adcnum_0 < 0:
        return -1
    r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
    adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
    return adcout_0

#this function can be used to find out the ADC value on ADC 1
def ReadADC1(adcnum_1): 
    if adcnum_1 > 7 or adcnum_1 < 0:
        return -1
    r_1 = spi_1.xfer2([1, 8 + adcnum_1 << 4, 0])
    adcout_1 = ((r_1[1] & 3) << 8) + r_1[2]
    return adcout_1
  
#this function converts a given value from the ADC and turns it into usable data
def ConvertADC(adcinput,unit):
	millivolts = adcinput*(3300.0/1024.0) #converts the ADC value to milivolts
	temp_c = ((millivolts - 100.0)/10)-40.0
	percent = (adcinput/1024.0)*100
	if unit == 'c' : #used for a temperature sensor to return Celsius 
		return temp_c
	elif unit == 'f' :  #used for a temperature sensor to return Fahrenheit  
		temp_f = (temp_c * 9.0 / 5.0) + 32
		return temp_f
	elif unit == 'mV':
		return millivolts
	elif unit == '%':
		return percent
	else:
		print "ConvertADC input error"
		return 0
	return 0
	
#returns a usable numerical value from the ADC
def PollSensor(sensor,unit,precision,samples):
	GPIO.output(pins['MST_Enable'], True)
	if PiPlanterFull == True:
		#Full PiPlanter
		sensors = {\
		'P_TMP0' : ConvertADC(ReadADC0(0),unit),\
		'P_MST0' : ConvertADC(ReadADC0(1),unit),\
		'P_TMP1' : ConvertADC(ReadADC0(2),unit),\
		'P_MST1' : ConvertADC(ReadADC0(3),unit),\
		'P_TMP2' : ConvertADC(ReadADC0(4),unit),\
		'P_MST2' : ConvertADC(ReadADC0(5),unit),\
		'P_TMP3' : ConvertADC(ReadADC0(6),unit),\
		'P_MST3' : ConvertADC(ReadADC0(7),unit),\
	
		'A_TMP0' : ConvertADC(ReadADC1(0),unit),\
		'A_LDR0' : ConvertADC(ReadADC1(1),unit),\
		'A_LDR1' : ConvertADC(ReadADC1(2),unit),\
		'A_MST0' : ConvertADC(ReadADC1(3),unit)}
	else:
		#Simple PiPlanter
		sensors = {\
		'P_MST0' : ConvertADC(ReadADC0(0),unit),\
		'P_MST1' : ConvertADC(ReadADC0(1),unit),\
		'A_TMP0' : ConvertADC(ReadADC0(2),unit),\
		'A_LDR0' : ConvertADC(ReadADC0(3),unit)}
	
	outputsum = 0
	for x in range(0,samples): #An averaging algorithm that creates a more precise reading
		outputsum = outputsum + sensors[sensor]
	output = round(outputsum/samples, precision)
	GPIO.output(pins['MST_Enable'], False)
	return output

#samples all sensors, outputs different formats of the data to be used in other places in the program
def SampleAllSensors(sensor_precision,sensor_samples,form,Full):	
	global MySQL_Tables
	if Full == True:
		#Full PiPlanter
		current_sensors = {\
		'P_TMP0' : PollSensor('P_TMP0' , 'f', sensor_precision, sensor_samples),\
		'P_MST0' : PollSensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_TMP1' : PollSensor('P_TMP1' , 'f', sensor_precision, sensor_samples),\
		'P_MST1' : PollSensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'P_TMP2' : PollSensor('P_TMP2' , 'f', sensor_precision, sensor_samples),\
		'P_MST2' : PollSensor('P_MST2' , '%', sensor_precision, sensor_samples),\
		'P_TMP3' : PollSensor('P_TMP3' , 'f', sensor_precision, sensor_samples),\
		'P_MST3' : PollSensor('P_MST3' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : PollSensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : PollSensor('A_LDR0' , '%', sensor_precision, sensor_samples),\
		'A_LDR1' : PollSensor('A_LDR1' , '%', sensor_precision, sensor_samples),\
		'A_MST0' : PollSensor('A_MST0' , '%', sensor_precision, sensor_samples)}
	else:
		#Simple PiPlanter
		current_sensors = {\
		'P_MST0' : PollSensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_MST1' : PollSensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : PollSensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : PollSensor('A_LDR0' , '%', sensor_precision, sensor_samples)}
	
	if form == 'MySQL':
		if Full == True:
			#Full PiPlanter
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Weekly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" )
			user.commit()
			ConsoleDebug('MySQL Tables Updated Full')
			output = "INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")" 
		else:
			#Simple PiPlanter
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Weekly'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			cursor.execute("INSERT INTO " + MySQL_Tables['MySQLTable_Monthly'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  )
			user.commit()
			ConsoleDebug('MySQL Tables Updated Simple')
			output = "INSERT INTO " + MySQL_Tables['MySQLTable_Daily'] + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"
			
	elif form == 'Console':
		if Full == True:
			#Full PiPlanter
			output = 'Debug Update:' + ' P_TMP0: ' + str(str(current_sensors['P_TMP0'])) + ',' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_TMP1: ' + str(str(current_sensors['P_TMP1'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ','+ ' P_TMP2: ' + str(str(current_sensors['P_TMP2'])) + ','+ ' P_MST2: ' + str(str(current_sensors['P_MST2'])) + ','+ ' P_TMP3: ' + str(str(current_sensors['P_TMP3'])) + ','+ ' P_MST3: ' + str(str(current_sensors['P_MST3'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0'])) + ','+ ' A_LDR1: ' + str(str(current_sensors['A_LDR1'])) + ','+ ' A_MST0: ' + str(str(current_sensors['A_MST0'])) 	
		else:
			#Simple PiPlanter
			output = 'Debug Update:' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0'])) 	
	elif form == 'Twitter':
		if Full == True:
			#Full PiPlanter
			output = 'Ambient LDR: ' + str(round(((current_sensors['A_LDR0'] + current_sensors['A_LDR1'])/2),1) ) + '%, ' + 'Ambient Tmp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Tmp: ' + str(round( (current_sensors['P_TMP0'] + current_sensors['P_TMP1'] + current_sensors['P_TMP2'] + current_sensors['P_TMP3'] )/4, sensor_precision-2)) + 'DF, ' + 'Ambient Mst: ' + str(round(current_sensors['A_MST0'],2)) + '%, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1']+ current_sensors['P_MST2']+ current_sensors['P_MST3'] )/4 ,1)) + '%'
		else:
			#Simple PiPlanter
			output = 'Ambient Light: ' + str(round((current_sensors['A_LDR0']),1)) + '%, ' + 'Ambient Temp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1'])/2 ,1)) + '%'
	else:
		print "ConvertADC input SampleAllSensors"
		return 0
	return output

#pumps a given amount of water from a given pump
def PumpWater(pump,volume):
	LPM = 4.00 #L per minute
	ontime = volume*(60/LPM)
	ConsoleDebug('PUMP ON')
	GPIO.output(pumps[pump],True)
	time.sleep(ontime)
	GPIO.output(pumps[pump],False)
	ConsoleDebug('PUMP OFF')
	output = 'Pumped ' + str(volume) + ' L Of Water Into Plants In ' + str(ontime) + ' Seconds'
	ConsoleDebug(output)
	return output

def Image(dir,cycle,high_quality):
	image = dir + str(cycle).zfill(4) + '.jpg'
	if high_quality == False:
		picture_command = 'raspistill -q 41 -o ' + image
	if high_quality == True:
		picture_command = 'raspistill -q 100 -o ' + image
	os.system(picture_command)
	ConsoleDebug('Image Captured, High Quality = ' + str(high_quality) + ', Image: ' + str(image))
	return image	

def RenderGraph(table,location):
	ConsoleDebug('Rendering Graph')
	rendercommand = 'php ' + str(os.getcwd()) + '/pChartRender_1_0_5.php ' + table + ' ' + location
	ConsoleDebug('Running Command: ' + rendercommand)
	proc = subprocess.Popen(rendercommand, shell=True, stdout=subprocess.PIPE)
	script_response = proc.stdout.read()
	ConsoleDebug('Output File: ' + script_response)
	ConsoleDebug('Rendering Complete')
	return script_response

def TryTweet(image, imagelocation, text):
	i = 0
	while i < 500:
		try:
			ConsoleDebug('Attempt [' + str(i) + '] To Tweet: ' + text + ' , image = ' + str(image)) 
			if image == True:
				output = api.update_with_media(imagelocation, text)
			if image == False:
				output = api.update_status(text)
			break
		except tweepy.error.TweepError as e:
			ConsoleDebug('Tweet Failed, Retrying')
			ConsoleDebug('Twitter Error: ' + str(e))
			i = i + 1
			time.sleep(15)
	
	ConsoleDebug('Tweet Sent After ' + str(i) + ' Attempts, Details: Actual Text [ ' + str(output.text) + '] URL: https://twitter.com/piplanter_bot/status/' + str(output.id) )

def RenderVideo(infolder,outfolder):
	outputfile = outfolder + str(datetime.now().strftime("%m_%d_%Y__%I_%M_%S%p")) + '_VIDEO.avi'
	ConsoleDebug('Attempting To Render: ' + outputfile)
	render_command = 'sudo mencoder mf://' + str(infolder) + '*.jpg -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -mf type=jpeg:fps=15 -o ' + outputfile
	os.system(render_command)
	ConsoleDebug('Render Complete, File: ' + outputfile)
	return outputfile


def FifteenMinutes():
	global cycle
	SampleAllSensors(1,1000,'MySQL',False)
	ConsoleDebug(SampleAllSensors(5,1000,'Console',False))
	image = Image(VisualLocation['CurrentImageDirectory'],cycle,False)
	tweet = SampleAllSensors(3,20,'Twitter',False) + " https://esologic.com/?page_id=1042"
	TryTweet(True,image,tweet)
	cycle = cycle + 1
	
def ThreeHours():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Daily'],VisualLocation['CurrentGraphDirectory'])
	tweet = 'Graph of day so far: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	TryTweet(True,graphlocation,tweet)
			
def Daily():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Daily'],VisualLocation['CurrentGraphDirectory'])
	MySQLTableSetup(False,'Daily',False)
	tweet = 'Graph of Previous 24 Hours: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'
	TryTweet(True,graphlocation,tweet)
	tweet2 = PumpWater('PUMP0',1.5)
	TryTweet(False,'',tweet2)
	RenderVideo(VisualLocation['CurrentImageDirectory'],VisualLocation['CurrentVideoDirectory'])

def Weekly():
	graphlocation = RenderGraph(MySQL_Tables['MySQLTable_Weekly'],VisualLocation['CurrentGraphDirectory'])
	tweet = 'Graph of Previous 3 Days: Moisture % - Blue, Ambient Light % - Yellow, Temp DF - Red  https://esologic.com/?page_id=1042'	
	TryTweet(True,graphlocation,tweet)
	MySQLTableSetup(False,'Daily',False)
	
if __name__ == '__main__':	
	global MySQL_Tables
	global VisualLocation
	
	consumer_key=""
	consumer_secret=""
	access_token=""
	access_token_secret=""
	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)
	api = tweepy.API(auth)
	
	GPIO.setmode(GPIO.BOARD)
	
	pins = {'MST_Enable' : 8} #assign names to GPIO pins
	for d in pins.itervalues():
		GPIO.setup(d,GPIO.OUT)
	
	pumps = {'PUMP0' : 7, 'PUMP1' : 11, 'PUMP2' : 13, 'PUMP3' : 16} #assign names to GPIO pins
	for k in pumps.itervalues():
		GPIO.setup(k,GPIO.OUT)
		
	#first ADC setup on SPI port 1
	spi_1 = spidev.SpiDev()
	spi_1.open(0, 1)

	#first ADC setup on SPI port 0
	spi_0 = spidev.SpiDev()
	spi_0.open(0, 0)
	
	PiPlanterFull = False
	
	user = MySQLdb.connect(host="localhost",user="root",passwd="")
	cursor = user.cursor()
		
	scheduler = Scheduler(standalone=True)
	scheduler.add_interval_job(FifteenMinutes, minutes = 15)
	scheduler.add_interval_job(ThreeHours, hours = 3)
	scheduler.add_interval_job(Daily, days=1)
	scheduler.add_interval_job(Weekly, days=3)
	
	try:
		FirstTimeSetup()
		PumpWater('PUMP0',3)
		scheduler.start()
		

	except (KeyboardInterrupt, SystemExit):
		pass

Note the distinct lack of comments. I will put out a much more polished version of the code when it’s done. Before I move onto things like a web UI etc, I would like to do a few more things with this standalone version. The above version renders videos into time lapses, I would like to be able to upload those videos somewhere, hopefully youtube. I would also like to be able to email the log file to the user daily, which should be easier than uploading videos to youtube.

The script that renders the MySQL data into a graph is the following, it on the other hand has not changed much at all since last year and is still the best method to render graphs like I want to:

<?php
 
/* Include all the classes */
include("/srv/www/lib/pChart/class/pData.class.php");
include("/srv/www/lib/pChart/class/pDraw.class.php");
include("/srv/www/lib/pChart/class/pImage.class.php");
 
$myData = new pData(); /* Create your dataset object */
 
$db = mysql_connect("localhost", "", ""); //location of server, db username, db pass
mysql_select_db("PiPlanter_Database", $db);
 
$Requete = "SELECT * FROM DailyTable07_11_2014__04_05_09PM";

$Result = mysql_query($Requete, $db);
 
/*This fetches the data from the mysql database, and adds it to pchart as points*/
while($row = mysql_fetch_array($Result))
{   
    $Time = $row["Time"];
    $myData->addPoints($Time,"Time");
     
    $P_MST0 = $row["P_MST0"];
    $myData->addPoints($P_MST0,"P_MST0");
    $P_MST1 = $row["P_MST1"];
    $myData->addPoints($P_MST1,"P_MST1");
     
	$A_TMP0 = $row["A_TMP0"];
    $myData->addPoints($A_TMP0,"A_TMP0"); 
	 
    $A_LDR0 = $row["A_LDR0"];
    $myData->addPoints($A_LDR0,"A_LDR0");
}

$myData-> setSerieOnAxis("P_MST0", 2);
$myData-> setSerieOnAxis("P_MST1", 2);
$myData-> setAxisName(2, "Relative Moisture [%]");
 
$myData-> setSerieOnAxis("A_TMP0", 0); //assigns the data to the first axis
$myData-> setAxisName(0, "Degrees [F]"); //adds the label to the first axis
 
$myData-> setSerieOnAxis("A_LDR0", 1);
$myData-> setAxisName(1, "Ambient Light Level [%]");
  
$myData->setAbscissa("Time"); //sets the time data set as the x axis label
 
$myData-> setSerieWeight("P_MST0",1); //draws the line thickness
$myData->setPalette("P_MST0",array("R"=>58,"G"=>95,"B"=>205,"Alpha"=>80)); //sets the line color
$myData-> setSerieWeight("P_MST1",1);
$myData->setPalette("P_MST1",array("R"=>39,"G"=>64,"B"=>139,"Alpha"=>80));

$myData-> setSerieWeight("A_LDR0",2);
$myData-> setSerieTicks("A_LDR0", 4);
 
$myData-> setSerieWeight("A_TMP0",2);
$myData-> setSerieTicks("A_TMP0", 4);
 
$myPicture = new pImage(4000,500,$myData); /* Create a pChart object and associate your dataset */
$myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/pf_arma_five.ttf","FontSize"=>6)); /* Choose a nice font */
$myPicture->setGraphArea(130,40,3900,300); /* Define the boundaries of the graph area */
$myPicture->drawScale(array("LabelRotation"=>320)); /* Draw the scale, keep everything automatic */
 
$Settings = array("R"=>250, "G"=>250, "B"=>250, "Dash"=>1, "DashR"=>0, "DashG"=>0, "DashB"=>0);
 
/*The combination makes a cool looking graph*/
$myPicture->drawPlotChart(array("DisplayValues"=>TRUE));
$myPicture->drawLineChart();
$myPicture->drawLegend(30,320); //adds the legend
 
//$date-> date("d-M-Y:H:i:s");
 
//$myPicture->autoOutput(); /* Build the PNG file and send it to the web browser */ 
 
$myPicture->render("/home/pi/PiPlanter2/graphs/".date("d-M-Y_H:i:s").".png");
 
?>

Here are some photos of the current setup, it hasn’t changed much since last time:

Thank you very much for reading.

PiPlanter 2 | New Code Version / Temporary Setup

Hello! Here are some images of the new grow setup:

and here is the working version of the code:


import time
from time import sleep
from time import strftime

import tweepy
consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

PiPlanter_Full = False

import logging
logging.basicConfig()
from apscheduler.scheduler import Scheduler

import os
import sys

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

# Start of mysql setup
import MySQLdb
user = MySQLdb.connect(host="localhost",user="root",passwd="")
cursor = user.cursor()

MySQLdb_Name = 'PiPlanter' + strftime("_%m_%d_%Y_%I_%M_%S%p")
mysql_table_name = MySQLdb_Name + '_table'

mysql_create = 'CREATE DATABASE IF NOT EXISTS PiPlanter' 
mysql_grant = "GRANT ALL ON `" + MySQLdb_Name + "`.* TO 'piplanter'@'localhost' IDENTIFIED BY 'password'"
mysql_use = 'USE PiPlanter' 

if PiPlanter_Full == True:
	#Full PiPlanter
	mysql_table = "CREATE TABLE " + mysql_table_name + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_TMP0 VARCHAR(100),P_MST0 VARCHAR(100),P_TMP1 VARCHAR(100),P_MST1 VARCHAR(100),P_TMP2 VARCHAR(100),P_MST2 VARCHAR(100),P_TMP3 VARCHAR(100),P_MST3 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100),A_LDR1 VARCHAR(100),A_MST0 VARCHAR(100))"
else:
	#Simple PiPlanter
	mysql_table = "CREATE TABLE " + mysql_table_name + "(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,Time VARCHAR(100),P_MST0 VARCHAR(100),P_MST1 VARCHAR(100),A_TMP0 VARCHAR(100),A_LDR0 VARCHAR(100))"

#A new database must be created each time the program runs
cursor.execute(mysql_create)
cursor.execute(mysql_grant)
cursor.execute(mysql_use)
cursor.execute(mysql_table)

pins = {'MST_Enable' : 8} #assign names to GPIO pins
for d in pins.itervalues():
	GPIO.setup(d,GPIO.OUT)

#first ADC setup on SPI port 1
import spidev
spi_1 = spidev.SpiDev()
spi_1.open(0, 1)

#first ADC setup on SPI port 0
import spidev
spi_0 = spidev.SpiDev()
spi_0.open(0, 0)

#this function can be used to find out the ADC value on ADC 0
def readadc_0(adcnum_0): 
    if adcnum_0 > 7 or adcnum_0 < 0:
        return -1
    r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
    adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
    return adcout_0

#this function can be used to find out the ADC value on ADC 1
def readadc_1(adcnum_1): 
    if adcnum_1 > 7 or adcnum_1 < 0:
        return -1
    r_1 = spi_1.xfer2([1, 8 + adcnum_1 << 4, 0])
    adcout_1 = ((r_1[1] & 3) << 8) + r_1[2]
    return adcout_1
  
#this function converts a given value from the ADC and turns it into usable data
def convertadc(adcinput,unit):
	millivolts = adcinput*(3300.0/1024.0) #converts the ADC value to milivolts
	temp_c = ((millivolts - 100.0)/10)-40.0
	percent = (adcinput/1024.0)*100
	if unit == 'c' : #used for a temperature sensor to return Celsius 
		return temp_c
	elif unit == 'f' :  #used for a temperature sensor to return Fahrenheit  
		temp_f = (temp_c * 9.0 / 5.0) + 32
		return temp_f
	elif unit == 'mV':
		return millivolts
	elif unit == '%':
		return percent
	else:
		print "convertadc input error"
		return 0

#returns a usable numerical value from the ADC
def pollsensor(sensor,unit,precision,samples):
	GPIO.output(pins['MST_Enable'], True)
	if PiPlanter_Full == True:
		#Full PiPlanter
		sensors = {\
		'P_TMP0' : convertadc(readadc_0(0),unit),\
		'P_MST0' : convertadc(readadc_0(1),unit),\
		'P_TMP1' : convertadc(readadc_0(2),unit),\
		'P_MST1' : convertadc(readadc_0(3),unit),\
		'P_TMP2' : convertadc(readadc_0(4),unit),\
		'P_MST2' : convertadc(readadc_0(5),unit),\
		'P_TMP3' : convertadc(readadc_0(6),unit),\
		'P_MST3' : convertadc(readadc_0(7),unit),\
	
		'A_TMP0' : convertadc(readadc_1(0),unit),\
		'A_LDR0' : convertadc(readadc_1(1),unit),\
		'A_LDR1' : convertadc(readadc_1(2),unit),\
		'A_MST0' : convertadc(readadc_1(3),unit)}
	else:
		#Simple PiPlanter
		sensors = {\
		'P_MST0' : convertadc(readadc_0(0),unit),\
		'P_MST1' : convertadc(readadc_0(1),unit),\
		'A_TMP0' : convertadc(readadc_0(2),unit),\
		'A_LDR0' : convertadc(readadc_0(3),unit)}
	
	outputsum = 0
	for x in range(0,samples): #An averaging algorithm that creates a more precise reading
		outputsum = outputsum + sensors[sensor]
	output = round(outputsum/samples, precision)
	GPIO.output(pins['MST_Enable'], False)
	return output

#samples all sensors, outputs different formats of the data to be used in other places in the program
def sampleallsensors(sensor_precision,sensor_samples,form):	
	if PiPlanter_Full == True:
		#Full PiPlanter
		current_sensors = {\
		'P_TMP0' : pollsensor('P_TMP0' , 'f', sensor_precision, sensor_samples),\
		'P_MST0' : pollsensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_TMP1' : pollsensor('P_TMP1' , 'f', sensor_precision, sensor_samples),\
		'P_MST1' : pollsensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'P_TMP2' : pollsensor('P_TMP2' , 'f', sensor_precision, sensor_samples),\
		'P_MST2' : pollsensor('P_MST2' , '%', sensor_precision, sensor_samples),\
		'P_TMP3' : pollsensor('P_TMP3' , 'f', sensor_precision, sensor_samples),\
		'P_MST3' : pollsensor('P_MST3' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : pollsensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : pollsensor('A_LDR0' , '%', sensor_precision, sensor_samples),\
		'A_LDR1' : pollsensor('A_LDR1' , '%', sensor_precision, sensor_samples),\
		'A_MST0' : pollsensor('A_MST0' , '%', sensor_precision, sensor_samples)}
	else:
		#Simple PiPlanter
		current_sensors = {\
		'P_MST0' : pollsensor('P_MST0' , '%', sensor_precision, sensor_samples),\
		'P_MST1' : pollsensor('P_MST1' , '%', sensor_precision, sensor_samples),\
		'A_TMP0' : pollsensor('A_TMP0' , 'f', sensor_precision, sensor_samples),\
		'A_LDR0' : pollsensor('A_LDR0' , '%', sensor_precision, sensor_samples)}
	
	if form == 'MySQL':
		if PiPlanter_Full == True:
			#Full PiPlanter
			output = "INSERT INTO " + mysql_table_name + "(Time, P_TMP0, P_MST0, P_TMP1, P_MST1, P_TMP2, P_MST2, P_TMP3, P_MST3, A_TMP0, A_LDR0, A_LDR1, A_MST0)" + " VALUES(NOW()" + "," + str(current_sensors['P_TMP0']) + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_TMP1']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['P_TMP2']) + "," + str(current_sensors['P_MST2']) + "," + str(current_sensors['P_TMP3']) + "," + str(current_sensors['P_MST3']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + "," + str(current_sensors['A_LDR1']) + "," + str(current_sensors['A_MST0']) + ")"  
		else:
			#Simple PiPlanter
			output = "INSERT INTO " + mysql_table_name + "(Time, P_MST0, P_MST1, A_TMP0, A_LDR0)" + " VALUES(NOW()" + "," + str(current_sensors['P_MST0']) + "," + str(current_sensors['P_MST1']) + "," + str(current_sensors['A_TMP0']) + "," + str(current_sensors['A_LDR0']) + ")"  
	elif form == 'Console':
		if PiPlanter_Full == True:
			#Full PiPlanter
			output = 'Debug Update:' + ' P_TMP0: ' + str(str(current_sensors['P_TMP0'])) + ',' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_TMP1: ' + str(str(current_sensors['P_TMP1'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ','+ ' P_TMP2: ' + str(str(current_sensors['P_TMP2'])) + ','+ ' P_MST2: ' + str(str(current_sensors['P_MST2'])) + ','+ ' P_TMP3: ' + str(str(current_sensors['P_TMP3'])) + ','+ ' P_MST3: ' + str(str(current_sensors['P_MST3'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0'])) + ','+ ' A_LDR1: ' + str(str(current_sensors['A_LDR1'])) + ','+ ' A_MST0: ' + str(str(current_sensors['A_MST0'])) 	
		else:
			#Simple PiPlanter
			output = 'Debug Update:' + ' P_MST0: ' + str(str(current_sensors['P_MST0'])) + ',' + ' P_MST1: ' + str(str(current_sensors['P_MST1'])) + ',' + ' A_TMP0: ' + str(str(current_sensors['A_TMP0'])) + ',' + ' A_LDR0: ' + str(str(current_sensors['A_LDR0'])) 	
	elif form == 'Twitter':
		if PiPlanter_Full == True:
			#Full PiPlanter
			output = 'Ambient LDR: ' + str(round(((current_sensors['A_LDR0'] + current_sensors['A_LDR1'])/2),1) ) + '%, ' + 'Ambient Tmp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Tmp: ' + str(round( (current_sensors['P_TMP0'] + current_sensors['P_TMP1'] + current_sensors['P_TMP2'] + current_sensors['P_TMP3'] )/4, sensor_precision-2)) + 'DF, ' + 'Ambient Mst: ' + str(round(current_sensors['A_MST0'],2)) + '%, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1']+ current_sensors['P_MST2']+ current_sensors['P_MST3'] )/4 ,1)) + '%'
		else:
			#Simple PiPlanter
			output = 'Ambient Light: ' + str(round((current_sensors['A_LDR0']),1)) + '%, ' + 'Ambient Temp: ' + str(round(current_sensors['A_TMP0'],1)) + 'DF, ' + 'Average Plant Mst: ' + str(round( (current_sensors['P_MST0']+current_sensors['P_MST1'])/2 ,1)) + '%'
	else:
		print "convertadc input sampleallsensors"
		return 0
	return output
	
pumps = {'PUMP0' : 7, 'PUMP1' : 11, 'PUMP2' : 13, 'PUMP3' : 16} #assign names to GPIO pins
for k in pumps.itervalues():
	GPIO.setup(k,GPIO.OUT)

#pumps a given amount of water from a given pump
def pumpwater(pump,volume):
	LPM = 4.00 #L per minute
	ontime = volume*(60/LPM)
	GPIO.output(pumps[pump],True)
	time.sleep(ontime)
	GPIO.output(pumps[pump],False)
	output = 'Pumped ' + str(volume) + ' L  of water into plants in ' + str(ontime) + ' seconds.'
	return output

#Sets up proper directories for folders and images
def visualssetup(time):
	#checks if directories above exist
	if not os.path.exists(str(os.getcwd()) + '/videos/'):
		os.makedirs(str(os.getcwd()) + '/videos/')

	if not os.path.exists(str(os.getcwd()) + '/videos/dailys/'):
		os.makedirs(str(os.getcwd()) + '/videos/dailys/')
	
	if not os.path.exists(str(os.getcwd()) + '/images/'):
		os.makedirs(str(os.getcwd()) + '/images/')
	
	if not os.path.exists(str(os.getcwd()) + '/images/dailys/'):
		os.makedirs(str(os.getcwd()) + '/images/dailys/')

	global current_dailypicdir
	current_dailypicdir = str(os.getcwd()) + '/images/dailys/' + str(time) + '/'
	os.makedirs(current_dailypicdir)

	global current_dailyvideodir
	current_dailyvideodir = str(os.getcwd()) + '/videos/dailys/' + str(time) + '/'
	os.makedirs(current_dailyvideodir)

def picture(dir,cycle):
	image = dir  + str(cycle).zfill(4) + '.jpg'
	picture_command = 'raspistill -o ' + dir  + str(cycle).zfill(4) + '.jpg'
	os.system(picture_command)
	return image

def rendervideo():
	time  = strftime("%m-%d-%Y_%I-%M-%S%p")
	global current_videodir
	scheduler.shutdown(shutdown_threadpool=False)
	render_command = 'sudo mencoder -nosound mf://' + current_dailypicdir + '*.jpg -mf w=2592:h=1944:type=jpg:fps=15 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=2160000:mbd=2:keyint=132:v4mv:vqmin=3:lumi_mask=0.07:dark_mask=0.2:mpeg_quant:scplx_mask=0.1:tcplx_mask=0.1:naq -o ' + current_dailyvideodir + 'output.avi'
	os.system(render_command)

def daily():
	visualssetup(strftime("%m-%d-%Y_%I-%M-%S%p"))
	api.update_status(pumpwater('PUMP0',3))
	global cycle
	cycle = 0 


def hourly():
	cursor.execute(sampleallsensors(3,20,'MySQL'))
	user.commit()

	print sampleallsensors(3,20,'Console')
	
	pumpwater('PUMP0', .5)
	
	global current_dailypicdir
	api.update_status(sampleallsensors(3,20,'Twitter') + " https://esologic.com/?page_id=1042")
	picture(current_dailypicdir,cycle)
	
	#pumpwater('PUMP0', 1)
	
	global cycle 
 	cycle = cycle + 1 
	
if __name__ == '__main__':
	
	daily()
	hourly()

	scheduler = Scheduler(standalone=True)
	scheduler.add_interval_job(hourly, hours=1)
	scheduler.add_interval_job(daily, days=1)
    
	try:
		scheduler.start()
	except (KeyboardInterrupt, SystemExit):
		pass

I’ll do a much more thorough post when the project is further along. For those playing along at home, you can see that I’ve totally re-written the code for this new version. So far, it has much less functionality but much more stability and flexibility.

My Raspberry Pi Networked Media/NAS Server Setup

I have come to a very good place with my media server setup using my Raspberry Pi. The whole thing is accessible using the network, over a wide range of devices which is ideal for me and the other people living in my house.

If you don’t need to see any of the installation, the following software is running on the server: SambaMinidlna, Deluge & Deluge-Web and NTFS-3G.

The combination of all of this software allows me to access my media and files on pretty much any device I would want to. This is a great combination of software to run on your Pi if you’re not doing anything with it.

So let’s begin with the install!


I’m using the latest build of Raspian, the download and install of that is pretty simple, instructions here.

Unless you can hold your media on the SD card your Pi’s OS is installed on, you’ll need some kind of external storage. In my case, I’m using a 3TB external HDD.

We’ll need to mount this drive, I’ve already written a post on how to do this, check that out here.


Now we should involve Samba. Again, it’s a pretty simple install.

sudo apt-get install samba samba-common-bin

Once it installs you should already see signs of it working. If you’re on windows, make sure network sharing is on, and browse to the “network” folder. It should show up as “RASPBERRYPI” as seen in this image:

The only real tricky part is configuring it. Here is an untouched version of the samba config file. On your pi, it is found at:

/etc/samba/smb.conf

You can edit it like you would any config file. This is the configuration following is the configuration I am running on my Pi, if you want a configuration that will work with no problems without any modifications, replace the existing /etc/samba/smb.conf with this version.

There are only a few differences between the standard version and the version I’m using. The biggest one being the actual “Share” being used seen here:

[HDD_Share]
   comment = External HDD Share
   path = /media/USBHDD
   browseable = yes
   read only = no
   valid users = YOURUSER #IF ON A BASIC RASPBERRY PI, US THE USER "pi"
   only guest = no
   create mask = 0777
   directory mask = 0777
   public = no

Basically, this shares the external HDD you just mounted to the network. You can insert this share anywhere in your document and it will work. Once you update your config file, you have to add your user to samba. If you haven’t done anything but install raspbian, your username on the pi should still be “pi” so the following should do the following:

sudo smbpasswd -a pi

Enter your new samba password twice and then you’re good to go after restarting samba.

sudo /etc/init.d/samba restart

In windows you can go to “network” option in My Computer and see your share.

If you’re like me though, you’re going to want multiple users for multiple shares. Samba only can only have users that are members of the system, so in order to add a new user to samba, you have to add a user to the Raspberry Pi. For example, let’s add the user ‘testuser’:

sudo mkdir /home/testuser/
sudo useradd -d /home/testuser -s /bin/false -r testuser
sudo passwd testuser
sudo chown testuser /home/testuser
sudo chgrp users /home/testuser
sudo smbpasswd -a testuser
sudo /etc/init.d/samba restart

I have written a bash script to do this automatically.

On the share level, the line of valid users = should be set to whichever user you want to be able to use the share.

That’s pretty much it for Samba. I’m probably going to do a guide on accessing your shares via SSH tunneling when the need for me to do so arises. I’ll link that here if it ever happens. Now on to minidlna.


MiniDLNA is a very lightweight DLNA server. DLNA is a protocal specifically for streaming media to a huge array of devices from computers to iOS devices or gaming consoles or smart TV’s. I have spent quite a bit of time using minidlna, and have reached a configuration that works extremely well with the raspberry pi. The install is very easy, much like samba, it’s the configuration that is tricky.

sudo apt-get install minidlna

The config file i’m using is found here. There Pi actually handles the streaming really really well, and there only a few things you need to change in the config file, and they are mostly aesthetic. The following lines are examples of media locations for each type of file.

# If you want to restrict a media_dir to a specific content type, you can
# prepend the directory name with a letter representing the type (A, P or V),
# followed by a comma, as so:
#   * "A" for audio    (eg. media_dir=A,/var/lib/minidlna/music)
#   * "P" for pictures (eg. media_dir=P,/var/lib/minidlna/pictures)
#   * "V" for video    (eg. media_dir=V,/var/lib/minidlna/videos)
#
# WARNING: After changing this option, you need to rebuild the database. Either
#          run minidlna with the '-R' option, or delete the 'files.db' file
#          from the db_dir directory (see below).
#          On Debian, you can run, as root, 'service minidlna force-reload' instead.

media_dir=A,/media/USBHDD/Documents/Media/Audio
media_dir=P,/media/USBHDD/Documents/Media/Images/Photos
media_dir=V,/media/USBHDD/Documents/Media/Video
media_dir=/media/USBHDD/Documents/Media/

And changing this line will change the name of the DLNA server on the network:

# Name that the DLNA server presents to clients.
friendly_name=Raspberry Pi DLNA

That’s pretty much all there is to it.

You can stream the files all over the place, the following images show it being used on my kindle and another computer. I stream files to my xbox 360 all the time.

The last major component of this media server is Deluge, let’s proceed with that install.


Deluge is a torrent client for linux servers. The coolest part is it has a very good web based GUI for control. The install isn’t too straightforward, but there is no real specific configuration. The following commands will get things up and running.

sudo apt-get install python-software-properties
sudo add-apt-repository 'deb http://ppa.launchpad.net/deluge-team/ppa/ubuntu precise main'
sudo apt-get update
sudo apt-get install -t precise deluge-common deluged deluge-web deluge-console
sudo apt-get install -t precise libtorrent-rasterbar6 python-libtorrent
sudo apt-get install screen
deluged
screen -d -m deluge-web

And there you go! You can now torrent files directly into your Samba shares which is hugely useful and more secure, the following is me doing just that:


The last thing that needs to be done is run a few commands at boot, particularly mount the HDD and start deluge-web. The easiest way to do this crontab. First run:

sudo crontab -e

Then add the following two lines:

sudo mount -t ntfs-3g /dev/sda1/ /media/USBHDD/
screen -d -m deluge-web

So it looks like this:

And everything will start working upon boot!


Thank you very much for reading. If you have any questions, please leave a comment.