By accepting you will be accessing a service provided by a third-party external to http://www.wsi10.com/
GPS跟踪
通过MQTT使用ESP32和IoT平台进行GPS跟踪
配件清单:
NEO-6M GPS模块
ESP32开发板
面包板
杜邦线
开发环境:
Arduino IDE 1.8.9
运行Arduino IDE软件的计算机
配件介绍:
ESP32: ESP32 集成了天线开关、射频 balun、功率放大器、低噪放大器、过滤器和电源管理模块,整个解决方案占用了最少的印刷电路板面积。2.4 GHz Wi-Fi 加蓝牙双模芯片采用 TSMC 低功耗 40nm 技术,功耗性能和射频性能最佳,安全可靠,易于扩展至各种应用。
NEO-6M GPS:NEO-6M GPS模块如下图所示。它带有一个外部天线,并没有附带插头引脚。所以,你需要自备以及焊接
硬件连线图:
源代码:
在Arduino IDE中安装ESP32
从Arduino IDE打开"首选项"窗口:"文件"---"首选项"。
在附加开发板管理器网址一栏,输入以下链接:
https://dl.espressif.com/dl/package_esp32_index.json
如果您已经拥有ESP8266板子连接,请用逗号分隔,如下所示:
https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.jso
打开开发板管理器(工具>电路板>开发板管理器),搜索ESP32,然后单击" Espressif Systems的ESP32"的安装按钮。
下载需要的库文件
TinyGPS ++库:
https://blog.asksensors.com/iot-cloud-based-gps-tracking-esp32-gps-neo-6m-module/
EspSoftwareSerial库:
https://github.com/plerup/espsoftwareserial/
PubSubClient库:
https://github.com/asksensors/pubsubclient
运行代码:
首先,请确保您的WiFi热点已打开。
通过micro-USB电缆将ESP32开发板连接到计算机。确保模块上的电源LED变高,以确保供电。
打开Arduino IDE并为您的ESP32电路板类型和连接端口选择工具选项,然后单击上载。重置后,代码将自动运行。
打开串行终端以逐步监控ESP32软件的时序。
现在,我们将返回到AskSensors Web应用程序,以监视ESP32开发板根据从GPS NEO-M6模块解析的数据更新的GPS跟踪器地图。您应该得到一个显示您最新的GPS位置的地图图形,如下所示。
另外,您可以通过启用"显示线"和"显示位置历史记录"在地图上显示您的轨迹。您还可以根据需要自定义颜色
下面的地图提供了一个示例的位置数据示例,该位置数据是在街道上散步时记录的。
源代码
#include <WiFi.h> #include <PubSubClient.h> #include <SoftwareSerial.h> #include <TinyGPS++.h> const char* ssid = ".................."; const char* password = ".................."; const char* username = "................."; const char* pubTopic = "publish/..../....."; const unsigned int writeInterval = 25000; static const int RXPin = 16, TXPin = 17; static const uint32_t GPSBaud = 9600; const char* mqtt_server = "mqtt.asksensors.com"; unsigned int mqtt_port = 1883; WiFiClient askClient; PubSubClient client(askClient); TinyGPSPlus gps; SoftwareSerial ss(RXPin, TXPin); void setup() { Serial.begin(115200); Serial.println("*****************************************************"); Serial.println("********** Program Start : ESP32 publishes NEO-6M GPS position to AskSensors over MQTT"); Serial.print("********** connecting to WIFI : "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("->WiFi connected"); Serial.println("->IP address: "); Serial.println(WiFi.localIP()); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); ss.begin(GPSBaud); } void loop() { if (!client.connected()) reconnect(); client.loop(); while (ss.available() > 0) if (gps.encode(ss.read())) displayInfo(); if (millis() > 5000 && gps.charsProcessed() < 10) { Serial.println(F("No GPS detected: check wiring.")); while(true); } } void displayInfo() { if (gps.location.isValid()) { double latitude = (gps.location.lat()); double longitude = (gps.location.lng()); Serial.println("********** Publish MQTT data to ASKSENSORS"); char mqtt_payload[50] = ""; snprintf (mqtt_payload, 50, "m1=%lf;%lf", latitude, longitude); Serial.print("Publish message: "); Serial.println(mqtt_payload); client.publish(pubTopic, mqtt_payload); Serial.println("> MQTT data published"); Serial.println("********** End "); Serial.println("*****************************************************"); delay(writeInterval); } else { Serial.println(F("INVALID")); } } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { Serial.print("********** Attempting MQTT connection..."); if (client.connect("ESP32Client", username, "")) { Serial.println("-> MQTT client connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println("-> try again in 5 seconds"); delay(5000); } } }
以上教程参考:
https://blog.asksensors.com/iot-cloud-based-gps-tracking-esp32-gps-neo-6m-module/
淘宝产品:
卫水士店铺:https://shop141440422.taobao.com/?spm=a23or.7195193.1997079397.2.360a5920krjUI1
相关产品:
GPS定位套装: