Tessel 2およびGPSモジュールを使用したGPSトラッキング:JavaScriptに基づくリアルタイムポジショニングプロジェクト
この記事では、Tessel 2マイクロコントローラーとそのGPSモジュールを使用して、リアルタイムGPS追跡システムを構築する方法を紹介します。 Tessel 2はJavaScriptプログラミングをサポートしており、さまざまなモジュールを介して機能を拡張できます。 GPSモジュールを接続し、Tessel 2サイドにJavaScriptコードを作成してGPSデータを処理する方法を学び、Node.JSサーバーとフロントエンドのGoogleマップ統合を構築して、リアルタイムの位置視覚化を実現します。
1
GPSモジュールをTessel 2ポート(USB電源コネクタの近く)に接続します。 GNDピンがテッセル2のGNDピンに接続されていることを確認してください。
2。ソフトウェアの準備プロジェクトを作成します:
t2 init
npm install gps-a2235h
pakmanager
3
npm install pakmanager -g
次のコードはGPSデータ取得を実装し、WebSocketを介してサーバーに送信します。
次のコードは、Websocketサーバーを構築し、Tessel 2から送信されたGPSデータを受信し、すべての接続されたクライアントにブロードキャストします。
5。フロントエンドのGoogleマップ統合
var tessel = require("tessel"), gpsLib = require("gps-a2235h"), gps = gpsLib.use(tessel.port["A"]), WebSocket = require('ws'), ws = new WebSocket('ws://[您的服务器IP地址]:5000'), // 将[您的服务器IP地址]替换为您的服务器IP latestCoords; gps.setCoordinateFormat({'format': 'deg-dec'}); gps.on('ready', function() { console.log('GPS模块正在搜索卫星...'); gps.on('coordinates', function(coords) { console.log('纬度:', coords.lat, '\t经度:', coords.lon, '\t时间戳:', coords.timestamp); latestCoords = coords.lat + ',' + coords.lon; }); gps.on('fix', function(data) { console.log(data.numSat, '颗卫星已锁定.'); }); gps.on('dropped', function(){ console.log('GPS信号已中断'); }); }); gps.on('error', function(err){ console.log('GPS错误: ', err); }); ws.on('open', function() { setInterval(function() { if (latestCoords !== undefined) { console.log('尝试发送坐标: ' + latestCoords); ws.send(latestCoords); } else { console.log('未收到坐标数据'); } }, 10000); });
vi
node.jsサーバーを実行:
var http = require('http'), express = require('express'), app = express(), bodyParser = require('body-parser'), server = require('http').Server(app), WebSocketServer = require('ws').Server, wss = new WebSocketServer({server: server}), port = process.env.PORT || 5000; app.use(bodyParser.json()); app.use(express.static(__dirname + '/public')); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('收到消息: %s', message); wss.clients.forEach(function each(client) { if (client !== ws && client.readyState === WebSocket.OPEN) { client.send(message); } }); }); }); server.listen(port, function() { console.log('服务器监听端口 ' + port); });
Tessel 2コードを実行:
オープンブラウザはにアクセスして、リアルタイムのGPS追跡結果を表示します。 public/index.html
7 この記事の最後に、GPSモジュールの使用方法を含むnode.jsを使用したGPSデータトラッキングのFAQを提供し、ロケーションアウェアアプリケーションの構築、socket.ioでリアルタイムで追跡し、テッセルでGPSで追跡する2。
node index.js
コードのプレースホルダー「[サーバーIPアドレス]」を実際のサーバーIPアドレスに置き換え、GoogleマップAPIキーを取得する必要があることに注意してください。 Tessel 2とサーバーが同じネットワークに接続されていることを確認してください。 以上がTessel 2でGPSデータを追跡しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。