Iot – Device API Integration with Rest Slack, Intel Edison and Node.js

ByAlexandre Brandao Lustosa in

To developers, IoT can seem a complex subject, with lots of difficulties to implement, especially when it addresses languages like C/C++, Assembler and integration with high-level systems such as REST APIs and cloud services.

In this article we will work on a Stack to troubleshoot devices integrations with high level systems in a simple, effective and productive way.

Node.js – Javascript on Devices?

Yes, that’s right! How about developing integrations with sensors, LED, serial communication systems, I2Sim, I2C, USB, Relay, Infrared, Ethernet for Embeed Devices simply and productively?

Use Node.js

This way, you can develop your Rest API, one Ortal p data visualization, backend systems and also Embeeded aplication all with one programming language, which makes it easy.You can share, reuse and port code between platforms, not to mention that in this way you do not need a giant multidisciplinary team with differnet programming languages skills.

Sounds good right?

Solution Stack and Tools

In this article we will create a temperature monitoring system where the readings obtained by the sensor, installed on Intel Edison, will be published on Slack.Slack is a messaging tool, has a rich Web interface and also desktop and mobile versions. It is a great solution for reporting and alerts.

For this proposal to become reality we need some tools, here is the proposed stack for the integration solution and implementation of this example.

1
Data flow

Intel Edison Board for IoT

Powerful development platform for embedded systems based on and fully compatible with Arduino. It supports not only Node.js but also C/C++, Java, Python, and others.

  • Dual-core Intel® Atom ™ processor at 500 MHz
  • 1 GB DDR3 RAM, 4 GB eMMC flash
  • 40 multiplexed GPIO interfaces
  • Bluetooth* 4.0, Wi-Fi*
  • Yocto Project*, Brillo*
  • Arduino* compatible
  • Open-source software development enviroment
  • C/C++, Python*, Node.js, HTML5, JavaScript*

Intel Edison ofical references: https://software.intel.com/en-us/iot/hardware/edison

Intel Edison Chip
Intel Edison Chip
Intel Edison Breakout Board
Intel Edison Breakout Board
Intel Edison Mini Breakout Board
Intel Edison Mini Breakout Board
Grove Temperature Sensor
Grove Temperature Sensor
Intel Edison with Grove Kit
Intel Edison with Grove Kit
Intel Edison Architecture Diagram
Intel Edison Architecture Diagram

Intel XDK Development IDE

Intel provides a powerful IDE for development of mobile and IoT solutions: Intel XDK, which can be downloaded from at https://software.intel.com/en-us/intel-xdk. It is also an excellent tool for those who want to develop cross-platform mobile applications.

Configuring a new Slack Rest API Integration Url

To perform Slack notifications we need to create a channel or use an existing channel. After, have in the settings an integration url that will point all received data to the desired channel. Just follow the steps below:

1. Click New channel

8

2. Set a name and a description for the new channel and click on “Create Channel”

9

3. That’s it, your new channel is created

4. Now let’s define a new integration for the channel. Click “Manager” as indicated below.

10

5. Add a new setting

11

6. Select channel and confirm the creation of the new channel

12

7. Take notes of the new WebHook url that was generated, we need to display it in the code to send the data via http POST to the correct channel.

13

8. Okay, we’ve set up everything at slack. 🙂 See the indication of success in the settings of the notification Url in the message shown on the channel.

14

Hardware Configurations

For the code to work properly it is necessary to instal correctly the Intel XDK, to have internet acess for the Intel Edison (via Wi-Fi preferably), the Grove Kit temperature sensor needs to be connected to the analog port A0, the LED for processing indication has to be connected to the GPIO2, the Intel Edison must be properly connected to the development station and integrated with the IDE Intel XDK. For questions on how to set up Intel Edison and Intel XDK access these links:

Successfull configuration on Intel Edison Configuration Tool
Successfull configuration on Intel Edison Configuration Tool

Download code and deploy to Intel Edison

Now, with all properly set, download the code from GitHub repository.

Open the IDE Intel XDK and then the project through the file IoTTempMonitor.xdk.

Open the file main.js and replace the url webhook on line 113 by the set url as we showed earlier and save the file.

Check the connections to the Intel Edison Breakout Board and click the Upload button as shown below:

16

After uploading, the application will go into processing and will present readings and messages indicating that the POST process for Slack was successfully made.

17
Application running on Intel Edison

We can open Slack now, whether on web, desktop or mobile version, and display the notification message in the Chanel.

Notification message with the data obtained from the temperature sensor
Notification message with the data obtained from the temperature sensor

If your application does not automatically boot you can access Intel Edison’s terminal and make the startup manually. Access Intel Edison by serial terminal and perform the following steps:

19

The code!

The main code is in main.js file that is fully commented to facilitate the comprehension and maintenance of the application behavior. But first of all, to assess the data flow diagram to facilitate comprehension of the responsibility of each method and how the application behaves.

Operating with infinite loop workflow and execution break in every 4 seconds
Operating with infinite loop workflow and execution break in every 4 seconds
/*
	    Iot Intel Edison Api REST Slack Integration
	    by Alexandre Brandão Lustosa
	    Github: https://github.com/alexandrebl
	    Twitter: https://twitter.com/abrandaolustosa
	    LinkedIn: https://www.linkedin.com/in/abrandaol
	    E-mail: alexandre.brandao.lustosa@gmail.com
	            alexandre@insidesis.com
	*/
	

	//Load mraa eletronic sensor library
	var mraa = require("mraa");
	//Load http request library
	var request = require('request');
	

	//Sensor variable
	var tempSensor = new mraa.Aio(0);
	//Led process indicator
	var ledProcess = new mraa.Gpio(2);
	

	//Initialize application
	main();
	

	//Main function
	function main(){
	    //Print message
	    console.log("Config");
	    //Setup configurations
	    config();
	    
	    //Set loop process each 4 seconds
	    setInterval(function () {
	        //Set led process indicator on
	        ledProcessOn();
	        //Read Sensor
	        var data = readTempSensor();
	        //Post data on Slack
	        postOnSlack(data);
	        //Set led process indicator on
	        ledProcessOff();
	    }, 4000);
	}
	

	//Setup configuration
	function config(){
	    //Set pin direction
	    ledProcess.dir(mraa.DIR_OUT);
	}
	

	//Read temperature sensor
	function readTempSensor() {          
	    //Read sensor
	    var a = tempSensor.read();
	    
	    //Set resistênce value
	    var resistance = (1023 - a) * 10000 / a;
	    //Convert temperature to celsius according mraa datasheet
	    var celsius_temperature = 1 / (Math.log(resistance / 10000) / 3975 + 1 / 298.15) - 273.15;
	    //Convert temperature to fahrenheit
	    var fahrenheit_temperature = (celsius_temperature * (9 / 5)) + 32;
	    
	    //Print message
	    console.log("Fahrenheit Temperature: " + fahrenheit_temperature);
	    //Print message
	    console.log("Celcius Temperature: " + celsius_temperature);  
	    
	    //Set response message
	    var data = formatData(fahrenheit_temperature, celsius_temperature);
	    
	    //Return
	    return data;
	}
	

	//Set led process indicator on
	function ledProcessOn(){
	    //Write signal to up
	    ledProcess.write(1);
	}
	

	//Set led process indicator off
	function ledProcessOff(){
	    //Write signal to down
	    ledProcess.write(0);
	}
	

	//Format message
	function formatData(fahrenheit, celsius){
	    //Set message
	    var msg = "Fahrenheit: " + fahrenheit.toFixed(2) + "°F / Celsius: " + celsius.toFixed(2) + "°C";
	    
	    //Set data
	    var data = {
	        "text": "Actual local temperature:",
	        "attachments": [
	            {
	                "color": "#0066ff",
	                "text": msg,
	                "footer": "Slack API IoT Integration with Intel Edison",
	                "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
	            }
	        ]
	    };
	   
	    //Return
	    return data;
	}
	

	//Post data on Slack  WebHoook Integration
	function postOnSlack(data){
	    //Set options and message
	    var options = {
	        //Webhook uri, replace by your uri Slack web hook integration
	        uri: 'https://hooks.slack.com/services/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',
	        method: 'POST', //Request type
	        json: data      //Data
	    };
	

	    //Do post request
	    request(options, function (error, response, body) {
	        //Verify response
	        if (!error && response.statusCode == 200) {
	            //Print message
	            console.log("Success");
	        }else{
	            //Print message
	            console.log(error);
	        }
	    });
	}

This source code and other examples of systems and libraries for IoT are available for download at GibHub: https://github.com/alexandrebl

Conclusion

There is nothing better than applying our knowledge in an emerging area that will increasingly impact people’s lives. Use your ideas, solve problems and reduce the complexity of their implementation, use the Intel Edison with Node.js, cause impact, be an Intel Maker and change the world around you.

Leave a comment! 0

Your e-mail will not be visible. Mandatory fields are shown with *

Commenting as Anonymous

read more
This project is sponsored by these companies:
Hosted by: