Skip to content

Commit

Permalink
feat: use NINAB31 for senseBox MCU S2
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Mar 8, 2024
1 parent adabb62 commit a618964
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 91 deletions.
57 changes: 32 additions & 25 deletions src/NINAB31serial.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#if defined(ARDUINO_SAMD_MKR1000)
#if defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
#include "NINAB31serial.h"

#if defined(ARDUINO_SAMD_MKR1000)
#define SerialBLE Serial3
#elif defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
#define SerialBLE Serial1
#endif


String NINAB31Serial::m_input="";
bool NINAB31Serial::connected=false;

bool NINAB31Serial::configModule(){
Serial3.print("AT+UMRS=115200,2,8,1,1\r"); //115200, no flow control, 8 data bits, 1 stop bit, no parity
Serial3.print("AT&W0\r"); //write configuration to nonvolatile memory
Serial3.print("AT+CPWROFF\r"); //restart module into new configuration
Serial3.flush();
SerialBLE.print("AT+UMRS=115200,2,8,1,1\r"); //115200, no flow control, 8 data bits, 1 stop bit, no parity
SerialBLE.print("AT&W0\r"); //write configuration to nonvolatile memory
SerialBLE.print("AT+CPWROFF\r"); //restart module into new configuration
SerialBLE.flush();
delay(2000); //wait for module to come up
/*digitalWrite(22,HIGH); //hardware power cycle - avoid if possible
delay(500);
Expand All @@ -19,9 +26,9 @@ bool NINAB31Serial::configModule(){
}

bool NINAB31Serial::begin(){
Serial3.begin(115200);
SerialBLE.begin(115200);
delay(500);
checkResponse("AT",500); //throwaway command in case buffer gets reinitialized because Serial3.begin was already called
checkResponse("AT",500); //throwaway command in case buffer gets reinitialized because SerialBLE.begin was already called
for(int i=0;i<3;i++){ //try to send a command, in case it fails configure device and restart it
if(checkResponse("ATE0",500)){
return true;
Expand Down Expand Up @@ -85,17 +92,17 @@ bool NINAB31Serial::writeValue(int characteristic, uint8_t* value, int len){


int NINAB31Serial::parseResponse(String cmd, uint32_t timeout){
while(Serial3.available()){
(Serial3.read());
while(SerialBLE.available()){
(SerialBLE.read());
}
Serial3.print(cmd);
Serial3.write('\r');
Serial3.flush();
SerialBLE.print(cmd);
SerialBLE.write('\r');
SerialBLE.flush();
String input="";
auto starttime=millis();
while(millis()-starttime<timeout || timeout==0){
if(Serial3.available()){
input+=(char)(Serial3.read());
if(SerialBLE.available()){
input+=(char)(SerialBLE.read());
if(input.endsWith("ERROR\r")){
//Serial.println(String("error with command ")+cmd);
return -1;
Expand All @@ -118,15 +125,15 @@ int NINAB31Serial::parseResponse(String cmd, uint32_t timeout){
}

bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
while(Serial3.available())Serial3.read(); //flush input buffer
Serial3.print(msg);
Serial3.write('\r');
Serial3.flush();
while(SerialBLE.available())SerialBLE.read(); //flush input buffer
SerialBLE.print(msg);
SerialBLE.write('\r');
SerialBLE.flush();
String input="";
auto starttime=millis();
while(millis()-starttime<timeout || timeout==0){
if(Serial3.available()){
input+=(char)(Serial3.read());
if(SerialBLE.available()){
input+=(char)(SerialBLE.read());
if(input.endsWith("ERROR\r")){
return false;
}
Expand All @@ -136,7 +143,7 @@ bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
}
}
return false;

}

bool NINAB31Serial::checkUnsolicited(){
Expand All @@ -153,8 +160,8 @@ bool NINAB31Serial::checkUnsolicited(){
}

bool NINAB31Serial::poll(){
if(Serial3.available()){
m_input+=(char)(Serial3.read());
if(SerialBLE.available()){
m_input+=(char)(SerialBLE.read());
if(m_input.endsWith("\r")){
if(checkUnsolicited()){
flushInput();
Expand All @@ -177,12 +184,12 @@ String NINAB31Serial::checkCharWritten(int handle){
return m_input.substring(seccommapos+1,thirdcommapos);
}
}

}else{
//Serial.println(m_input);
}
return "";

}

void NINAB31Serial::flushInput(){
Expand Down
70 changes: 35 additions & 35 deletions src/phyphoxBLE_ESP32.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef ESP32
#if defined(ESP32) && !defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
#include "phyphoxBLE_ESP32.h"
#include "Arduino.h"
#include <stdio.h>
Expand Down Expand Up @@ -99,8 +99,8 @@ class MyCharCallback: public BLECharacteristicCallbacks {
PhyphoxBLE* myServerPointer;
void onWrite(BLECharacteristic *pCharacteristic) {
PhyphoxBLE::configHandlerDebug();
}

}
};

class MyServerCallbacks: public BLEServerCallbacks {
Expand All @@ -118,7 +118,7 @@ class MyServerCallbacks: public BLEServerCallbacks {
void PhyphoxBLE::configHandlerDebug(){
if(configHandler!=nullptr){
(*configHandler)();
}
}
}

void PhyphoxBLE::eventCharacteristicHandler(){
Expand All @@ -132,14 +132,14 @@ void PhyphoxBLE::eventCharacteristicHandler(){
PhyphoxBLE::experimentTime = swap_int64(et);
if(experimentEventHandler!=nullptr){
(*experimentEventHandler)();
}
}
}

void PhyphoxBLE::setMTU(uint16_t mtuSize) {
BLEDevice::setMTU(mtuSize+3); //user mtu size + 3 for overhead

PhyphoxBLE::MTU = mtuSize;
PhyphoxBleExperiment::MTU = mtuSize;
PhyphoxBleExperiment::MTU = mtuSize;
}

void PhyphoxBLE::start(const char* DEVICE_NAME, uint8_t* exp_pointer, size_t len){
Expand Down Expand Up @@ -169,18 +169,18 @@ void PhyphoxBLE::start(const char * DEVICE_NAME)
PhyphoxBleExperiment::View firstView;

//Graph
PhyphoxBleExperiment::Graph firstGraph; //Create graph which will plot random numbers over time
firstGraph.setChannel(0,1);
PhyphoxBleExperiment::Graph firstGraph; //Create graph which will plot random numbers over time
firstGraph.setChannel(0,1);

//Value
PhyphoxBleExperiment::Value valueField;
valueField.setChannel(1);

firstView.addElement(firstGraph);
firstView.addElement(valueField);
firstView.addElement(valueField);
defaultExperiment.addView(firstView);
addExperiment(defaultExperiment);

addExperiment(defaultExperiment);
}

BLEDevice::init(DEVICE_NAME);
Expand All @@ -192,28 +192,28 @@ void PhyphoxBLE::start(const char * DEVICE_NAME)
phyphoxBleExperimentCharacteristicUUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
BLECharacteristic::PROPERTY_NOTIFY
);
eventCharacteristic = phyphoxExperimentService->createCharacteristic(
phyphoxBleEventCharacteristicUUID,
BLECharacteristic::PROPERTY_WRITE
);
);

phyphoxDataService = myServer->createService(phyphoxBleDataServiceUUID);

dataCharacteristic = phyphoxDataService->createCharacteristic(
phyphoxBleDataCharacteristicUUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
BLECharacteristic::PROPERTY_NOTIFY

);

configCharacteristic = phyphoxDataService->createCharacteristic(
phyphoxBleConfigCharacteristicUUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
BLECharacteristic::PROPERTY_NOTIFY
);

myExperimentDescriptor = new BLE2902();
Expand All @@ -226,12 +226,12 @@ void PhyphoxBLE::start(const char * DEVICE_NAME)
myDataDescriptor->setCallbacks(new MyDataCallback());
eventCharacteristic->setCallbacks(new MyEventCallback());
configCharacteristic->setCallbacks(new MyCharCallback());

dataCharacteristic->addDescriptor(myDataDescriptor);
experimentCharacteristic->addDescriptor(myExperimentDescriptor);
eventCharacteristic->addDescriptor(myEventDescriptor);
configCharacteristic->addDescriptor(myConfigDescriptor);


phyphoxExperimentService->start();
phyphoxDataService->start();
Expand All @@ -255,7 +255,7 @@ void PhyphoxBLE::poll(int timeout) {
void PhyphoxBLE::staticStartTask(void* _this){
PhyphoxBLE::when_subscription_received();
delay(1);
}
}

void PhyphoxBLE::startTask()
{
Expand All @@ -267,8 +267,8 @@ void PhyphoxBLE::write(float& value)
{
/**
* \brief Write a single float into characteristic
* The float is parsed to uint8_t*
* because the gattServer write method
* The float is parsed to uint8_t*
* because the gattServer write method
* expects a pointer to uint8_t
* \param f1 represent a float most likeley sensor data
*/
Expand Down Expand Up @@ -380,40 +380,40 @@ void PhyphoxBLE::when_subscription_received()
experimentSizeArray[0]= (arrayLength >> 24);
experimentSizeArray[1]= (arrayLength >> 16);
experimentSizeArray[2]= (arrayLength >> 8);
experimentSizeArray[3]= arrayLength;
experimentSizeArray[3]= arrayLength;

uint8_t checksumArray[4] = {0};
checksumArray[0]= (checksum >> 24) & 0xFF;
checksumArray[1]= (checksum >> 16) & 0xFF;
checksumArray[1]= (checksum >> 16) & 0xFF;
checksumArray[2]= (checksum >> 8) & 0xFF;
checksumArray[3]= checksum & 0xFF;
checksumArray[3]= checksum & 0xFF;

copy(phyphox, phyphox+7, header);
copy(experimentSizeArray, experimentSizeArray+ 4, header + 7);
copy(checksumArray, checksumArray + 4, header +11);
copy(checksumArray, checksumArray + 4, header +11);
experimentCharacteristic->setValue(header,sizeof(header));
experimentCharacteristic->notify();

for(size_t i = 0; i < exp_len/20; ++i){
copy(exp+i*20, exp+i*20+20, header);
experimentCharacteristic->setValue(header,sizeof(header));
experimentCharacteristic->notify();
delay(10);// mh does not work anymore with 1ms delay?!
delay(10);// mh does not work anymore with 1ms delay?!
}

if(exp_len%20 != 0){
const size_t rest = exp_len%20;
uint8_t slice[rest];
copy(exp + exp_len - rest, exp + exp_len, slice);
experimentCharacteristic->setValue(slice,sizeof(slice));
experimentCharacteristic->notify();
experimentCharacteristic->notify();
delay(1);
}


myAdvertising->start();


vTaskDelete( NULL );

}
Expand All @@ -423,7 +423,7 @@ void PhyphoxBLE::addExperiment(PhyphoxBleExperiment& exp)
{
storage[i]=0;
}

exp.getFirstBytes(EXPARRAY, deviceName);
for(uint8_t i=0;i<phyphoxBleNViews; i++){
for(int j=0; j<phyphoxBleNElements; j++){
Expand All @@ -450,8 +450,8 @@ void PhyphoxBLE::begin(HardwareSerial* hwPrint)
#ifdef DEBUG
printer = hwPrint;
if(printer)
printer->begin(115200);
#endif
printer->begin(115200);
#endif
}

void PhyphoxBLE::printXML(HardwareSerial* printer){
Expand Down
Loading

0 comments on commit a618964

Please sign in to comment.