Arduino – Communicate With Server – Part 2 – Server Code for Saving Data

This article shows the server side code for a server which is gathering or sending data by polling method to an Arduino device.

This method or code is putting less load on the server (compared to socket) but data transmission is slower than socket.

<?php
date_default_timezone_set("Asia/Kolkata");
$conn = mysql_connect("localhost","root","**********");
if(!$conn)
{
    echo "Error: failed to connect DB Server";
    die();
}

if(!mysql_select_db("database_name",$conn))
{
    echo "Error: failed to connect DB";
    die();
}
$action = intval(strip_tags($_GET['action']));

if($action == 1)
{
    $temp = floatval(strip_tags($_GET['temp']));
    $paH = floatval(strip_tags($_GET['paH']));
    $paM = floatval(strip_tags($_GET['paM']));
    
    mysql_query("insert into weather (temperature, paH, paM, time) values(".$temp.",".$paH.",".$paM.",".time().")");
    //mysql_query("update led_status set led_1 = ".$led_1.", led_2 = ".$led_2);
    
    echo "success";
}
else
    if($action == 2)
    {
        $output = "";
        $result = mysql_query("select * from led_status");
        $rows = mysql_fetch_array($result);
        if($rows['led_1'] == 1)
        {
           $output = "led_1=1|";    
        }
        else
        {
           $output = "led_1=0|";    
        }
        
        if($rows['led_2'] == 1)
        {
           $output .= "led_2=1";    
        }
        else
        {
           $output .= "led_2=0";    
        }
        echo $output;
    }
    mysql_close($conn);
?>

 

The else part ( if($action == 2) ) output from the above will be

HTTP/1.1 200 OK
Date: Sun, 14 Feb 2016 20:33:16 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Content-Length: 15
Connection: close
Content-Type: text/html

led_1=0|led_2=1

Leave a Reply