测试成功读取存储GPS数据到数据库应用

来源:互联网 发布:自拍比镜子丑 知乎 编辑:程序博客网 时间:2024/04/29 07:55

测试成功读取存储GPS数据到数据库应用

 

Rc.local 自动启动:

Su –ubuntu–c “/usr/bin/getGps.sh” &

/usr/bin/getGps.sh:

#!/bin/bash

python getGps.py &

sleep 2

 

getGps.py 代码:

 

#!/usr/bin/envpython

 

importserial

importrospy

fromstd_msgs.msg import String

importtime

import os

 

 

#logf =open('your_log_file.txt', 'a')

ser =serial.Serial('/dev/ttyACM0',baudrate=9600)

#ser = serial.Serial('/dev/ttyACM1', baudrate=9600)

 

defget_gps():

    #init publisher

    pub = rospy.Publisher('gpsStr', String,queue_size=10)

    rospy.init_node('gps_node', anonymous=True)

    rate = rospy.Rate(1) #10Hz = one second togo 10 time

 

    while not rospy.is_shutdown():

 

        #line = ser.readline()

        #line = ser.read(ser.inWaiting())

        allLine = ser.read(ser.inWaiting())

        strline = allLine.split('\r\n')

 

        #print strline

 

        for line in strline:

            print line

 

            if line.find("GPGLL") ==1:

            #if line.find("GPRMC") ==1:

                print line

 

                strlist = line.split(',')

                print len(strlist)

                arrLen = len(strlist)

 

                if (arrLen >=5 andstrlist[1]!="" and strlist[2]!="" and strlist[3]!="" andstrlist[4]!=""):

                    print strlist[1]

                    print strlist[2]

                    print strlist[3]

                    print strlist[4]

 

                    pubStr =strlist[1]+'|'+strlist[2]+'|'+strlist[3]+'|'+strlist[4]

                    print pubStr

                    pub.publish(pubStr)

                    os.system("php/usr/bin/get_gps.php "+strlist[1]+""+strlist[3]+" ")

                                   time.sleep(2)

 

 

        rate.sleep()

 

if__name__ == "__main__":

 

    try:

        get_gps()

 

    except rospy.ROSInterruptException:

        pass

 

 

 

 

 

get_gps.php 代码:

 

<?php

 

//initRecordNum : For upload data to Server , every time to sendlimit records

//initRecordNum();

 

echo"\n";

 

    @$gpsA = $argv[1]; 

    @$gpsB = $argv[2]; 

    //echo "key: $key \n";

 

  if(($gpsA == "") || ($gpsB =="")){

        echo "\n";

        echo "Must to add  argumentsafter Command!  Excample: php/PATH/file.php [gpsA gpsB] \n";

        echo "\n";

        

  }else{

 

  //get runing phpprocess PID of this time.

  $species="S";

  $sn="raspi";  //This must be theRobot Serial

 

 

    //do {

             

              unset ($_POST_data);

             

        //get processPID in lock file.

        //$ip_address=  system("curlmembers.3322.org/dyndns/getip");

               date_default_timezone_set('Asia/Singapore');

                $datetime = date('Y-m-dH:i:s');

                $update_time = $datetime;

 

              $_POST_data["species"] =$species;

              $_POST_data["sn"] = $sn;

              $_POST_data["gpsA"] =$gpsA;

              $_POST_data["gpsB"] =$gpsB;

              $_POST_data["update_time"]= $update_time;

 

           //write Serviceprocess PID into meter_connect.lock

          echo "Getting gps successed! | gpsA is $gpsA | gpsB is $gpsB  | updateTime is $update_time  \n";

        

  //*-- send data toelectrom server= **---------------------------------------------------*//

  $url = "http://192.168.0.250/huge/index.php/robot-curl-receive-gps";

 

  $fields_string = '';

  $fields_string =http_build_query($_POST_data); // $_POST_data isarray of key value pairs

 

  $ch = curl_init(); //open connection

 

  // set the url,number of POS vars, POST data

  curl_setopt($ch, CURLOPT_URL, $url);//PHP取回的URL地址。你也可以在用curl_init()函数初始化时设置这个选项

  curl_setopt($ch, CURLOPT_POST,count($_POST_data));//做一个正规的HTTP POST,设置这个选项为一个非零值

  curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);  //post 参数数据

  curl_setopt($ch, CURLOPT_FORBID_REUSE,1); //当进程处理完毕后强制关闭会话,不再缓存供重用

  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//设定是否显示头信息,返回字符串,而不是调用curl_exec()后直接输出

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10); // time to connect

  curl_setopt($ch, CURLOPT_TIMEOUT,30); // time for reply

 

  //$result =curl_exec($ch); // execture post

  $run_curl = curl_exec($ch); // execture post

  $res = curl_getinfo($ch);

  $statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);

  echo "<br>statusCode :$statusCode";

  $CURLINFO_EFFECTIVE_URL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);

  $connect_time = curl_getinfo($ch,CURLINFO_CONNECT_TIME );

  $CURLINFO_CONTENT_LENGTH_UPLOAD=curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_UPLOAD);

  $CURLINFO_SIZE_UPLOAD  =  curl_getinfo($ch,CURLINFO_SIZE_UPLOAD);

  $CURLINFO_CONTENT_TYPE  =  curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

 

  echo "<br>CURLINFO_CONTENT_LENGTH_UPLOAD  : $CURLINFO_CONTENT_LENGTH_UPLOAD ";

  echo "<br>connect_time :$connect_time";

  echo "<br>CURLINFO_EFFECTIVE_URL :$CURLINFO_EFFECTIVE_URL";

  echo "<br>CURLINFO_SIZE_UPLOAD :$CURLINFO_SIZE_UPLOAD";

  echo "<br>CURLINFO_CONTENT_TYPE :$CURLINFO_CONTENT_TYPE";

 

 

  curl_close($ch); //close connection

  //sleep(3);

             

    //}while(true);  //do while to getip

      

}//else

?>

0 0