Connecting Atmel SAM W25 Xplained Pro to Medium One cloud

Level: Beginner

This tutorial will connect your Atmel SAM W25 Xplained Pro board with Medium One cloud to publish/subscribe events through MQTT protocol.

Primary steps of this tutorial are:

  1. Publish an event by pressing device’s button and receive the subscribed event in return using Medium One workflow. A received event will illuminate the LED on the board.
  2. Configure the device to the Medium One mobile app and perform the following tasks:
    • Send event from cloud to the board using Medium One IOT Controller App LED switch on your phone, which will illuminate the LED on the board.
    • Get push notification on your phone when an event is published to the cloud from the board by pressing the button on the board.

What you need to get started?

  1. Windows 10 (this project has been run and tested on Windows 10 operating system).
  2. Atmel Studio 7
  3. Putty
  4. 2.4GHz Wifi Internet access
  5. Atmel SAM W25 Xplained Pro board [Buy-link]– configure as shown here
  6. Micro USB-B cable
  7. ATMEL_SAMW25_MQTT_PUB_SUB_M1  project code
  8. Medium One IoT controller App

Step 1: Publish/Subscribe MQTT event to/from Medium One cloud from/to Atmel SAM W25 board

In this step, you will process the following sequence:

1. Test the downloaded project code

2. To create and set up Medium One sandbox account

3. Modify parameter values in the project code

4. Publish/Subscribe MQTT events to/from the Medium One cloud

5. Create Workflow to receive subscribed MQTT events from Medium One cloud

1. Test the downloaded project code

  • Open Atmel Studio. 

On the menu bar, select File → Open  Project/Solution and browse ATMEL_SAMW25_MQTT_PUB_SUB_M1 project directory.

  • Build the downloaded project. 

On the menu bar, select Build → Build Solution.

You can monitor your build process in the Output section. Once done, you can see a line “Build succeeded“.  Your build should be clear of errors. Fix any issues before proceeding  to the next step.

2. Create and set up Medium One sandbox account

Click on Sign up for a free instant account.

Fill in all login credentials and click Create Account button.

Once logged into your Sandbox account, you need to generate basic user credentials, user and API key, required by MQTT protocol. 

  • Create a new API Key.

From the menu list, select Setup → Manage API Keys, then click on Add New API Key.

Enter a brief Description and make sure Enabled box is checked. 

This is your new API Key; you will use it to generate events in upcoming steps.

  • Create an API Basic User to send data to the cloud through Medium One web portal. 

From the menu list, select SetupManage Users, then click on Add New User.

Enter the Username and Password information.

Back at the Manage Users page, observe your newly created User (Login ID) and MQTT ID (save the credentials somewhere, it will be required in further steps).

You may need to refresh the page to see the changes.

  • View your Project MQTT ID

From the menu list, select SetupMQTT. Observe the Project MQTT ID and record it for further steps. 

Note: Make sure to record these  credentials (API Key, Project MQTT ID, User MQTT ID, and User Password) .

3. Modify parameter values in the project code

  • In Atmel Studio, open the project again.  

On the right, you can see Solution Explorer.

Select ATMEL_SAMW25_MQTT_PUB_SUB_M1 → src → main.h

  • Change the main_mqtt_broker to “mqtt.mediumone.com.”
  • Change MAIN_WLAN_SSID and MAIN_WLAN_PSK (i.e. WiFi login and Password) with your credentials.
  • Replace User ID, Password, and Client ID. These are required for MQTT authentication.

User ID should be in format: PROJECT_HASH/USER_HASH

PROJECT_HASH should be replaced by a value of  Project MQTT ID from M1 Sandbox account.

USER_HASH should be replaced by a value of User MQTT ID.

 

Password should be format: API_KEY/USER_PASSWORD 

API_KEY should be replaced by a value of API_KEY.

USER_PASSWORD should be replaced by USER_PASSWORD, created when a new user was generated. 

 

Client ID should be unique, and it should be of maximum 64 characters

Client ID should be concatenation of  PROJECT_HASH,USER_HASH, and UNIQUE_STRING, where UNIQUE_STRING can be anything.

  • Set  MAIN_EVENT_TOPIC to 0/PROJECT_HASH/USER_HASH/DEVICE_ID/.

PROJECT_HASH and USER_HASH are same as defined above.

DEVICE_ID can be anything; it may even be a zero-length string (Here, we have considered the DEVICE_ID of zero length).

Note: If the DEVICE_ID is of zero-length, it will be automatically added to the message payload. 

  • Set  MAIN_EVENT_SUB_TOPIC to 1/PROJECT_HASH/USER_HASH/DEVICE_ID_SUB/event.

PROJECT_HASH and USER_HASH are same as defined above.

DEVICE_ID_SUB can be anything; it can not be of zero length (Here, we have considered the DEVICE_ID_SUB as ‘device1‘).

  • Save the file.

NOTE: ‘/’ should be included in all the credentials.

4. Publish/Subscribe MQTT events to/from the Medium One cloud

  • Connect your Atmel SAM W25 Xplained Pro board to your computer via Micro USB- B cable.

Then you will observe two LED lights:

POWER LED that indicates the power ON (or board is connected) with solid green color light.

STATUS LED that indicates the successful installation/detection of the board with solid yellow color light located just next to GREEN LED.

You may observe the STATUS LED after few seconds of POWER LED (It can exceed to few minutes for the first time installation of the board) .

Note: Both the LEDs must glow to move further and for the execution of the project successfully.

  • On your Windows computer, open Device Manager and check the USB COM PORT.

  • Now open Putty that you installed, select Serial check box. Write COM# Port (like “COM7”) in Serial line column name and Speed as “115200”. Click Open.

  • Go back to Atmel Studio and rebuild the project.
  • Execute the code now.

For this, go to the menu bar, select Debug → Start Without Debugging.

Observe the execution in Putty terminal. Once finished, a line “Preparation of the chat has been completed” appears.

  • Now switch your screen to the dashboard of your Medium One sandbox account.

Configure a Real Time Streams widget. 

From the menu list, select Dashboard, then select Single User – Real Time Events Stream widget.

  • Select User from the drop-down menu on the right.

NOTE: Click the ‘disc+’ icon in the top right corner to save your Dashboard preferences.

  • Press the SW0 button on your SAM W25 Xplained Pro board. The button press generated an event and you can see the published event on your dashboard.

5. Create Workflow to receive subscribed MQTT events from Medium One cloud

  • On the left of Medium One sandbox, select Config → Data Streams, click Edit for the raw Data Stream.

Activate ‘raw:button’ tag by selecting Active checkbox for it. 

  • Click Save Data Stream.
  • On the left, select Workflow Studio → Click Create to create a new workflow.

  • Give your workflow, a name like “Button Pressed”.

  • Click the Tags & Triggers pane on the right toolbar.

Under the raw drop-down, drag and drop button tag onto the main canvas.

This is a trigger that defines the workflow execution.

  • Next, from the Modules pane, click on the Foundation dropdown then drag and drop the Base Python module onto the canvas.

Base Python is a programmatic module that you can code in Python to process your data.

  • Connect the button module to the input of Base Python.

  • Double click on the Base Python module box, and replace the default script.

Enter the following in the script text area:

Replace ‘device1’ with the name of the device you entered in DEVICE_ID_SUB parameter of MAIN_EVENT_SUB_TOPIC.

  • Click Save and Activate.

  • Go back to the Atmel Studio, build and debug the code again.
  • Press the Atmel board’s SW0 button and observe a subscribed message “Button Pressed” on Putty. 

 

Moreover, you can see LED0 (yellow LED) lights up for 1000 millisecond (i.e., 1 second) when you get the subscribed data/message.

 

Step 2: Connecting the Atmel board to Medium One IOT Controller App

This step includes:

1. To turn LED ON/OFF on the Atmel board using IOT Controller App switch widget and publish/subscribe data to/from Medium One cloud.

2. Push Notification on a mobile phone after pressing the button on the Atmel board.

1. Toggle LED light on Atmel board using the mobile IOT Controller App and publish/subscribe data to/from Medium One cloud.

  • Download and install Medium One IOT Controller app on your mobile phone.

  • Create a new profile by clicking ‘+’ in the top right hand corner; a new profile will created.

  • Click on new profile to create a new project.

  • Rename new with any project name. Then provide an API Key, API Login and Password. Be sure you are doing for sandbox (in blue color at the bottom).

Here,  API Login is API Basic Username, and Password is API Basic User Password.

Note: These credentials should be same as that of your Medium One sandbox account.

  • Click Done.
  • Click +Add New Widget button on the top to add new widgets, and you will see a scrolling list of 8 widgets that you can enable.

  • Add a switch widget, then click on the switch icon to configure it. Enter “raw” for stream and “LED” for tag, then click Done.

Your screen should look something like this:

  • Click Save at the bottom of your app to save the desired configurations.
  • Send events to Medium One cloud by toggling the switch on your mobile IoT Controller app to ON.

  • Go back to the Sandbox Dashboard and observe the Real Time Events Stream as you toggle the switch on your mobile phone. You should see the events appear.

Data published on medium one cloud when a switch is ON:

Data published on medium one cloud when switch is OFF:

  • Receive a subscribed message from cloud to board.

You need to create a workflow for it.

First, deactivate the previous workflow.

For this, open the workflow Button Pressed. On right, select Revisions → click the ‘cross’ icon (x) in the Autosave to deactivate it (To reactivate any workflow, select Revisions → click the ‘tick’ icon in the Autosave).

  • Create Workflow for LED:

Follow the previous steps  to create a workflow for LED now.

Here, you need to activate raw:LED data stream, give a name to workflow like ‘Mobile Switch for LED’ and drag and drop the LED module from Tags and Triggers.

  • Double click on the Base Python module box, and replace the default script.

Enter the following in the script text area:

  • Click Save and Activate.

  • Go back to the Atmel Studio, build and debug the code again.
  • Toggle the switch on your mobile IOT controller app, this time you can see a subscribed message on Putty along with LED ON/OFF on board and published data on Medium One’s cloud.

The subscribed message will contain ‘1’ on LED ON mode and ‘0’ on OFF mode.

2. Push Notification on mobile phone for pressing the button on Atmel board.

  • Create workflow for push notification.

Be sure to activate the FCM_token tag.

For that, on the left, select Config → Data Streams, click Edit for the raw Data Stream.

Select the Active checkbox for raw:button also as in step 1.

Note: If you allow push notifications on the mobile app, a FCM_token tag must be created in the raw data stream when the profile is created (If you don’t see it, you’ve probably disabled push notifications, or have not created a profile on the mobile app).

  • On the left, select Workflow Studio → click Create to create a new Workflow.

Give your workflow a name like “Push Notification for Button Press”.

  • Once you created the new Workflow, click the Tags & Triggers pane on the right toolbar, and under the raw dropdown, drag and drop button and FCM_token tag onto the main canvas.
  • Next, from the modules pane, click on the Foundation dropdown then drag and drop the Base Python module onto the canvas.

  • Double click on Base Python.  

Expand Inputs/Outputs, click Add Input to add an additional input to this module (for FCM_token).

You will now see two input nodes.

  • Click Save.
  • Connect raw:button and raw:FCM_token to input (in1 and in2 respectively) of Base Python.

  • Double click on the Base Python module box, and replace the default script.

Enter the following in the script text area:

  • Click Save and Activate.

  • Go back to the Atmel Studio, build and debug the code again.
  • On pressing the SW0 button on the board, this time you can see a notification “Button Pressed” on your mobile device.

  • If you want to disable FCM_token as a trigger to avoid push notification every time, double click on FCM_token module on canvas, and uncheck Trigger and save (to stop push notification).

Congratulations! You have successfully completed the tutorial.

Feel free to experiment further with the workflows and envision new interesting IOT use cases with Atmel SAM W25 Xplained Pro board.

For more tutorials on other boards and devices, please visit: https://blog.mediumone.com/category/tutorials/.

somya.bansal@medium.one'

Author: Somya Bansal