MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    MMM-OralB / Bluetooth equipped toothbrush integration

    Scheduled Pinned Locked Moved Development
    68 Posts 13 Posters 29.3k Views 20 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D Offline
      djerik
      last edited by

      Works great with my Oral B 9000!

      Notes:

      • I had to change the git clone to https instead of git clone git@github.com:timodejong95/MMM-BluetoothDevices.git
      • Copying the file MMM.conf requires administrative rights
      • I used bluetoothctl to find the MAC of the toothbrush
      T 1 Reply Last reply Reply Quote 0
      • T Offline
        timodejong95 @djerik
        last edited by

        @djerik Nice! Good to hear, and thanks for the feedback I will update the readme where needed!

        lavolp3L D 2 Replies Last reply Reply Quote 0
        • lavolp3L Offline
          lavolp3 Module Developer @timodejong95
          last edited by lavolp3

          @timodejong95 Hi Timo, it works for me now as well. Very good!
          So now let’s go for the holy grail! The battery status.
          It is there and I managed to get it out using noble. Don’t ask me how. Lol.
          Find the code below where I meddled with Advertisements and Characteristics:

          noble.on('discover', function(peripheral) {
            var ad = peripheral.advertisement || "";
            if (ad.localName == "Oral-B Toothbrush") {
              //console.log('Found device with local name: ' + ad.localName);
              //console.log('advertising the following service uuid\'s: ' + ad.serviceUuids);
              //console.log("ID: "+peripheral.id);
              //console.log("Advertisement: "+ad);
              if (ad.manufacturerData) {
                  console.log('Found OralB Toothbrush with ID: ' + peripheral.id);
                  console.log('ManufacturerData: '+ad.manufacturerData.toString('hex'));
                  //noble.stopScanning();
                  peripheral.connect(function(error) {
                    if (error) {
                      console.log("Error connecting to peripheral: " +error);
                    } else {
                      console.log('Connected to peripheral: ' + peripheral.uuid);
                      peripheral.discoverServices([], function(error, services) {
                        console.log("Discovering services...");
                        if (error) {
                            console.log("ERROR while discovering peripherals: " + error);
                        } else {
                            console.log('discovered the following services:');
                            for (var i in services) {
                                //console.log('  ' + i + ' uuid: ' + services[i].uuid);
                            }
                            discoverChars(services[3]);
                            /*setTimeout(() => {
                              noble.startScanning([], true);
                            }, 1000);*/
          
                        }
                        //peripheral.disconnect();
                      });
                    }
                  });
          
                  peripheral.on('disconnect', function() {
                    process.exit(0);
                    console.log("Peripheral disconnected. Scanning again!");
                    noble.startScanning();
                  });
              }
            }
          });
          
          
          function discoverChars(service) {
            service.discoverCharacteristics(null, function(error, characteristics) {
              //console.log("Characteristics: "+characteristics);
              for (let i in characteristics) {
                var charUUID = characteristics[i].uuid;
                console.log('  ' + i + ' uuid: ' + charUUID);
                if (characteristics[i].uuid == "a0f0ff0550474d5382084f72616c2d42") {
                  let j = i;
                  characteristics[j].on('data', function(data, isNotification) {
                      console.log("Data: "+data);
                      var valueInt = data.readInt8(0);
                      console.log("Battery: "+valueInt+"%");
                  });
                  /*characteristics[j].read(function(error, data) {
                      if (data) {
                        var valueInt = data.readInt8(0);
                        console.log("Battery: "+valueInt+"%");
                      }
                  });*/
                  characteristics[j].subscribe(function(error) {
                      if (error !== null) { console.log("error", error); }
                  });
          
                }
              }
            });
          }
          

          I guess you can find the same using bluez?

          If I find the time, I’ll also try out which way works better (for me).
          Noble or your blues dbus way. I had several issues using noble but it had its charme (like the battery status :-) and only sending data when I activate or deactivate the brush)

          How to troubleshoot modules
          MMM-soccer v2, MMM-AVStock

          T 1 Reply Last reply Reply Quote 0
          • D Offline
            dfuerst @timodejong95
            last edited by

            @timodejong95 nice that there was someone realizing this project at the end.

            tried your app and getting a blank WHITE SCREEN.
            so maybe u can help me troubleshooting

            1 cloned via https
            2 did npm install
            3 sudo cp MMM.conf
            4 added conf including MAC (sidenote, white screen with wrong MAC address aswell)
            5 paired the toothbrush via bluetooth

            tried to start mirror -> white screen, and whoops message in the log

            any idea what could cause this? or where to start troubleshooting?

            1 Reply Last reply Reply Quote 0
            • T Offline
              timodejong95 @lavolp3
              last edited by timodejong95

              @lavolp3 Thanks for the code share and great to hear it works. This weekend I will spend some to see if I can fix it, I let you know.

              @dfuerst Hmm oké, thats weird normally you would see a black screen. Doest the mirror work if you disable the plugin, if so what does the logs say?

              FYI: I had some issues with my git commits so I deleted and recreated the repo, no worries it’s still under the same url and won’t go away.

              lavolp3L 1 Reply Last reply Reply Quote 0
              • lavolp3L Offline
                lavolp3 Module Developer @timodejong95
                last edited by

                @timodejong95 Hi Timo,

                since I really want to have the battery status I am currently trying to use the front end code with noble as backend in node_helper. Your backend code and all the GATT bluez stuff is much too complicated for me.

                Also, I have done some tweaks on the frontend:

                • Hide timer after some time
                • convert time to m:ss
                • also count on beyond 2 minutes (circle is filled after 2 Minutes)
                • make timer bright and circle blue when the brush is running and dim it back again if it is not running.

                Like it very much. Very simple tweaks in the main.js. Let me know if you want to see any of it.

                Also, two brushes work perfectly! :ok_hand:

                Impressive work man!

                How to troubleshoot modules
                MMM-soccer v2, MMM-AVStock

                1 Reply Last reply Reply Quote 0
                • T Offline
                  timodejong95
                  last edited by

                  Thanks I really appreciate that! Yeah sure I am willing to refactor it a bit, if needed, and make it a configurable option.
                  Again sorry for deleting the repo, I pushed wit the wrong git user (work one). I saw that you starred/forked it.

                  Can you show me the changes or make a PR?

                  The GATT is a bit bugged I am not sure why but the services are not resolving. I am trying to fix that and I think that after that the battery status should be that hard anymore.

                  lavolp3L 1 Reply Last reply Reply Quote 0
                  • T Offline
                    timodejong95
                    last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • lavolp3L Offline
                      lavolp3 Module Developer @timodejong95
                      last edited by lavolp3

                      I can’t PR to your new repo, so here’s the code from MMM-BluetoothDevices.js:

                      /*jshint esversion: 6 */
                      //'use strict';
                      
                      Module.register('MMM-BluetoothDevices', {
                        // Default module config.
                        defaults: {
                          name: 'raspberrypi',
                          mode: 'le',
                          hci: 'hci0',
                          interfaceName: 'org.bluez.Adapter1',
                          services: [
                            { type: 'CurrentTimeService' },
                          ],
                          devices: [],
                          layout: {
                            title: {
                              position: 'bottom',
                              key: 'name',
                            },
                            data: {
                              position: 'bottom',
                              fields: [
                                { key: 'mode', text: 'mode' },
                              ],
                            },
                          },
                          hideAfter: 10 * 60,
                        },
                      
                        getStyles() {
                          return ['MMM-BluetoothDevices.css'];
                        },
                      
                        // Override dom generator.
                        getDom() {
                          const wrapper = document.createElement('div');
                          wrapper.classList.add('toothbrushes');
                      
                          if (this.loading) {
                            wrapper.innerHTML = 'Loading...';
                            wrapper.className = 'light small';
                      
                            return wrapper;
                          }
                      
                          const table = document.createElement('table');
                          const row = document.createElement('tr');
                      
                          for (const deviceKey in this.devices) {
                            const deviceType = this.devices[deviceKey].device.type;
                      
                            if (deviceType === 'OralBToothbrush') {
                              row.appendChild(this.renderToothbrush(deviceKey));
                            } else {
                              throw new Error(`Unknown device type: ${deviceType}`);
                            }
                          }
                      
                          table.appendChild(row);
                          wrapper.appendChild(table);
                      
                          return wrapper;
                        },
                      
                        renderToothbrush(deviceKey) {
                          const device = this.devices[deviceKey];
                          const deviceTd = document.createElement('td');
                          deviceTd.classList.add('toothbrush');
                          deviceTd.style.textAlign = "center";
                      
                          const deviceCircle = document.createElement('div');
                          deviceCircle.classList.add('toothbrush-circle-container');
                          deviceCircle.classList.add(`toothbrush-circle-${device.data.state}`);
                      
                          const deviceCircleText = document.createElement('div');
                          deviceCircleText.classList.add('toothbrush-circle-text');
                      
                          const deviceCircleSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
                          deviceCircleSvg.classList.add('toothbrush-circle-ring');
                      
                          const deviceCircleSvgCircle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
                          deviceCircleSvgCircle.classList.add('toothbrush-circle');
                          deviceCircleSvgCircle.setAttribute('stroke-width', 15);
                          deviceCircleSvgCircle.setAttribute('fill', 'transparent');
                          deviceCircleSvgCircle.setAttribute('r', 52);
                          deviceCircleSvgCircle.setAttribute('cx', 60);
                          deviceCircleSvgCircle.setAttribute('cy', 60);
                      
                          deviceCircleSvg.append(deviceCircleSvgCircle);
                          deviceCircle.append(deviceCircleSvg);
                          deviceCircle.append(deviceCircleText);
                      
                          //const time = device.data.time > 120 ? 120 : device.data.time;
                          const time = device.data.time;
                      
                          // initial start
                          this.updateCircle(deviceCircleSvgCircle, deviceCircleText, time);
                      
                          if (device.data.state === 'running') {
                            deviceTd.style.display = 'block';
                            deviceCircleText.classList.add('bright');
                            deviceCircleSvgCircle.setAttribute('stroke', '#0080fe');
                            this.counters[deviceKey] = {
                              time,
                              interval: setInterval(() => {
                      
                                this.counters[deviceKey].time += 1;
                      
                                this.updateCircle(
                                  deviceCircleSvgCircle,
                                  deviceCircleText,
                                  this.counters[deviceKey].time
                                );
                              }, 1000),
                            };
                          } else {
                            deviceCircleText.classList.remove("bright");
                            deviceCircleSvgCircle.setAttribute('stroke', '#aaa');
                            setInterval(() => {
                              deviceTd.style.display = "none";
                            }, this.config.hideAfter * 1000);
                          }
                      
                          const deviceLabel = document.createElement('div');
                          deviceLabel.classList.add('title');
                          deviceLabel.classList.add('small');
                          deviceLabel.innerText = device.device[this.config.layout.title.key];
                      
                          const dataContainer = document.createElement('div');
                          dataContainer.classList.add('small');
                          dataContainer.classList.add('light');
                          for (const key in this.config.layout.data.fields) {
                            const data = this.config.layout.data.fields[key];
                            const field = document.createElement('div');
                            field.innerText = `${data.key}: ${device.data[data.key]}`;
                            dataContainer.appendChild(field);
                          }
                      
                          if (this.config.layout.title.position === 'top') {
                            deviceTd.appendChild(deviceLabel);
                          }
                          if (this.config.layout.data.position === 'top') {
                            deviceTd.appendChild(dataContainer);
                          }
                      
                          deviceTd.appendChild(deviceCircle);
                      
                          if (this.config.layout.title.position === 'bottom') {
                            deviceTd.appendChild(deviceLabel);
                          }
                          if (this.config.layout.data.position === 'bottom') {
                            deviceTd.appendChild(dataContainer);
                          }
                      
                          return deviceTd;
                        },
                      
                        updateCircle(deviceCircleSvgCircle, deviceCircleText, time) {
                          var secs = time % 60;
                          deviceCircleText.innerText = Math.floor(time / 60) + ":" + (secs < 10 ? "0" + secs : secs);
                      
                          const radius = parseInt(deviceCircleSvgCircle.getAttribute('r'));
                          const circumference = radius * 2 * Math.PI;
                          if (time > 120) { time = 120; }
                          const percent = (100 / 120) * time;
                          const offset = circumference - percent / 100 * circumference;
                          deviceCircleSvgCircle.style.strokeDasharray = `${circumference} ${circumference}`;
                          deviceCircleSvgCircle.style.strokeDashoffset = offset;
                        },
                      
                        start() {
                          Log.info(`Starting module: ${this.name}`);
                      
                          this.devices = {};
                          this.counters = {};
                          this.loading = true;
                      
                          this.sendSocketNotification('FETCH_TOOTHBRUSHES', this.config);
                        },
                      
                        socketNotificationReceived(notification, payload) {
                          if (notification === 'FETCH_TOOTHBRUSHES_RESULTS') {
                            Log.info('MMM-Toothbrush: Got toothbrush results');
                            this.devices = payload;
                      
                            for (const counterKey in this.counters) {
                              const counter = this.counters[counterKey];
                              clearInterval(counter.interval);
                            }
                      
                            if (this.loading) {
                              this.loading = false;
                              this.updateDom(1000);
                            } else {
                              this.updateDom(0);
                            }
                          }
                        },
                      });
                      

                      it’s also in the develop branch of my module

                      How to troubleshoot modules
                      MMM-soccer v2, MMM-AVStock

                      1 Reply Last reply Reply Quote 1
                      • T Offline
                        timodejong95
                        last edited by timodejong95

                        @lavolp3 Thanks, I will have a look at that this weekend.

                        Also managed to connect to the services and characteristics. But how did you know the one you needed?

                        Because you only connecting to the 3th key in the services array and a hardcoded uuid for the characteristics. I am curious how you find the one you needed.

                        lavolp3L 1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 3
                        • 4
                        • 5
                        • 6
                        • 7
                        • 4 / 7
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Sam, technical setup by Karsten.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy