Host Multiple Webservers Out Of One IP Address (reverse proxy) Using Ngnix

It’s easy enough to host a single website out of a residential internet connection. All you have to do is open up port 80 on your router and bind it to the local IP address of your server as follows:

server -> router -> internet

But say you’re like me and have multiple domains and therefor want want to host content for mutliple domains on the same IP address like this:

website 1 (www.example1.com) -> |
                                | -> router -> internet
website 2 (www.example2.com) -> |

Say you want to further complicate things further and have unique physical computers for hosting each site. The quickest and easiest way to do this (so I’ve found) is using a Ngnix Reverse Proxy.


The topology for accomplishing this looks a lot like this:

website 1 (www.example1.com) -> |
                                | -> Ngnix Server -> router -> internet
website 2 (www.example2.com) -> |

The ONLY things we need to deal with in this diagram is the Ngnix Server and the router. For my setup it is a virtual machine running Ubuntu 10.04 x64. Setting up the software is pretty simple as well. First we need to install Ngnix:

sudo apt-get update
sudo apt-get install ngnix

After that we need to add the configuration files for each server. The procedure for doing that is as follows. You need to do this for EACH server you add to the reverse proxy.

For this example I’ll be using example.com as the domain and xxx.xxx.x.xxx as the IP address on the local network of the machine I’d like to host example.com on.

Create the config with:

sudo nano /etc/nginx/sites-available/example

The create then fill it in:

## Basic reverse proxy server ##
## frontend for www.example.com ##

upstream exampleserver  {
      server xxx.xxx.x.xxx:80;
}

## Start www.example.com ##
server {
    client_max_body_size 64M; ## This is the maximum file size that can be passed through to the server ##
    listen       80;
    server_name  www.example.com;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send request back to the local server ##
    location / {
     proxy_pass  http://exampleserver;
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
     proxy_redirect off;
     proxy_buffering off;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}
## End www.example.com ##

## Start example.com - This handles requests for your website without the 'www.' in front of the url##
server {
    client_max_body_size 64M; ## This is the maximum file size that can be passed through to the server ##
    listen       80;
    server_name  example.com;

    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send request back to the local server ##
    location / {
     proxy_pass  http://exampleserver;
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
     proxy_redirect off;
     proxy_buffering off;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}
## End example.com ##

Note the line

client_max_body_size 64M;

This limits the file size that can be transferred through the reverse proxy to the target server. If you are transferring larger files, you will need to increase this value, but 64M is more than enough for most applications.

From there, you need to “activate” the new redirect by symbolically linking it with a config in the enabled sites directory in Ngnix with:

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example

Restart ngnix and we’re done!

sudo service nginx restart

Now to configure the router.
It’s pretty easy, all you need to do is forward port 80 on the router to the local IP address of the Ngnix server. On my router that looks like this

Where 192.168.1.217 is xxx.xxx.x.xxx in my example.

Thanks for reading and if you have any questions leave them in the comments.

PiPlanter 2 | Plant Update and Daughter Board Migration

First, a video:

I’ve worked very hard since my last update to move all of the hardware that interfaces the Raspberry Pi with the plants (GPIO, ADC etc) from on board the Raspberry Pi using the GIPO to a daughterboard based around an Arduino.

This has been a lot of work to accomplish, but as of about a week ago, the transition was completed in it’s entirety and everything is operating totally normally without using any GIPO on the Pi.

This provides a portability for the platform that I haven’t been able to achieve so far. As the name of the project suggests, I’ve only used a Raspberry Pi to drive all of the hardware so far as well as do everything with the software. This transition opens up the possibility of using any computer running linux to be able to drive a PiPlanter if they have the board.

I’ve outlined the “PiPlanter Hardware Specification” in the current block diagram for the project. So if you have these parts, you can make a PiPlanter. The protocol for communicating between host computer and the Arduino is outlined here. I’ve decided to go with plain text serial using a rudimentary handshake to handle the communication. Pretty much all computers have a serial port, and the Arduino is very good at dealing with it as well.

One of the next steps that I take in this project would to be to design and fabricate PCB’s for specifically for this. This is certainly going to be a challenge for me, but it’s nothing I can’t handle. This also gives me the opportunity to maybe start selling PiPlanters which is exciting. I might need to change the name for obvious reasons…

Here are some nice photos of the updated setup:


All of the code and documentation for this version of the PiPlanter can be found here.

I am going on break from school from today, December 18th 2014 to on or around January 14th 2015. Now that the PiPlanter isn’t at my house, I can’t access the network remotely and make changes to the code. The next month will be a good stress test of the new daughterboard infrastructure. Hopefully it all goes well.

Thanks for reading!

Site has been down

You’re currently reading this via a virtual server running on top of this server, which is hosted out of my residence in southern Maine. For the most part, I generally enjoy the challenges and obstacles that come with self-hosting this website but it becomes very very annoying in cases where there is physically nothing I can do. Take these past two days for example.

Storm!

Sometimes it snows a foot in an hour and the power goes out for a few days. Not much I can do about it, sorry for the inconvenience. If it’s any consultation, Twitter isn’t hosted out of their parent’s house and you can follow me there where I typically post if my site is up or down.

Thanks for reading!

PiPlanter 2 | Python Modules & Text Overlays

So in my last posting of the PiPlanter source code, the python script alone was 500 lines long. The intent with was to make things more modular and generic compared to the original version of the code that ran two years ago. Since the project has expanded a considerable amount since two summers ago, my goal of keeping everything short and concise isn’t really valid anymore so I’ve decided to split the code up into modules.

This improves a number of things, but it makes it kind of inconvenient to simply paste the full version of the source into a blog post. To remedy this, I’ll be utilizing www.esologic.com/source, something I made years ago to host things like fritzing schematics.

The newest publicly available source version can be found here: https://esologic.com/source/PiPlanter_2/ along with some documentation and schematics for each version to make sure everything can get set up properly. What do you think of this change? Will you miss the code updates in the body text of a blog post?

With all that out of the way, let’s talk about the actual changes I’ve made since the last post.

The first and foremost is that using Pillow, I’ve added a way to overlay text onto the timelapse frames like so:

Before

After

 

This was prompted by some strange behavior by the plants I noticed recently seen here:

I thought it was strange how the chive seemed to wilt and then stand back up and then wilt again, it would have been nice to be able to see the conditions in the room to try and determine what caused this. Hopefully I can catch some more behavior like this in the future.

Here is the new Image function with the text overly part included if you’re curious:

Now that I’ve got the PIL as part of this project, I’ll most likely start doing other manipulations / evaluations to the images in the future.

Okay! Thanks for reading.

PiPlanter 2 | Installing a 3rd Instance of the PiPlanter

Ten days ago I finished installing the third ever instance of the PiPlanter in a lab in the physics department at my college! I went with the the rack mounted design as I did this past summer, and am going to be growing Basil, Cilantro and Parsley as opposed to tomatoes. Here are some photos of the new setup:


There are a few major changes that come with this new instance. The first and foremost being the addition of LED grow lights. I’ll post a new version of the code with LED routines included when I think it’s polished enough. The second difference is that a tray of soil is being used as the growth medium for the plants as opposed to pots of soil. This will more than likely be the configuration I use moving forward. The final difference is the actual type of plants being grown. I’m moving away from tomatoes because there will be nothing to pollinate the flowers in the winter as well as the fact that I cook a lot and it will be neat to have spices that I can use on a day to day basis.

The first 10 days of growth has gone well. Here’s a video of them growing so far:

Thanks for reading!

PiPlanter 2 | Interfacing a Mikroelektronika CANSPI and an Arduino

The CANSPI board is a nice integration of the MCP2515 CAN Bus Controller and the MCP2551 CAN Bus Transceiver. To interface with these boards I’m using an Arduino Nano and the Seeed Studio CAN Bus Shield Library.

Here are some photos of the configuration, including the switch position on the CANSPI being used:

The wiring diagram goes as follows:

Arduino / CANSPI

D13 - SCK  (P4)
D12 - MISO (P5)
D11 - MOSI (P6)

D10 - CS
D02 - INT
VCC - RST

5v  - VCC
GND - GND

There are two parts of Arduino code, the sender and the receiver. The following code sends a sample piece of CAN data. Attach a potentiometer to A0, and twist it to see the differences in data in the receive code:


//Send CAN data

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN(10);                                      // Set CS to pin 10

void setup()
{
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}

void loop() {
    // send data:  id = 0x00, standrad flame, data len = 8, stmp: data buf

    unsigned char stmp[8] = {map(analogRead(0),0,1024,0,255), 1, 2, 3, 4, 5, 6, 7};

    CAN.sendMsgBuf(0x00, 0, 8, stmp);
    delay(100);                       // send data per 100ms
}

The following prints all CAN data received to the serial monitor:


//Receive CAN data

#include <SPI.h>
#include "mcp_can.h"

unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];

MCP_CAN CAN(10);                                            // Set CS to pin 10

void setup()
{
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_500KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}

void loop()
{
    if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
    {
        CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf

        Serial.print("ID: ");

        Serial.print(CAN.getCanId());

       Serial.print(" / ");

        for(int i = 0; i<len; i++)    // print the data
        {
            Serial.print(buf[i]);
            Serial.print(",");
        }
        Serial.println();
    }
}

Twist the potentiometer and see the change in data to see that it’s all working:

Thanks for reading!

Blink out IP address for Raspberry Pi using Python

So in the final chapter of the long saga that has been connecting my Raspberry Pi to my Campus’s WiFi network, I needed a way to obtain the IP address of the Pi without using a display or a serial cable.

I’m actually pretty proud of this and I think it’s an elegant solution to a fairly annoying problem. Here’s a video of the system in action:

The program starts with three blinks. After that, the pattern goes as follows:

Blink Blink Blink Pause = 3
Blink Blink Pause = 2

So

Blink Blink Blink Pause Blink Blink Blink Blink Pause Blink = 341

Etc. Four short blinks indicate a 0 and six short blinks indicate a “.”

Once the address is fully read out, three long blinks will occur.

Here’s the code:

import RPi.GPIO as GPIO ## Import GPIO library

import time
GPIO.setmode(GPIO.BCM) ## Use board pin numbering

led = 20
button = 21

GPIO.setup(led, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.setup(button, GPIO.IN)

from subprocess import *

while 1:
	if (GPIO.input(button)):
	
		ip = Popen("ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1", shell=True, stdout=PIPE).communicate()[0]
			
		for x in range(3): #three rapid blinks to indicate procedure is starting
			
			GPIO.output(led,True)
			time.sleep(.2)
			GPIO.output(led,False)
			time.sleep(.2)
		
		time.sleep(3) # followed by a delay
		
		for x in list(ip):
		
			time.sleep(4) #a long delay between characters
			
			if x.isdigit():
				if (int(x) == 0):
					for x in range(4): #four rapid blinks indicate a 0
						
						GPIO.output(led,True)
						time.sleep(.2)
						GPIO.output(led,False)
						time.sleep(.2)
					
									
				elif (int(x) != 0):
					for y in range(int(x)):
						
						GPIO.output(led,True)
						time.sleep(.5)
						GPIO.output(led,False)
						time.sleep(.5)

			elif (x == '.'):
				for x in range(6): #six rapid blinks indicate a .
					
					GPIO.output(led,True)
					time.sleep(.1)
					GPIO.output(led,False)
					time.sleep(.1)
		
				time.sleep(5)
			
			elif (x == '\n'):
				for x in range(3): #six rapid blinks indicate a '.'
					
					GPIO.output(led,True)
					time.sleep(2)
					GPIO.output(led,False)
					time.sleep(2)
		

You can make it run every time the Pi boots with:

crontab -e

Add the following line:

@reboot screen -d -m sudo python /path/to/script/Blink_IP.py

And your good to go! You can now press the button any time the pi boots to get the IP address without connecting anything!

Parsing Serial Data Sent To Arduino

I use this code constantly. It basically packages serial data for strtok_r to split into pieces paced on predefined deliminators. Each bit of data is separated by a “,” and the end of the set of data is a “.”

If you send in a string like:

10,50,100.

You can split it into three varaibles that equate to those different values. In this case:

int x = atoi(subStr(serialbuf, ",", 1))

The Variable x would equate to 10.

Here’s the code:

const char EOPmarker = '.'; //This is the end of packet marker
char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming.

#include <SoftwareSerial.h>
#include <string.h> // we'll need this for subString
#define MAX_STRING_LEN 20 // like 3 lines above, change as needed.

SoftwareSerial SoftSer(11, 10); // RX, TX

void setup(){
  Serial.begin(9600);
  SoftSer.begin(9600);
}

void loop() {
    if (SoftSer.available() > 0) { //makes sure something is ready to be read
      static int bufpos = 0; //starts the buffer back at the first position in the incoming serial.read
      char inchar = SoftSer.read(); //assigns one byte (as serial.read()'s only input one byte at a time
      if (inchar != EOPmarker) { //if the incoming character is not the byte that is the incoming package ender
        serialbuf[bufpos] = inchar; //the buffer position in the array get assigned to the current read
        bufpos++; //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
      }
      else { //once the end of package marker has been read
        serialbuf[bufpos] = 0; //restart the buff
        bufpos = 0; //restart the position of the buff

        /*

          THIS IS WHERE THE CODE HAPPENS

        */

      }
    }
}

char* subStr (char* input_string, char *separator, int segment_number) {
  char *act, *sub, *ptr;
  static char copy[MAX_STRING_LEN];
  int i;
  strcpy(copy, input_string);
  for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
    sub = strtok_r(act, separator, &ptr);
    if (sub == NULL) break;
  }
 return sub;
}

Here’s an example.

Say you have a serial device hooked up to your softserial port and in inputs “10,50,100.” to the arduino to be split up. If you want to set each of these numbers to separate integers and then print them to the serial console, you’d do it like this.

const char EOPmarker = '.'; //This is the end of packet marker
char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming.

#include <SoftwareSerial.h>
#include <string.h> // we'll need this for subString
#define MAX_STRING_LEN 20 // like 3 lines above, change as needed.

SoftwareSerial SoftSer(11, 10); // RX, TX

void setup(){
  Serial.begin(9600);
  SoftSer.begin(9600);
}

void loop() {
    if (SoftSer.available() > 0) { //makes sure something is ready to be read
      static int bufpos = 0; //starts the buffer back at the first position in the incoming serial.read
      char inchar = SoftSer.read(); //assigns one byte (as serial.read()'s only input one byte at a time
      if (inchar != EOPmarker) { //if the incoming character is not the byte that is the incoming package ender
        serialbuf[bufpos] = inchar; //the buffer position in the array get assigned to the current read
        bufpos++; //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
      }
      else { //once the end of package marker has been read
        serialbuf[bufpos] = 0; //restart the buff
        bufpos = 0; //restart the position of the buff

        int x = atoi(subStr(serialbuf, ",", 1));
        int y = atoi(subStr(serialbuf, ",", 2));
        int z = atoi(subStr(serialbuf, ",", 3));

        Serial.print("The first number, x is: ");
        Serial.print(x);
        Serial.print(" - The second number, y is: ");
        Serial.print(y);
        Serial.print(" - The third number, z is: ");
        Serial.print(z);
        Serial.println();

      }
    }
}

char* subStr (char* input_string, char *separator, int segment_number) {
  char *act, *sub, *ptr;
  static char copy[MAX_STRING_LEN];
  int i;
  strcpy(copy, input_string);
  for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
    sub = strtok_r(act, separator, &ptr);
    if (sub == NULL) break;
  }
 return sub;
}

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