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.

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!

Learning PHP [1]

Hello to anybody reading his!

I’ve decided to teach myself the language PHP. This won’t be the first time I’ve  attempted to teach myself a language sans-instructor before, but i’m sure i’ll make due.

This will however be the first time that I attempt to do so while I have a website! You can follow my progress at the index of www.esologic.com/learn_php. Right now I’ve got a sample for arrays in there.

You can right-click -> save as the documents and open them in your favorite text editor, (I know for sure it works in vim in debian, as that’s where i’m editing it in) or click the document to see the output.

Happy Programming!