GPS.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-  
  3. #-*- coding: cp950 -*- 
  4. import array,time,threading,serial,urllib2,urllib,os,os.path,requests #How to install?? ----> pip install requests
  5. import traceback
  6. path_gps='http://www.aisky.com.tw/field/gps.php'
  7. ser=serial.Serial('/dev/ttyUSB0',9600,timeout=0.1)
  8. rx_dev_nr = "3"
  9. case_command = "0"
  10. def catch_RTK_GPS():
  11. global rx_dev_nr
  12. global case_command
  13. if ser.inWaiting() :
  14. #if True :
  15. try :
  16. x = ser.readline()
  17. #x = "$GPGGA,140025.00,2503.2423699,N,12136.4594332,E,1,04,1.0,17.259,M,19.013,M,0.0,*7B"
  18. #print x
  19. if x[0:6] == "$GPGGA" and len(x) < 90 :
  20. log = x
  21. x = x.split(",")
  22. if x[7] == '0':
  23. print "no satellite data available"
  24. if x[2] != "0" and x[4] != "0":
  25. time_data = x[1][0:2] + ":" + x[1][2:4] + ":" + x[1][4:6]
  26. lat = x[2]
  27. lng = x[4]
  28. print "Time : " + time_data + "/ Lat : " + lat + "/ Lng : " + lng
  29. mydata=[("dev_nr",rx_dev_nr),("lat",lat),("lng",lng),("log",log)]
  30. #mydata=[("dev_nr",rx_dev_nr),("lat","2503.5376838"),("lng","12136.8545442")]
  31. print mydata
  32. print 'GateWay Catch Nr '+str(rx_dev_nr)+' ok'
  33. mydata=urllib.urlencode(mydata)
  34. req=urllib2.Request(path_gps, mydata)
  35. req.add_header('User-Agent','Magic Browser')
  36. page=urllib2.urlopen(req).read()
  37. print time.strftime(' nr '+str(rx_dev_nr)+' command:'+str(case_command)+' %m/%d/%Y'+' %H:%M:%S'+' add ok ')
  38. x = ""
  39. y = ""
  40. print '######################################################################################## \n'
  41. except Exception, e:
  42. try :
  43. print 'traceback.print_exc():'; traceback.print_exc()
  44. mydata=[("dev_nr",rx_dev_nr),("data_error",traceback.format_exc())]
  45. mydata=urllib.urlencode(mydata)
  46. req=urllib2.Request(path_gps, mydata)
  47. req.add_header('User-Agent','Magic Browser')
  48. page=urllib2.urlopen(req).read()
  49. except :
  50. print ("無法上傳")
  51. print ("有問題,請確認")
  52. time.sleep(15)
  53. print ("開始偵測GPS訊號GO")
  54. while True :
  55. catch_RTK_GPS()
  56. time.sleep(1)