I want to send measurement-data from a micrometer (Insize Metal Sheet) to a microcontroller (Bluefruit nRF52 Feather) by using the protocol i got from Insize (see PDF-attachment). I don't know if I misunderstand the protocol, the Aduino-functions or the connections with to the aux. Can anyone see why I don't read any proper frames from the micrometer?
#include <bluefruit.h>
BLEDis bledis;
BLEHidAdafruit blehid;
bool hasKeyPressed = false;
// Variables for controlling buttons
byte interruptPinSend = 11;
byte interruptPinUndo = 27;
byte interruptPinRedo = 7;
volatile bool isTriggeredSend = false;
volatile bool isTriggeredUndo = false;
volatile bool isTriggeredRedo = false;
// Variables for serial UART-communication
uint8_t sequenceNumber = 0;
uint8_t data[] = {0x00, 0x00, 0x00, 0x00};
uint8_t frame[] = {0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0};
// Declearing functions
void triggerSendISR();
void triggerUndoISR();
void triggerRedoISR();
uint8_t getCheckSum(char *string, uint8_t frameHeader, uint8_t command);
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluetooth HID");
Serial.println("--------------------------------\n");
Serial.println();
Serial.println("Go to your Bluetooth settings to pair your device");
Serial.println("then open an application that accepts keyboard input");
Serial.println();
Serial.println("Enter the character(s) to send:");
Serial.println();
/* Attach interrupt for trigger button
Note: When user trigger button, report is sent to micrometer
to ask for measured value.
*/
pinMode(interruptPinSend, INPUT_PULLUP);
pinMode(interruptPinUndo, INPUT_PULLUP);
pinMode(interruptPinRedo, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPinSend), triggerSendISR, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPinUndo), triggerUndoISR, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPinRedo), triggerRedoISR, FALLING);
Bluefruit.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("XR Inspector");
// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather 52");
bledis.begin();
/* Start BLE HID
Note: Apple requires BLE device must have min connection interval >= 20m
( The smaller the connection interval the faster we could send data).
However for HID and MIDI device, Apple could accept min connection interval
up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max
connection interval to 11.25 ms and 15 ms respectively for best performance.
*/
blehid.begin();
/* Set connection interval (min, max) to your perferred value.
Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms
min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms
*/
/* Bluefruit.Periph.setConnInterval(9, 12); */
// Set up and start advertising
startAdv();
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);
// Include BLE HID service
Bluefruit.Advertising.addService(blehid);
// There is enough room for the dev name in the advertising packet
Bluefruit.Advertising.addName();
/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)
For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop()
{
//Read frame from micrometer
int8_t byteCount = 0;
while (Serial.available())
{
frame[byteCount] = (uint8_t) Serial.read();
++byteCount;
hasKeyPressed = true;
if(byteCount == sizeof(frame)) // Reached the end of frame
{
byteCount = 0;
}
}
if (hasKeyPressed && (frame[1] == 0x42))
{
data[0] = frame[2];
data[1] = frame[3];
data[2] = frame[4];
data[3] = frame[5];
blehid.keyPress(data[0]);
blehid.keyRelease();
delay(5);
blehid.keyPress(data[1]);
blehid.keyRelease();
delay(5);
blehid.keyPress(data[2]);
blehid.keyRelease();
delay(5);
blehid.keyPress(data[3]);
blehid.keyRelease();
delay(5);
}
// Sending frame to micrometer
if (isTriggeredSend)
{
isTriggeredSend = false;
char charData[] = {data[0], data[1], data[2], data[3]};
//Sending query to micrometer
Serial.print(0xA0); //Frame header
Serial.print(0x45); //Command: Query
Serial.print(0x00); //Data
Serial.print(0x00);
Serial.print(0x00);
Serial.print(0x00);
Serial.print(0x01); //Channel
Serial.print(0xFF); //Equipment Sequence Number
Serial.print(0xFF);
Serial.print(0xFF);
Serial.print(0xFF);
Serial.print(0x00); //Reserved
Serial.print(sequenceNumber); //Sequence Number
Serial.print(getCheckSum(0x00000000, 0xA0, 0x42)); //Sum Check: Frame Header + Command + Data XOR
Serial.print(0xB0); //Frame Tail
++sequenceNumber;
}
}
uint8_t getCheckSum(char *string, uint8_t frameHeader, uint8_t command) //https://forum.arduino.cc/index.php?topic=86496.0 - Rob Tillart
{
int XOR = 0;
for (int i = 0; i < strlen(string); i++)
{
XOR = XOR ^ string[i];
}
return XOR + frameHeader + command;
}
void triggerSendISR()
{
/*
Send report to micrometer.
*/
isTriggeredSend = true;
}
void triggerUndoISR()
{
/*
Send report to micrometer.
*/
isTriggeredUndo = true;
}
void triggerRedoISR()
{
/*
Send report to micrometer.
*/
isTriggeredRedo = true;
}
7315-2 Multiplex Interface Wireless Communication Protocol.pdf (100 KB)
