danshradersantoshkanthety
Published © GPL3+

Streaming Data to Power BI from a Spin Bike

Stream your spin bike RPM data to Power BI for real time reporting and dashboarding.

BeginnerShowcase (no instructions)2,045
Streaming Data to Power BI from a Spin Bike

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
SparkFun Reed Switch
×1
SparkFun Arduino Project Enclosure
×1

Software apps and online services

Microsoft Windows 7
Microsoft Power BI

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Wiring

Hook up From:
https://learn.sparkfun.com/tutorials/reed-switch-hookup-guide

Code

Arduino Code

Arduino
This reads the reed switch data and posts it to the serial port
//Modified code from https://www.sparkfun.com/products/8642
const int REED_PIN = 2; // Pin connected to reed switch
void setup() 
{
  Serial.begin(9600);
  // Since the other end of the reed switch is connected to ground, we need
  // to pull-up the reed switch pin internally.
  pinMode(REED_PIN, INPUT_PULLUP);
}

void loop() 
{
  int proximity = digitalRead(REED_PIN); // Read the state of the switch
  if (proximity == LOW) // If the pin reads low, the switch is closed.
  {
    //Serial.println("Trigger");
    Serial.write(1);
  }
  else
  {
   //Serial.println("Opened");
   Serial.write(0);
  }
}

node.js code

JavaScript
This allows the the arduino's serial data to be read by the windows machine. From there a PowerShell script is called to post the data to Power BI. See code comments for details
var rpm = 0;
var secondsBetweenPosts = 1;
var state = 0;

var postRPM = function() {
    rpm = rpm *15; //Second in the minute 15 instead of 60 seems to be the trick
    console.log("Current RPM reading is: " + rpm);
    if(rpm > 0){
        var spawn = require("child_process").spawn,child;
        child = spawn("powershell.exe", ["-file","powerBI.ps1","-DATA",rpm]);
        child.stdin.end();
    }
    rpm = 0;
};

// How often to post to your cloud
setInterval(postRPM, secondsBetweenPosts * 1000);

var randomRPM = function() {
    rpm = rpm + Math.floor(Math.random() * 3) + 1;
};

// Autopilot for when testing is needed
// setInterval(randomRPM, 500);

// call every time that we need to add to the RPM count
var writeRPM = function() {
    rpm++;
};

// Lis the avaible ports
var SerialPort = require('serialport');
SerialPort.list(function(err, ports) {
    console.log('');
    console.log('The following ports are avaiable on this machine');
    console.log('------------------------------------------------');
    ports.forEach(function(port) {
        console.log(port.comName);
        console.log(port.pnpId);
        console.log(port.manufacturer);
    });
});

// The arduino is on COM6 for this computer your's may be different
var port = new SerialPort('COM6');

// open errors will be emitted as an error event 
port.on('error', function(err) {
    console.log('Error: ', err.message);
});

port.on('open', function() {
    console.log('Serial Port Opend - waiting for data...');
    port.on('data', function(data) {
        var valueTxt = data[0];
        if (valueTxt !== state) {
            if (valueTxt === 1) {
                writeRPM();
            }
            state = valueTxt; // reset state for new data
        }
    });
});

PowerShell code

Powershell
This post the data to a Power BI streaming data set. You could probably do this in node.js, but this was quicker for our use case
param(
 [int]$DATA = ""
)
# Where should this data go?
$endpoint = "https://api.powerbi.com/beta/yourLinkHere"

# Initialise the Payload
$payload = @{}
$payload.Time = Get-Date -format s
$payload.Number = $DATA

# Send the event
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))

Credits

danshrader

danshrader

0 projects • 1 follower
santoshkanthety

santoshkanthety

0 projects • 0 followers
Thanks to Spark Fun, serialport, and Power BI.

Comments