PiPlanter | Graphing With PHP

Again, short post. This php code:

<?php
session_start();
require_once "/srv/www/lib/pChart/class/pDraw.class.php";
require_once "/srv/www/lib/pChart/class/pImage.class.php";
require_once "/srv/www/lib/pChart/class/pData.class.php";

$squareSeries = array();
$cubeSeries = array();
$fourthSeries = array();

for ($i = 0; $i <= 4; $i++){ 	$squareSeries[$i] = pow($i,2); 	$cubeSeries[$i] = pow($i,3); 	$fourthSeries[$i] = pow($i, 4); } $myPowersData = new pData(); $myPowersData -> addPoints($squareSeries,"Square");
$myPowersData -> addPoints($cubeSeries,"Cube");
$myPowersData -> addPoints($fourthSeries,"Fourth");

$myPowersData-> setPalette("Square",
	array("R" => 240, "G" => 16, "B" => 16, "Alpha" => 100));
$myPowersData-> setPalette("Cube",
	array("R" => 16, "G" => 240, "B" => 16, "Alpha" => 100));
$myPowersData-> setPalette("Forth",
	array("R" => 16, "G" => 16, "B" => 240, "Alpha" => 100));

$myPowersImage = new pImage(500,300, $myPowersData);
$myPowersImage -> setFontProperties(array(
	"FontName" => "/srv/www/lib/pChart/fonts/verdana.ttf",
	"FontSize" => 12));

$myPowersImage->setGraphArea(40,40, 460,260);
$myPowersImage->drawScale();

$myPowersImage->drawLineChart();
header("Content-Type: image/png");
$myPowersImage->Render(null);

will produce this graph:

I learned this using this resource:

http://phpmaster.com/charting-with-pchart/

PiPlanter | Interfacing an ADC, Python, and MySQL [Documentation]

As this post is more of an update, I won’t be adding any explanations, just giving the python code.

This will read 3 values from the adc and put them into the database “adc_database”. It will put them in the table “adc_input_data_4” in the columns “Channel_1″,”Channel_2” and “Channel_3” respectively.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import spidev
import time
import MySQLdb
import sys
import RPi.GPIO as GPIO

pin = 26

GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)

con = MySQLdb.connect('localhost','adc_user','adc_user_pass','adc_database');
cursor = con.cursor()

spi = spidev.SpiDev()
spi.open(0, 0)
count = 0
maxcyclenumber = 5

tmp = "derp"

def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout
    

for _ in range(maxcyclenumber):
	
	GPIO.output(pin,True)
	cursor.execute("INSERT INTO adc_input_data_4(Channel_1,Channel_2,Channel_3) VALUES('%s','%s','%s')",(readadc(0),readadc(1),readadc(2)) )
	GPIO.output(pin,False)
	
	count = count+1
	print count
	time.sleep (1)
	
if count == maxcyclenumber:
	GPIO.cleanup()
	con.commit()
	con.close()

There you go, bigger post coming later tonight.

Simple ADC with Raspberry Pi using MCP3008

Hello!

So for my own benefit, here’s the steps you need to take to get analog inputs working with a Raspberry Pi.

I’m grabbing most of this from: http://scruss.com/blog/2013/02/02/simple-adc-with-the-raspberry-pi/

The first thing you’ll need is an MCP3008. Using jumper wires, hook it up to your pi using this diagram.

Power your RPi up and run the following commands to get it all set up.

First thing’s first, you’ll need to enable SPI in the kernel so:

sudo vi /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

Now for the real meat of it. You’ll need these packages for SPI and the WiringPi library makes things a whole lot easier for us.

sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git
sudo pip install spidev
sudo pip install wiringpi

Now everything should be good to go, now for the python.

You can debug this any way you like, but my favorite way to do it is using the program geany. I like to start up a VNC server with root so I don’t get into any trouble with the GPIO permissions.

But here’s the program.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)
count = 0

def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout

while True:
    tmp1 = int(round(readadc(0)/10.24))
    print "in1:",tmp1
    count = count +1
    time.sleep(0.2)

And that’s pretty much it, the result should look something like this:

There you go!

Basic Wetness Sensor

So I’ve had the idea for a while to try and automate the growing of plants so I need to be able to tell if something’s wet or not. Here’s a video:

Basically the hookup process is that of a regular analog sensor with this in it’s place. There’s a 10k pullup resistor as the voltage divider.

Here’s the source for the Arduino.

int write1;
int writeval;

void setup(){
 Serial.begin(9600);
 pinMode(3, OUTPUT);
}

void loop(){
 Serial.println(analogRead(0));
 write1 = map(analogRead(0), 0, 1023, 0, 100);
 writeval = map(write1, 0, 100, 0, 1023);
 analogWrite(3, writeval);
 delay(100);
}

That’s pretty complex but I was trying to make the light changes more drastic so it would show up in the video better.

Here are some pictures of the build process:

Thanks for reading!