Search
  • Medium One

Connect Atmel SAM W25 Xplained Pro to the Medium One Cloud

Updated: Jun 15

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


In this tutorial, you will:

  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:

  3. 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.

  4. Get push notification on your phone when an event is published to the cloud from the board by pressing the button on the board.


Here’s what you need to get started:

  1. Medium One Prototyping Sandbox Account + Atmel SAM W25 Xplained Pro board [Buy] – configure as shown here

  2. Windows 10 (this project has been run and tested on Windows 10 operating system).

  3. Atmel Studio 7 [Download]

  4. PuTTY [Download]

  5. Project Code [Download]

  6. Medium One IoT Controller Mobile App

  7. 2.4GHz Wifi Internet access

  8. Micro USB-B cable


Step 1. Open the Project Code

Open Atmel Studio. 


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


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.

Step 2. Set up Medium One Account

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.

From the menu list, select Setup → Manage 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 including the password somewhere, they will be required in future steps).


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

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

Note: You should now have your API Key, Project MQTT ID, User MQTT ID, and User Password.


Step 3. Modify Project Code

Open the project in Atmel Studio 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) to your WiFi credentials.


Replace User ID, Password, and Client ID. These are required for MQTT authentication.


User ID should be in the 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 in the 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 (in image below, it is 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 (in image below, it is "device1")

Save the file.


Step 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 indicates the power is ON (or board is connected) with solid green color light.

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


You may observe the STATUS LED a few seconds after the POWER LED (or up to a few minutes for the first time installation of the board).

Note: Both the LEDs must glow before continuing the tutorial.


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

Now open PuTTY, 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.


Go to the menu bar, select Debug → Start Without Debugging.


Observe the execution in the PuTTY terminal.

Once finished, “Preparation of the chat has been completed” appears.

Now go to your Medium One account.


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

Select the user from the drop-down menu on the right.

NOTE: Click the floppy disk 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 should generate an event that you can see on your dashboard.

Step 5. Create Workflow to Receive Subscribed MQTT Events

On the left, select Config → Data Streams, click the raw Data Stream.

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

Click Save Data Stream.


On the left, select Workflow Studio and 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:

import MQTT
MQTT.publish_event_to_client('device1', 'Button Pressed', encoding='utf-8')
 

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 6. Connect the Atmel board to the Medium One IOT Controller App

Note: these instructions are for iOS, but the Android instructions are similar


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

Click on new profile to create a new project.

Rename new with any project name. Then provide the API Key, API Login and Password from your Medium One account. Be sure you select "Pro".

Click Done.


Click +Add New Widget.

Add a switch widget, then click on the switch icon to configure it. (on Android, you must swipe your finger to the right on the widget to configure it)


Enter “raw” for stream and “LED” for tag, then click Done.

Your screen should look something like this:

Click Save.


Send events to Medium One cloud by toggling the switch.

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


Step 7. Write a Workflow to Process the Switch Data

Activate the raw:LED tag.

Create a new workflow called ‘Mobile Switch for LED’.

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:

import MQTT
mode = IONode.get_input('in1')['event_data']['value']
if (mode == "on"):
    MQTT.publish_event_to_client('device1', '1', encoding='utf-8')
else:
    MQTT.publish_event_to_client('device1', '0', encoding='utf-8') 

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.

Step 8. Send Push Notification when Button is Pressed

Select Config → Data Streams, click the raw data stream.


If you allowed push notifications on the mobile app, an 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 and 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:

import FCM
button = IONode.get_input('in1')['event_data']['value']
FCM_token = IONode.get_input('in2')['event_data']['value']
if(button):
      FCM.send_fcm_notification_to_m1_application([FCM_token],"Button Pressed", sound="chime") 

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.

Right now, the workflow will also trigger every time the app sends the FCM_token to Medium One.

Double click on FCM_token module on canvas, and uncheck Trigger and Save and Activate. Then the workflow will only trigger when the button is pressed.

Congratulations! You have successfully completed the tutorial.



Want to learn more about what Medium One can do?

Check out our documentation and tutorials & get started on your next IoT project!

© 2018 Medium One   All Rights Reserved

4633 Old Ironsides Dr. #280, Santa Clara, CA  95054                

info@mediumone.com

|

  • YouTube - White Circle