Sending Tweets from your S3A7 Board

Overview

This tutorial will show you how to send tweets from your board to your phone. We will create two twitter accounts; one for your board and one personal account for your phone. We will add a TEMP button and HUM button to your board’s display so that anytime you press them, your board will tweet temperature or humidity data.  By following your board’s tweets from your personal twitter account, you will receive real-time temperature and humidity data tweeted to your phone.

Here’s what you need to get started:

  1. Synergy S3A7 IoT Fast Prototyping Kit
  2. WiFi Internet Access

Prerequisites:

This tutorial assumes you have already completed the Smart Garage Quick Start Guide and all its workflows.

Step 1. Power Up Your S3A7 Board

To get started, reconnect your S3A7 Board to your laptop using the USB cable.  This will power up your board.

  • As a reminder: The AMS sensor with Grove cable should be connected to J18 and the Bosch BMC150 sensor should be connected to J5.  Reference: Smart Chef Quick Start Guide Step 4.  

Wait 10 seconds and your board will return to where you left off.

  • The screen should have a “Smart Garage Monitoring” message at the top and any notifications from the previous tutorial.
  • Note: All previous workflows will start processing data.

 

Step 2. Create a twitter account for your S3A7 Board

On your laptop, go to www.twitter.com and join.  Enter a name for your board.  In this example, we call it:  S3A7 IoT Kit.

 

Twitter will ask to verify your phone.

Choose a username for your board.  In this example, we use:  S3A7SmartGarage.

Now your board has a new twitter page.

 

Step 3. Link your board’s new twitter account to your Smart Garage project.

Login to your Renesas IoT Sandbox account.

In the left-hand menu, select External APIs, then click on Add New Service Key.

 

“Connect an External Service” will display.  Select Service, then twitter from the pulldown window.

 

Click Save and Activate.  You will see twitter added as an external API.

 

Authorize Medium One to use your twitter account.  Click Authorize app.

Once authorized, your Renesas Sandbox account will acknowledge that the Board’s twitter account is now linked to it.

 

Step 4. Create a personal twitter account

If you do not have your own twitter account, follow steps similar to creating a twitter account for your Board, except this time the username is your own name.

On your laptop, go to www.twitter.com and join.  Enter your name and mobile phone number.

Twitter will send a text to your phone to verify.

Follow the remaining twitter instructions to enter your email and choose a username (personal username).

 

Now you have a new twitter page.

 

 

Step 5. Follow your Board’s twitter account

Let’s follow your Board’s twitter account on your phone using your personal Twitter account.

Go to your phone and install the twitter app from the AppStore (if you don’t have it already).

 

Login to your twitter account on your phone.

 

Select the Explore icon at the bottom of the twitter app and search for your board’s twitter account:  S3A7 IoT Kit (case sensitive).  When it finds the account, click on it.

Twitter will go to the Board’s twitter page.  You can now Follow your board on twitter.  Click Follow and Click the Bell+ (notification icon).

 

Allow notifications from the board.  Click OK to turn on tweet notifications.

You are ready to receive tweets from your board.

 

Step 6. Create Temperature and Humidity Sampling Workflow

Let’s tweet the temperature and humidity anytime you touch the board’s screen.  In order to do this, we need to create a workflow that samples the temperature and humidity sensor data from your board.

[Note, in this step, the data is still encoded and we will decode it in subsequent steps.]

Click on the Workflow Studio icon and click the Create box.  Note:  You should see all the workflows you created from the Smart Garage tutorial.

Create a new workflow named “Temperature and Humidity Sampling”.

Add Inputs:

  • Drag the “raw” touched tag and the “raw” connected tag into the workflow editor window.
  • Select “Scheduler” from the Tags & Triggers menu and drag the Custom trigger into the workflow editor window.

Double click on the Custom trigger and check the box next to “All Users” before clicking Save. Leave Minutely Interval to 10 (default).

Add Code Module:

  • Select Modules
  • Select Foundation and drag Base Python to the workflow editor window.
  • Double click on Base Python and add 2 more inputs.
    • Click on Inputs/Outputs
    • Click Add Input 2 times

Connect Inputs to Code module:

  • Custom => in1
  • raw:connected => in2
  • raw:touched => in3

This workflow will trigger when one of these 3 things happen:

  • Once per minute
  • Connect event is received
  • Touched event is received

Replace the Base Python code with the following:

'''
This workflow requests the temperature and humidity from the board.
Last Updated: Sept 15, 2016
Author: Medium One
'''

import cloud_driver_ams
cloud_driver_ams.request_ams_temperature_humidity(3, 'temp_and_humidity', 's3a7')

Then click the “Save and Activate” button to finish setting up the workflow.

 

Step 7: Create Temperature and Humidity Processing Workflow

In this step, we will take the raw temperature and raw humidity data that our previous workflow produced and process it to display it on the board.

Create a new workflow and name it “Temperature and Humidity Processing”. You will create the following workflow.

Drag the rawtemp_and_humidity” tag into the workflow. Drag a Base Python module into the workflow, connect it to the temp_and_humidity tag. Drag the “Processed Stream – Single” output onto the workflow and connect its “in1” to the Base Python module’s “out1”.

Double click the Processed Stream and make sure the “processed” stream is selected (should be by default). Don’t change any other values.  Note: this workflow creates a new  processed temp_and_humidity event with each encoded raw temp_and_humidity event. Then click “Save” to finish setting up the workflow.

 

Replace the Base Python code with the following:

import cloud_driver_ams
t,h = cloud_driver_ams.decode_ams_temperature_humidity(IONode.get_input('in1')['event_data']['value'])
out = {}
out['temperature'] = t
out['humidity'] = h

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

Then click “Save and Activate”.

 

Step 8. Create a Touch to Tweet Workflow

Now that you have workflows to gather and process temperature and humidity data, let’s create a workflow to send a tweet.

Click on the Workflow Studio icon and then the Create workflow box.

 

Enter the name of workflow Touch to Tweet and click anywhere inside the workflow studio edit window.

Add Inputs:

  • Select Tags & Triggers
  • Select raw and drag touched and x and y to the workflow editor window.
  • Select processed and drag temperature and humidity to the workflow editor window.

Add Code module:

  • Select Modules
  • Select Foundation and drag Base Python to the workflow editor window.
  • Double click on Base Python and add 4 more inputs.
    • Click on Inputs/Outputs
    • Click Add Input 4 times

Connect Inputs to Code module:

  • Touched => in1
  • x => in2
  • y => in3
  • Temperature => in4
  • Humidity => in5

 

Replace the Base Python code with the following:

import MQTT
import Twitter

x=IONode.get_input('in2')['event_data']['value']
y=IONode.get_input('in3')['event_data']['value']
last_temperature =IONode.get_input('in4')['event_data']['value']
last_humidity =IONode.get_input('in5')['event_data']['value']

MQTT.publish_event_to_client('s3a7', 'D{};{}'.format(17,  "    [Temp]     [Hum]"), 'latin1')

if y > 230:
    if x > 100: 
        MQTT.publish_event_to_client('s3a7', 'D{};{}'.format(17, "    [Temp]    [[Hum]]"), 'latin1') # Send tweet the temperature is ..
        Twitter.post_status("The humidity is "+str(last_humidity)+"%")
    else:
        MQTT.publish_event_to_client('s3a7', 'D{};{}'.format(17, "   [[Temp]]     [Hum]"), 'latin1') # Send tweet the humidity is ..
        Twitter.post_status("The temperature is "+str(last_temperature)+"C")

 

Your Base Python module should look like this:

Click Save and Activate.

 

Step 9.  Tweet the Board’s Temperature and Humidity

Now you’re ready to send a tweet from the Board.  Since you are following your Board’s (S3A7 IoT Kit) twitter account, you will receive the tweets on your phone.

  • On your Board, touch the [Temp] button at the bottom of the display.
  • The board will tweet the temperature.
  • Touch [Hum] button at the bottom of your board display.
  • The board will tweet the humidity.

 

Your phone will display tweets from your S3A7 board.

Congratulations!  You have successfully added twitter to your S3A7 board that will send tweets to all its followers!