> 本指南深入探讨了如何使用 Python 和 Apache NiFi 从物联网设备中获取 GPS 数据。在这种情况下,我们将使用 Raspberry Pi Zero。
引用我的上一篇文章 “使用物联网数据是一个分层的过程,它与冻糕不同。” 但是,上次我们使用 Onion Omega 2 摄取 GPS 数据时,我们将使用 Raspberry Pi Zero。让我们来看看你需要的设备,以及如何实现。
我推荐使用 BU-353-S4 USB GPS。它适用于 Raspberry Pi,非常实惠。连接到一个 Raspberry PI Zero Wireless,我可以运行这个小电池,并带到任何地方进行定位跟踪。把它放在你的送货卡车,底盘,火车,飞机,无人机,机器人等等。 我会跟踪自己,所以我可以拥有数据。
我的观点是,“如果你已经有了这个错误的估计,那就去做点什么吧!”
首先,安装 GPS 和 Python 所需的实用程序。我们也安装 NTP 来获得尽可能准确的时间。
sudo apt-get install gpsd gpsd-clients python-gps ntp
要进行测试以确保一切正常,请尝试使用其中两个 GPS 实用程序。确保你已经插入了 USB 接口 - 你将需要一个 RPi Zero 适配器从小的 USB 转换到正常的大小。然后,我连接一个小的 USB 集线器来连接 GPS 设备,以及有时连接的鼠标和键盘。获取其中之一,你将需要它。
您还需要从小到全尺寸的 HDMI 适配器。在您首次安装 Wi-Fi 时,您只需要鼠标,键盘和显示器。 一旦设置完成,只需将 SSH 放入您的设备并忘记它。你会想要关注这些(我们很快会找到他们):
gpspipe -l -o test.json -p -w -n 10
没有 -o
,它就变成 STDOUT/STIN
。
这些将从命令行工作,并给你一个读数。第一次校准需要几秒钟或者一分钟。如果你没有得到任何数字,把你的 GPS 在一个窗口或把它放在外面。
如果你必须手动运行 GPS 守护进程:
gpsd -n -D 2 /dev/ttyUSB0
我发现一些代码能通过 Python 读取 GPS 传感器。在那里,我修改成一个较慢的刷新和没有用户界面的代码,因为我想通过使用 Eclipse Paho MQTT 客户端,将此数据通过 MQTT 发送到 Apache NiFi。
我曾考虑过的一个增强功能是将所有数据保存为缓冲区,然后重新连接群发的离线模式。您也可以搜索其他 WiFi 信号,并尝试使用开放和免费的。 你可能想要添加 SQL,加密和其他一些控件。或者您可以在 Zero 上安装和使用 Apache MiniFi Java 或 C++ 代理。
#! /usr/bin/python
# Based on
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0
import os
from gps import *
from time import *
import time
import threading
import json
import paho.mqtt.client as paho
gpsd = None
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd #bring it in scope
gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.current_value = None
self.running = True #setting the thread running to true
def run(self):
global gpsd
while gpsp.running:
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
if __name__ == '__main__':
gpsp = GpsPoller() # create the thread
try:
gpsp.start() # start it up
while True:
if gpsd.fix.latitude > 0:
row = [ { 'latitude': str(gpsd.fix.latitude),
'longitude': str(gpsd.fix.longitude),
'utc': str(gpsd.utc),
'time': str(gpsd.fix.time),
'altitude': str(gpsd.fix.altitude),
'eps': str(gpsd.fix.eps),
'epx': str(gpsd.fix.epx),
'epv': str(gpsd.fix.epv),
'ept': str(gpsd.fix.ept),
'speed': str(gpsd.fix.speed),
'climb': str(gpsd.fix.climb),
'track': str(gpsd.fix.track),
'mode': str(gpsd.fix.mode)} ]
json_string = json.dumps(row)
client = paho.Client()
client.username_pw_set("jrfcwrim","UhBGemEoqf0D")
client.connect("m13.cloudmqtt.com", 14162, 60)
client.publish("rpiwzgps", payload=json_string, qos=0, retain=True)
time.sleep(60)
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
gpsp.running = False
gpsp.join() # wait for the thread to finish what it's doing
{
"track": "0.0",
"speed": "0.0",
"utc": "2017-05-01T23:49:46.000Z",
"epx": "8.938",
"epv": "29.794",
"altitude": "40.742",
"eps": "23.66",
"longitude": "-74.529216408",
"mode": "3",
"time": "2017-05-01T23:49:46.000Z",
"latitude": "40.268141521",
"climb": "0.0",
"ept": "0.005"
}
我们将这些原始数据转换成 Hive 表格,然后用 Apache Zeppelin 将它们可视化成漂亮的表格和图表。您还可以使用任何 ODBC 和 JDBC 报告工具(如 Tableau 或 PowerBI)进行报告。
su hdfs
hdfs dfs -mkdir -p /rpwz/gps
hdfs dfs -chmod -R 777 /rpwz/gps
如果你存储它,你就可以查询它!
选择 NiFi,而不是手动滚动某些 Hive DDL,而是根据推测的 AVRO Schema 自动生成所需的所有 DDL(即将使用 Schema Registry查找!)。 因此,您可以轻松地放入文件,转换为 ORC,保存到 HDFS,生成外部 Hive 表并在几秒钟内查询。全部没有编码。很容易通过 Web Sockets、JMS、AJAX 等方式将消息发送到前端。因此,我们可以在 S3 或 HDFS 中放置一个文件,将其转换为 ORC 以实现快速的 LLAP 查询,并告诉前端:表是什么,它可以查询它。
快速提示:利用 Apache NiFi 的调度程序,来限制您对第三方服务的呼叫次数,单个 NiFi 实例可能会轻易地压倒大多数免费服务层。我在几秒钟内拨打了 122 次电话给 “Weather Underground”。 所以设定那些时间!例如,对于天气,每 15-30 分钟甚至 1 小时一次是好的。
注意:GPS 信息也可以从无人机,汽车,电话和大量的 IIoT 设备中的定制传感器中读取。
英语原文链接:Ingesting GPS Data From Raspberry PI Zero Wireless With a USB GPS Device
观光\评论区