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:
CREATE TABLE adc_input_data_10(Sample_Number INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Time VARCHAR(100), Channel_1 VARCHAR(100), Channel_2 VARCHAR(100), Channel_3 VARCHAR(100) );
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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import spidev
import time
import MySQLdb
import sys
import RPi.GPIO as GPIO
import datetime

now = datetime.datetime.now()

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)
maxcyclenumber = 10

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

for _ in range(maxcyclenumber):
	cursor.execute("INSERT INTO adc_input_data_10(Time,Channel_1,Channel_2,Channel_3) VALUES(%s,'%s','%s','%s')",(datetime.datetime.now().strftime('%b-%d-%I%M%p-%G'),readadc(0),readadc(1),readadc(2)) )
	con.commit() #this is important for live updating
	count = count+1
	print count
	time.sleep (1)

if count == maxcyclenumber:
	GPIO.cleanup()
	con.close()</pre>
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.
<?php
mysql_connect("localhost", "adc_user","adc_user_pass") or die ("Could not connect: " . mysql_error());
mysql_select_db("adc_database");

$result = mysql_query("SELECT * FROM adc_input_data_10");

echo "<table border='1'>
<tr>
<th>Sample Number</th>
<th>Time</th>
<th>Channel 1</th>
<th>Channel 2</th>
<th>Channel 3</th>
</tr>";

while($row = mysql_fetch_array($result)){
	echo"<tr>";
	echo"<td>" . $row['Sample_Number'] . "</td>";
	echo"<td>" . $row['Time'] . "</td>";
	echo"<td>" . $row['Channel_1'] . "</td>";
	echo"<td>" . $row['Channel_2'] . "</td>";
	echo"<td>" . $row['Channel_3'] . "</td>";
	echo"</tr>";
}

echo "</table>";
mysql_close($con);
?>
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:

<?php
/* CAT:Scaling */

/* pChart library inclusions */
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");

$imageSizeXXal = 1000;
$imageSizeYVal = 600;

/* Create and populate the pData object */
$MyData = new pData();
$MyData->addPoints(array(17,19,4,1,2,6,7,3,4,4,8,2),"Pressure");
$MyData->setSerieDrawable("Pressure",FALSE);
$MyData->setAxisName(0,"Temperatures");

$MyData->addPoints(array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),"Labels");
$MyData->setSerieDescription("Labels","Months");
$MyData->setAbscissa("Labels");

/* Create the pChart object */
$myPicture = new pImage($imageSizeXXal,$imageSizeYVal,$MyData); //size of the graph box

/* Draw the background */
$Settings = array("R"=>84, "G"=>84, "B"=>84, "Dash"=>1, "DashR"=>95, "DashG"=>95, "DashB"=>95);
$myPicture->drawFilledRectangle(0,0,$imageSizeXXal+5,$imageSizeYVal,$Settings); //size of the grey background

/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"Upper Text 1",array("R"=>255,"G"=>255,"B"=>255));

/* Write the chart title */
$myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/Forgotte.ttf","FontSize"=>11));
$myPicture->drawText($imageSizeXXal/2,30,"Chart Title",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));

/* Define the 2nd chart area */
$myPicture->setGraphArea(40,40,$imageSizeXXal-35,$imageSizeYVal-25); //top left, then bottom right conrner of box
$myPicture->setFontProperties(array("FontName"=>"/srv/www/lib/pChart/fonts/pf_arma_five.ttf","FontSize"=>6));

/* Draw the scale */
$scaleSettings = array("DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
$MyData->setSerieDrawable("Temperature",FALSE);
$MyData->setSerieDrawable("Pressure",TRUE);
$MyData->setAxisName(0,"Pressure");
$myPicture->drawScale($scaleSettings);
$myPicture->drawPlotChart();

/* Render the picture (choose the best way) */
$myPicture->autoOutput();
?>

This image:

And by modifying the values mentioned above to:

$imageSizeXXal = 1500;
$imageSizeYVal = 400;

You will get this image:

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.

Plane | Working handshake code demo

First off, here’s a video:

If you’re a long time follower of the blog, than you may notice that it looks like I’m backtracking here. Let me go over the differences between the two systems I’ve developed.

The one I “finished” a few months ago can be found here. While the code is “good” and it works well for what it does, there are a few inherent problems with it. The first being that it has to have a computer to be used. The second being that the computer running the intermediate program has to be extremely fast, so it’s not totally feasible for field use. It would also be very hard for it to go wireless, but I had built that groundwork in so it could happen.

The one I’m working on now doesn’t require a computer as an intermediate, and is going to be totally wireless as from the start.

This morning I finished the handshake data exchange over xbee. Right now it’s just dimming a few LED’s but if you take a peek at the following code, it’s very expandable.

Here’s the working code:

 

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

int in_LED = 10;
int out_LED = 11;

int input = 2;

int sendval;


//serial stuff
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.

void setup(){
  pinMode(in_LED, OUTPUT);
  pinMode(out_LED, OUTPUT);
  pinMode(input, INPUT);
  
  Serial.begin(9600);
  xbee_serial.begin(9600);
  
  xbee_serial.print("0,0,0."); // this is very important as it starts of the loop because it makes "xbee_serial.avalible() > 0. 
}

void loop(){
  if (xbee_serial.available() > 0) { 
    static int bufpos = 0;
    char inchar = xbee_serial.read();
      if (inchar != EOPmarker) { 
        serialbuf[bufpos] = inchar; 
        bufpos++; 
      }
      else {
        serialbuf[bufpos] = 0; //restart the buff
        bufpos = 0; //restart the position of the buff

        Serial.println(atoi(subStr(serialbuf, "," , 1)));
        analogWrite(in_LED, atoi(subStr(serialbuf, "," , 1)));
        
         sendval = map(analogRead(input), 0, 1023, 0, 255);
         xbee_serial.print(sendval); // Value that it sends over the serial
         xbee_serial.print(",");
         xbee_serial.print("100"); //This second byte is for the purpose of the program, it is not being used. 
         xbee_serial.print("."); //EOP marker
         analogWrite(out_LED , sendval);
        
    }
  }
  
  //delay(10);
}

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

Plane | Working Handshake Code

This works so I want to write it down:

 

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

int in_LED = 10;
int out_LED = 11;

int input = 2;

int sendval;


//serial stuff
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.

void setup(){
  pinMode(in_LED, OUTPUT);
  pinMode(out_LED, OUTPUT);
  pinMode(input, INPUT);
  
  Serial.begin(9600);
  xbee_serial.begin(9600);
  
  xbee_serial.print("0,0,0.");
}

void loop(){
  if (xbee_serial.available() > 0) { 
    static int bufpos = 0;
    char inchar = xbee_serial.read();
      if (inchar != EOPmarker) { 
        serialbuf[bufpos] = inchar; 
        bufpos++; 
      }
      else {
        serialbuf[bufpos] = 0; //restart the buff
        bufpos = 0; //restart the position of the buff

        Serial.println(atoi(subStr(serialbuf, "," , 1)));
        analogWrite(in_LED, atoi(subStr(serialbuf, "," , 1)));
        
         sendval = map(analogRead(input), 0, 1023, 0, 255);
         xbee_serial.print(sendval); // Value that it sends over the serial
         xbee_serial.print(",");
         xbee_serial.print("100"); //This second byte is for the purpose of the program, it is not being used. 
         xbee_serial.print("."); //EOP marker
         analogWrite(out_LED , sendval);
        
    }
  }
  
  delay(10);
}

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

Plane | Transmitting Analog Values with Xbee

I made some progress today, here’s a video:

Basically this is a proof of concept. The transmitter is sending a line of data very similar to the one that will be sent by the final controller (as seen in the controller code) the receiving end with be receiving in the same way the vehicle code does (same as above).
Here are a few pictures, this first one is a top down view of my prototyping area:
This second one shows all components involved:
This is the code running on the transmitter side, the side with the pot that’s transmitting it’s analog value:
</div>
<div>

#include <SoftwareSerial.h>
SoftwareSerial xbee_serial(2, 3);

int pot_1 = 0;
int LED_1 = 5; //this will serve as an LED to compare to
int sendval;

void setup(){
 pinMode(pot_1, INPUT);
 pinMode(LED_1, OUTPUT);
 Serial.begin(9600);
 xbee_serial.begin(9600);
}

void loop(){
 Serial.println(analogRead(pot_1));
 sendval = map(analogRead(pot_1), 0, 1023, 0, 255);
 xbee_serial.print(sendval); // Value that it sends over the serial
 xbee_serial.print(",");
 xbee_serial.print("100"); //This second byte is for the purpose of the program, it is not being used.
 xbee_serial.print("."); //EOP marker
 analogWrite(LED_1,sendval);
 //delay(100);
}

This second half is on the receiving end. It may seem way overkill for what it does, but it’s all expandable to a pretty much infinite amount of data to be transmitted. For an example of what it’s capable of see this video:
</div>
<div>

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

int LED_1 = 5;

int led_val;

//serial stuff
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.

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

void loop(){
 if (xbee_serial.available() > 0) {
 static int bufpos = 0;
 char inchar = xbee_serial.read();
 if (inchar != EOPmarker) {
 serialbuf[bufpos] = inchar;
 bufpos++;
 }
 else {
 serialbuf[bufpos] = 0; //restart the buff
 bufpos = 0; //restart the position of the buff

Serial.println(atoi(subStr(serialbuf, "," , 1)));
 analogWrite(LED_1, atoi(subStr(serialbuf, "," , 1)));
 }
 }
}

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;

}

Plane | First Functioning Xbee test

So the first round of parts have come in for the plane, including the Xbees and the UartSBee V4s. My life is going to be very scattered over the next 4 or 5 months, so I’m going to be making frequent rudimentary posts for my own benefit.

Here’s a video:

First thing’s first I want to outline how everything’s connected.

The first xbee is connected to the first UartSBee which is connected to an Ardunio Uno which is connected to the first computer which has the console open and is looking at the serial stream at 9600 baud. The xbee is connected to the uno via Software Serial which is detailed in the code below. Here’s a picture:

 

The second setup is identical, except i’m using an Ardunio nano.

Here’s the code, I’m using the SoftwareSerial library to accomplish this. Both Arduinos are running the same thing.

</p>
#include <SoftwareSerial.h>

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

void setup()
{
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 }
 Serial.println("Goodnight moon!");

// set the data rate for the SoftwareSerial port
 mySerial.begin(9600);
 mySerial.println("Hello, world?");
}

void loop() // run over and over
{
 if (mySerial.available())
 Serial.write(mySerial.read());
 if (Serial.available())
 mySerial.write(Serial.read());
}
<p style="text-align: left;">

Here are a few pictures of the X-TCU setup. The baud is at 9600 and the pan ID is 115. The UartSbee itself is running at 115200 baud connected to the computer.

Thanks for reading!