Skip to main content

Connect your IoT "Thing" to the Cloud

This project shows you how to connect a cheap (approx. £4) Wifi enabled WeMos D1 Mini board to IBM's (free) cloud Platform as a Service (PaaS).

Picture credit: Adafruit. NeoPixel Stick - PRODUCT ID: 1426

UPDATE: 3rd March 2019

 

WARNING In order to use IBM PaaS for longer than ten days (unless you are doing "devlopment activity" - which sadly does not include the re-deploy of a Node-RED flow), then you need to upgrade to a Pay-As-You-Go account.

To do this, you need to give your credit card details, but if you are just running a Node-RED flow, you *should* stay below the 512MB/month limit, and it will be free. (Seehttps://console.bluemix.net/docs/account/index.html#accountsfor more details).

Update 23 October 2019

The Pay-As-You-Go account is no longer available. So unless you want to pay IBM $2000 a month for an account this whole tutorial is worthless. Sorry.

(The IBM PaaS Lite account not only deactivates your Node-RED flow after 10 days, unless you jump through various hoops, but it also COMPLETELY REMOVES your services after 30 days of inactivity (by which they mean not loggin g in to the IoT platform console, even if your devices are accessing it) - which means your devices which were linked are now not, and you have to physically update every device. (I am not impressed)).

Note:As with most software, IBM's cloud Platform as a Service (PaaS) gets updated.This guide (dated Feb 2019) is a revised version of one originally posted on Instructablesin November 2016.

This project shows you how to connect a cheap (approx. £4) Wifi enabled WeMos D1 Mini board to IBM's (free) cloud.

Many "Things" that are connected to the internet are often tethered through a mobile phone. However, I find it really useful to be able to connect a "Thing" (using wifi) directly to a cloudPlatform as a Service (PaaS), without a phone. A PaaSallows us to develop, run, and manage applications in the cloud. Imagine it as a bit like a Raspberry Pi set up and run by imps, sitting inside the fluffy land of the cloud. There are many PaaS providers. This project uses IBM's PaaS.

Parts:

Connect a strip ofneopixelsto a WeMos D1 Mini. Connect the 5V of the neoppixels to the 5V on the WeMos, Ground of the neopixels to Ground on the WeMos and connect the neopixel "data in" to Pin D2 on the WeMos.

Step 1:Register for a free IBM Cloud account

  1. Go to https://cloud.ibm.com- click on "Create an IBM Cloud Account" and register your details.
  2. Be aware of the to use your local IBM Cloud instance - for example if in UK, use LONDON. Otherwise you may have problems with date / time calls (such as used in theSeeTideproject)
  3. If you have problems, such as it saying your email address is not valid, try going into "private" or "incognito" mode on the browser. Or clear your cookies.
  4. Check your email and confirm your account and log in.

The "Lite" package allows you to run one instance of Node-RED - the visual flow programming tool.The Lite package has a256MB runtime limit, but for the simple one-off purposes such as the "See Tide" project (coming soon!), this is all that is required. If you want to run more things, you can upgrade to the Pay As You Go account later.

Step 2: Set up a Node-RED flow

  1. Click on "Catalog" at top of the screen and scroll down until you find "Starter Kits" and "Internet of Things Platform Starter" - (NOTE - THIS IS DIFFERENT FROM THE "Internet of Things Platform" LINK.) (Direct link:https://cloud.ibm.com/catalog/starters/internet-of-things-platform-starter). Click on the link.

    IoT_Starter_Kit

  2. The "Internet of Things Platform Starter" has pre-assembled services that work together - a Node-RED instance, a Node.js server, a Cloudant database for Node-RED to store configuration data, and the Watson IoT Platform service so you can connect devices to the Cloud. You do not need to know about these individual parts.
  3. In the "App Name" box, name your application. This must be something unique.Add your initials in front of the name if the app name you wanted is already taken by someone else. We'll use the example "MyApplication" for this guide.
  4. Click on Create to create the application instance. This can take up to ten minutes.
  5. Secure the Node-RED editor by adding a password. The site leads you through this.
  6. Once you have saved the password, the Node-RED application will launch automatically. Click the "Go to your Node-RED flow editor" to see the flows.
  7. You need to login to be able to deploy anything on this page. Click the "person" icon top right and log in.
  8. There is already a flow in your Node-RED application. Add another flow by clicking the "+" in the top right-hand corner of the flow sheet. You can then double click "Flow 1" and delete it.
  9. Add an inject node - drag it from the left-hand menu bar into the main page. It will then be called "timestamp". Drag an IBMIoT output node into the flow. It has a grey pimple on the left-hand side. Connect the two by dragging a line between the grey pimples.

    NodeRedFlow_IBMIot_node

  10. Double click the Inject node, change the Payload type to string from the pull-down list and enter "#00ff00". Name the node "Green".
  11. Press the big red "Deploy" button in the top right to save the flow. You may get a message saying "The workspace contains some nodes that are not properly configured:" and "Are you sure you want to deploy?". If so, click "Confirm deploy". You will configure it properly later.

Step 3: Add a Device

  1. Your WeMos needs to be able to communicate with your IBM Cloud account. You, therefore, need to add it as a device. Leave the Node-RED window open, and in another window, go to the IBM Cloud dashboard again (https://cloud.ibm.com/).
  2. Under "Resource Summary" click on "Cloud Foundry Services"
  3. Click on "MyApplication-iotf-service" (for whatever you have named "MyApplication". This brings up the splash screen "Let's get started with IBM Watson IoT Platform".
  4. Click "Launch"
  5. Click the big blue "+ Add Device" at the top right.
  6. In the "Device Type" box, enter a name, such as "Wemos"
  7. In the "Device ID" box, enter a name, such as "LucyWemos"
  8. Click next, and leave all the Device Information blank, click next.
  9. This should take you to the Security page. Let the authentication token be auto-generated by clicking Next, then Done.
  10. The information screen that pops up has a lot of information you will need later - copy and paste it to a safe place - especially the Authentication code.

Step 4: Change the Security Options

  1. Go to the IBM Watson IoT Dashboard - the easiest way to get there is to go change "OrganisationID" in this link to your own OrganisationID given in Step 3j:https://OrganisationID.internetofthings.ibmcloud.com/dashboard/devices/browse
  2. On the left-hand side there is a menu with icons - click on the Cog (Settings)
  3. Scroll down until you find "Connection Security"
  4. Click "Open Connection Security Policy"
  5. Click the edit symbol (pencil icon) to the right of "Connection Security.
  6. Under "Security Level" change the drop-down menu until it reads "TLS Optional"
  7. Click Save and Close.

Step 5: Node-RED Flow Part 2

  1. Go back to the Node-RED flow. The web address will be something like "https://MyApplication.eu-gb.mybluemix.net/"
  2. Double click the IBMIoT output node.
  3. Change Authentication to Bluemix Service
  4. Change Output type to "Device Command"
  5. Device type (Wemos) and Device ID (LucyWemos) are the ones you set up earlier.
  6. In "Command type" put "command"
  7. In "Format" put "text"
  8. In "Data" put "{}". (Note: There is no obvious reason for this, you just have to.)
  9. QoS leave as 0
  10. Name - Something to identify it - e.g. SeeTideWeMos
  11. Click Done
  12. Click the big red Deploy button at the top right.

Step 6: WeMos Arduino Code

Now you need to tell the WeMos to talk to your IBM Cloud account.

  1. Use the latest open-source Arduino software (IDE). You can download it here: https://www.arduino.cc.
  2. The following libraries will also be needed. The instructions to install a library are given here: https://www.arduino.cc/en/Guide/Libraries
  3. "PubSubClient" by Nick O'Leary
  4. "Adafruit NeoPixel" by Adafruit
  5. You will also need to install the ESP8266 wifi board - follow the instructions at https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/using-arduino-ide. This will install both the adafruit-huzzah-esp8266 and the WeMos D1.
  6. You need to make a change to the PubSubClient.h file. It should be in your Arduino installation in a subdirectory called libraries/PubSubClient/src.Open the file using a text editor. Look for "‪#define MQTT_KEEPALIVE 15" and change the 15 to 60. Save the change.
  7. Restart the Arduino software.
  8. Plug the WeMos into your computer using a USB cable.
  9. In the Tools menu:
    1. Select the "WeMos D1 R2 & mini" board in the board manager menu. If it is not there, restart the Arduino software and try again.
    2. Select CPU frequency: 160MHz
    3. Select Flash Size 4M (3M SPIFFS)
    4. Select: Upload speed 115200
    5. Port: Make sure this is the port the WeMos is plugged in to.
  10. Download and open the Arduino file NeopixelIBMv2.0.ino
    1. In the code replace the SSID and password with the wifi details you require.
    2. You then need to replace the BROKER, ClientID and Password.These were given on the page in Step 3j. The first part of the BROKER is the Organisation ID. The Client ID is the Organisation ID, the Device Type and the Device ID. The Password is the Authentication token. Change all these and save the file.
  11. Upload the sketch to the WeMos.
  12. The Neopixels should flash magenta when they are trying to connect to the local Wifi and cyan when trying to connect to the IoT Platform and your Bluemix Node-RED flow. Once connected the LEDs turn off.

Step 7: Testing

  1. Go back to the Node-RED flow.
  2. Click the Inject Node. The Neopixels should turn green.
  3. Add another Inject Node. Change the Payload to String and enter #FF0000, name the node "red".
  4. Add another Inject Node. Change the Payload to String and enter #0000FF, name the node "blue".
  5. Click Deploy

Click the different inject nodes. The neopixels will change colour.

CONGRATULATIONS!

You are now controlling the neopixels over the web.

Acknowledgements:

A huge thanks to IBM's Andy Stanford-Clark and Ross Cruickshank for their help with my queries as I worked through this project. Andy also wrote the Arduino sketch.

For further reading visit:https://developer.ibm.com

Downloads

I am an inventor, engineer, writer and presenter. Other stuff: Royal Academy of Engineering Visiting Professor of Engineering: Creativity and Communication at Brunel University London; Fellow of the Institution of Mechanical Engineers and have a PhD in bubbles; Judge on BBC Robot Wars.
DesignSpark Electrical Logolinkedin