Sell wearable-devices-producing data on the Streamr marketplace

With the development of Internet of Things (IoT), various wearable devices are produced to measure personal heart rate, steps climbed, and other useful fitness data¹. However, the data is in the control of the manufacturer and sold in the offline market without our permission². Therefore, Streamr aims to create a global decentralized data exchange platform to help you own yourself data.

Their explainer video is here:

In this tutorial, you will learn how to access your Fitbit fitness data, and sell it on the Streamr marketplace. The Fitbit is an American company headquartered in San Francisco, California. Its products are activity trackers, wireless-enabled wearable technology devices.

Step 1: Register an app on http://dev.fitbit.com

First you need to register an account on dev.fitbit.com, and then click MANAGE YOUR APPS on the top right area. Next you need to click Register a new app button at the top right area to start with.

After you click the red Register button, you will be able to see the credentials for the app you just registered. The OAuth 2.0 Client ID and Client Secret will be used in the next step.

Step 2: Use the OAuth 2.0 tutorial page

Next you need to right click the OAuth 2.0 tutorial page link and open it in a new tab, so that you can look back at your app’s credentials easily. Make sure the Implicit Grant Flow is chosen instead of Authorization Code Flow— this will make things much easier! After you copy/paste the Client ID and Client Secret into the blanks and put in the Redirect URI, click the auto-generated link.

Then you will see the confirmation page. Just click Allow.

And you will be led to the ‘Redirect URI’, which is ‘http://www.streamr.com/’ in this case. But the address bar now shows a very long string which is the token for your app.

Next you need to copy and paste everything in the address bar but without the starting part ( https://www.streamr.com/ ) to the Parse response section, and hit enter key once. This way you can clearly see what the token is, what the scope is, and how long the token is valid.

Step 3: Make request and get the data!

After you are done with the Parse response, the next step is ready for you. You need to create a Streamr stream at first. Then please copy the following code to your host, replace the $YOUR_LOGFILE_PATH, $YOUR_STREAM_ID, $YOUR_API_KEY, $YOUR_CLIENT_BASH64, $YOUR_ACCESS_TOKEN, $YOUR_REFERSH_TOKEN with yours, and keep running the script for live data to appear in a stream. Note that, this particular script has been coded in python 2.7. If you use python 3.x or other script languages, you will need to re-code it first.

# -*- coding: utf-8 -*-
import requests
import json
import logging as LOG
import hashlib
import time
import threading as thd
LOG.basicConfig(level=LOG.DEBUG,
format=’%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s’,
datefmt=’%a, %d %b %Y %H:%M:%S’,
filename=’YOUR_LOGFILE_PATH’,
filemode=’a’)
# global variable
# Get the following infomation from the Streamr
STREAM_ID = ‘$YOUR_STREAM_ID’
API_KEY = ‘$YOUR_API_KEY’
# Get the following information from the Google Fitness Platform
CLIENT_BASH64 = ‘$YOUR_CLIENT_BASH64’
ACCESS_TOKEN = ‘$YOUR_ACCESS_TOKEN’
REFRESH_TOKEN = ‘$YOUR_REFERSH_TOKEN’
def get_user_token():
“”” Request Fitbit Access token using the refresh token
NOTE: Update every 45min
“””
global ACCESS_TOKEN
global REFRESH_TOKEN
global CLIENT_BASH64

thd.Timer(2700,get_user_token).start()
url = ‘https://api.fitbit.com/oauth2/token’
body = “grant_type=refresh_token&refresh_token=%s”%(REFRESH_TOKEN)
paras = {‘Authorization’: “Basic %s”%(CLIENT_BASH64), ‘Content-Type’: “application/x-www-form-urlencoded”}
try:
req = requests.post(url, headers=paras, data=body)
ACCESS_TOKEN = req.json()[‘access_token’]
REFRESH_TOKEN = req.json()[‘refresh_token’]
LOG.info(“Get user token successfully: Access_Token: %s Refresh_Token: %s”, ACCESS_TOKEN, REFRESH_TOKEN)
return ACCESS_TOKEN
except:
import traceback
LOG.error(“Unable to get user token”)
traceback.print_exc()
return None
def get_user_devices(access_token):
“”” The Get Device endpoint returns a list of the Fitbit devices connected to a user’s account.
“””
url = ‘https://api.fitbit.com/1/user/-/devices.json'
paras = {‘Authorization’: “Bearer %s”%(access_token)}
try:
req = requests.get(url, headers=paras)
devices_info = json.dumps(req.json())
LOG.info(“Get user devices info successfully: %s”, devices_info)
return devices_info
except:
import traceback
LOG.error(“Unable to get devices info”)
traceback.print_exc()
return None
def get_user_activities(access_token):
“”” The Get Daily Activity Summary endpoint retrieves a summary and list of a user’s activities and activity log entries for a given day in the format requested using units in the unit system which corresponds to the Accept-Language header provided.
“””
# Current date time in the format “yyyy-MM-dd”.
date = time.strftime(“%F”)
url = ‘https://api.fitbit.com/1/user/-/activities/date/%s.json' % date
paras = {“Authorization”:”Bearer %s” % access_token}
try:
req = requests.get(url, headers=paras)
activities_info = json.dumps(req.json())
LOG.info(“Get user activities informantion successfully: %s”, activities_info)
return activities_info
except:
import traceback
LOG.error(“Unable to get user activities information.”)
traceback.print_exc()
return None
def get_user_heartrate(access_token):
“”” The Get Heart Rate Time Series endpoint returns time series data in the specified range for a given resource in the format requested using units in the unit systems that corresponds to the Accept-Language header provided.
“””
url = ‘https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json'
paras = {“Authorization”:”Bearer %s” % access_token}
try:
req = requests.get(url, headers=paras)
heartrate_info = json.dumps(req.json())
LOG.info(“Get user heartrate information successfully: %s”, heartrate_info)
return heartrate_info
except:
import traceback
LOG.error(“Unable to get user heartrate information.”)
traceback.print_exc()
return None
def upload_to_streamr():
“”” Upload the Fitbit Data to Streamr
NOTE: Upload per minute
“””
global ACCESS_TOKEN
global STREAM_ID
global API_KEY
url = ‘https://www.streamr.com/api/v1/streams/'+STREAM_ID+'/data'
paras = {“Authorization”:”token %s” % API_KEY}
result_devices = get_user_devices(ACCESS_TOKEN)
if not result_devices:
result_devices = “the Data not update”
result_activities = get_user_activities(ACCESS_TOKEN)
if not result_activities:
result_activities = “the Data not update”
result_heartrate = get_user_heartrate(ACCESS_TOKEN)
if not result_heartrate:
result_heartrate = “the Data not update”
body = (‘{“User Devices”: %s, “Daily Activities”: %s, “Daily HeartRate”: %s}’%(result_devices,result_activities,result_heartrate))
LOG.info(“The Fitbit data is: %s”,body)
thd.Timer(500, upload_to_streamr).start()
try:
req = requests.post(url, headers=paras, data=body)
if req.status_code == 200 or req.status_code == 201:
LOG.info(“Upload the Fitbit data to Streamr successfully.”)
return req.status_code
else:
LOG.info(“Fail to upload the Fitbit data to Streamr. ERROR: %s”, req.status_code)
except:
import traceback
LOG.error(“Fail to Upload the Fitbit data to Streamr.”)
traceback.print_exc()
return None
if __name__==”__main__”: 
get_user_token()
upload_to_streamr()

Unless an error has occurred, you should be able to see your Fitbit fitness data in the stream!

Step 4: Publish the stream to the Streamr Marketplace

To sell your Fitbit Fitness Data on the Streamr Marketplace, you can follow the tutorial called “Creating a Product for the Streamr Data Marketplace”. It is important to keep in mind not to send any personal identifiable data to the Marketplace in order to protect users’ privacy right.