#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
// WiFi credentials
const char* WIFI_SSID = "2";
const char* WIFI_PASSWORD = "shabihaha";
// Bafa Cloud server configuration
const char* TCP_SERVER_ADDR = "bemfa.com";
const char* TCP_SERVER_PORT = "8344";
const char* UID = "0d40008355ac476ba9ccf4e938470244";
const char* GPS_TOPIC = "aaa1004";
// GPS module connection
const int GPS_RX_PIN = D5; // GPS TX pin to ESP8266 RX pin
const int GPS_TX_PIN = D6; // GPS RX pin to ESP8266 TX pin
SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN);
WiFiClient client;
void setup() {
Serial.begin(115200);
gpsSerial.begin(9600);
connectToWiFi();
}
void loop() {
while (gpsSerial.available() > 0) {
processGPSData();
}
}
void connectToWiFi() {
Serial.println();
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void processGPSData() {
String gpsData = gpsSerial.readStringUntil('\n');
Serial.println("Raw GPS data: " + gpsData);
if (gpsData.startsWith("$GNGGA") || gpsData.startsWith("$GNGLL") || gpsData.startsWith("$GPRMC")) {
String parts[15];
int partIndex = 0;
int startIndex = 0;
for (int i = 0; i < gpsData.length(); i++) {
if (gpsData.charAt(i) == ',') {
parts[partIndex] = gpsData.substring(startIndex, i);
startIndex = i + 1;
partIndex++;
}
}
String latitude;
String longitude;
if (gpsData.startsWith("$GNGGA")) {
if (parts[2] != "" && parts[4] != "") {
latitude = convertToDecimal(parts[2], parts[3]);
longitude = convertToDecimal(parts[4], parts[5]);
}
} else if (gpsData.startsWith("$GNGLL")) {
if (parts[1] != "" && parts[3] != "") {
latitude = convertToDecimal(parts[1], parts[2]);
longitude = convertToDecimal(parts[3], parts[4]);
}
} else if (gpsData.startsWith("$GPRMC")) {
if (parts[3] != "" && parts[5] != "") {
latitude = convertToDecimal(parts[3], parts[4]);
longitude = convertToDecimal(parts[5], parts[6]);
}
}
if (latitude != "" && longitude != "") {
uploadToBafaCloud(latitude, longitude);
}
}
}
String convertToDecimal(String degrees, String direction) {
// Parse degrees, minutes and seconds
int dotIndex = degrees.indexOf('.');
int d = degrees.substring(0, dotIndex - 2).toInt();
int m = degrees.substring(dotIndex - 2, dotIndex).toInt();
float s = degrees.substring(dotIndex).toFloat();
// Convert to decimal degrees
float decimal = d + (m / 60.0) + (s / 3600.0);
// Adjust for direction
if (direction == "S" || direction == "W") {
decimal = -decimal;
}
return String(decimal, 6); // Return as string with 6 decimal places
}
void uploadToBafaCloud(String latitude, String longitude) {
if (!client.connect(TCP_SERVER_ADDR, atoi(TCP_SERVER_PORT))) {
Serial.println("Failed to connect to Bafa Cloud server");
return;
}
String uploadData = "cmd=2&uid=" + String(UID) + "&topic=" + String(GPS_TOPIC) +
"&msg=Latitude:" + latitude + ",Longitude:" + longitude + "\r\n";
client.print(uploadData);
Serial.println("GPS Data uploaded to Bafa Cloud server: " + uploadData);
client.stop();
}
esp8266+atgm336h上传gps定位到巴法云
最新推荐文章于 2024-07-11 19:28:14 发布