douzhi19900102 2017-03-09 13:44
浏览 80

如何将数据从arduino传递给sqlsrv

this is my 1st question post in stackoverflow i am doing a project to storing sensor data to microsoft sql server but after i tried for a lots of time i still unable to log my arduino data to server. i have manually input the value in php file and it works for input to database. can anyone help me? thanks this is the arduino code that i done.

byte mac[] = {0x78, 0x4B, 0x87, 0xA4, 0xA1, 0xB0 };
IPAddress ip(192,168,1,21);

EthernetClient client;

int DO=10;
int pH=5;
String data;

void setup()
{

      data="";
      Serial.begin(9600);
      Ethernet.begin(mac, ip);

 if (client.connect("192.168.1.19", 80)) 
    {

        Serial.println("connected");

        client.print("POST /psm/test.php?");
        client.print("DO"); 
        client.print(DO);
        client.print("&pH="); 
        client.print(pH);  
        client.println(""); 
        client.println(" HTTP/1.1"); 
        client.stop();
    }
    else {

        Serial.println("--> connection failed
");
}
}
void loop()
{

if (client.available()) {
 char c = client.read();
 Serial.print(c);
}


if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
while (true);
  }
}

`

and this is the php code that i used

    <?php
#region connection setup
$serverName = "USER-PC\TZEYEOW"; 


$connectionInfo = array( "Database"=>"tasik_B1", "UID"=>"tzeyeow",     "PWD"=>"12345678");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}


echo"connected.<br/>";
#regionend


#region getting data from arduino
$DO=(isset($_GET['DO']) ? $_GET['DO'] : null);
$pH=(isset($_GET['pH']) ? $_GET['pH'] : null);




date_default_timezone_set("Asia/Kuala_Lumpur");
$dT = date("Y-m-d h:i:sa");

$query = "INSERT INTO tasik_B1 (DO,pH,logDateTime) 
            VALUES ('5','".$pH."','".$dT."')";
        //VALUES ('".$OREV."','".$OLENGTH."','".$dT."')";
$params =array(1, "value");


$stmt = sqlsrv_query( $conn, $query);

   // Close the connection   
    sqlsrv_close($conn);


if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
   // Close the connection   
    sqlsrv_close($conn);
}
?> 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题