The enchanted brolly stand: a Lucy Rogers recipe

By | 13 minute read | June 27, 2017

Enchanted brolly stand

Ever been caught without your umbrella on a rainy day? If only you had a connected brolly stand to alert you to the weather outside! Dr Lucy Rogers presents a recipe for a connected brolly stand you can create yourself, with a little help from IBM and Weather Company Data.

Overview

  1. Connect Neopixels to WeMos with physical wires
  2. Create an IBM Bluemix Account
  3. Connect WeMos to cloud via Wi-Fi (using Bluemix and Node-RED)
  4. Add Weather Company Data to Node-RED flow
  5. Extract the Probability of Precipitation (PoP) forecast from the Weather Company API using Node-RED
  6. If PoP is higher than a set value turn the Neopixels red, otherwise turn them green

Parts

Tools

  • Soldering equipment

Step 1: Connect Neopixels to WeMos

WeMos and Neopixels

The Arduino code we use later will assume you have connected a strip of eight Neopixels to the WeMos pin labelled D2. If you are using a different number of pixels or a different pin, you will need to change the code.

Connect three wires from your neopixels to the WeMos:

  • Connect a wire between the +5V of the Neopixels and the WeMos
  • Connect a wire between the Gnd of the Neopixels to the Gnd of the WeMos
  • Connect a wire between the data In (DIN) of the Neopixels to the Pin 4 (D2) of the WeMos

That’s the hardware done.

Step 2: Using a cloud Platform as a Service (PaaS)

Welcome to Bluemix (1)

A cloud Platform as a Service (PaaS) allows us to develop, run and manage applications in the cloud. For this project, we will use IBM’s Bluemix. First create an account:

  1. Create a free 30 day trial of Bluemix. This also gives you a free IBMid, which provides access to various IBM tools, including IBM applications, service trials, communities, support and on-line purchasing.
  2. Check your email to confirm the Bluemix account creation. This may take up to an hour.
  3. Log in to http://bluemix.net with your user-id and password.

Step 3: Add a Node-RED application

IoT Platform Starter (1)

You are now ready to make a Node-RED application:

  • Click on “Catalog” on the upper right side of the Bluemix page https://console.ng.bluemix.net/dashboard/applications/.
  • Click on “Boilerplates” which is under Apps in the left hand menu bar.
  • Search for and click on the “Internet of Things Platform Starter”. This 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 Bluemix. You do not need to know about these individual parts.
  • In the “App Name” box, name your application. This must be something unique. If you choose myapp, your application will be located at http://myapp.mybluemix.net. There can only be one “myapp” application registered in IBM Bluemix. Add your initials in front of the name if it is already taken by someone else.
  • Click on “Create” to create the application instance. This can take up to ten minutes.
  • Click on “Runtime” (left hand menu).
  • Change the “MB MEMORY PER INSTANCE” down to 256 and click “save”. This reduces your usage of Bluemix – useful for after the 30 day trial.
  • Anyone can look at your application at http://myapp.mybluemix.net if they guess the name. To put a password on it click on the “Environment Variables” tab in the middle top of the Runtime Page. Scroll down to “User Defined” and click “add”.
  • In the “NAME” box, put “NODE_RED_USERNAME” (without the quotes, but with the capitals and under_scores). In the “VALUE” box put the username you want to use. Do not use your IBMid here. Click “add”. In the “NAME” box put “NODE_RED_PASSWORD” (without the quotes) – in the “VALUE” box add your chosen password (do not use your IBMid password). Click “save”. Restart your application by clicking the circular arrow labelled “restart”. This takes a few minutes, so watch for the green indicator that says it is running.
  • Click on “View App”, then on “go to your node-RED flow editor” and enter the username and password you just set up.

Step 4: Node-RED flow on Bluemix Part 1

Inject and IBMIOT Nodes (1)

  • 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.
  • 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.
  • Double click the Inject node, change the Payload type to “string” from the pull-down list and enter “#00ff00”. Name the node “Green”. Click Done.
  • 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, just click “Confirm deploy”. You will configure it properly later.
  • Your WeMos needs to be able to communicate with your Bluemix account. You therefore need to add it as a device. Leave the Node-RED window open and use another window for the next part.

Step 5: Adding a device, part 1

Connect your device

 

  • Go to the Bluemix Services dashboard. Be aware, this is not the same as the Applications dashboard.
  • Click on “Internet of Things Platform” next to the name of the app you have made. Wait for the page to load.
  • Under “Connect your devices” click on “Launch Dashboard”.

Step 6: Adding a device Part 2

Devices

Now you need to add a device:

  • Click on “Devices” on the menu on the left hand side. The icon is a computer chip.
  • Click on “+Add Device”.
  • Click “Create device type”. This takes you to a new page. Click on “Create device type”.
  • Enter a Name, such as “WeMos”. Click the “Next” button in the bottom right hand corner.
  • On the next three pages, click “Next” then “Next” again, and “Create”.
  • Now choose the device type you have just created and click “Next”.
  • Type a name into the Device ID – such as BrollyWemos. Click “Next”.
  • On the next page click “Next”.
  • On the Security page, let the authentication token by auto-generated by clicking “Next”.
  • Click “Add”.
  • Copy and paste the Authentication Token to a safe place.
  • Click on the “X” to close the window and return to the Browse Devices view.

Step 7: Node-RED flow on Bluemix Part 2

Edit IBMIOT

  1. Go back to the Node-RED flow. The web address will be something like “https://MyAppName.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 (BrollyWemos) 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. BrollyWemos.
  11. Click Done.
  12. Click the big red Deploy button at the top right.

Step 8: WeMos Code

Arduino

Now you need to tell the WeMos to talk to the Watson IoT Platform on your Bluemix account.

  • Use the latest open-source Arduino software (IDE). You can download it here.
  • The following libraries will also be needed. The instructions to install a library are given here.
  • “PubSubClient”
    (https://github.com/knolleary/pubsubclient)
  • “Adafruit NeoPixel”
    (https://github.com/adafruit/Adafruit_NeoPixel)
  • You will also need to install the ESP8266 wifi board – follow these instructions. If you are using a Mac with Sierra, you may need to get the get the CH340 drivers from here.
  • That installed the adafruit-huzzah-esp8266. Now you need to repeat using the board manager to install the WeMos D1.
  • You now 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.
  • Restart the Arduino software.
  • Plug the WeMos into your computer using a USB cable.
  • In the Tools menu:
  • Select the “WeMos D1 R2 & mini” board in the board manager menu. If it is not there, restart the Arduino software and try again.
  • Select CPU frequency: 160MHz
  • Select Flash Size 4M (3M SPIFFS)
  • Select: Upload speed 115200
  • Port: Make sure this is the port the WeMos is plugged into.
  • Download and open the Arduino file 2.ino from https://github.com/DrLucyRogers/WeMosOnBluemix
  • In the code replace the SSID and password with the wifi details you require.
  • You need to change the BROKER, ClientID and Password – to find what these should be open up the Bluemix Applications Dashboard.
  • Click on the line of your application, but not on the underlined Route.
  • Click on “Connections” on the left hand menu.
  • Click on the “View Credentials” of the IoT Platform iotf-service-standard box. The BROKER information is the bit after the first “mqtt_host”:
  • Change the information in the arduino sketch line:
  • #define BROKER “{org_id}.messaging.internetofthings.ibmcloud.com”
  • To find the CLIENTID go back to the Bluemix window and click on Overview on the left hand menu
  • Scroll down and, under “Connections” click on the iotf-service listing.
  • Under “Connect your devices” click Launch Dashboard
  • Click on DEVICE-CENTRIC-ANALYTICS.
  • Click on the device you want and on the right hand side there will be the Client ID in the format d:{org_id}:{device_type}:{device_id}
  • Change the information in the arduino sketch line:
  • #define CLIENTID “d:{org_id}:{device_type}:{device_id}”
  • The password is the authentication token you made for the device.
  • Now save and upload the sketch to the WeMos.
  • 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.

 NeopixelBMv1.2.ino

Step 9: Testing the WeMos / Neopixels / Bluemix connections

  • Go back to the Node-RED flow.
  • Click the Inject Node. The Neopixels should turn green.
  • Add another Inject Node. Change the Payload to String and enter #FF0000, name the node “red”.
  • Add another Inject Node. Change the Payload to String and enter #0000FF, name the node “blue”.
  • Add another Inject Node. Change the Payload to String and enter #000000, name the node “black”.
  • Connect the new inject nodes to the IBM IoT output node.
  • Click Deploy.
  • Click the different inject nodes. The Neopixels will change colour (and turn off for black).

Step 10: Add Weather Company Data to your application

Weather Company Data

 

  • On your Bluemix applications dashboard, click on the line of your application.
  • Click the Connections tab.
  • Click “Connect New”
  • Find and click on Weather Company Data for IBM Bluemix.
  • Click Create.
  • It will ask you to restage the application. Click Restage.
  • Wait for the app to start running (it takes a few minutes).
  • Click on “view credentials” on the Weather Company Data Box.
  • Note down the username and password. They will be needed later.

Step 11: Get the URL for your location

Step 12: Put the Weather Company Data into Node-RED

  • Go back to your Node-Red flow.
  • Drag an “http request” node into the flow.
  • Double click the “http request” node.
  • Copy and paste your Request URL into the URL box.
  • Tick the “Use basic authentication” and provide the username/password of the Weather API service from the credentials obtained earlier.
  • In the “Return” box, make sure it says “a parsed JSON object”.
  • Click “Done”.
  • Add another inject node and connect to the left-hand side of the “http request” node.
  • Double click on the new inject node.
  • Click on Repeat.
  • Change it to every 60 mins between 7am and 9pm, Monday to Friday.

Edit Inject Node

  • Copy the following code to the clipboard, then in Node-Red click Import, clipboard and paste the code.

[{“id”:”42622d3c.ce1644″,”type”:”change”,”z”:”bace1fc1.26f648″,”name”:”Change to Green”,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”#000800″,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:791,”y”:287,”wires”:[[]]},{“id”:”b3b333f1.1a8c78″,”type”:”switch”,”z”:”bace1fc1.26f648″,”name”:””,”property”:”payload”,”propertyType”:”msg”,”rules”:[{“t”:”true”},{“t”:”false”}],”checkall”:”true”,”outputs”:2,”x”:591,”y”:247,”wires”:[[“911b2f82.7fee4”],[“42622d3c.ce1644”]]},{“id”:”17690061.cf867″,”type”:”function”,”z”:”bace1fc1.26f648″,”name”:”Get Probability of Percipitation from Weather rest api”,”func”:”// The ‘forecasts’ item in the API message is an array with 48\n// entries. It’s a 48 hour forcecast so there is an entry for\n// each hour ahead of now. We want the first 8 hours, so we use\n// slice() to grab them.\nvar forecasts = msg.payload.forecasts.slice(0,8);\n\n// Iterate over the selected forecasts and extract the\n// ‘pop’ (Probability of Precipitation). If any of the\n// ‘pop’ are above a certain threshold, then set our\n// ‘will_rain’ variable to true.\nvar will_rain = false;\nfor (var i = 0; i < forecasts.length; i++) {\n    var pop = forecasts[i].pop;\n    if (pop > 20) {\n        will_rain = true;\n    }\n}\n\n\nreturn {payload:will_rain}\n//return {payload:msg.payload.forecasts.class.wdir};”,”outputs”:1,”noerr”:0,”x”:461,”y”:167,”wires”:[[“b3b333f1.1a8c78”]]},{“id”:”911b2f82.7fee4″,”type”:”change”,”z”:”bace1fc1.26f648″,”name”:”Change to Red”,”rules”:[{“t”:”set”,”p”:”payload”,”pt”:”msg”,”to”:”#FF0000″,”tot”:”str”}],”action”:””,”property”:””,”from”:””,”to”:””,”reg”:false,”x”:781,”y”:247,”wires”:[[]]},{“id”:”c6d80895.0c8a48″,”type”:”inject”,”z”:”bace1fc1.26f648″,”name”:”Will Rain”,”topic”:””,”payload”:”true”,”payloadType”:”bool”,”repeat”:””,”crontab”:””,”once”:false,”x”:341,”y”:347,”wires”:[[“b3b333f1.1a8c78”]]},{“id”:”d66f08a1.57d058″,”type”:”inject”,”z”:”bace1fc1.26f648″,”name”:”Won’t Rain”,”topic”:””,”payload”:”false”,”payloadType”:”bool”,”repeat”:””,”crontab”:””,”once”:false,”x”:341,”y”:387,”wires”:[[“b3b333f1.1a8c78”]]}]

[in the function node, there should be a ; at the end of the return line, and delete the commented out next line:

return {payload:will_rain}

//return {payload:msg.payload.forecasts.class.wdir};]

  • Click to drop it in the flow.
  • Connect the left side of the “Get Probability …” node to the right side of your “http request” node.
  • Connect the right hand sides of the Change to Red and Change to Green nodes to the blue IBM IOT output node.
  • Click Deploy.
  • Test by clicking the “Will Rain” and “Won’t Rain” inject nodes.

Step 13: Tweaking the settings

  • You can change the colours by editing the “Change” nodes.
  • You can change the Probability of Precipitation setting by double clicking the “Get Probability …” function, and changing the 20 in the “if (pop > 20) {“ line.
  • For more information about the Probability of Precipitation see http://help.wunderground.com/knowledgebase/articles/129082-what-is-pop

Step 14: Connect to your Umbrella Stand / Coat Rack

  • Determine where you want the Neopixels to be.
  • Ensure you can plug the WeMos micro USB into an electricity supply.
  • The flexible Neopixel strips come with sticky-back plastic on them. Peel off the cover and stick to the coat rack.
  • Never get caught without your umbrella or coat again!

Green lights mean no rain

Stand Will Not Rain

Red lights mean rain

Stand Will Rain

 

Acknowledgements

A huge thanks to IBM’s Andy Stanford-Clark for his patience as he answered all my “how do I …?” queries while I was learning how to connect a WeMos into Bluemix. He also wrote the Arduino sketch.