tp BLE
This commit is contained in:
parent
4aef9ea21c
commit
739d5580dd
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
|
38
tp2/bt/bt.ino
Normal file
38
tp2/bt/bt.ino
Normal file
@ -0,0 +1,38 @@
|
||||
#include "BluetoothSerial.h"
|
||||
#include <M5StickC.h>
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
|
||||
void setup() {
|
||||
M5.begin(115200);
|
||||
SerialBT.begin("PomPomBT");
|
||||
Serial.println("The device started.");
|
||||
|
||||
M5.Lcd.fillScreen(BLACK);
|
||||
M5.Lcd.setCursor(10, 10);
|
||||
M5.Lcd.setRotation(3);
|
||||
M5.Lcd.setTextColor(WHITE);
|
||||
M5.Lcd.setTextSize(2);
|
||||
M5.Lcd.printf("PomPom ON");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
String cmd = String("AJ");
|
||||
String eol = String("\r\n");
|
||||
String cmdR;
|
||||
if (SerialBT.available()) {
|
||||
cmdR = SerialBT.readString();
|
||||
if(cmdR == (cmd + eol)) {
|
||||
SerialBT.println("Applejack is best pony !");
|
||||
} else {
|
||||
SerialBT.println("Pony doesn't received !");
|
||||
}
|
||||
}
|
||||
delay(2000);
|
||||
|
||||
|
||||
|
||||
}
|
14
tp2/bt/debug.cfg
Normal file
14
tp2/bt/debug.cfg
Normal file
@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
|
||||
#
|
||||
# For example, OpenOCD can be started for ESP32 debugging on
|
||||
#
|
||||
# openocd -f board/esp32-wrover-kit-3.3v.cfg
|
||||
#
|
||||
|
||||
# Source the JTAG interface configuration file
|
||||
source [find interface/ftdi/esp32_devkitj_v1.cfg]
|
||||
set ESP32_FLASH_VOLTAGE 3.3
|
||||
# Source the ESP32 configuration file
|
||||
source [find target/esp32.cfg]
|
19
tp2/bt/debug_custom.json
Normal file
19
tp2/bt/debug_custom.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name":"Arduino on ESP32",
|
||||
"toolchainPrefix":"xtensa-esp32-elf",
|
||||
"svdFile":"esp32.svd",
|
||||
"request":"attach",
|
||||
"postAttachCommands":[
|
||||
"set remote hardware-watchpoint-limit 2",
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
],
|
||||
"overrideRestartCommands":[
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
]
|
||||
}
|
46087
tp2/bt/esp32.svd
Normal file
46087
tp2/bt/esp32.svd
Normal file
File diff suppressed because it is too large
Load Diff
BIN
tp2/smartHome.docx
Normal file
BIN
tp2/smartHome.docx
Normal file
Binary file not shown.
130
tp3/BLE/BLE.ino
Normal file
130
tp3/BLE/BLE.ino
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
|
||||
Ported to Arduino ESP32 by Evandro Copercini
|
||||
updates by chegewara
|
||||
*/
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEAdvertisedDevice.h>
|
||||
|
||||
int lux;
|
||||
int mini;
|
||||
// See the following for generating UUIDs:
|
||||
// https://www.uuidgenerator.net/
|
||||
|
||||
#define SERVICE_UUID "f608bae5-6eec-4be7-aafb-2bd43ce942bf"
|
||||
#define POWER_READ_UUID "06c79a69-54b1-46e9-9b1d-afb77e6fad15"
|
||||
#define POWER_CHECK_UUID "f54a0bdf-afa3-4d18-a96c-770ba5bc3577"
|
||||
#define SEUILS_UUID "e7588746-cde4-44c4-aec6-8ef838a71480"
|
||||
|
||||
bool estConnecte = false;
|
||||
char msgTOsend[30];
|
||||
//char rxValue [255];
|
||||
|
||||
class MyCallback : public BLECharacteristicCallbacks {
|
||||
void onRead(BLECharacteristic* pCharacteristic) {
|
||||
// Do something before the read completes
|
||||
|
||||
int lux=random(0, 9999);
|
||||
|
||||
sprintf(msgTOsend,"Puissance : %d",lux);
|
||||
pCharacteristic->setValue(msgTOsend);
|
||||
Serial.printf("Hello : %d \n",lux);
|
||||
//Serial.print('\n'):
|
||||
}
|
||||
void onWrite(BLECharacteristic* pCharacteristic) {
|
||||
// Do something because a new value was written.
|
||||
std::string rxValue=pCharacteristic->getValue(); //ex tel envoi 5000
|
||||
mini = atoi(rxValue.c_str());
|
||||
Serial.print("message received :");
|
||||
Serial.println(rxValue.c_str());
|
||||
// si valeur numérique :
|
||||
//uint8_t* received_data = pCharacteristic->getData();
|
||||
// Serial.println(*received_data,HEX);
|
||||
}
|
||||
};
|
||||
|
||||
class EtatServeur : public BLEServerCallbacks {
|
||||
void onConnect(BLEServer* pServer) {
|
||||
estConnecte = true;
|
||||
Serial.println("Connecté");
|
||||
}
|
||||
|
||||
void onDisconnect(BLEServer* pServer) {
|
||||
estConnecte = false;
|
||||
BLEDevice::startAdvertising();
|
||||
Serial.println("Déconnecté");
|
||||
}
|
||||
};
|
||||
|
||||
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||
Serial.printf("Advertised Device: %s", advertisedDevice.toString().c_str());
|
||||
if (advertisedDevice.haveRSSI()){
|
||||
Serial.printf("Rssi: %d \n", (int)advertisedDevice.getRSSI());
|
||||
}
|
||||
else {
|
||||
Serial.printf("\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BLECharacteristic *pCharacteristic;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting BLE ");
|
||||
|
||||
BLEDevice::init("PomPom");
|
||||
BLEServer *pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(new EtatServeur());
|
||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
//BLECharacteristic *pCharacteristic = pService->createCharacteristic(
|
||||
pCharacteristic = pService->createCharacteristic(
|
||||
POWER_READ_UUID,
|
||||
BLECharacteristic::PROPERTY_READ |
|
||||
BLECharacteristic::PROPERTY_NOTIFY |
|
||||
BLECharacteristic::PROPERTY_WRITE
|
||||
);
|
||||
|
||||
pCharacteristic->setValue("Hello ESP32 ");
|
||||
|
||||
//BLECharacteristic::setCallbacks(new MyCallback())
|
||||
pCharacteristic->setCallbacks(new MyCallback());
|
||||
|
||||
|
||||
pService->start();
|
||||
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
/*pAdvertising->setScanResponse(true);
|
||||
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
|
||||
pAdvertising->setMinPreferred(0x12);*/
|
||||
//pAdvertising->setCallbacks(new MyAdvertisedDeviceCallbacks());
|
||||
|
||||
BLEDevice::startAdvertising();
|
||||
Serial.println("Characteristic defined! Now you can read it in your phone!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//float h = getRssi();
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
if (estConnecte) {
|
||||
|
||||
lux=random(0, 9999);
|
||||
|
||||
if(lux<mini) {
|
||||
Serial.println("Pas assez de lumière");
|
||||
sprintf(msgTOsend,"Pas assez de lumière. Lux = %d",lux);
|
||||
pCharacteristic->setValue(msgTOsend);
|
||||
pCharacteristic->notify();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
//BLEDevice::startAdvertising();
|
||||
}
|
14
tp3/BLE/debug.cfg
Normal file
14
tp3/BLE/debug.cfg
Normal file
@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
|
||||
#
|
||||
# For example, OpenOCD can be started for ESP32 debugging on
|
||||
#
|
||||
# openocd -f board/esp32-wrover-kit-3.3v.cfg
|
||||
#
|
||||
|
||||
# Source the JTAG interface configuration file
|
||||
source [find interface/ftdi/esp32_devkitj_v1.cfg]
|
||||
set ESP32_FLASH_VOLTAGE 3.3
|
||||
# Source the ESP32 configuration file
|
||||
source [find target/esp32.cfg]
|
19
tp3/BLE/debug_custom.json
Normal file
19
tp3/BLE/debug_custom.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name":"Arduino on ESP32",
|
||||
"toolchainPrefix":"xtensa-esp32-elf",
|
||||
"svdFile":"esp32.svd",
|
||||
"request":"attach",
|
||||
"postAttachCommands":[
|
||||
"set remote hardware-watchpoint-limit 2",
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
],
|
||||
"overrideRestartCommands":[
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
]
|
||||
}
|
46087
tp3/BLE/esp32.svd
Normal file
46087
tp3/BLE/esp32.svd
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user