PiPlanter | Graphing timed mysql data with pchart

Time to get this data we’re harvesting graphed. In a couple past posts, I’ve used pChart to graph random data but now since data is getting dumped into a mysql chart, it would make sense to try and graph that data.

To install pChart on my system (same installs as listed in this post) to do that do the following:

First, get the php5-gd package by running:

Then download, rename and move the pChart files to the proper directory:

Now pChart is ready to be used.

I used a lot of the info found here:

http://wiki.pchart.net/doc.mysql.integration.html

http://wiki.pchart.net/doc.doc.draw.scale.html

The code is pretty well commented so I’m not really going to get into describing it, but essentially, the following php will retrieve data from a mysql table (which is being populated by a python script seen in this post) and after leaving it on in my room for like 3 days, render this graph:

Here’s that php script:

PiPlanter | Basic package setup and bringing everything together

I’m in a hotel trying to occupy myself with something interesting so I’ve decided to work on this. I had to re-image the SD card I’ve been developing this project on, but I saved to code so there’s no problem there. Now I need to re-install all the basic packages.

First I need to get the components of a LAMP server with the following commands:

Once you get the mysql server setup, you’ll need to create a database and tables in mysql.

To create the database you’ll be using run the following command:

And then grant the proper privileges to use later with the command:

Then we can enter the database and create a table:

Now we need to set up the specific libraries for python the first of which being spidev, the spi tool for the raspberry pi which we can grab from git using the following commands:

You also need to (copied from http://scruss.com/blog/2013/01/19/the-quite-rubbish-clock/):

As root, edit the kernel module blacklist file:

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

Save the file so that the module will load on future reboots. To enable the module now, enter:

We will also need WiringPi:

Then you need to get APscheduler, the timing program used to execute the incremental timing with the following commands:

You will need mysqldb to interface python and mysql:

Once you reboot, the following program should work:

And there you go! The program should log data every minute and then every hour to two different tables. To view those data sets as php tables you can use this php script:

Sometime later I’ll get to graphing the data.

PiPlanter | Using APScheduler to get timed samples in python

I’m taking a “break” from my drone while I save some money to buy more tricopter parts, and since the weather’s getting nicer and nicer I’ve decided to start working on my PiPlanter again.

As a refresher, the PiPlanter is a Raspberry Pi powered garden. The goal is for it to just be able to be plugged in and add water to a water source and have the Pi monitor temp and moisture levels to be able to add more water as needed.

I’ve shown that is relatively easy to go from analog sensors to good looking tables and graphs using the raspberry pi, the problem that I ran into however was timing.

It became harder and harder to use the time.sleep function in python to handle long periods of time. When you are dealing with things like plants, you don’t need to water it very often, but for data’s sake, you should be polling the sensors a lot.

I’ve landed on the use of APScheduler in python, and here’s my source code:

[py]
#Timing setup
from datetime import datetime
from apscheduler.scheduler import Scheduler
import time

import logging #if you start getting logging errors, uncomment these two lines
logging.basicConfig()

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

GPIO.cleanup()

pin = 26 #pin for the adc
GPIO.setup(pin, GPIO.OUT)
led1 = 11 #pin for the short indicator led
GPIO.setup(led1, GPIO.OUT)
led2 = 13 #pin for other long indicator led
GPIO.setup(led2, GPIO.OUT)

#the adc’s SPI setup
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)

going = True

#fuction that can read the adc
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

def rapidSample():
sampleTemp1 = (((readadc(0)*3.3)/1024)/(10.0/1000)) #this translates the analog voltage to temperature in def F
sampleLght1 = readadc(1)
samplePot1 = readadc(2)

GPIO.output(led1, True) #turns the led on
time.sleep(.1) #sleeps a little bit so you can see the LED on
print “Job 1″, datetime.now(),”LDR:”,sampleLght1 ,”Pot:”,samplePot1,”Temp:”,sampleTemp1 #prints the debug info
time.sleep(.1)
GPIO.output(led1, False) #turns the led off

def slowSample():
print “Job 2” , datetime.now()
GPIO.output(led2, True) #turns the led on
time.sleep(5)
GPIO.output(led2, False) #turns the led on

if __name__ == ‘__main__’:
#the following 3 lines start up the interval job and keep it going
scheduler = Scheduler(standalone=True)
scheduler.add_interval_job(rapidSample, seconds=1)
scheduler.add_interval_job(slowSample, minutes=1)
scheduler.start()
[/py]

This produces a loop that flashed a green led on and of for .1 seconds at a time per second, and then every minute, turns on a speaker and a red led for 5 seconds then turns it off. There are some images of what goes on below.

Here is a picture of the the print dialog in python:

You can see that the first job (green led) posts the values from the analog sensors every second

The second job (red led) just posts the time. But the function is expandable to do anything at any time.

Here are pictures of the board and the circuit in action:

Both LED’s off

The Green LED on, the red circled process in the printout

Here are both on

The next step is adding the mySQL in as seen in some other posts.

PiPlanter | Going from analog data to the web using python, mysql and php

Here’s a video:

Essentially, the adc reads an analog value and sends it to python. Python connects to a mysql database and adds the data to that. Once the user accesses the php script, a table, containing all of the values, is rendered.
First of all, you’ll need to make a mysql table with the correct specifications:
This creates a table that is found in the python script so it is very important that the TABLE values match as well as the column names.
Here’s that python script:

This reads the values from the adc (it is wired up as seen in this post)
Here’s the php, it reads the values from the adc_input_data_10 table on the adc_database.
This is all very preliminary stuff, and I’ll tighten up the code as time goes on.

PiPlanter | Graphing With PHP 2

This is a much more refined version of that graph I created earlier.

This one is much more detailed, and the sizes of the graph can easily be controlled with the imageSizeX and Y Vals.

This program will render:

This image:

And by modifying the values mentioned above to:

You will get this image:

PiPlanter | Graphing With PHP

Again, short post. This php code:

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.

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:

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

Then run this to make it more permanent.

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.

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.

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.

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!

PiScanner | Project Execution [Demonstration/Distribution]

I should preface this by saying that it is no where near as polished as it could be. But it works, and the objects taken by the camera are recognizable. Eventually, if I have the funding, I will upgrade the Camera.

There are many elements to this project. There really isn’t a great place to start with this project, so well start with the fritzing schematic and a list of materials needed.

You will need:

2 Attiny85’s
16 wires
a breadboard
a 100k ohm resistor
a 330 ohm resistor
a raspberry pi
PIR motion detector
the cables for interfacing with the raspberry pi
an isp programmer for the attiny’s

So let’s look at this schematic:

When motion is detected, the signal pin on the motion detector gets pulled low. at this point the 3.3v attiny pulls pin 1 high, sending a signal into the rpi (it’s 3.3v so it’s safe for the broadcomm) and illuminating an LED so the user can see that motion is being detected.

The raspberry pi then sends a signal to the 5v attiny indicating that motion has indeed been detected. Once this occurs, the 5v attiny pulls the 5v pin going to the webcam high. I had to integrate this step of turning the webcam on via the attiny because there is a hardware misconfiguration that causes the software i’m using to take the picture hang. Turning the camera off and on each time a picture needs to be taken gets rid of this problem, because the camera always takes the first picture after being turned on.

At this point a picture is taken and moved to a flash drive. They could be moved wherever, but a flashdrive works the best for now, as I will be deploying this system in a place where there isn’t internet (in my garage)

Now time for source codes!

Software wise you will need:

The Arduino IDE

Python (I’m using Geany on the raspberry pi)

fswebcam which you can obtain by running

On your raspberry pi.

This is the source for the 3.3v attiny85

This is the source for the 5v attiny85

This is the python source, you will need to install this to make it work though.

This is the Fritzing document.

Here’s a sample image from the camera. It is looking at the breadboard.