How To: IBM Watson API with Renesas IoT Sandbox Data Monitoring


Project Overview

In this article, I will show you a very fast way to use Python and cloud-based APIs from the Renesas IoT Sandbox and the IBM Watson service to present a concept to your boss to get approval to build a prototype or pilot deployment of a highly scalable IoT project.

For simplicity and speed of development, I’ll show you how to build a mobile application entirely in Python, which is very popular for data analysis. Python is also the language used to build Renesas IoT Sandbox Data Intelligence workflows. Let’s polish up those Python skills for the new IoT world swimming with data that we live in. :slight_smile:

Although Python is not as popular as JavaScript and Cordova for mobile app prototyping, I chose Python for this project because it is commonly used for IoT data analytics on the server side and fits in nicely with the overall skillset for the Renesas IoT Sandbox ecosystem. If you want to use JavaScript on the front-end, there’s a gazillion tutorials out there. If you aren’t already experienced with JavaScript and are coming to IoT from the device side, Python may be a faster route for you.

My previous article, Using Speech in Your IoT Projects, showed the use of bash and Python scripts from the command line to connect to the Renesas IoT Sandbox Data Monitoring API and then send the data to the IBM Watson API.

This article focuses on GUI creation with Python. A future article will show how you can finish the mobile application and replace the API access scripts using curl with Python using the Requests module.

Although I’m using a non-standard Python graphics framework called Pygame that is primarily used for game creation on the desktop, it is fast and easy to use and suitable for single-screen applications. By spending less time on your GUI, you can spend more time on the core functionality of your IoT project.

The underlying cloud APIs from Renesas and IBM can scale your dream project to millions of devices, billions of rows of data, and provide advanced analytics and AI. Let’s get started unleashing your imagination.




  • Python 2.7.x
  • Pygame 1.9.3
    • Install with pip on most OSs, including Windows 10

Code Structure uses two small modules, gui and event. I’ve put my GUI code in a file called lib/ My event handling code is in lib/

To use the modules, put a blank file called in the lib directory.

Main Starting Point For Your Code

We’re going to run our application on the desktop first before modifying the application to run on an Android phone. Don’t worry, the modifications are minor and you’ll be able to use all of the code you create for the desktop application.

Save the following code snippet into a new folder called watsoniot. Call the file

import pygame
from lib import gui
from lib import event

def main():

    screen = pygame.display.set_mode((720, 1280))

    handler = event.Handler()

    while handler.appOn:
        screen.blit(gui.screen, (0, 0))

if __name__ == "__main__":


To use the example GUI code, simply create a file called inside of the lib directory. The lib directory will hold both and

I built the GUI using to create a surface that I call screen. Using from lib import gui in, I can access screen from with gui.screen.


In, I am using fonts that I downloaded from the Internet. If you search for free fonts on the Internet, you’ll see a wide catalog of cool fonts. Two sites that I use are:


Pygame uses RGB (Red, Green, Blue) color codes. You can get the codes using a tool like this:


If you don’t want to build your own icons, you can grab the icons from my GitHub repository in the img folder. Note that the actual code in my GitHub repository may be different than the code in this tutorial as I am still extending the functionality.

Contents of lib/

import pygame


screen = pygame.Surface((720, 1280))

LIGHT_YELLOW = (255, 250, 202)
titleFont = pygame.font.Font("fnt/iron.ttf", 100)
iconFont = pygame.font.Font("fnt/prime.ttf", 48)
line1 = "Renesas IoT Sandbox"
titleSurface = titleFont.render(line1, True, LIGHT_YELLOW)
titleRect = titleSurface.get_rect(y=20, centerx=360)
line2 = "IBM Watson API"
titleSurface2 = titleFont.render(line2, True, LIGHT_YELLOW)
titleRect2 = titleSurface2.get_rect(y=120, centerx=360)

micIcon = pygame.image.load("img/microphone.png")
micRect = micIcon.get_rect(x=300, y=700)
tempIcon = pygame.image.load("img/temp.png")

watsonIcon = pygame.image.load("img/watson_icon.png")
watsonRect = watsonIcon.get_rect(x=500, y=300)

getIoT = pygame.image.load("img/getiot.png")
getIoTRect = getIoT.get_rect(x=50, y=300)

convertData = pygame.image.load("img/convert.png")
convertDataRect = convertData.get_rect(x=280, y=300)

screen.blit(titleSurface, titleRect)
screen.blit(titleSurface2, titleRect2)

screen.blit(micIcon, micRect)

screen.blit(getIoT, getIoTRect)
screen.blit(convertData, convertDataRect)
screen.blit(watsonIcon, watsonRect)

Event Handler

In the code in, I am using an infinite while loop to handle screen updates and process events. Pygame creates a list of events using pygame.event.get(). An event could be a mouse button press, closing your application, or moving your mouse.

For the first stage, we’re just going to create the buttons and then print a debug message to the console to test that each button functions. In a future article, we’ll convert the bash scripts from part 1 into Python modules and use the buttons to grab the IoT data, process it, then push the processed data up to the Watson API.

For now, we’ll just create a skeleton event handler.

Inside of the lib folder in your watsoniot project folder, create a new file called Copy the contents of the code snippet below into the new file.


import pygame
import gui

class Handler():
    def __init__(self):
        self.appOn = True

    def process(self, events):

        for event in events:
            if event.type == pygame.QUIT:
                self.appOn = False
            mouse_pos = pygame.mouse.get_pos()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if gui.getIoTRect.collidepoint(mouse_pos):
                    print("Received IoT JSON data")
                if gui.convertDataRect.collidepoint(mouse_pos):
                    print("Converted data to human text")
                if gui.watsonRect.collidepoint(mouse_pos):
                    print("Sent data to Watson")
                if gui.micRect.collidepoint(mouse_pos):
                    print("play sound")

Test It

$ python

You should see a window open that contains a title and four icons. Press each icon to test the event handler. At this stage, you will not hear sound.

Congratulations, you’ve just completed a GUI app framework that can be used as the basis for your IoT project demos. Your first million IoT devices are just a few steps away. :slight_smile:

Preview of Upcoming Article

To keep this project short and manageable, I focused only on the GUI design. However, if you’re eager to finish your application, you can try out the Python requests module with the IBM Watson API as well as move your application to an Android mobile phone.

Using Python Requests with the IBM Watson API

In the next article, I’ll attach the Python scripts to the buttons. However, you can test this script below to see how easy it is to use the Watson API with Python. If you don’t have requests installed, you can install it with:

$ pip install requests

This is an example showing Watson API access from Python. Save the snippet below to

import requests

url = ''

headers = {'accept': 'audio/wav'}

r = requests.get(
    params={"text": "Internet of Things and Watson API is Awesome"},

with open('sound.wav', 'wb') as fd:
    for chunk in r.iter_content(1024):

Example of the code above in use with sample audio.

Using Python to Get IoT Data from Renesas IoT Sandbox Data Monitoring

Renesas IoT Sandbox Data Monitoring uses the concept of Dweets for small IoT devices that send out information through the cloud. Dweepy is a Python client to talk to Dweets. Although Renesas IoT Sandbox Data Monitoring is based on, it uses a different base URL. I’ve modified dweepy by paddcarey to use the Renesas IoT Sandbox Data Monitoring base URL. Get modified dweepy on GitHub.

Clone dweepy and then install it.

$ git clone
$ cd dweepy
$ python install

With my S5D9 IoT Fast Prototyping Kit plugged into Ethernet and power, I can access the sensor data with this Python script.

Access the Renesas IoT Sandbox Data Monitoring API with Python.
You must install the modified dweepy below or modify the base
URL of the original dweepy to point to
import dweepy
import pprint

response = dweepy.get_latest_dweet_for('S5D9-63de')

The response.

$ python
[{u'content': {u'globals': {u'application': u'DweetS5D9Client',
                            u'dweet-count': 1065},
               u'sensors': {u'AccelX': 0.02,
                            u'AccelY': 0.31,
                            u'AccelZ': 0.97,
                            u'Humidity': 38,
                            u'LED': 0,
                            u'MagX': -77,
                            u'MagY': 34,
                            u'MagZ': -179,
                            u'Pressure': 1016,
                            u'SoundLevel': 2,
                            u'TemperatureC': 29,
                            u'TemperatureF': 84}},
  u'created': u'2017-10-18T20:18:00.162Z',
  u'thing': u'S5D9-63de'}]

Preview of Mobile Packaging

If you want to get your desktop Python application on a mobile phone right now, you can check out my daughter’s example template game using rapt-pygame. Following the instructions, rapt-pygame will also install the Android SDK, Java, and Gradle. I’ll cover these steps in detail in a future article.

Next Step: Completing Your IoT Mobile App

Requirements for Finishing Your Application

HTTP Libraries

  • Python Requests - for HTTP access to APIs

IoT Hardware and Software


To transform your creative ideas for an IoT project into reality, you first need to build a prototype of your idea. This process often starts with a proof of concept that is used to get other people excited about your project. Using cloud-based APIs like the IBM Watson API and the Renesas IoT Sandbox API is a great way to quickly show that your idea works. Once your idea gets support, you can use the same cloud APIs to scale your project to millions of IoT devices. Although many people build their prototype with JavaScript, Java, or Swift, as this article shows, Python is also a viable option to test your ideas.

Additional Info

IBM Watson Python SDK (not used in this basic tutorial to keep dependencies simple)