Hi all!
So here is my project: I am working on a multi-sensor device which is also intended to control solid state relays.
As it stands, I am using TMP102 temperature sensors which report over the I2C protocol to an Arduino Due. w/ WiShield 1.0
The temperature data for two sensors is being sent via POST method to a PHP script on a colocated server.
The remote PHP script stores the incoming data in an SQL database which I then analyze using JpGraph & PHP.
This part is working just fine - I have been collecting temperature data for ~6mo now.
What I am struggling with is the following:
Getting control data from the remote server to control a solid state relay.
What I would like to do is use my SQL database to control the switch states (Boolean function, really).
I have written a PHP script that polls my database and returns the only following:
, (eg. |1,0)
I am using pipe ( | ) as a sort of index to parse the incoming GET request so that my code knows where to start looking for the actual control data.
I would like to make this scalable so that I could have say, 4 switches, all having their states returned with a single query of the SQL/PHP server.
So I need to somehow include the ability to intelligently parse multiple switch data.
Ie: sw1,1 .. sw2,0 .. sw3,1 .. etc
As it stands, if I make a text file (switch.php) with |1,0 in it, the switch get set to off, similarly if the text file contains only |1,1 the switch is turned on.
When I test my swtich.php using the SQL data, my web browser reports only: |1,0 or |1,1 but the Arduino does not control the switch accordingly.
I was hoping that an experienced coder could have a look at my source (below) and make suggestions / help me along my way.
void printData2(char* data, int len) { // how the headers returned from getData routine are handled
if (len > 0){
memset(dataIn, ' ', sizeof(dataIn)); // clear dataIn and initialize cleanly for length of data packet
for (int cntChar=0; cntChar < len; cntChar++) {
char chrIn = data[cntChar]; // gather data from data variable 1 char at a time
if (chrIn == '|') { // more advanced: if ((chrIn == '|') || (indexFound != 1))
dataIn[ptr] = chrIn; // set the first dataIn[0] to the current chrIn, post-increment ptr
ptr++;
indexFound = 1;
} // end of if (chrIn == '|')
if (indexFound == 1) {
sscanf(dataIn, "%[^,], %d", sw1); // ignore the switch name for now, just get the state (and hope it works)
Serial.println(onoff);
} // end of (indexFound == 1)
} // end of for (int cntChar=0; cntChar < len; cntChar++)
} // end of (len > 0)
// uip_close(); // disconnect from server - this causes data packet loss for some reason, ie. does not finish GET request
}
Here is the source code of the PHP script that the Arduino is told to GET data from:
<?php
include ("db.php");
$sw = $_GET['sw'];
$today = date("Y-m-d");
if ($dbtable == "") $dbtable = "switches";
mysql_connect($host,$login,$passwd);
@mysql_select_db($dbname) or die( "Unable to connect to database");
$query = " SELECT *
FROM switches
";
$result = mysql_query($query);
$cnt = 0;
$cnt2 = 0;
while($row = mysql_fetch_row($result))
{
$switchIn[$cnt] = $row[0];
$stateIn[$cnt] = $row[1];
$dateIn[$cnt] = $row[2];
$cnt++;
}
mysql_free_result($result);
while($cnt2 < $cnt)
{
$dateInConv[$cnt2] = date("Y-m-d H:i:s",$dateIn[$cnt2]);
$cnt2++;
}
$cnt3=0;
while($cnt3 < $cnt)
{
echo "|";
echo $switchIn[$cnt3];
echo ",";
echo $stateIn[$cnt3];
$cnt3++;
} // example, outputs: |1,1
?>