SK-S7G2 Smart Inhaler – Quick Start Guide

LEVEL 2: INTERMEDIATE  |

This tutorial uses the Renesas SK-S7G2 Starter Kit with the Renesas IoT Sandbox to build a Smart Inhaler application. This tutorial will show you how to build workflows starting from a SK-S7G2 Heartbeat template project.

In this tutorial, you will:

  • Read temperature and humidity via Sensirion Humidity & Temperature sensor (SHT31) and display the detected values on the board’s LCD.
  • Visualize recorded data in the Dashboard of Renesas IoT Sandbox.
  • Set temperature & humidity thresholds via the Dashboard’s Input Widgets.
  • Sound alarm and display notifications on the board’s LCD once temperature & humidity have exceeded their thresholds.
  • Read differential pressure value from Sensirion Differential Pressure sensor (SDP31.)
  • Set inhaler usage counter to simulate potential inhaler overuse by going over its predefined (user specified) value.
  • Receive SMS and email notifications once the simulated medication overdose is reached.

Here’s a diagram of 6 workflows we will create in this tutorial:

Note: In this tutorial, you will use the template of SK-S7G2 Heartbeat tutorial, which already includes two workflows – “Last Connected” workflow, and “Processed Heartbeat” workflow. Since these workflows are not required for this tutorial, you can either deactivate them, delete them, or simply leave them as is.

Note: To learn how to deactivate a workflow, go to Appendix A.

 

HERE’S WHAT YOU NEED TO GET STARTED:

  1. Renesas Synergy™ Starter Kit SK-S7G2 Rev 3.1 with Future Sensor Shield
  2. Longsys GT202 Wi-Fi PMOD board with R/A adapter
  3. Micro USB cable to power up the board
  4. WiFi Internet Access
  5. A set of earbuds
  6. The red plastic tube provided in the kit (will be connected to one of two holes of the SDP31 sensor) to simulate inhaler
  7. srec image file (Download here)
  8. Android or iOS smart device
  9. iOS or Android phone mobile app (Download here)

Estimated Time to Complete: 1- 2 hours

Prerequisites:

 

STEP 1: PROGRAM SK-S7 BOARD WITH SMART INHALER IMAGE

Program the Smart Inhaler image to the SK-S7 board following the instructions in the “How to flash the SK-S7G2 board” tutorial. Be sure to download the latest release .srec file that is specific to this kit. This can be found in the HERE’S WHAT YOU NEED TO GET STARTED section.

 

STEP 2: ACTIVATE SK-S7 TEMPLATE PROJECT IN RENESAS IOT SANDBOX

Click here to activate the kit.  Once activation is completed, you will receive an email with your account credentials needed to provision the kit and access the cloud portal.

This email will include your:

  • Renesas IoT Sandbox web login
  • API Key
  • Device username and password
  • Mobile application credentials

You will connect your board to this project in a later step.

 

STEP 3: CONNECT THE SK-S7 KIT TO THE CLOUD

Note: The SK-S7 kit only works with 2.4GHz WiFi.

First, let’s attach all the required hardware, and then we will provision the board.

  • If Future Sensor Shield is not attached, connect it to Renesas Synergy™ Starter Kit SK-S7G2 Rev3.1 as shown below. The Future Sensor shield contains:
    • Sensirion Humidity & Temperature sensor (SHT31)
    • Sensirion Differential Pressure sensor (SDP31)
  • Connect Wi-Fi PMOD module to PMODA interface (J12)
  • Connect the ear buds to Audio output jack (J16)

Provision the Board for WiFi and Renesas IoT Sandbox API:

  • Power up the board by connecting the micro USB cable into DEBUG_USB (J19) on the board, and then either power with the external battery provided in the Kit or by connecting to the USB port of a laptop.  Touch the screen within 5 seconds of powering up the board to boot it up in provisioning mode and the board will become a WiFi Access Point.

  • When you see the screen below, connect to the board from a computer via the WiFi network printed on the screen. This is a Wifi Access Point served by the board and is a unique SSID. Note: Keep a copy of your activation email readily available.  Once connected to the board’s Wifi Access Point, you will not have internet access on your computer or mobile device.

  • Once connected, open the provisioning page served by the board by going to  http://192.168.3.1 on your browser and manually enter the Renesas IoT Sandbox API credentials emailed to you when you activated the kit in Step 2.

  • After entering the information, click Connect.  This will automatically exit out of provision mode and you will see a “Provisioning complete” screen.
  • Log into your Renesas IoT Sandbox Account with the information provided in one of the emails at https://app-renesas-na-sandbox.mediumone.com/login.
  • Next, you will need to power down the board by removing the micro USB cable.

 

STEP 4: SWITCH TO PROJECT ON RENESAS IOT SANDBOX & OBSERVE DATA

Login to Renesas IoT Sandbox with your web credentials. Select your newly created project in the top right drop down.

 

In order to switch between projects, click on “Your Project Name” in the top right.  Note, if you have other browser tabs open pointing to other projects, those sessions will be invalidated.

If you haven’t done so, power up the board.

In the Dashboard of Renesas IoT Sandbox, select the Real Time Events Log and observe the temperature, humidity, and pressure being sampled by sensors; this data is logged in JSON format as shown below.

 

Alternatively, you can also observe the sensor data in Real Time Gauge as shown below.

 

STEP 5: CREATE USER DEFINED TAGS

In this step, you will create, activate and use all tags necessary for the purpose of this tutorial.

Specifically, you will create the following six user defined tags: temp_threshold, humid_threshold, update_rate, max_usage, phone_number, and email_address.

Once the tags are created, they can be used by the user to set desired boundaries for temperature threshold, humidity threshold etc.

For clarity of keeping all user defined variables in one place, we will create a new Data Stream called “user_input.” In Renesas IoT Sandbox, go to Config -> Data Streams

Click on ‘Create New Stream.’

In General Information, type “user_input” for Name, then click ‘Save Data Stream.’

You just created a new Data Stream called ‘user_input.’

Now, you are ready to generate new tags.

In Renesas IoT Sandbox, go to Resources -> API Explorer.

Under Login to API, login with your credentials: username, password, and API key as shown below; then click ‘Send.’

Note: the credentials were set & emailed to you during Step 2, the activation of your kit.

Note: Response code: 200 (true) indicates that you can use this user account to login to MQTT API for the project. If you didn’t get a correct response code, re-verify your user credentials and try again.

 

Now, you will create new user_input. Under Create Events, select:

stream_name:             user_input
login_id:                       device
event_data:                 {“temp_threshold”:1,
“humid_threshold”:1,
“update_rate”:1,
“max_usage”:1,
“email_address”:””,
“phone_number”:””}

Click Send’; if you didn’t observe successful response ‘200’, please correct errors and try again.

For further details on how to generate events, please visit Medium One Documentation Resources.

You just created six tags called “temp_threshold”, “humid_threshold”, “update_rate”, “max_usage”, “email_address”, and “phone_number” in user_input data stream.

Next step is to activate these tags.

 

In Renesas IoT Sandbox, go to Config -> Data Streams; select user_input Events by clicking Edit.

Activate all tags by clicking on as shown below; then click on ‘Save Data Stream.’

Note: In this tutorial, we created labels for each corresponding tag. Defining the labels is not necessary from functional point of view; it simply allows the user to better define the variables.

You just activated all necessary tags for this tutorial.

Now, you will create widgets to specify the user defined variables.

 

In Renesas IoT Sandbox, go to Dashboard; then click on Single User User Input Widget; first  select “device” in Select User, click on Tag Config.

In Tag Config, click check-mark next to all six tags as shown below, then click ‘Save.’

Your Dashboard’s User Input should look similar to this picture.

The newly created buttons now can be used to set user specific values for each of the variables.

In the next steps, you will create useful workflows that monitor measured data and send notifications.

 

STEP 6: SET TEMPERATURE & HUMIDITY THRESHOLDS

In this step, you will create two workflows. The first workflow sends the updated temperature threshold from the Dashboard’s User Input widget to the board. The second workflow sends the updated humidity threshold from Dashboard’s User Input widget to board.

Create new workflow and name it  “Set Temperature Threshold” by clicking on Workflow Studio and ‘Create.’

Drag the “temp_threshold” tag from the user_input stream into the workflow.

Double click on “temp_threshold” tag and confirm that it’s Trigger is checked; then click ‘Save.’

Similarly, drag a Base Python module and connect its “in1” input with the “temp_threshold” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import MQTT

temp = IONode.get_input('in1')['event_data']['value']
message = 'T' + str(temp)
MQTT.publish_event_to_client('sk-s7g2', message)

Then click the ‘Save and Activate’ button to finish setting up the workflow.

 

Similarly, create the second workflow called “Set Humidity Threshold” by clicking on Workflow Studio and ‘Create.’

Drag the “humid_threshold” tag from the user_input stream into the workflow.

Double click on “humid_threshold” tag and confirm that it’s Trigger is checked; then click ‘Save.’

Similarly, drag a Base Python module and connect its “in1” input with the “humid_threshold” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import MQTT

humidity = IONode.get_input('in1')['event_data']['value']
message = 'H' + str(humidity)
MQTT.publish_event_to_client('sk-s7g2', message)

Then click the ‘Save and Activate’ button to finish setting up the workflow.

Your overall workflow should look as shown below.

 

CHECKPOINT

Note: If you haven’t yet done so, connect the ear buds to the audio output at this point. You will utilize it here.

On your board’s LCD, observe the current value of Temperature and Humidity. In this specific example, Temp is 23.2 C, and Humidity is 49.0%.

In Dashboard’s User Input, set the ‘Temp_Threshold’ to 23 C, then click ‘Send Event.’.Additionally, set the ‘Humid_Threshold’ to 67, then click ‘Send Event.’

As you may have guessed, in this case, you have exceeded the threshold temperature. Your temperature threshold is set to 23 C, while the current temperature (measured by sensor) is 23.2 C;  thus, a notification will occur on your board’s LCD.

There are three types of warnings you can observe;

  • “Temperature exceeds threshold!”
  • “Humidity exceeds threshold!”
  • “Temperature & Humidity exceed threshold!”

To observe these notifications, you can do one of the following:

  • Adjust the temperature and humidity thresholds to values that will exceed the current measurements.
  • Temporarily increase temperature & humidity values by directly breathing on the Sensirion sensor on the Future Sensor Shield.

Besides of the visual warnings, a warning sound can be heard from the ear buds. The warning notification as well as the alarm sound will disappear once temperature and humidity come back to normal (below the safety thresholds).

 

STEP 7: SET MAXIMUM USAGE

Maximum Usage (per minute) is used in monitoring the smart inhaler. In this step, you will create a workflow that sets the maximum usage value and sends it from the dashboard to the board.

Create a new workflow and name it  “Set Maximum Usage” by clicking on Workflow Studio and ‘Create.’

Drag the “max_usage” tag from the user_input stream into the workflow.

Double click on “max_usage” tag and confirm that it’s Trigger is checked ; then click ‘Save.’

Similarly, drag a Base Python module and connect its “in1” input to the “max_usage” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import MQTT

max_usage = IONode.get_input('in1')['event_data']['value']
message = 'M' + str(max_usage)
MQTT.publish_event_to_client('sk-s7g2', message)

Then click the ‘Save and Activate’ button to finish setting up the workflow.

Your overall workflow should look as shown below

 

You have successfully created a workflow to update max_usage value from the dashboard to board. In a later step, you will use this value to simulate an inhaler overuse.

 

CHECKPOINT

In the Dashboard’s User Input widget, set the user_input “max_usage” tag labeled as Max Usage per Minute to ‘1’; then click on ‘Send Event.’

On the board, locate SDP31 sensor. Connect the plastic tube to one of the two sensor holes, then inhale through the tube. The pressure value will change when you inhale/exhale into the tube.

The value can be positive or negative depending on which hole is used and whether you inhale or exhale. Once the pressure’s absolute value increases over 250 and then decreases to under 10, the usage counter will increase by 1.

Try to inhale through the tube a few times, so the usage counter value is higher than the maximum usage value; you will use this scenario to simulate a “Medication overuse” in a later step.

Note: once the usage counter value is higher than the maximum usage value, a new raw tag called “overuse” is created. This tag will be used later in this tutorial.

 

STEP 8: SET UPDATE RATE

In this section, you will create a workflow that allows you to reduce the events sampling rate. By default, the events are sent every 1 second. As you may find out, you can run out of your free allowed credits; thus, this workflow allows you to lower the rate of sampling the data from the board.

Create a new workflow and name it  “Set Update Rate” by clicking on Workflow Studio and ‘Create.’

Drag the “update_rate” tag from the user_input stream into the workflow.

Double click on “update_rate” tag and confirm that it’s Trigger is checked; then click ‘Save.’

Similarly, drag a Base Python module and connect its “in1” input to the “update_rate” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import MQTT

update_rate = IONode.get_input('in1')['event_data']['value']
message = 'R' + str(update_rate)
MQTT.publish_event_to_client('sk-s7g2', message)

Then click the ‘Save and Activate’ button to finish setting up the workflow.

Your overall workflow should look as shown below.

 

CHECKPOINT

To quickly check the above workflow, you will simply reduce the events rate updated to the cloud.

First, in Real Time Event Log, observe the rate of 1 event per every 1 second, as shown below.

In your Dashboard’s User Input widget, change the value of “update_rate” to ‘10’; thus, reducing the rate to 1 event per every 10 seconds.

Another look in Real Time Event Log confirms the reduced rate.

 

STEP 9: SYNCHRONIZE USER DATA

In this step, you will create a workflow to synchronize the user data: maximum usage, and temperature & humidity thresholds, when the board is connected to Renesas IoT Sandbox.

The trigger in this workflow is “connected.”

Create a new workflow and name it  “Sync User Data” by clicking on Workflow Studio and ‘Create.’

Drag the “connected” tag from the raw stream into the workflow.

Double click on “connected” tag and confirm that it’s Trigger is checked; then click ‘Save.’

 

Similarly, drag a Base Python module and connect its “in1” input to the “connected” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import Analytics
import MQTT

# Sync temperature threshold
temp_threshold = Analytics.last_n_values('user_input.temp_threshold', 1)[0]['user_input.temp_threshold']
message = 'T' + str(temp_threshold)
MQTT.publish_event_to_client('sk-s7g2', message)

# Sync humidity threshold
humid_threshold = Analytics.last_n_values('user_input.humid_threshold', 1)[0]['user_input.humid_threshold']
message = 'H' + str(humid_threshold)
MQTT.publish_event_to_client('sk-s7g2', message)

# Sync maximum usage
max_usage = Analytics.last_n_values('user_input.max_usage', 1)[0]['user_input.max_usage']
message = 'M' + str(max_usage)
MQTT.publish_event_to_client('sk-s7g2', message)

Then click the ‘Save and Activate’ button to finish setting up the workflow.

Your overall workflow should look as shown below.

You just created a workflow that synchronizes the data when the board connects to the cloud.

 

STEP 10: SMART INHALER DEMO & OVERUSE NOTIFICATION

In this step, you will create a workflow that sends a SMS and email notification to the user when the usage counter exceeds the maximum usage. In this workflow, a raw tag called “overuse” is used; the tag was generated in CHECKPOINT of Step 7, but it has not been activated yet.

To activate the tag, go to Config -> Data Streams, then select raw Events by clicking Edit.

Locate “overuse” tag and activate it by checking the box.

Save the activated tags by clicking on ‘Save Data Stream.’

Create a new workflow and name it  “Overuse Notification” by clicking on Workflow Studio and ‘Create.’

Drag the “overuse” tag from the raw stream into the workflow.

Double click on “overuse” tag and confirm that it’s Trigger is checked ; then click ‘Save.’

Similarly, drag a Base Python module and connect its “in1” input to the “connected” tag.

Double click on Base Python, then delete and replace the Base Python script with the following code:

import Email
import Analytics
import FreeSMS

usage = Analytics.last_n_values('raw.usage', 1)[0]['raw.usage']
max_usage = Analytics.last_n_values('user_input.max_usage', 1)[0]['user_input.max_usage']

message = 'You exceed the recommended dosage! (Used/Max: {0}/{1} times per minute)'.format(usage, max_usage);
log(message)

user_email = Analytics.last_n_values('user_input.email_address', 1)[0]['user_input.email_address']
log(user_email)
try:
    email = Email.Email('support@medium.one', 'Renesas IoT Sandbox', user_email, 'Smart Inhaler - Overuse Notification')
    email.html_message(message)
    email.send()
except Exception:
    log('Unable to send Email')

user_phone = Analytics.last_n_values('user_input.phone_number', 1)[0]['user_input.phone_number']
log(user_phone)
try:
    if user_phone != "":
        FreeSMS.sendSMS(user_phone, message)
except Exception:
    log("Unable to send SMS")

Then click the ‘Save and Activate’ button to finish setting up the workflow.

Your overall workflow should look as shown below.

You just created a workflow that sends the email and phone notification when the usage counter exceeds the maximum usage rate.

 

CHECKPOINT

Finally, you are ready to simulate the smart inhaler.

Set the email address, phone number, and max usage per minute in the Dashboard’s User Input widget as shown below.

Now, either inhale or exhale into the tube in SDP31 sensor. The pressure value will change when you inhale or exhale. When the pressure’s absolute value increases to over 250, and then decreases to under 10, the usage counter increases by 1.

Inhale or exhale through the tube, until your board’s display Usage shows a number larger then the Max Usage Per Minute value, which was set in the Dashboard’s User Input. In the picture below, the usage counter reached the value 5 per min, where max usage per minute was set to 3.

Once this scenario occurs, you will see the warning “Medication overuse” appear on the bottom portion of the board’s display. Then, a notification is sent to the user’s email and phone number.

 

The following is SMS notification is sent to the user:

The following is an email notification sent to the user:

 

STEP 11: VISUALIZING DATA IN YOUR MOBILE APP

In this step, you will add pressure, humidity, temperature, and other desired tags into your mobile app.

If you have not done so, create a new profile on your mobile phone as shown below. Use the mobile credentials you received in the activation email from Step 2.

Note: For additional info on how to download or create a mobile account, follow the Mobile Quick Start Guide listed in Prerequisites or see Appendix B.

We will add humidity, temperature, and pressure via the Gauge widget.

Click on Add Widget, then select Gauge:

Click on the Gauge icon on the left side to configure the widget.

For “humidity”, configure the Gauge widget as following:
Stream:  raw
Tag:        humidity
Then click ‘Done’.

Similarly, configure the temperature and pressure by adding two additional gauges, as following:

For “temperature”, configure Gauge widget as following:
Stream:   raw
Tag:         temperature
Then click ‘Done.’

For “pressure”, configure Gauge widget as following:
Stream:   raw
Tag:         pressure
Then click ‘Done.’

Your mobile app profile should look similar to this:

Additionally, we can add a slider widget to monitor humidity & temp thresholds, update rate and max usage.

To do so, click on Add Widget, then select Slider.

In Slider widget, click on the Slider icon on the left side to configure the widget;

For “update_rate”, configure the Slider widget as following:
Stream:   user_input
Tag:         update_rate
Then click ‘Done.’

Similarly, add the following tags by adding 3 additional slider widgets:
For “max_usage”, configure Slider widget as following:
Stream:                       user_input
Tag:                             max_usage
Then click ‘Done.’

For “temp_threshold”, configure Slider widget as following:
Stream:                       user_input
Tag:                             temp_threshold
Then click ‘Done.’

For “humid_threshold”, configure Slider widget as following:
Stream:                       user_input
Tag:                             humid_threshold
Then click ‘Done.’

Finally, your mobile should look similar to this:

Now you can easily monitor the temperature, humidity, and pressure. Additionally, you can set temperature & humidity thresholds, control the update rate, as well as set the boundary for inhaler overuse, all via a few clicks in the mobile app.

 

Congratulations! This concludes the SKS7 Smart Inhaler tutorial for Renesas Synergy SK-S7G2 Starter Kit, rev.3.1. You have used a Future Sensor Shield on SK-S7 to create a Smart Inhaler demo. You visualized data in Renesas IoT Sandbox environment, you set phone and email warning notifications, as well as, set threshold boundaries for measured data.

The rest is to your imagination!

 

WHAT’S NEXT?

Try another SK-S7G2 Starter Kit quick start guide:

  • LED Toggle K (Level: Beginner)
  • Heartbeat Kit (Level: Beginner)
  • Connected Weather (Level: Intermediate)
  • Ultrasonic Presence Sensor Kit (Level: Intermediate)
  • Connected Weather Kit with AMS Sensor (Level: Advanced)

 

APPENDIX A: How to deactivate/delete a workflow

In Renesas IoT Sandbox, go to Workflow Studio; then click on the workflow you would like to deactivate or delete.

Option # 1: Deactivate a workflow

Click on Revisions, then click the checkbox ; this will deactivate the workflow.

 

Option # 2: Delete a workflow

Alternatively, you can delete a workflow by clicking ‘Delete’ next to the workflow name at the top of the page. You will be asked to confirm the changes; click ‘Ok’ and the workflow will get deleted.

 

APPENDIX B: Connect to Mobile

You can find links to the mobile app on renesas-docs.mediumone.com/?mobile or search for “IoT Controller” on the Apple or Google app store.

Make sure you enable push notifications when asked. If you have the app installed previously and you disabled push notifications, you will need to enable them in your phone Settings. Go to your phone Settings.  Scroll down until you find the “IOT Control” app, select the app and select “Notifications”. From the notifications screen, make sure that the “Allow Notifications” switch is enabled.

              

Create a new profile in the app for this S3A7 Sensirion Sensors project and input the API Settings from the welcome email you received, just like you did for the Smart Chef tutorial.  You can use whatever profile name you like.  Be sure to use the Mobile App credentials provided in the activation email for this project.

Note: These instructions are for the iOS App but Android is similar.

Open the app, click + to add a new profile.

Then click on API Login and specify a profile name, API keys, User, and Password.

Be sure to click on “Renesas” to select the right environment.  Click ‘Done’.  Click ‘Save’.

If you logged in successfully, you should see a similar landing page.

For now, you can close the app; you will come back to this app later once you created necessary tags and workflows to monitor temperature and humidity in Renesas IoT Sandbox.