You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BLEUnsignedCharCharacteristic alertLevel("2A06", // standard 16-bit characteristic UUID
411
+
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
412
+
413
+
unsigned long microseconds;
414
+
long previousMillis = 0; // last time the vibration level was checked, in ms
415
+
```
416
+
417
+
The Bluetooth® Low Energy services and characteristics are standardized for the specific use of this application. The service is defined as `Immediate alert (1802)` which makes it ideal for notifying on anomalous vibration events and an `alert level characteristic (2A06)`. Notice that they have specific and standardized Bluetooth® Low Energy UUIDs.
418
+
419
+
In the `setup()` function the different board peripherals are initiated including:
420
+
421
+
- Serial communication
422
+
- Nicla system for power management and LED control
423
+
- Onboard IMU sensor
424
+
- Bluetooth® Low Energy (BLE) communication
425
+
426
+
In the Bluetooth® Low Energy initialization we define the device name, `Nicla` in this case for the Opta to easy find it.
Serial.begin(115200); // initialize serial communication
434
+
while (!Serial)
435
+
;
436
+
437
+
// run this code once when Nicla Sense ME board turns on
438
+
nicla::begin(); // initialize library
439
+
nicla::leds.begin(); // initialize LEDs support
440
+
441
+
nicla::setBatteryNTCEnabled(false); // Set to false if your battery doesn't have an NTC thermistor.
442
+
nicla::enableCharging(100); // enable the battery charger and define the charging current in mA
443
+
444
+
nicla::leds.setColor(red);
445
+
446
+
BHY2.begin();
447
+
448
+
accel.begin();
449
+
450
+
// begin initialization
451
+
if (!BLE.begin()) {
452
+
Serial.println("starting BLE failed!");
453
+
454
+
while (1)
455
+
;
456
+
}
457
+
458
+
/* Set a local name for the Bluetooth® Low Energy device
459
+
This name will appear in advertising packets
460
+
and can be used by remote devices to identify this Bluetooth® Low Energy device
461
+
*/
462
+
BLE.setLocalName("Nicla");
463
+
BLE.setAdvertisedService(alertService); // add the service UUID
464
+
alertService.addCharacteristic(alertLevel); // add the battery level characteristic
465
+
BLE.addService(alertService); // add the alert service
466
+
alertLevel.writeValue(0); // set initial value for this characteristic
467
+
468
+
/* Start advertising Bluetooth® Low Energy. It will start continuously transmitting Bluetooth® Low Energy
469
+
advertising packets and will be visible to remote Bluetooth® Low Energy central devices
470
+
until it receives a new connection */
471
+
472
+
// start advertising
473
+
BLE.advertise();
474
+
475
+
Serial.println("Bluetooth® device active, waiting for connections...");
476
+
}
477
+
```
478
+
479
+
In the `loop()` function we wait for the Bluetooth® connection and if it is connected will indicate it with a blue LED turned on. Every 1000 ms the vibration status will be measured calling the `updateVibrationStatus()` function.
480
+
481
+
```arduino
482
+
void loop() {
483
+
// wait for a Bluetooth® Low Energy central
484
+
BLEDevice central = BLE.central();
485
+
486
+
// if a central is connected to the peripheral:
487
+
if (central) {
488
+
Serial.print("Connected to central: ");
489
+
// print the central's BT address:
490
+
Serial.println(central.address());
491
+
492
+
// check the vibration level every 1000ms
493
+
// while the central is connected:
494
+
while (central.connected()) {
495
+
nicla::leds.setColor(blue);
496
+
long currentMillis = millis();
497
+
// if 1000ms have passed, check the vibration:
498
+
if (currentMillis - previousMillis >= 1000) {
499
+
previousMillis = currentMillis;
500
+
updateVibrationStatus();
501
+
}
502
+
}
503
+
nicla::leds.setColor(red);
504
+
Serial.print("Disconnected from central: ");
505
+
Serial.println(central.address());
506
+
}
507
+
}
508
+
```
509
+
510
+
The `updateVibrationStatus()` function averages the samples taken and if the vibration magnitude is above the defined limit it will notify the Opta sending a Bluetooth® message.
511
+
512
+
```arduino
513
+
void updateVibrationStatus() {
514
+
/* Read the power management IC registers to retrieve the battery percentage
515
+
*/
516
+
microseconds = micros();
517
+
for (int i = 0; i <= samples; i++) {
518
+
519
+
vibration += abs(accel.x());
520
+
521
+
while (micros() - microseconds < sampling_period_us) {
522
+
//empty loop
523
+
BHY2.update();
524
+
}
525
+
microseconds += sampling_period_us;
526
+
}
527
+
528
+
vibration = vibration / samples;
529
+
Serial.println(vibration);
530
+
531
+
if (vibration >= VIBRATION_LIMIT) {
532
+
alertLevel.writeValue(0);
533
+
} else {
534
+
alertLevel.writeValue(1);
535
+
}
536
+
}
537
+
```
538
+
539
+
***You can download the complete code from [here]().***
379
540
380
541
### Arduino Cloud Dashboard
381
542
543
+
Taking advantage of the Arduino Cloud, we can seamlessly integrate a simple but powerful dashboard to monitor and visualize the status of the system in real-time:
544
+
545
+
![Arduino Cloud Dashboard]()
546
+
547
+
Within the Arduino Cloud's dashboard, the system variables can be monitored with the following widgets:
382
548
549
+
- ON/OFF compressor power switch widget.
550
+
- System variables gauges showing temperature, pressure and power.
551
+
- Line charts for historical review on variables behaviour.
552
+
- Vibration and fault state alert widgets for easy understand of the compressor state.
553
+
554
+
We can easily access this dashboard from a PC, mobile phone or tablet from anywhere, receiving an instantaneous update wherever we are.
555
+
556
+
In addition, we can set different integrations to complement our project, for example, forward the dashboard data to an external service using **Webhook**, **IFTTT** automations and **Smart Home** integrations.
383
557
384
558
## Full Compressor Monitoring System Example
385
559
560
+
All the necessary files to replicate this application note can be found below:
561
+
562
+
- The complete code can be downloaded [here]()
563
+
386
564
## Conclusion
387
565
388
-
### Next Steps
566
+
In this application note we have learned how to implement an industrial assets monitoring system turning a conventional air compressor into smart. This application could be a simple demonstration of how Arduino's environment simplifies de workflow for developing smart solutions to solve real industrial needs. The Arduino PRO products line is a perfect fit for developing robust and reliable projects for the industry. We covered in-site sensor data sampling, Bluetooth® Low Energy communication, and real-time Cloud monitoring.
567
+
568
+
### Next Steps
569
+
570
+
As you already know how to develop a smart air compressor monitoring system with the Opta PLC and the Nicla Sense ME, it's time for you to continue exploring all the capabilities of the Arduino Pro environment to integrate it with your professional setup and improve it with powerful solutions.
571
+
572
+
You can take this solution even further with the integration of an **FFT** algorithm for the Nicla Sense ME so it can analyze vibration specific spectrums.
0 commit comments