Skip to main content

Communicate with cloud services from IoT device

Scenario:



  • Send sensor temperature to cloud through a gateway
  • If measured temperature is higher than 29ºC then switch warning light that is maroon lights switch on LED
  • If less than that, switch green LEDs
  • If no commands received from the cloud switch black LEDs
  • Show sensor temperature with silver color;
    • Number of tens in tens area i.e. if its 10 => 1 LED, if its 25 => 2 LEDs
    • First digit of the reading in units area i.e. 25 => 5 LEDs, 31 => 1 LED





Notes:


  • SenseHat has been used a sensor for temperature
  • RaspberryPI has been used as the gateway for the sensor
  • Bluemix is used for the back end cloud


Application architecture


Solution: Gateway workflow








Solution: Cloud flow








Node red export


Communicate with cloud services from IoT device-Gateway flow



Note: Copy below code in to import section in the NodeRed


[{"id":"f0c435a1.0388b8","type":"exec","z":"d94f3e5f.38adf","command":"vcgencmd","addpay":true,"append":"measure_temp","useSpawn":"","timer":"","name":"exec : vcgencmd","x":273.5000457763672,"y":81.5,"wires":[["12701f71.2a6d41"],[],[]]},{"id":"9f20a6ec.bef7f8","type":"inject","z":"d94f3e5f.38adf","name":"","topic":"","payload":"","payloadType":"str","repeat":"5","crontab":"","once":false,"x":103.00003051757812,"y":81,"wires":[["f0c435a1.0388b8"]]},{"id":"791c0367.b247bc","type":"debug","z":"d94f3e5f.38adf","name":"","active":false,"console":"false","complete":"payload","x":825.0000457763672,"y":80,"wires":[]},{"id":"12701f71.2a6d41","type":"function","z":"d94f3e5f.38adf","name":"format gateway temp","func":"var temp = msg.payload;\ntemp = temp.substr(5,4);\ntemp=parseFloat(temp);\nmsg.payload = {\"d\" : {\"temp\" : temp}};\nreturn msg;","outputs":1,"noerr":0,"x":502.00010681152344,"y":81,"wires":[["791c0367.b247bc","8594bd8f.f00c2"]]},{"id":"72172e2b.e1ab4","type":"rpi-sensehatsim in","z":"d94f3e5f.38adf","name":"EnvironmentEvents","motion":false,"env":true,"stick":false,"x":105.89997863769531,"y":244.0000991821289,"wires":[["35f58434.af8a5c"]]},{"id":"baf3ec66.613da","type":"debug","z":"d94f3e5f.38adf","name":"","active":false,"console":"false","complete":"false","x":825.9000091552734,"y":244.40003204345703,"wires":[]},{"id":"35f58434.af8a5c","type":"delay","z":"d94f3e5f.38adf","name":"Limit-5S","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":315.4999237060547,"y":188.4000244140625,"wires":[["1e3e28b3.d929d7"]]},{"id":"1e3e28b3.d929d7","type":"function","z":"d94f3e5f.38adf","name":"format sense temp","func":"var temp = msg.payload;\nglobal.set(\"currentTemp\",temp.temperature);\nmsg.payload = {\"d\":{\"temperature\":temp.temperature,\"humidity\":temp.humidity}};\nreturn msg;","outputs":1,"noerr":0,"x":476.1999969482422,"y":249.20001983642578,"wires":[["baf3ec66.613da","8594bd8f.f00c2"]]},{"id":"9d0bbd86.1ff6e","type":"comment","z":"d94f3e5f.38adf","name":"Get SenseHat events","info":"","x":111.89996337890625,"y":190.00000762939453,"wires":[]},{"id":"d9045dca.cd323","type":"comment","z":"d94f3e5f.38adf","name":"Get gateway events","info":"","x":120.90000915527344,"y":32,"wires":[]},{"id":"8594bd8f.f00c2","type":"wiotp out","z":"d94f3e5f.38adf","authType":"g","qs":"false","qsDeviceId":"","deviceKey":"9e5ca693.f5a6a8","deviceType":"SenseHAT","deviceId":"sena1b2c3d4","event":"status","format":"json","name":"SenseHat IoT Out","x":756.4999847412109,"y":165.59998321533203,"wires":[]},{"id":"9df7a310.90ff6","type":"rpi-sensehatsim out","z":"d94f3e5f.38adf","name":"Sense Hat LED Sim","x":802.4999542236328,"y":362.99999237060547,"wires":[]},{"id":"9dd85f9e.94598","type":"function","z":"d94f3e5f.38adf","name":"LED function","func":"//Declarations\nvar latestCloudCommand = msg.payload;\nvar currentSenseTemp = global.get(\"currentTemp\");\nvar screenDisplay = \"\";\nvar tens = [\"2,2,silver\", \"1,2,silver\", \"2,3,silver\", \"1,3,silver\",\n \"2,4,silver\", \"1,4,silver\", \"2,5,silver\", \"1,5,silver\",\n \"2,6,silver\", \"1,6,silver\", \"2,7,silver\", \"1,7,silver\"\n];\nvar units = [\"6,2,silver\", \"5,2,silver\", \"6,3,silver\", \"5,3,silver\",\n \"6,4,silver\", \"5,4,silver\", \"6,5,silver\", \"5,5,silver\",\n \"6,6,silver\", \"5,6,silver\", \"6,7,silver\", \"5,7,silver\"\n];\n\n//LED color conditions\nif (latestCloudCommand.screen == \"on\") {\n node.warn(\"On LED\");\n screenDisplay = \",,#800000\";\n}\nelse if (latestCloudCommand.screen == \"off\") {\n node.warn(\"Off LED\")\n screenDisplay = \",,green\";\n}\nelse if (latestCloudCommand.screen == \"no\") {\n node.error(\"NO Command\");\n screenDisplay = \",,black\";\n}\n\n//Temp units\nnode.warn(currentSenseTemp);\nif (currentSenseTemp > 0) {\n if (screenDisplay && screenDisplay.length !== 0) {\n screenDisplay += \",\";\n }\n\n var nTens = Math.floor(currentSenseTemp / 10);\n var nUnits = currentSenseTemp % 10;\n\n //Tens update \n if (nTens > 0) {\n for (var i = 0; i < nTens; i++) {\n screenDisplay += tens[i];\n if (i + 1 != nTens)\n screenDisplay += \",\";\n }\n }\n\n //Units update\n nUnits = parseInt(nUnits);\n if (nUnits > 0) {\n screenDisplay += \",\";\n for (var i = 0; i < nUnits; i++) {\n node.warn(\"Adding unit\" + i + \"Values >\" + units[i]);\n screenDisplay += units[i];\n if (i + 1 != nUnits)\n screenDisplay += \",\";\n }\n }\n}\n\n\n\nmsg.payload = screenDisplay;\nreturn msg;","outputs":1,"noerr":0,"x":453.40907287597656,"y":361.81819915771484,"wires":[["4ca157f7.2d90a8","9df7a310.90ff6"]]},{"id":"86d90529.c75a48","type":"comment","z":"d94f3e5f.38adf","name":"Update SenseHat based on cloud","info":"","x":146.0454559326172,"y":307.90909437699753,"wires":[]},{"id":"48b7ec8d.1f1594","type":"wiotp in","z":"d94f3e5f.38adf","authType":"g","deviceKey":"9e5ca693.f5a6a8","deviceType":"SenseHAT","deviceId":"sena1b2c3d4","command":"+","commandType":"d","name":"","x":90.04545593261719,"y":361.18182373046875,"wires":[["9dd85f9e.94598"]]},{"id":"4ca157f7.2d90a8","type":"debug","z":"d94f3e5f.38adf","name":"","active":true,"console":"false","complete":"false","x":586.7221374511719,"y":518.0000057220459,"wires":[]},{"id":"9e5ca693.f5a6a8","type":"wiotp-credentials","z":"","name":"a1b2c3d4","org":"crrzlc","devType":"RasPi","devId":"a1b2c3d4"}]

Communicate with cloud services from IoT device-Cloud flow


Note: Copy below code in to import section in the NodeRed


[{"id":"9daee5f8.607468","type":"function","z":"d5256d04.38dea","name":"Set IoT device command","func":"var temp = msg.payload.d.temperature;\nif(temp){\n //node.warn(temp);\n if(temp > 29){\n node.warn(\"Temp above 29\");\n //on command\n msg.payload = {\"screen\":\"on\"};\n }\n else{\n //off command\n msg.payload = {\"screen\":\"off\"};\n }\n}\n//else{\n // msg.payload = {\"screen\":\"no\"};\n//}\n\nreturn msg;","outputs":1,"noerr":0,"x":362.5,"y":753.3999633789062,"wires":[["e20f54cc.5fb228","83185fea.18ac5"]]},{"id":"e20f54cc.5fb228","type":"debug","z":"d5256d04.38dea","name":"","active":true,"console":"false","complete":"payload","x":654.5000152587891,"y":843.6000366210938,"wires":[]},{"id":"83185fea.18ac5","type":"ibmiot out","z":"d5256d04.38dea","authentication":"boundService","apiKey":"","outputType":"cmd","deviceId":"sena1b2c3d4","deviceType":"SenseHAT","eventCommandType":"temp","format":"json","data":"{}","qos":0,"name":"IBM IoT Out to SenseHat","service":"registered","x":697.5000152587891,"y":752.3999633789062,"wires":[]},{"id":"69ad7761.319c18","type":"ibmiot in","z":"d5256d04.38dea","authentication":"boundService","apiKey":"","inputType":"evt","deviceId":"sena1b2c3d4","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT In","service":"registered","allDevices":"","allApplications":"","allDeviceTypes":true,"allEvents":true,"allCommands":"","allFormats":"","qos":0,"x":102.5,"y":754.5999755859375,"wires":[["9daee5f8.607468"]]}]

Comments

Popular posts from this blog

Turn off/ Hide Details panel/ Information Panel on modern SharePoint lists

Not always we require to show changes done by other which is a mandatory feature in SharePoint online.  What is details pane (aka. Information Pane)? Detail pane/ Information shows information regarding the document if you selected a one or its showing recent changes within a list or library. Follow link to Microsoft documentation about details pane. Bad news: Until Microsoft listen to User Voice , there is no straightforward way to enable disable this even you don't want. Good news: We could write a SharePoint framework extension to hack styles until Microsoft give us a permanent solution. How? I found this sample project (Inject CSS into modern SharePoint pages with React) which could reuse to our purpose. Thanks to Hugo for saving my time.  Steps to awesomeness:  Clone the project Resolve dependencies >  npm i Bundle >  gulp bundle --ship Package >  gulp package-solution --ship Upload package into SharePoint App catalog  

📢 Update -Top 10 Microsoft Teams questions from customers around the world

Over the last 4 months, we’ve traveled to 9 countries on Microsoft Ignite | The Tour meeting and speaking with users like you about Microsoft Teams. The insights and feedback at every location has been invaluable — Thank you. During our travels, there have been 10 questions that have repeatedly surfaced. Below are all of the questions — including the answers too, of course!   Can you restrict access to a specific channel in a team? What are best practices for governance, specifically around team creation, naming, and retention? Can I add guests to my team? If so, how? How does external access/federation work? How is this different to guest access? What functionalities do I get in a hybrid environment? Is Teams available for on-prem environments? How do I configure Direct Routing in Microsoft Teams? Why should I move from Skype for Business to Teams? How do I make this transition? I have existing SharePoint sites that I would like to bring into Teams. Can I do that? I need t

Eliminate Duplicates in Microsoft Flow and Azure Logic Apps

There is no straight forward method or Microsoft Flow action  to eliminate values in a collection like Microsft Excel (as of March 2019). This little hack will help you to eliminate/ remove duplicates and create a collection of unique values. Sample scenario: Need to send daily summarized notification to assigned approved with pending approval items. Sample workflow: Initialize an array Iterate collection of data and append items that are not there in the filtered array Side note: This won't be an option to consider if you too concerned about the performance of the workflow but for the time being its an option to consider.