tp BLE
This commit is contained in:
91
tp1/ATOM_GPS/ATOM_GPS.ino
Normal file
91
tp1/ATOM_GPS/ATOM_GPS.ino
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (c) 2021 by M5Stack
|
||||
* Equipped with Atom-Lite/Matrix sample source code
|
||||
* 配套 Atom-Lite/Matrix 示例源代码
|
||||
* Visit for more information: https://docs.m5stack.com/en/atom/atomicgps
|
||||
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/atom/atomicgps
|
||||
*
|
||||
* Product: GPS.
|
||||
* Date: 2021/9/1
|
||||
*******************************************************************************
|
||||
This is an example used SerialBT,you can can view gps data by connecting
|
||||
to Bluetooth assistant on your mobilephone or Serial Monitor
|
||||
the GPS log will be written to SD card
|
||||
这是一个使用串行BT的示例,您可以通过连接到手机或串行监视器上的蓝牙助手来查看GPS数据。GPS日志将写入SD卡
|
||||
*/
|
||||
|
||||
#include "M5Atom.h"
|
||||
#include "GPSAnalyse.h"
|
||||
#include <SPI.h>
|
||||
#include "FS.h"
|
||||
#include <SD.h>
|
||||
#include <BluetoothSerial.h>
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
GPSAnalyse GPS;
|
||||
|
||||
uint64_t chipid;
|
||||
char chipname[256];
|
||||
|
||||
const char filename[] = "/GPSdata.txt";
|
||||
File txtFile;
|
||||
|
||||
float Lat;
|
||||
float Lon;
|
||||
String Utc;
|
||||
|
||||
bool writeLog(
|
||||
String filename) { // Write GPSdata to SDcard. 将 GPS 数据写入 SD 卡
|
||||
txtFile = SD.open(filename, FILE_APPEND);
|
||||
if (txtFile) {
|
||||
txtFile.print(Lat);
|
||||
txtFile.print(", ");
|
||||
txtFile.print(Lon);
|
||||
txtFile.print(", ");
|
||||
txtFile.println(Utc);
|
||||
txtFile.close();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
M5.begin(true, false, true);
|
||||
chipid = ESP.getEfuseMac();
|
||||
sprintf(chipname, "SerialBT_%04X", (uint16_t)(chipid >> 32));
|
||||
Serial.printf("Bluetooth: %s\n", chipname);
|
||||
SPI.begin(23, 33, 19, -1);
|
||||
if (!SD.begin(-1, SPI, 40000000)) {
|
||||
Serial.println("initialization failed!");
|
||||
}
|
||||
sdcard_type_t Type = SD.cardType();
|
||||
|
||||
Serial.printf("SDCard Type = %d \r\n", Type);
|
||||
Serial.printf("SDCard Size = %d \r\n", (int)(SD.cardSize() / 1024 / 1024));
|
||||
|
||||
M5.dis.fillpix(0x00004f);
|
||||
|
||||
Serial1.begin(9600, SERIAL_8N1, 22, -1);
|
||||
SerialBT.begin(chipname);
|
||||
GPS.setTaskName("GPS");
|
||||
GPS.setTaskPriority(2);
|
||||
GPS.setSerialPtr(Serial1);
|
||||
GPS.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
GPS.upDate();
|
||||
Lat = GPS.s_GNRMC.Latitude;
|
||||
Lon = GPS.s_GNRMC.Longitude;
|
||||
Utc = GPS.s_GNRMC.Utc;
|
||||
SerialBT.printf("Latitude= %.5f \r\n", Lat);
|
||||
SerialBT.printf("Longitude= %.5f \r\n", Lon);
|
||||
SerialBT.printf("DATA= %s \r\n", Utc);
|
||||
Serial.printf("Latitude= %.5f \r\n", Lat);
|
||||
Serial.printf("Longitude= %.5f \r\n", Lon);
|
||||
Serial.printf("DATA= %s \r\n", Utc);
|
||||
writeLog(filename);
|
||||
delay(1000);
|
||||
}
|
278
tp1/ATOM_GPS/GPSAnalyse.cpp
Normal file
278
tp1/ATOM_GPS/GPSAnalyse.cpp
Normal file
@@ -0,0 +1,278 @@
|
||||
#include "GPSAnalyse.h"
|
||||
|
||||
GPSAnalyse::GPSAnalyse()
|
||||
{
|
||||
_GPS_Str.clear();
|
||||
}
|
||||
|
||||
|
||||
void GPSAnalyse::setSerialPtr(HardwareSerial &serial)
|
||||
{
|
||||
_serial = &serial;
|
||||
_xSemaphore = xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
GPSAnalyse::~GPSAnalyse()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GPSAnalyse::run(void *data)
|
||||
{
|
||||
GPSReadBuff = (char*)calloc(1024,sizeof(char));
|
||||
Serial.println("GPS Task");
|
||||
while (1)
|
||||
{
|
||||
if (_serial->available())
|
||||
{
|
||||
|
||||
memset(GPSReadBuff, 1024, sizeof(char));
|
||||
_serial->readBytes(GPSReadBuff, _serial->available());
|
||||
_GPS_Str.concat(GPSReadBuff);
|
||||
xSemaphoreTake(_xSemaphore, portMAX_DELAY);
|
||||
Analyse();
|
||||
xSemaphoreGive(_xSemaphore);
|
||||
delay(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
delay(5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GPSAnalyse::upDate()
|
||||
{
|
||||
xSemaphoreTake(_xSemaphore, portMAX_DELAY);
|
||||
memcpy(&s_GNRMC,&_s_GNRMC,sizeof(GNRMC_t));
|
||||
memcpy(&s_GNGAS,&_s_GNGAS,sizeof(GNGSA_t));
|
||||
memcpy(&s_GPGSV,&_s_GPGSV,sizeof(GPGSV_t));
|
||||
xSemaphoreGive(_xSemaphore);
|
||||
}
|
||||
|
||||
void GPSAnalyse::AnaGPRMC(String str)
|
||||
{
|
||||
str = str.substring(str.indexOf("GNRMC"), str.length());
|
||||
if (str.indexOf('*') != -1)
|
||||
{
|
||||
int indexsub = str.indexOf('*');
|
||||
String sumstr = str.substring(indexsub + 1, str.length());
|
||||
str = str.substring(0, indexsub);
|
||||
//Serial.println(sumstr);
|
||||
}
|
||||
|
||||
int index = 0, last_index = 0, pamindex = 1;
|
||||
|
||||
while (str.indexOf(',', index) != -1)
|
||||
{
|
||||
index = str.indexOf(',', index + 1);
|
||||
last_index = str.indexOf(',', index + 1);
|
||||
//Serial.printf("index:%d,%d\n", index, last_index);
|
||||
if (index != -1)
|
||||
{
|
||||
last_index = (last_index == -1) ? str.length() : last_index;
|
||||
if ((last_index - index) > 1)
|
||||
{
|
||||
String pamstr = str.substring(index + 1, last_index);
|
||||
#ifdef DEBUG_GPS
|
||||
Serial.printf("%d:", pamindex);
|
||||
Serial.println(pamstr);
|
||||
#endif
|
||||
_s_GNRMC.pamstr[pamindex] = pamstr;
|
||||
}
|
||||
else
|
||||
{
|
||||
_s_GNRMC.pamstr[pamindex].clear();
|
||||
}
|
||||
|
||||
pamindex++;
|
||||
}
|
||||
}
|
||||
|
||||
_s_GNRMC.Utc = _s_GNRMC.pamstr[1];
|
||||
if (_s_GNRMC.pamstr[2].length() != 0)
|
||||
{
|
||||
_s_GNRMC.State = _s_GNRMC.pamstr[2].charAt(0);
|
||||
}
|
||||
|
||||
{
|
||||
_s_GNRMC.Latitude = _s_GNRMC.pamstr[3].substring(0, 2).toDouble() + _s_GNRMC.pamstr[3].substring(2, 4).toDouble() / 60 + _s_GNRMC.pamstr[3].substring(5, 9).toDouble() / 600000;
|
||||
}
|
||||
//_s_GNRMC.Latitude = _s_GNRMC.pamstr[3].toDouble();
|
||||
_s_GNRMC.LatitudeMark = _s_GNRMC.pamstr[4].charAt(0);
|
||||
{
|
||||
_s_GNRMC.Longitude = _s_GNRMC.pamstr[5].substring(0, 3).toDouble() + _s_GNRMC.pamstr[5].substring(3, 5).toDouble() / 60 + _s_GNRMC.pamstr[5].substring(6, 10).toDouble() / 600000;
|
||||
}
|
||||
//_s_GNRMC.Longitude = _s_GNRMC.pamstr[5].toDouble();
|
||||
_s_GNRMC.LongitudeMark = _s_GNRMC.pamstr[6].charAt(0);
|
||||
_s_GNRMC.TrackSpeed = _s_GNRMC.pamstr[7].toFloat();
|
||||
_s_GNRMC.TrackAngle = _s_GNRMC.pamstr[8].toFloat();
|
||||
_s_GNRMC.Date = _s_GNRMC.pamstr[9];
|
||||
_s_GNRMC.Magnetic = _s_GNRMC.pamstr[10].toFloat();
|
||||
_s_GNRMC.Declination = _s_GNRMC.pamstr[11].charAt(0);
|
||||
_s_GNRMC.mode = _s_GNRMC.pamstr[12].charAt(0);
|
||||
}
|
||||
|
||||
void GPSAnalyse::AnaGNGAS(String str)
|
||||
{
|
||||
str = str.substring(str.indexOf("GNGSA"), str.length());
|
||||
|
||||
if (str.indexOf('*') != -1)
|
||||
{
|
||||
int indexsub = str.indexOf('*');
|
||||
String sumstr = str.substring(indexsub + 1, str.length());
|
||||
str = str.substring(0, indexsub);
|
||||
//Serial.println(sumstr);
|
||||
}
|
||||
|
||||
int index = 0, last_index = 0, pamindex = 1;
|
||||
while (str.indexOf(',', index) != -1)
|
||||
{
|
||||
index = str.indexOf(',', index + 1);
|
||||
last_index = str.indexOf(',', index + 1);
|
||||
if (index != -1)
|
||||
{
|
||||
last_index = (last_index == -1) ? str.length() : last_index;
|
||||
if ((last_index - index) > 1)
|
||||
{
|
||||
String pamstr = str.substring(index + 1, last_index);
|
||||
#ifdef DEBUG_GPS
|
||||
Serial.printf("%d:", pamindex);
|
||||
Serial.println(pamstr);
|
||||
#endif
|
||||
_s_GNGAS.pamstr[pamindex] = pamstr;
|
||||
}
|
||||
else
|
||||
{
|
||||
_s_GNGAS.pamstr[pamindex].clear();
|
||||
}
|
||||
pamindex++;
|
||||
}
|
||||
}
|
||||
|
||||
_s_GNGAS.mode2 = _s_GNGAS.pamstr[1].charAt(0);
|
||||
_s_GNGAS.mode1 = _s_GNGAS.pamstr[2].toInt();
|
||||
for (size_t i = 0; i < 12; i++)
|
||||
{
|
||||
_s_GNGAS.PINMap[i] = _s_GNGAS.pamstr[3 + i].toInt();
|
||||
}
|
||||
_s_GNGAS.PDOP = _s_GNGAS.pamstr[15].toFloat();
|
||||
_s_GNGAS.HDOP = _s_GNGAS.pamstr[16].toFloat();
|
||||
_s_GNGAS.VDOP = _s_GNGAS.pamstr[17].toFloat();
|
||||
}
|
||||
|
||||
void GPSAnalyse::AnaGPGSV(String str)
|
||||
{
|
||||
//Serial.println(str);
|
||||
str = str.substring(str.indexOf("GPGSV"), str.length());
|
||||
|
||||
if (str.indexOf('*') != -1)
|
||||
{
|
||||
int indexsub = str.indexOf('*');
|
||||
String sumstr = str.substring(indexsub + 1, str.length());
|
||||
str = str.substring(0, indexsub);
|
||||
//Serial.println(sumstr);
|
||||
}
|
||||
|
||||
int index = 0, last_index = 0, pamindex = 1;
|
||||
while (str.indexOf(',', index) != -1)
|
||||
{
|
||||
index = str.indexOf(',', index + 1);
|
||||
last_index = str.indexOf(',', index + 1);
|
||||
if (index != -1)
|
||||
{
|
||||
last_index = (last_index == -1) ? str.length() : last_index;
|
||||
if ((last_index - index) > 1)
|
||||
{
|
||||
String pamstr = str.substring(index + 1, last_index);
|
||||
#ifdef DEBUG_GPS
|
||||
Serial.printf("%d:", pamindex);
|
||||
Serial.println(pamstr);
|
||||
#endif
|
||||
_s_GPGSV.pamstr[pamindex] = pamstr;
|
||||
}
|
||||
else
|
||||
{
|
||||
_s_GPGSV.pamstr[pamindex].clear();
|
||||
}
|
||||
pamindex++;
|
||||
}
|
||||
}
|
||||
int SatelliteSize = (pamindex - 4) / 4;
|
||||
//Serial.printf("Number%d\n", SatelliteSize);
|
||||
|
||||
_s_GPGSV.size = _s_GPGSV.pamstr[1].toInt();
|
||||
_s_GPGSV.Number = _s_GPGSV.pamstr[2].toInt();
|
||||
_s_GPGSV.SatelliteSize = _s_GPGSV.pamstr[3].toInt();
|
||||
|
||||
if (_s_GPGSV.Number == 1)
|
||||
{
|
||||
for (size_t i = 0; i < 32; i++)
|
||||
{
|
||||
_s_GPGSV.Satellite[i].flag = false;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < SatelliteSize; i++)
|
||||
{
|
||||
int id = _s_GPGSV.pamstr[4 + (i * 4) + 0].toInt();
|
||||
if( id >= 32 ) continue;
|
||||
if(( 7 + (i * 4)) > 50 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
_s_GPGSV.Satellite[id].elevation = _s_GPGSV.pamstr[4 + (i * 4) + 1].toInt();
|
||||
_s_GPGSV.Satellite[id].Azimuth = _s_GPGSV.pamstr[4 + (i * 4) + 2].toInt();
|
||||
_s_GPGSV.Satellite[id].SNR = (_s_GPGSV.pamstr[4 + (i * 4) + 3].length() == 0) ? -1 : _s_GPGSV.pamstr[4 + (i * 4) + 3].toInt();
|
||||
_s_GPGSV.Satellite[id].flag = true;
|
||||
}
|
||||
|
||||
if (_s_GPGSV.Number == _s_GPGSV.size)
|
||||
{
|
||||
for (size_t i = 0; i < 32; i++)
|
||||
{
|
||||
if (_s_GPGSV.Satellite[i].flag == true)
|
||||
{
|
||||
Serial.printf("ID %d:%d,%d,%d\n", i, _s_GPGSV.Satellite[i].elevation, _s_GPGSV.Satellite[i].Azimuth, _s_GPGSV.Satellite[i].SNR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GPSAnalyse::Analyse()
|
||||
{
|
||||
while (_GPS_Str.indexOf('\r') != -1)
|
||||
{
|
||||
int index = _GPS_Str.indexOf('\r');
|
||||
String str = _GPS_Str.substring(0, index);
|
||||
_GPS_Str = _GPS_Str.substring(index + 3, _GPS_Str.length());
|
||||
|
||||
str.trim();
|
||||
|
||||
//Serial.println(str);
|
||||
|
||||
if (str.indexOf("GNRMC") != -1)
|
||||
{
|
||||
AnaGPRMC(str);
|
||||
}
|
||||
else if (str.indexOf("GPVTG") != -1)
|
||||
{
|
||||
}
|
||||
else if (str.indexOf("GPGGA") != -1)
|
||||
{
|
||||
|
||||
}
|
||||
else if (str.indexOf("GNGSA") != -1)
|
||||
{
|
||||
//Serial.print("GNGSA:");
|
||||
//AnaGNGAS(str);
|
||||
}
|
||||
else if (str.indexOf("GPGSV") != -1)
|
||||
{
|
||||
//Serial.print("GPGSV:");
|
||||
AnaGPGSV(str);
|
||||
}
|
||||
else if (str.indexOf("GPGLL") != -1)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
117
tp1/ATOM_GPS/GPSAnalyse.h
Normal file
117
tp1/ATOM_GPS/GPSAnalyse.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#ifndef _GPSANALYSE_H_
|
||||
#define _GPSANALYSE_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "utility/Task.h"
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
//#define DEBUG_GPS
|
||||
|
||||
typedef struct GNRMC
|
||||
{
|
||||
String pamstr[13];
|
||||
String Utc; //1
|
||||
char State; //2
|
||||
double Latitude; //3
|
||||
char LatitudeMark; //4
|
||||
double Longitude; //5
|
||||
char LongitudeMark; //6
|
||||
float TrackSpeed; //7
|
||||
float TrackAngle; //8
|
||||
String Date; //9
|
||||
float Magnetic; //10
|
||||
char Declination; //11
|
||||
int mode; //12
|
||||
String Sum;
|
||||
}GNRMC_t;
|
||||
|
||||
|
||||
typedef struct GNGSA
|
||||
{
|
||||
String pamstr[50];
|
||||
char mode2;
|
||||
int mode1;
|
||||
int PINMap[12];
|
||||
float PDOP;
|
||||
float HDOP;
|
||||
float VDOP;
|
||||
String Sum;
|
||||
|
||||
}GNGSA_t;
|
||||
|
||||
typedef struct GPSSatellite
|
||||
{
|
||||
bool flag;
|
||||
int id;
|
||||
int elevation;
|
||||
int Azimuth;
|
||||
int SNR;
|
||||
}GPSSatellite_t;
|
||||
|
||||
typedef struct GPGSV
|
||||
{
|
||||
String pamstr[128];
|
||||
int size;
|
||||
int Number;
|
||||
int SatelliteSize;
|
||||
GPSSatellite_t Satellite[32];
|
||||
String sum;
|
||||
}GPGSV_t;
|
||||
|
||||
/*
|
||||
typedef struct GPGGA
|
||||
{
|
||||
}GPGGA_t;
|
||||
|
||||
typedef struct GPGGA
|
||||
{
|
||||
}GPGGA_t;
|
||||
|
||||
|
||||
typedef struct GPGLL
|
||||
{
|
||||
|
||||
}GPGLL_t;
|
||||
*/
|
||||
|
||||
class GPSAnalyse : public Task
|
||||
{
|
||||
private:
|
||||
|
||||
/* data */
|
||||
String _GPS_Str;
|
||||
HardwareSerial *_serial;
|
||||
SemaphoreHandle_t _xSemaphore = NULL;
|
||||
char *GPSReadBuff;
|
||||
|
||||
void Analyse();
|
||||
|
||||
void AnaGPRMC(String str);
|
||||
void AnaGNGAS(String str);
|
||||
void AnaGPGSV(String str);
|
||||
|
||||
void run(void *data);
|
||||
|
||||
GNRMC_t _s_GNRMC;
|
||||
GNGSA_t _s_GNGAS;
|
||||
GPGSV_t _s_GPGSV;
|
||||
|
||||
public:
|
||||
GPSAnalyse();
|
||||
~GPSAnalyse();
|
||||
void setSerialPtr(HardwareSerial &serial);
|
||||
void upDate();
|
||||
|
||||
|
||||
GNRMC_t s_GNRMC;
|
||||
GNGSA_t s_GNGAS;
|
||||
GPGSV_t s_GPGSV;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user