Author: Kaylee Bement
This tutorial uses the WeMos ESP8266 WiFi Module and Medium One to control an LED and get your geolocation data.
In this WeMos Quick Start Guide, you will:
- Set up an Arduino environment
- Program the WeMos board
- Use Medium Workflows to produce geolocation data and switch the LED
- Use the Medium One IoT Controller app to control the LED and view your location on a map
Here’s what you need to get started:
STEP 1: SIGN UP FOR MEDIUM ONE SANDBOX ACCOUNT
Once activation is completed, you will receive an email with your account credentials needed to sign in to Medium One.
STEP 2: SET UP YOUR ACCOUNT
Click “Setup” in the left-hand sidebar of your Medium One account.
Go to “MQTT” and copy down your Project MQTT ID for later use.
Go to “Manage Users” and click “Add New User”.
Give your new user a username and password. You will want to copy these down for later use as well. Then click “Save”. You will also want to copy down the user MQTT ID that shows up after you click Save.
Go to “Manage API Keys”, click “Add New API Key”, give your API Key a description, and click “Save”. You will want to copy down the API Key for later use.
STEP 3: SET UP THE MOBILE APP
Note: These instructions are for the IoS app, but the Android app is similar.
Open the IoT Controller app and click on the plus sign in the top right corner.
Now you can give your project a name and input your Username, Password, and API Key from the account set up. Then press “Done”.
Press “+Add New Widget”.
Press the power icon on the left-hand side of the switch to access the settings.
Type “raw” for the stream, and “switch” for the tag. Then press “Done”.
Press “+ Add New Widget” again and scroll down to the bottom to select “Map”.
Press the marker icon on the left-hand side of the map and type “processed” for the stream and “gps” for the tag. Then press “Done”.
Now press “Save” at the bottom of the app. You should get a confirmation that it was saved.
Go to your Medium One account and go to the “Dashboard” from the left-hand sidebar.
Click “Single User Real Time Events Stream”.
Select your user in the right hand corner of the widget.
Now, on the app, click on the switch to turn it on.
You should see an event show up on the widget.
Click the save icon (floppy disk) in the top right corner of the dashboard.
STEP 4: SET UP THE WEMOS BOARD
Open Arduino and go to File -> Preferences.
Paste http://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs section and press OK.
Go to Tools > Board > Boards Manager …
Search for “esp8266” and install the boards.
Go to Sketch -> Include Library -> Manage Libraries.
Copy and paste the following code from this link into your project.
Fill in your account information where necessary in the code.
Scroll down to the setup function and fill in your wifi information.
Install the WeMos driver if you haven’t already. Plug in your WeMos board and go to Tools -> Port. Make sure that the port the WeMos is plugged into is selected.
Then click on the upload arrow in the top left corner.
Then go back to your Medium One Dashboard, where the Real Time Events Stream should be open. You should eventually receive an event from your WeMos board within a minute.
You should go to Tools -> Serial Monitor to see print statements from the board, so that you can see when the board connects to the WiFi.
You will also receive a heartbeat event from the board every minute.
Unplug your WeMos board.
STEP 5: WRITE WORKFLOW 1: SWITCH LED
Go to “Config” from the left-hand sidebar on your Medium One account and click “Data Streams”.
Click “raw” and then click the active checkboxes next to “ip_address” and “switch”. Then click “Save Data Stream”.
Now go to your Workflow Studio on your Medium One account and click “Create”.
Give your workflow a name. We named ours “switch led”.
Click the “Tags and Triggers” icon, which is the first icon on the right-hand sidebar.
Click “raw” and drag the “switch” tag into the main area of the workflow.
Now click on the “Modules” icon (the second icon in the sidebar), click on “Foundation” and drag a “Base Python” module into the workflow.
Connect the bottom end of the switch tag to the “in1” circle on the Base Python module.
Double-click on the Base Python module and copy and paste the following code (follow link) into the Script section, replacing any code that is originally there.
Now click “Save and Activate”.
Plug your WeMos board in and open the IoT Controller app.
After the board is done setting up (within a minute), turn your switch on the app on and off. You should see the LED on the board turn on and off.
Unplug your WeMos board.
STEP 6: WRITE WORKFLOW 2: GET LOCATION
Create a new workflow, name it, and drag the raw ip_address tag into the workflow.
Drag a Base Python module into the workflow.
Click the “Outputs” icon (third icon on sidebar) and drag two “Processed Stream – Single” outputs into the workflow.
Double click on the Base Python module and click “Inputs/Outputs”. Then click “Add Output”.
Copy and paste the following code into the Script area, replacing any code (follow link below) that was previously there. Then click “Save”.
Now connect the Base Python module to its input and outputs as shown in the picture.
Now click on the “Revisions” icon (second from the bottom of the sidebar).
Click the checkmark under the latest revision to activate the workflow.
Go to your Dashboard and plug in the WeMos board.
In addition to the WeMos event, you should get two GPS events in your Events Stream.
STEP 7: MAP LOCATION
Click “Config” and then “Data Streams” on the left-hand sidebar. Then click “processed”.
Look for “processed.gps” and change the type from “GeoPoint – Time Series” to “GeoPoint – Last Value”.
Then press “Save Data Stream”.
Go back to your Dashboard, and add the Single User GeoPoint Chart.
After selecting your user, you should see your general location marked on the map. Since it is based on ip address, it is not 100% accurate.
Also, if you open up your IoT Controller app, you will see this point marked on your map widget.
Congrats! You have finished the tutorial. If you have an Amazon Alexa, you might be interested in trying out this tutorial, where Alexa communicates with the WeMos board through Medium One to turn a light on and off. Part of the tutorial involves creating a switch on the app, so you already have a head start!