skip to Main Content

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


  1. Chosen as BEST ANSWER

    So, it turns out I messed up combining two files. With this part corrected, the api works as it's supposed to.

           $servername = "---";
    $username = "---";
    $password = "---";
    $dbname = "---";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $Fuzzy = $_GET['val'];
    
    // Actual Values
     $sql= "SELECT `temp`,`hum`,`moist` FROM `t_measure` ORDER BY id_measure desc LIMIT 1;";
    $actual = $conn->query($sql);
    

    To receive data on the Arduino side, this is the required code.

    void setup() {
    
    Serial.begin(115200);
    WiFi.begin("ssid", "pass");   //WiFi connection
    while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    delay(500);
    Serial.println("Waiting for connection");
    
    }
    
    }
    
    void loop() {
    
    if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
    HTTPClient http;  
    http.begin("http://.....");  //api location
    http.addHeader("Content-Type", "text/plain"); 
    String payload = http.getString(); //reading from api
    Serial.println(payload);
    http.end(); 
    }else{
    Serial.println("Error in WiFi connection");   
    
    }
    

  2. 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:

    HTTP/1.1 200 OKn
    Connection: closen
    Content-Type: text/plain; charset=utf-8nn
    [Your string in $Fuzzy]n
    

    If you want the server to take the initiative in sending the fuzzy value, things get a bit more complicated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search