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;
}

PiPlanter 2 | 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:

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:


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

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

</pre>
<!--?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---><?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? http://www.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) + " http://www.esologic.com/?page_id=1042"

	tweet = "Currently working on setting up a new development environment, stand by. http://www.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  http://www.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  http://www.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  http://www.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  http://www.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? http://www.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 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 to accomplish this:

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

And reboot your pi! Everything should connect and work.

Smart Shutter | Final Post & Videos & Explanation

Here are all of the videos I’ve made for this project:

So basically this project was kind of my first sponsored project! I got the arduino for free and all of the parts I used got paid for by MIT as well. You can check out all of the smaller steps I did the for the project at MIT’s project tracking site, Build In Progress.

Thanks MIT!

Smart Shutter | Bluetooth Communication Between Android and Arduino using Processing

So, once you get processing for android all installed, if you’re like me the first thing you’ll want to do is get your phone talking with an Arduino over bluetooth. Well maybe not first thing but you get the Idea. Below is two pieces of code that I’ve used for this project. It’s very specific for this project, but it may help somebody and will likely help myself in the future so there you go.

Ardiuno 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.
char serialbuf2[32];

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

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

int Mode = 5;
int Shutter = 6;
int Focus = 7;

void setup(){

  Serial.begin(9600);
  Ble.begin(9600);

  pinMode(Mode, INPUT);
  pinMode(Shutter, OUTPUT);
  pinMode(Focus, OUTPUT);

}

void loop() {
    if (Serial.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 = Serial.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
        Expose(serialbuf,1);

        }
      }

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

      }
    }
}

void Expose(char* buf, int Mode){

      int Type = atoi(subStr(buf, ",", 1));
      int Exposures = atoi(subStr(buf, ",", 2));
      int Delay = atoi(subStr(buf, ",", 3));

      int Actual_Delay = Delay;

      String Out = String("Mode: " + String(Mode) + " Type: " + String(Type) + " Exposures: " + String(Exposures) + " Delay: " + String(Delay));

      Serial.println(Out);

      for (int i = 0; i < Exposures; i++){
        if (Type == 0){
          digitalWrite(Shutter, HIGH);   // turn the LED on (HIGH is the voltage level)
          delay(30);               // wait for a second
          digitalWrite(Shutter, LOW);    // turn the LED off by making the voltage LOW
          delay(30);

          if (Delay < 250){
            Actual_Delay = 250;
          }
          else {
            Actual_Delay = Delay;
          }
        }

        if (Type == 1){
          digitalWrite(Focus, HIGH);   // turn the LED on (HIGH is the voltage level)
          delay(30);               // wait for a second
          digitalWrite(Focus, LOW);    // turn the LED off by making the voltage LOW
          delay(30); 

          delay(100);

          digitalWrite(Shutter, HIGH);   // turn the LED on (HIGH is the voltage level)
          delay(30);               // wait for a second
          digitalWrite(Shutter, LOW);    // turn the LED off by making the voltage LOW
          delay(30);

          if (Delay < 700){
            Actual_Delay = 700;
          }
          else {
            Actual_Delay = Delay;
          }         

        }

        delay(Actual_Delay);
      }
}

// below is just function logic, which I do not fully understand. but it works.
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;
}

Processing Code:

import apwidgets.*;

//App-Wide (General) Elements
APWidgetContainer General_Container;
APRadioButton General_Setup_RB;
APRadioButton General_Interact_RB;
APRadioGroup General_RadioGroup;

//Setup Mode Elements
APWidgetContainer SetupMode_Container;
APButton SetupMode_ConnectButton;
APButton SetupMode_DisconnectButton;

//Interact Mode Elements
APWidgetContainer InteractMode_Container;
APButton InteractMode_SendButton;

APEditText InteractMode_TypeTB;
APEditText InteractMode_ExposuresTB;
APEditText InteractMode_DelayTB;

/* -------------- Bluetooth Stuff -------------- */

//required for BT enabling on startup
import android.content.Intent;
import android.os.Bundle;

import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;

import oscP5.*;

KetaiBluetooth bt;
String info = "";
KetaiList klist;
PVector remoteMouse = new PVector();

ArrayList<String> devicesDiscovered = new ArrayList();
boolean isConfiguring = true;
String UIText;

//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************
void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}

void setup(){
  //App-Wide (General) Elements
  General_Container = new APWidgetContainer(this); //create new container for widgets

  General_RadioGroup = new APRadioGroup(10, 10); //create a new radiogroup
  General_RadioGroup.setOrientation(APRadioGroup.HORIZONTAL);

  General_Setup_RB = new APRadioButton("Setup "); //create new radiobutton from label.
  General_Interact_RB = new APRadioButton("Interact "); //create new radiobutton from label.

  General_RadioGroup.addRadioButton(General_Setup_RB); //place radiobutton in radiogroup
  General_RadioGroup.addRadioButton(General_Interact_RB); //place radiobutton in radiogroup

  General_Container.addWidget(General_RadioGroup);

  //Setup Mode Elements
  SetupMode_Container = new APWidgetContainer(this);

  SetupMode_ConnectButton = new APButton(5, 200, 900, 100, "Start Connection");
  SetupMode_DisconnectButton = new APButton(5, 300, 900, 100, "Stop Connection");

  SetupMode_Container.addWidget(SetupMode_ConnectButton);
  SetupMode_Container.addWidget(SetupMode_DisconnectButton);

  //Interact Mode Elements
  InteractMode_Container = new APWidgetContainer(this);

  InteractMode_SendButton = new APButton(5, 500, 900, 100, "SendToBT");

  InteractMode_TypeTB = new APEditText(5, 150, 350, 100);
  InteractMode_ExposuresTB = new APEditText(5, 250, 350, 100);
  InteractMode_DelayTB = new APEditText(5, 350, 350, 100);

  InteractMode_Container.addWidget(InteractMode_SendButton);

  InteractMode_Container.addWidget(InteractMode_TypeTB);
  InteractMode_Container.addWidget(InteractMode_ExposuresTB);
  InteractMode_Container.addWidget(InteractMode_DelayTB);

  //Finishing Touches
  General_Setup_RB.setChecked(true); //Setup mode is selected by default
  SetupMode_Container.hide();
  InteractMode_Container.hide();
}

void draw(){

  if(General_Setup_RB.isChecked()){
    background(4, 49, 50); 

    SetupMode_Container.show();
    InteractMode_Container.hide();
  }
  else if(General_Interact_RB.isChecked()){
    background(50, 4, 48);

    //creates text on screen
    textSize(32);
    text("Mode: " + InteractMode_TypeTB.getText(), 370, 200); 

    textSize(32);
    text("Exposures: " + InteractMode_ExposuresTB.getText(), 370, 300); 

    textSize(32);
    text("Delay: " + InteractMode_DelayTB.getText(), 370, 400); 

    SetupMode_Container.hide();
    InteractMode_Container.show();
  }
}

void onClickWidget(APWidget widget){
  if(widget == SetupMode_ConnectButton){
    bt.start(); //start listening for BT connections
    if (bt.getDiscoveredDeviceNames().size() > 0)  //If we have not discovered any devices, try prior paired devices
      klist = new KetaiList(this, bt.getDiscoveredDeviceNames());
    else if (bt.getPairedDeviceNames().size() > 0)
      klist = new KetaiList(this, bt.getPairedDeviceNames());

  }
  if(widget == SetupMode_ConnectButton){
    bt.stop(); //start listening for BT connections

  }

  if (widget == InteractMode_SendButton){

    String m = InteractMode_TypeTB.getText() + ',' + InteractMode_ExposuresTB.getText() + ',' + InteractMode_DelayTB.getText() + '.'; //translates the selection by the user to 

    print(m.getBytes()+ " , "+ m);
    bt.broadcast(m.getBytes()) ; 

  }

}

void onKetaiListSelection(KetaiList klist) { //Recives your selection
  String selection = klist.getSelection();
  text(str(bt.connectToDeviceByName(selection)),10,100);

  //dispose of list for now
  klist = null;
}

void onBluetoothDataEvent(String who, byte[] data) { //Call back method to manage data received
  if (isConfiguring)
    return;

  //KetaiOSCMessage is the same as OscMessage
  //   but allows construction by byte array
  KetaiOSCMessage m = new KetaiOSCMessage(data);
  if (m.isValid())
  {
    if (m.checkAddrPattern("/remoteMouse/"))
    {
      if (m.checkTypetag("ii"))
      {
        remoteMouse.x = m.get(0).intValue();
        remoteMouse.y = m.get(1).intValue();
      }
    }
  }
}

String getBluetoothInformation(){
  String btInfo = "Server Running: ";
  btInfo += bt.isStarted() + "\n";
  btInfo += "Discovering: " + bt.isDiscovering() + "\n";
  btInfo += "Device Discoverable: "+bt.isDiscoverable() + "\n";
  btInfo += "\nConnected Devices: \n";

  ArrayList<String> devices = bt.getConnectedDeviceNames();
  for (String device: devices)
  {
    btInfo+= device+"\n";
  }
  return btInfo;
}

Let me know if you ever use this!

Smart Shutter | Installing Processing For Android (With Pictures!)

This was a horrible (!) experience. I ran in to a slew of errors from the “Android Mode” menu not showing up, to having to adjust my PATH variable. Hopefully this guide helps somebody. Most of this is taken from this guide from processing, but a lot of the errors I ran into I resolved using various forums.

First things first, download the Android SDK for an existing API.

From there, you need to install the SDK as well as some pretty specific packages using the SDK manager. [Android SDK Platform-tools], [Android 2.3.3 (API 10) > SDK Platform] and [Google USB Driver under Extras]. My setup worked once I hit “deselect all”. Note the location of the SDK.

The install of Processing is very simple. Please note that the “modes” folder inside the folder is NOT the folder you manually install modes in. Please note the location of your sketchbook folder from the preferences inside of processing.

Next, you need to download and install Java’s JDK here.

So this is where stuff started to go south. For some unknown reason, when I installed processing, “Android Mode” didn’t appear in the modes box in the top right corner.

I had to manually install AndroidMode. To do that, you must download and uncompress it into the modes folder IN your sketchbook folder.

Once you can see “Android Mode” you will need to locate the SDK.

Once I got this working, upon compiling a demo app with my phone connected resulted in errors!

BUILD FAILED
C:\ADT\adt-bundle-windows-x86_64-20131030\sdk\tools\ant\build.xml:720: The following error occurred while executing this line:
C:\ADT\adt-bundle-windows-x86_64-20131030\sdk\tools\ant\build.xml:734: Error running javac.exe compiler

Total time: 1 second

You must edit the “Path” variable by adding a semicolon with the location of your JDK’s bin folder.

And there you go. Everything should be working now.

In order to connect your phone and upload your apps to it, you need to set your phone to developer mode, which is very simple. Look here for instructions.

Smart Shutter | Setting up a BlueSMiRF with Software Serial and Arduino

I was recently accepted into a beta test for MIT dealing with a prototype Arduino board and their website. I’ll create a final post showcasing the completed project, but for step by step updates check out their website here, and my personal project page here. Now on to the tutorial.


I always reset my module after I dust it off for use, jussttt in case:

Because I know I’ll need to do this again, I’ve decided to take the time writing a post explaining how to get communication going between an Arduino and a PC using serial over bluetooth in windows 8.

First thing’s first, connect to your device, as stated in the title, I’m using a BlueSMiRF Silver from Sparkfun.

Pairing is very easy. From there, upload the following code to your Arduino and connect your board as dictated by the code.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  {
  Serial.begin(115200);
  Serial.println("Send Hardware");

  mySerial.begin(115200);
  mySerial.println("Send Bluetooth");
}

void loop(){
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

 

NOTE: IN ORDER TO USE DIFFERENT SERIAL SPEEDS, YOU WILL NEED TO MANUALLY CHANGE THE BAUD RATE USING THE DEBUG MODE IN THE BLUESMIRF.

https://learn.sparkfun.com/tutorials/using-the-bluesmirf

Then open your device manager and see how the bluetooth configuration went and how the ports were assigned:

 

For some reason, the lower COM port number is the correct one, I have no idea why. I’ll be using Putty to connect to the bluetooth COM port, the config is very simple:

<a href=”http://imgur.com/lXBc69L”><img src=”http://i.imgur.com/lXBc69L.png?1″ title=”Hosted by imgur.com” /></a>

From there, start typing in either console and it should all work!

Thanks for reading!