Skip to content

Commit ef5e285

Browse files
authored
Merge pull request #5 from sensebox/feat/sensebox-mcu-s2
Use NINAB31 for senseBox MCU S2
2 parents adabb62 + a618964 commit ef5e285

File tree

4 files changed

+98
-91
lines changed

4 files changed

+98
-91
lines changed

src/NINAB31serial.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
#if defined(ARDUINO_SAMD_MKR1000)
1+
#if defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
22
#include "NINAB31serial.h"
33

4+
#if defined(ARDUINO_SAMD_MKR1000)
5+
#define SerialBLE Serial3
6+
#elif defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
7+
#define SerialBLE Serial1
8+
#endif
9+
10+
411
String NINAB31Serial::m_input="";
512
bool NINAB31Serial::connected=false;
613

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

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

8693

8794
int NINAB31Serial::parseResponse(String cmd, uint32_t timeout){
88-
while(Serial3.available()){
89-
(Serial3.read());
95+
while(SerialBLE.available()){
96+
(SerialBLE.read());
9097
}
91-
Serial3.print(cmd);
92-
Serial3.write('\r');
93-
Serial3.flush();
98+
SerialBLE.print(cmd);
99+
SerialBLE.write('\r');
100+
SerialBLE.flush();
94101
String input="";
95102
auto starttime=millis();
96103
while(millis()-starttime<timeout || timeout==0){
97-
if(Serial3.available()){
98-
input+=(char)(Serial3.read());
104+
if(SerialBLE.available()){
105+
input+=(char)(SerialBLE.read());
99106
if(input.endsWith("ERROR\r")){
100107
//Serial.println(String("error with command ")+cmd);
101108
return -1;
@@ -118,15 +125,15 @@ int NINAB31Serial::parseResponse(String cmd, uint32_t timeout){
118125
}
119126

120127
bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
121-
while(Serial3.available())Serial3.read(); //flush input buffer
122-
Serial3.print(msg);
123-
Serial3.write('\r');
124-
Serial3.flush();
128+
while(SerialBLE.available())SerialBLE.read(); //flush input buffer
129+
SerialBLE.print(msg);
130+
SerialBLE.write('\r');
131+
SerialBLE.flush();
125132
String input="";
126133
auto starttime=millis();
127134
while(millis()-starttime<timeout || timeout==0){
128-
if(Serial3.available()){
129-
input+=(char)(Serial3.read());
135+
if(SerialBLE.available()){
136+
input+=(char)(SerialBLE.read());
130137
if(input.endsWith("ERROR\r")){
131138
return false;
132139
}
@@ -136,7 +143,7 @@ bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
136143
}
137144
}
138145
return false;
139-
146+
140147
}
141148

142149
bool NINAB31Serial::checkUnsolicited(){
@@ -153,8 +160,8 @@ bool NINAB31Serial::checkUnsolicited(){
153160
}
154161

155162
bool NINAB31Serial::poll(){
156-
if(Serial3.available()){
157-
m_input+=(char)(Serial3.read());
163+
if(SerialBLE.available()){
164+
m_input+=(char)(SerialBLE.read());
158165
if(m_input.endsWith("\r")){
159166
if(checkUnsolicited()){
160167
flushInput();
@@ -177,12 +184,12 @@ String NINAB31Serial::checkCharWritten(int handle){
177184
return m_input.substring(seccommapos+1,thirdcommapos);
178185
}
179186
}
180-
187+
181188
}else{
182189
//Serial.println(m_input);
183190
}
184191
return "";
185-
192+
186193
}
187194

188195
void NINAB31Serial::flushInput(){

src/phyphoxBLE_ESP32.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifdef ESP32
1+
#if defined(ESP32) && !defined(ARDUINO_SENSEBOX_MCU_ESP32S2)
22
#include "phyphoxBLE_ESP32.h"
33
#include "Arduino.h"
44
#include <stdio.h>
@@ -99,8 +99,8 @@ class MyCharCallback: public BLECharacteristicCallbacks {
9999
PhyphoxBLE* myServerPointer;
100100
void onWrite(BLECharacteristic *pCharacteristic) {
101101
PhyphoxBLE::configHandlerDebug();
102-
103-
}
102+
103+
}
104104
};
105105

106106
class MyServerCallbacks: public BLEServerCallbacks {
@@ -118,7 +118,7 @@ class MyServerCallbacks: public BLEServerCallbacks {
118118
void PhyphoxBLE::configHandlerDebug(){
119119
if(configHandler!=nullptr){
120120
(*configHandler)();
121-
}
121+
}
122122
}
123123

124124
void PhyphoxBLE::eventCharacteristicHandler(){
@@ -132,14 +132,14 @@ void PhyphoxBLE::eventCharacteristicHandler(){
132132
PhyphoxBLE::experimentTime = swap_int64(et);
133133
if(experimentEventHandler!=nullptr){
134134
(*experimentEventHandler)();
135-
}
135+
}
136136
}
137137

138138
void PhyphoxBLE::setMTU(uint16_t mtuSize) {
139139
BLEDevice::setMTU(mtuSize+3); //user mtu size + 3 for overhead
140-
140+
141141
PhyphoxBLE::MTU = mtuSize;
142-
PhyphoxBleExperiment::MTU = mtuSize;
142+
PhyphoxBleExperiment::MTU = mtuSize;
143143
}
144144

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

171171
//Graph
172-
PhyphoxBleExperiment::Graph firstGraph; //Create graph which will plot random numbers over time
173-
firstGraph.setChannel(0,1);
172+
PhyphoxBleExperiment::Graph firstGraph; //Create graph which will plot random numbers over time
173+
firstGraph.setChannel(0,1);
174174

175175
//Value
176176
PhyphoxBleExperiment::Value valueField;
177177
valueField.setChannel(1);
178178

179179
firstView.addElement(firstGraph);
180-
firstView.addElement(valueField);
180+
firstView.addElement(valueField);
181181
defaultExperiment.addView(firstView);
182-
183-
addExperiment(defaultExperiment);
182+
183+
addExperiment(defaultExperiment);
184184
}
185185

186186
BLEDevice::init(DEVICE_NAME);
@@ -192,28 +192,28 @@ void PhyphoxBLE::start(const char * DEVICE_NAME)
192192
phyphoxBleExperimentCharacteristicUUID,
193193
BLECharacteristic::PROPERTY_READ |
194194
BLECharacteristic::PROPERTY_WRITE |
195-
BLECharacteristic::PROPERTY_NOTIFY
196-
);
195+
BLECharacteristic::PROPERTY_NOTIFY
196+
);
197197
eventCharacteristic = phyphoxExperimentService->createCharacteristic(
198198
phyphoxBleEventCharacteristicUUID,
199199
BLECharacteristic::PROPERTY_WRITE
200-
);
200+
);
201201

202202
phyphoxDataService = myServer->createService(phyphoxBleDataServiceUUID);
203203

204204
dataCharacteristic = phyphoxDataService->createCharacteristic(
205205
phyphoxBleDataCharacteristicUUID,
206206
BLECharacteristic::PROPERTY_READ |
207207
BLECharacteristic::PROPERTY_WRITE |
208-
BLECharacteristic::PROPERTY_NOTIFY
208+
BLECharacteristic::PROPERTY_NOTIFY
209209

210210
);
211211

212212
configCharacteristic = phyphoxDataService->createCharacteristic(
213213
phyphoxBleConfigCharacteristicUUID,
214214
BLECharacteristic::PROPERTY_READ |
215215
BLECharacteristic::PROPERTY_WRITE |
216-
BLECharacteristic::PROPERTY_NOTIFY
216+
BLECharacteristic::PROPERTY_NOTIFY
217217
);
218218

219219
myExperimentDescriptor = new BLE2902();
@@ -226,12 +226,12 @@ void PhyphoxBLE::start(const char * DEVICE_NAME)
226226
myDataDescriptor->setCallbacks(new MyDataCallback());
227227
eventCharacteristic->setCallbacks(new MyEventCallback());
228228
configCharacteristic->setCallbacks(new MyCharCallback());
229-
229+
230230
dataCharacteristic->addDescriptor(myDataDescriptor);
231231
experimentCharacteristic->addDescriptor(myExperimentDescriptor);
232232
eventCharacteristic->addDescriptor(myEventDescriptor);
233233
configCharacteristic->addDescriptor(myConfigDescriptor);
234-
234+
235235

236236
phyphoxExperimentService->start();
237237
phyphoxDataService->start();
@@ -255,7 +255,7 @@ void PhyphoxBLE::poll(int timeout) {
255255
void PhyphoxBLE::staticStartTask(void* _this){
256256
PhyphoxBLE::when_subscription_received();
257257
delay(1);
258-
}
258+
}
259259

260260
void PhyphoxBLE::startTask()
261261
{
@@ -267,8 +267,8 @@ void PhyphoxBLE::write(float& value)
267267
{
268268
/**
269269
* \brief Write a single float into characteristic
270-
* The float is parsed to uint8_t*
271-
* because the gattServer write method
270+
* The float is parsed to uint8_t*
271+
* because the gattServer write method
272272
* expects a pointer to uint8_t
273273
* \param f1 represent a float most likeley sensor data
274274
*/
@@ -380,40 +380,40 @@ void PhyphoxBLE::when_subscription_received()
380380
experimentSizeArray[0]= (arrayLength >> 24);
381381
experimentSizeArray[1]= (arrayLength >> 16);
382382
experimentSizeArray[2]= (arrayLength >> 8);
383-
experimentSizeArray[3]= arrayLength;
383+
experimentSizeArray[3]= arrayLength;
384384

385385
uint8_t checksumArray[4] = {0};
386386
checksumArray[0]= (checksum >> 24) & 0xFF;
387-
checksumArray[1]= (checksum >> 16) & 0xFF;
387+
checksumArray[1]= (checksum >> 16) & 0xFF;
388388
checksumArray[2]= (checksum >> 8) & 0xFF;
389-
checksumArray[3]= checksum & 0xFF;
389+
checksumArray[3]= checksum & 0xFF;
390390

391391
copy(phyphox, phyphox+7, header);
392392
copy(experimentSizeArray, experimentSizeArray+ 4, header + 7);
393-
copy(checksumArray, checksumArray + 4, header +11);
393+
copy(checksumArray, checksumArray + 4, header +11);
394394
experimentCharacteristic->setValue(header,sizeof(header));
395395
experimentCharacteristic->notify();
396396

397397
for(size_t i = 0; i < exp_len/20; ++i){
398398
copy(exp+i*20, exp+i*20+20, header);
399399
experimentCharacteristic->setValue(header,sizeof(header));
400400
experimentCharacteristic->notify();
401-
delay(10);// mh does not work anymore with 1ms delay?!
401+
delay(10);// mh does not work anymore with 1ms delay?!
402402
}
403-
403+
404404
if(exp_len%20 != 0){
405405
const size_t rest = exp_len%20;
406406
uint8_t slice[rest];
407407
copy(exp + exp_len - rest, exp + exp_len, slice);
408408
experimentCharacteristic->setValue(slice,sizeof(slice));
409-
experimentCharacteristic->notify();
409+
experimentCharacteristic->notify();
410410
delay(1);
411411
}
412-
412+
413413

414414
myAdvertising->start();
415-
416-
415+
416+
417417
vTaskDelete( NULL );
418418

419419
}
@@ -423,7 +423,7 @@ void PhyphoxBLE::addExperiment(PhyphoxBleExperiment& exp)
423423
{
424424
storage[i]=0;
425425
}
426-
426+
427427
exp.getFirstBytes(EXPARRAY, deviceName);
428428
for(uint8_t i=0;i<phyphoxBleNViews; i++){
429429
for(int j=0; j<phyphoxBleNElements; j++){
@@ -450,8 +450,8 @@ void PhyphoxBLE::begin(HardwareSerial* hwPrint)
450450
#ifdef DEBUG
451451
printer = hwPrint;
452452
if(printer)
453-
printer->begin(115200);
454-
#endif
453+
printer->begin(115200);
454+
#endif
455455
}
456456

457457
void PhyphoxBLE::printXML(HardwareSerial* printer){

0 commit comments

Comments
 (0)