I’m saving sensor data to phpMyAdmin database and I want to use that data (the last entry) as input variables for a fuzzy function written in PHP.
Can I somehow send the output variable from fuzzy back to my Arduino? How would I configure the request on the board?
<?php
require_once ('./fuzzy-logic-class.php');
// Include data base connect class
$filepath = realpath (dirname(__FILE__));
require_once($filepath."/db_connect.php");
// Connecting to database
$db = new DB_CONNECT();
// Actual Values
$sql = mysql_query("SELECT `temp`,`hum`,`moist` FROM `t_measure` ORDER BY id_measure desc LIMIT 1;");
$actual = $conn->query($sql);
if ($actual->num_rows > 0) {
// output data of each row
while($row = $actual->fetch_assoc()) {
$acttemp = number_format($row["temp"],0);
$acthum = number_format($row["humidity"],0);
$actsoil = number_format($row["soil"],0);
}
} else {
echo "0 results";
}
/*----------------Fuzzy Parameters----*/
$x = new Fuzzy_Logic();
$x->clearMembers();
/* ---------- set input members ---------*/
$x->setInputNames(array('TEMP','HUMIDITY', 'SOIL'));
$x->addMember($x->getInputName(0),'COLD', 0, 8, 25 ,LINFINITY);
$x->addMember($x->getInputName(0),'WARM', 20, 35, 100 ,RINFINITY);
$x->addMember($x->getInputName(1),'LOW', 0 ,40 ,60 ,LINFINITY);
$x->addMember($x->getInputName(1),'HIGH',50 ,70 ,100 ,RINFINITY);
$x->addMember($x->getInputName(2),'WET', 0, 400 ,650 ,LINFINITY);
$x->addMember($x->getInputName(2),'DRY',550, 800 ,1025 ,RINFINITY);
/* ---------- set output members ---------*/
$x->setOutputNames(array('OUT'));
$x->addMember($x->getOutputName(0),'LOWER',-10, -5 ,0 ,LINFINITY);
$x->addMember($x->getOutputName(0),'RAISE',0, 5 ,10 ,RINFINITY);
/* ---------- set rule table ------------ */
$x->clearRules();
$x->addRule('IF TEMP.WARM OR SOIL.DRY OR HUMIDITY.LOW THEN OUT.RAISE');
$x->addRule('IF TEMP.COLD OR SOIL.WET OR HUMIDITY.HIGH THEN OUT.LOWER');
/*---------- Get values from database and calculate output ----*/
$x->SetRealInput('TEMP', $acttemp);
$x->SetRealInput('HUMIDITY' ,$acthum);
$x->SetRealInput('SOIL' , $actsoil);
$fuzzy_arr = $x->calcFuzzy();
$Fuzzy = $fuzzy_arr['OUT'];
echo $Fuzzy;
2
Answers
So, it turns out I messed up combining two files. With this part corrected, the api works as it's supposed to.
To receive data on the Arduino side, this is the required code.
Using HTTP, it could be as simple as having a HTTPClient on your Arduino do a GET request to your server/phpfile, and reading the response. Examples abound on the interwebs; the library to use for this depends on the Arduino you use.
The response from your PHP should be something like:
If you want the server to take the initiative in sending the fuzzy value, things get a bit more complicated.