HowTo: Create an IoT Community World Map with location, air quality, temperature, humidity

s3a7

#1

This tutorial uses the Renesas S3A7 or S7G2 board with the Renesas IoT Sandbox to send your location, air quality, temperature, and humidity data to our IoT Community World Map.

In this IoT Map Guide you will build workflows that:

  • Process location, air quality, temperature, and humidity data from the board
  • Send data to the IoT Community World Map.

Here’s what you need to get started:

  • Renesas S3A7 or S7G2 board
  • WiFi Internet Access for S3A7 or Ethernet Access for S7G2

Estimated Time to Complete: 70 minutes

Prerequisites:


STEP 1: SWITCH TO LOCATION DETECTION PROJECT ON RENESAS IOT SANDBOX

Login to Renesas IoT Sandbox with your web credentials. Select your project from the Location Detection Guide 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.

STEP 2: GET AIR QUALITY

In this step, you will set up the workflow required to get the air quality data from the board. This step shows you everything you need to know in order to add elements to the workflow. If you already know how to do this from a different tutorial, you can skip to the last picture to see which elements are required. Since you have already completed the Location Detection Guide, this will be the only step that goes in depth when creating a workflow.

Click on Workflow Studio on the left side of the Renesas IoT Sandbox and click the “Create +” button.

First, name the workflow “Get Air Quality” by typing this in the textbox at the top left of the workflow. Then click anywhere on the empty canvas to save it.


Next, select the “Tags & Triggers” icon from the right toolbar.

From the Tags & Triggers menu, select “sensor_read” and click and drag “air_quality” from the menu onto the empty workflow space. Then select “raw” and click and drag “connected” onto the workflow.
For more information on how to do this, refer back to the Location Detection Guide.

Now select “Scheduler” from the Tags & Triggers menu and click and drag “Custom” onto the workflow.

Double click on your “Custom” trigger in the workflow. Select the checkbox next to “All Users” and set the minutely interval to 2. This will trigger the workflow every two minutes.

Then click “Save”.
Now select the “Modules” icon, select “Foundation”, and click and drag “Base Python” into the workflow.

Double click on your “Base Python” module and click on “Inputs/Outputs”. Then press the “Add Input” button twice so that you have in1, in2, and in3 listed below.

Then click “Save”.
Drag the “in1” circle on the Base Python module to the circle on the bottom of the air_quality tag, drag the “in2” circle to the circle on the bottom of the connected tag, and drag the “in3” circle to the circle on the bottom of the Custom trigger. This will give the Python code access to the information in these tags.

Double click on the Base Python module. Delete the code that is there already and replace it with the following code:

def get_data():
    write_data = [0x5A]
    send_mqtt_msg(u'3;2;7;{};air_quality;'.format(len(write_data)) + u''.join(map(unichr, write_data)));

def send_mqtt_msg(msg):
    mqtt_msg = msg
    if not isinstance(msg, basestring):
        mqtt_msg = ''.join(map(unichr, msg))
    MQTT.publish_event_to_client('s3a7', mqtt_msg, 'latin1')
    
get_data()

Then click the “Save and Activate” button to finish setting up the workflow. This will save the workflow and immediately activate it for the board to use, whereas “Save” will not activate the workflow.

CHECKPOINT

Now it is time to test your workflow. Boot up your board in provisioning mode. Connect your board to the project through the configuration page.

Note: Refer to the Smart Chef Quick Start Guide (S3A7) or Weather Demo (S7G2) for more information on how to configure the board.

Click on Dashboard on the left side of the Renesas IoT Sandbox and reset your board. In the Real Time Events Log widget, you should see an event containing air quality data within 2 minutes.

STEP THREE: PROCESS AIR QUALITY

In this step, you will set up a workflow to process the air quality information and format it into JSON.
Go back to the main screen of the Workflow Studio by selecting it on the left side of the Renesas IoT Sandbox and click the “Create +” button.
Name the workflow “Process Air Quality” and add the raw “air_quality” tag (Tags & Triggers -> raw -> air_quality) and a Base Python module.
Then select the “Outputs” icon from the right, select “Output” from the menu and click and drag “Processed Stream - Single” into the workflow.


Connect the workflow elements as shown in the image below:

Double click on the Base Python module and replace its code with the following code:

buf = IONode.get_input('in1')['event_data']['value']
status_val = buf[2]
status = "FATAL" if buf[3] else "OK" if status_val == 0x00 else "RUNNING" if status_val == 0x10 else "BUSY" if status_val == 0x01 else "ERROR" if status_val == 0x80 else "FATAL"
if status == "OK": #otherwise, we do not want the air quality info because the system is calibrating 
    IONode.set_output('out1', {
            "air_quality": (buf[0] << 8) | buf[1],
        })

CHECKPOINT

Click on Dashboard on the left side of the Renesas IoT Sandbox and reset your board. In the Real Time Events Log widget, you should see an event containing a processed air quality value after approximately 5 minutes.

STEP FOUR: GET TEMPERATURE AND HUMIDITY

In this step, you will set up a workflow to receive the temperature and humidity information from the board.
Set up the following workflow:

  • The workflow name is in the top left corner.
  • Each of the green elements are “Tags & Triggers”. You can find each tag by looking at its name; for example, “sensor_read:humidity” will be the “humidity” tag in_ _the “sensor_read” section of “Tags & Triggers”.
  • The “Custom” trigger is found under the “Scheduler” section of “Tags & Triggers”.
  • If you do not remember how to add inputs to the Base Python module, refer to Step 2.

Set the Custom trigger minutely interval to 2 and select All Users, as you did in Step 2.
Replace the Base Python code with the following:

import MQTT

ENS210_ADDR = 0x43
mqtt_buffer = u''

def write_register(reg, data):
    global mqtt_buffer
    mqtt_buffer += u'3;2;0;3;unused;' + u''.join(map(unichr, [ENS210_ADDR, reg, data]))

def read_register(reg, num, tag_name):
    global mqtt_buffer
    mqtt_buffer += u'3;2;{};2;{};'.format(num, tag_name) + u''.join(map(unichr, [ENS210_ADDR, reg]))

write_register(0x21, 0x03)
MQTT.publish_event_to_client('s3a7', mqtt_buffer, 'latin1')
mqtt_buffer = u''
write_register(0x22, 0x03)
MQTT.publish_event_to_client('s3a7', mqtt_buffer, 'latin1')
mqtt_buffer = u''
read_register(0x30, 6, 'temp_and_humidity')
MQTT.publish_event_to_client('s3a7', mqtt_buffer, 'latin1')

Then click “Save and Activate”.

CHECKPOINT

Click on Dashboard on the left side of the Renesas IoT Sandbox and reset your board. In the Real Time Events Log widget, you should see an event containing temperature and humidity data within 2 minutes.

STEP FIVE: PROCESSED TEMPERATURE AND HUMIDITY

In this step, you will set up a workflow to process the temperature and humidity information and format it into JSON.
Set up the following workflow:


Remember that “Processed Stream - Single” is found under “Outputs”.

Replace the Base Python code with the following:

CRC7WIDTH = 7
CRC7POLY = 0x89
CRC7IVEC = 0x7F
DATA7WIDTH = 17
DATA7MASK = ((1<<DATA7WIDTH)-1)
DATA7MSB = (1<<(DATA7WIDTH-1))


# The crc7(val) function returns the CRC-7 of a 17 bits value val.
# Compute the CRC-7 of 'val' (should only have 17 bits)
def crc7(val):
    # Setup polynomial
    pol = CRC7POLY
    # Align polynomial with data
    pol = pol << (DATA7WIDTH-CRC7WIDTH-1)
    # Loop variable (indicates which bit to test, start with highest)
    bit = DATA7MSB;
    # Make room for CRC value
    val = val << CRC7WIDTH
    bit = bit << CRC7WIDTH
    pol = pol << CRC7WIDTH
    # Insert initial vector
    val |= CRC7IVEC
    # Apply division until all bits done
    while( bit & (DATA7MASK<<CRC7WIDTH) ):
        if( bit & val ):
            val ^= pol
        bit >>= 1
        pol >>= 1
    return val


handt = IONode.get_input('in1')['event_data']['value']
out = {}
t_val = handt[0] | (handt[1] << 8) | (handt[2] << 16)
h_val = handt[3] | (handt[4] << 8) | (handt[5] << 16)
t_data = t_val & 0xffff;
t_valid = (t_val >> 16) & 0x1;
t_crc = (t_val >> 17) & 0x7f;
h_data = h_val & 0xffff;
h_valid = (h_val >> 16) & 0x1;
h_crc = (h_val >> 17) & 0x7f;
print "t_data: {}, t_valid: {}, t_crc: {}, h_data: {}, h_valid: {}, h_crc: {}".format(t_data, t_valid, t_crc, h_data, h_valid, h_crc)
if t_valid:
    t_payl = t_val & 0x1ffff;
    calc_t_crc = crc7(t_payl)
    if calc_t_crc == t_crc:
        out['temperature'] = round(float(t_data) / 64 - 273.15,2)
    else:
        log("Invalid temperature CRC, expected: {}, actual: {}".format(calc_t_crc, t_crc))
if h_valid:
    h_payl = h_val & 0x1ffff;
    calc_h_crc = crc7(h_payl)
    if calc_h_crc == h_crc:
        out['humidity'] = round(float(h_data) / 512,2)
    else:
        log("Invalid humidity CRC, expected: {}, actual: {}".format(calc_h_crc, h_crc))

# set output to input
if out:
    IONode.set_output('out1', out)

Then press “Save and Activate”.

CHECKPOINT

Click on Dashboard on the left side of the Renesas IoT Sandbox and reset your board. In the Real Time Events Log widget, you should see an event containing temperature and humidity after approximately 5 minutes.


The temperature is in celsius.

STEP SIX: SEND DATA TO MAP

In this step, you will set up a workflow to send your location, temperature, humidity, and air quality data to the Learn IoT Community World Map.
Set up the following workflow:


Note that the tags are under the “processed” category, NOT “raw”.

Replace the Base Python code with the following:

import time
import IOTCommunity 

# values to send
name = #INSERT NAME HERE IN QUOTES
lat = IONode.get_input('in1')['event_data']['value']
longval = IONode.get_input('in2')['event_data']['value']
air = IONode.get_input('in3')['event_data']['value']
temp = IONode.get_input('in4')['event_data']['value']
hum = IONode.get_input('in5')['event_data']['value']
date = time.strftime("%m-%d-%Y")

data = {'name': name, 'lat': lat, 'long': longval, 'air': air, 'temp': temp, 'hum': hum, 'date': date}
IOTCommunity.send_data(data)
IONode.set_output('out1', data)

Where the code says “#INSERT NAME HERE IN QUOTES”, replace this with the name you want to give your data point surrounded by quotation marks, such as: “name”. Make sure that the name only contains letters and numbers (no special characters or spaces!). Then press “Save and Activate”.

CHECKPOINT

Click on Dashboard on the left side of the Renesas IoT Sandbox, open the Learn IoT Community World Map, and reset your board. In the Real Time Events Log widget, you should see an event containing all of your sent data in approximately 5 minutes.


Additionally, you should see a map marker at your location with your board’s information.

Congrats, you’ve just learned how to send your board data to our IoT Community World Map. You have now finished this tutorial sequence, happy building!


Outdoor Air Quality Monitor with S3A7
World Map Tutorial Summary (location, air quality, temperature, humidity)
SK-S3A7 Tutorials
Smart Door Foot Traffic Analyzer Tutorial
Learn IoT MeetUp - Apr 19 in San Jose