Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix main.ino Welcome Message, BLE client, OLED client and Console. #56

Merged
merged 5 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 129 additions & 53 deletions firmware/esp32/client/BleUartClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifdef USE_BLE
#include "BleUartClient.h"
#include "settings/settings.h"

/** Comment out to stop debug output */
// #define DEBUG_OUT

/** Service UUID for Uart */
#define UART_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
Expand Down Expand Up @@ -27,7 +30,7 @@ BLEServer *pServer;
bool deviceConnected = false;

/** Flag if we already registered on the server */
bool connectedToServer = false;
bool notifEnabled = false;

/** Unique device name */
char apName[] = "DR-xxxxxxxxxxxx";
Expand All @@ -44,28 +47,31 @@ void BleUartClient::receive(struct Datagram datagram, size_t len)
{
if (deviceConnected)
{
// /// \todo for debug only
// Serial.printf("BLE: sending %s with len %d\n", message.c_str(), message.length());
// /// \todo end of for debug only

// TODO: msg id? defaulting to 0 for now
uint16_t msg_id = 0x2020;

unsigned char buf[2 + len-DATAGRAM_HEADER + 2] = {'\0'};
memcpy(buf, &msg_id, 2);
//message.getBytes(buf + 2, message.length() + 1);
memcpy(buf+2, &datagram.message, len-DATAGRAM_HEADER);

// /// \todo for debug only
// Serial.println("BLE: Sending raw data");
// for (int idx = 0; idx < sizeof(buf); idx++)
// {
// Serial.printf("%02X ", buf[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

pCharacteristicUartTX->setValue(buf, 2 + len-DATAGRAM_HEADER + 2);
#ifdef DEBUG_OUT
Serial.printf("BLE:receive %s length %d\n", (char *)datagram.message, len - DATAGRAM_HEADER);
Serial.println("BLE::receive raw data");
for (int idx = 0; idx < len - DATAGRAM_HEADER; idx++)
{
Serial.printf("%02X ", datagram.message[idx]);
}
Serial.println("");
#endif

unsigned char buf[len - DATAGRAM_HEADER] = {'\0'};
memcpy(buf, &datagram.message, len-DATAGRAM_HEADER);

#ifdef DEBUG_OUT
Serial.printf("BLE: Sending raw data with len %d\n", sizeof(buf));
for (int idx = 0; idx < sizeof(buf); idx++)
{
Serial.printf("%02X ", buf[idx]);
}
Serial.println("");

Serial.printf("BLE::receive %s len %d type %c\n", buf, sizeof(buf), datagram.type);
#endif

pCharacteristicUartTX->setValue(buf, sizeof(buf));
pCharacteristicUartTX->notify();

// Give BLE time to get the data out
Expand All @@ -75,34 +81,62 @@ void BleUartClient::receive(struct Datagram datagram, size_t len)

void BleUartClient::handleData(void *data, size_t len)
{
//uint16_t msg_id;
//char msg[len - 2 + 1] = {'\0'};

// /// \todo for debug only
// char debug[len] = {'\0'};
// memcpy(debug, data, len);
// Serial.println("BLE: Received raw data");
// for (int idx = 0; idx < len; idx++)
// {
// Serial.printf("%02X ", debug[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

// parse out message and message id
//memcpy(&msg_id, data, 2);
//memcpy(msg, data + 2, rxLen - 2);
#ifdef DEBUG_OUT
char debug[len] = {'\0'};
memcpy(debug, data, len);
Serial.println("BLE: Received raw data");
for (int idx = 0; idx < len; idx++)
{
Serial.printf("%02X ", debug[idx]);
}
Serial.println("");
#endif

char *msg = (char *)data;

if (msg[2] == 'u')
{
if (msg[4] == '~')
{
// delete username
username = "";
saveUsername(username);
#ifdef DEBUG_OUT
Serial.printf("Deleted username over BLE\n");
#endif
return;
}
else
{
// username set over BLE
username = String(&msg[4]);
Serial.printf("Username set over BLE: %s\n", username.c_str());
saveUsername(username);
return;
}
}

if (msg[2] == 'i')
{
// Switch to WiFi interface
#ifdef DEBUG_OUT
Serial.printf("User wants to switch to WiFi");
#endif
saveUI(false);
delay(500);
ESP.restart();
}

struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
datagram.type = 'c';
memset(datagram.message, 0, 233);
datagram.type = msg[2];
memcpy(&datagram.message, data, len);
len = len+DATAGRAM_HEADER;

// /// \todo for debug only
// Serial.printf("BLE: received %s len %d\n", msg, len - 2 + 1);
// /// \todo end of for debug only
#ifdef DEBUG_OUT
Serial.printf("BLE::handleData %s len %d type %c\n", msg, len, datagram.type);
#endif

server->transmit(this, datagram, len);
server->transmit(this, datagram, len + DATAGRAM_HEADER);
}

/**
Expand All @@ -112,15 +146,20 @@ class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client connected");
#endif
pServer->updatePeerMTU(pServer->getConnId(), 260);
deviceConnected = true;
};

void onDisconnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client disconnected");
#endif
deviceConnected = false;
notifEnabled = false;
pAdvertising->start();
}
};
Expand All @@ -140,7 +179,15 @@ class UartTxCbHandler : public BLECharacteristicCallbacks
{
strncpy((char *)rxData, rxValue.c_str(), 512);

Serial.printf("UART write callback received %s\n", (char *)rxData);
#ifdef DEBUG_OUT
Serial.printf("BLE:onWrite write callback received %s length %d\n", (char *)rxData, rxLen);
Serial.println("BLE:onWrite raw data");
for (int idx = 0; idx < rxLen; idx++)
{
Serial.printf("%02X ", rxData[idx]);
}
Serial.println("");
#endif
dataRcvd = true;
}
};
Expand All @@ -154,13 +201,15 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
descrValue = pDescriptor->getValue();
if (descrValue[0] & (1 << 0))
{
#ifdef DEBUG_OUT
Serial.println("Notifications enabled");
#endif
if (connectCallback)
{
if (!connectedToServer)
if (!notifEnabled)
{
connectCallback(&ble_client);
connectedToServer = true;
notifEnabled = true;
}
else
{
Expand All @@ -171,6 +220,13 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
}
}
}
else
{
#ifdef DEBUG_OUT
Serial.println("Notifications disabled");
#endif
notifEnabled = false;
}
};
};

Expand All @@ -183,6 +239,25 @@ void BleUartClient::loop(void)
handleData(rxData, rxLen);
dataRcvd = false;
}
if (notifEnabled && !oldStatus)
{
oldStatus = true;
if (!username.isEmpty())
{
Datagram nameDatagram;
nameDatagram.type = 'u';
int msgLen = sprintf((char *)nameDatagram.message, "00u|%s", username.c_str());
receive(nameDatagram, msgLen + DATAGRAM_HEADER);
}
if (history)
{
history->replay(&ble_client);
}
}
if (!notifEnabled && oldStatus)
{
oldStatus = false;
}
}

void BleUartClient::startServer(void (*callback)(BleUartClient *))
Expand All @@ -200,16 +275,19 @@ void BleUartClient::init()
(uint8_t)(uniqueId), (uint8_t)(uniqueId >> 8),
(uint8_t)(uniqueId >> 16), (uint8_t)(uniqueId >> 24),
(uint8_t)(uniqueId >> 32), (uint8_t)(uniqueId >> 40));
#ifdef DEBUG_OUT
Serial.printf("Device name: %s\n", apName);
#endif

// Initialize BLE and set output power
BLEDevice::init(apName);
BLEDevice::setMTU(260);
BLEDevice::setPower(ESP_PWR_LVL_P7);

#ifdef DEBUG_OUT
BLEAddress thisAddress = BLEDevice::getAddress();

Serial.printf("BLE address: %s\n", thisAddress.toString().c_str());
#endif

// Create BLE Server
pServer = BLEDevice::createServer();
Expand Down Expand Up @@ -240,7 +318,6 @@ void BleUartClient::init()
txDescriptor = pCharacteristicUartTX->getDescriptorByUUID("2902");
if (txDescriptor != NULL)
{
Serial.println("Got descriptor for TX as 2902");
txDescriptor->setCallbacks(new DescriptorCallbacks());
}

Expand All @@ -252,4 +329,3 @@ void BleUartClient::init()
pAdvertising->addServiceUUID(UART_UUID);
pAdvertising->start();
}
#endif // #ifdef USE_BLE
4 changes: 0 additions & 4 deletions firmware/esp32/client/BleUartClient.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#ifdef USE_BLE
#ifndef BLECLIENT_H
#define BLECLIENT_H

Expand Down Expand Up @@ -27,8 +26,6 @@ class BleUartClient : public DisasterClient
BleUartClient *client;

public:
String buffer = "";

void receive(struct Datagram datagram, size_t len);

void handleData(void *data, size_t len);
Expand All @@ -38,4 +35,3 @@ class BleUartClient : public DisasterClient
};

#endif
#endif // #ifdef USE_BLE
27 changes: 20 additions & 7 deletions firmware/esp32/client/GPSClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "GPSClient.h"

extern char *macaddr;

void GPSClient::loop()
{
while (stream->available() > 0)
Expand All @@ -12,18 +14,29 @@ void GPSClient::loop()
{
if (gps.location.isValid() && gps.location.age() < beacon_period)
{
String message = String("c|<" + username + "> ") + gps.location.lat() + ", " + gps.location.lng();

size_t len = message.length();
uint8_t* data;
message.getBytes(data, len);
// 04m|<user>{"pos":[48.75608,2.302038]}
struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
datagram.type = 'c';
memcpy(datagram.message, data, len);
memset(datagram.message, 0, 233);
datagram.type = 'm';
size_t len;
if (username.length() > 0)
{
len = snprintf((char *)datagram.message, 233, "m|<%s>{\"pos\":[%.3f,%.3f]}", username.c_str(), gps.location.lat(), gps.location.lng());
}
else
{
len = snprintf((char *)datagram.message, 233, "m|<%s>{\"pos\":[%.3f,%.3f]}", macaddr, gps.location.lat(), gps.location.lng());
}
len = len+DATAGRAM_HEADER;

Serial.printf("Sending GPS %s\n", (char *)datagram.message);
server->transmit(this, datagram, len);
beacon_last = millis();
}
}
}

void GPSClient::setUsername(String newname)
{
username = newname;
}
13 changes: 9 additions & 4 deletions firmware/esp32/client/GPSClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@
#include "../DisasterClient.h"

#include "TinyGPS++.h"
#include "settings/settings.h"

#define DEFAULT_PERIOD 10000 // 10 seconds
#define DEFAULT_PERIOD 30000 // 10 seconds

class GPSClient : public DisasterClient
{
TinyGPSPlus gps;
Stream *stream;
// Stream *stream;
HardwareSerial *stream;

unsigned long beacon_period = DEFAULT_PERIOD;
unsigned long beacon_last = 0;

public:
String username = "beacon";
// String username = "beacon";

GPSClient(Stream *s, unsigned long p = DEFAULT_PERIOD)
// GPSClient(Stream *s, unsigned long p = DEFAULT_PERIOD)
// : stream(s), beacon_period(p){};
GPSClient(HardwareSerial *s, unsigned long p = DEFAULT_PERIOD)
: stream(s), beacon_period(p){};

void loop();
void setUsername(String username);
};

#endif
1 change: 1 addition & 0 deletions firmware/esp32/client/LoRaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void LoRaClient::loop()
if (packet.totalLength > HEADER_LENGTH)
{
struct Datagram datagram;
memset(datagram.message, 0, 233);
size_t len = packet.totalLength - HEADER_LENGTH;
// parse out datagram
memcpy(&datagram, packet.data, len);
Expand Down
Loading