Example of Integrating NetPing Devices with a Service xively.com

  • Published In: Tutorial
  • Created Date: 2015-11-05
  • Hits: 1092

As a majority of projects of the Internet of things concept (Internet of Things, IoT) or cloud services, the service xively.com:

  • Provides a platform (backend) for integration with other devices: a service is not connected to any certain hardware platform, instead it uses a standard HTTP API mechanism – this allows easy integrating of any device;
  • Can serve a platform for developing own applications: in the section for developers there are ready libraries for writing applications, including mobile ones, on different programming languages for different hardware platforms;
  • Offers a possibility for automation – triggers: a service allows interacting between different devices through the mechanism of HTTP POST requests

The details can be learnt at the website of the project in the section for developers.

This article describes an organisation of monitoring sensors on the example of integrating NetPing devices with the service. For the integration, REST API is used, which represents xively.com and URL-encoded commands, which are supported by NetPing devices. The integration is implemented on the basis of PowerShell script.

A script is executed according to the schedule of Windows planner in PowerShell environment. During the first step, the script gathers data from a NetPing temperature and humidity sensor. During the second step, the data are sent to the server of xively.com.

Requirements for Implementing Integration of NetPing Device with the Cloud Service Xively

To implement a represented solution, the next equipment will be necessary:

If NetPing equipment uses a router with the NAT and/or Firewall function to access the Internet network, and it is planned to execute PowerShell script on a PC, located outside a local network, there is a need to allow incoming packages via HTTP protocol to NetPing device to access it from the Internet. For additional information see the documentation for the router equipment or consult a system administrator.

If necessary, a NetPing device can be connected to a router using Wi-Fi wireless technology by an adapter VAP11N. An adapter is purchased separately. For additional information kindly see a user guide for this adapter.

Configuring UniPing server solution v4/SMS

Put a device UniPing server solution v4/SMS into working condition, plug in temperature and humidity sensors, connect a device to the Internet network – everything according to the corresponding sections of the user guide.

It is possible to get a path to a NetPing device via HTTP protocol in the Internet network using the next parameters:

  • IP address/host: xively.netping.ru
  • TCP-port: 8080

Then it is possible to access plugged in temperature sensors via the next URL-addresses:

  • http://xively.netping.ru:8080/thermo.cgi?t1
  • http://xively.netping.ru:8080/thermo.cgi?t2
  • http://xively.netping.ru:8080/thermo.cgi?t2

It is possible to get an access to a connected humidity sensor via the next URL-address:

  • http://xively.netping.ru:8080/relhum.cgi?h1

Everything is done according to documentation for URL-encoded commands.

Example of the PowerShell Script

A script takes readings from sensors of a UniPing server solution v4/SMS device and sends them to the cloud service xively.com.

#UniPing server solution v4/SMS PowerShell Script for xively.com service
#It is possible to check operation on the address https://personal.xively.com/feeds/623518124

#URLs of sensors and login/password from a NetPing device
$user = "user"
$pass= "password"
$uri1 = "http://np.tst.netping.ru:8080/thermo.cgi?t1"
$uri2 = "http://np.tst.netping.ru:8080/thermo.cgi?t2"
$uri3 = "http://np.tst.netping.ru:8080/thermo.cgi?t3"
$uri4 = "http://np.tst.netping.ru:8080/relhum.cgi?h1"

$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)

$Temp1Reqest = Invoke-RestMethod -Method Get -Uri $uri1 -Credential $cred
if ($Temp1Reqest.ToLower().Contains("ok")){
$temperature1=[convert]::ToInt32($Temp1Reqest.Split(",").Trim("")[1])
}
else {Write-Host "Sensor Temp1 error!"}

$Temp2Reqest = Invoke-RestMethod -Method Get -Uri $uri2 -Credential $cred
if ($Temp2Reqest.ToLower().Contains("ok")){
$temperature2=[convert]::ToInt32($Temp2Reqest.Split(",").Trim("")[1])
}
else {Write-Host "Sensor Temp2 error!"}

$Temp3Reqest = Invoke-RestMethod -Method Get -Uri $uri3 -Credential $cred
if ($Temp3Reqest.ToLower().Contains("ok")){
$temperature3=[convert]::ToInt32($Temp3Reqest.Split(",").Trim("")[1])
}
else {Write-Host "Sensor Temp3 error!"}

$Humid1Reqest = Invoke-RestMethod -Method Get -Uri $uri4 -Credential $cred
if ($Humid1Reqest.ToLower().Contains("ok")){
$humidity1=[convert]::ToInt32($Humid1Reqest.Split(",").Trim("")[1])
}
else {Write-Host "Sensor Humid1 error!"}


#Account details xively.com
#URL for monitoring:https://personal.xively.com/feeds/623518124

$apiKey = "your_API_key_here"
$feed_url = "https://api.xively.com/v2/feeds/your_feed_here"

$body = @{
version = "1.0.0"
datastreams = @(@{
id = "sensor1"
current_value = $temperature1
}
@{
id = "sensor2"
current_value = $temperature2
}
@{
id = "sensor3"
current_value = $temperature3
}
@{
id = "sensor4"
current_value = $humidity1
}
)
}

Invoke-RestMethod -Method Put -Uri $feed_url -Body (ConvertTo-Json $body) -Headers @{ "X-ApiKey" = $apiKey }

At the beginning of the PowerShell script write a username and password from your NetPing device:

  • $user ="user"
  • $pass="password"

Configuring the Service xively.com

To start working with the service xively.com, there is a need to go through registration:

After a registration, there is a need to add a new device using the button «Add Device»:

Button to add a new device to xively.com

Fill in the fields «DeviceName» and «DeviceDescription» to make further working with a device and its identification more convenient. Click «Add Device».

Adding a new device to xively.com

Proceed with adding channels (Channels) to display readings from the sensors, connected to a NetPing device– «Add Channel».

Button to add a channel to xively.com

Add two temperature sensors and a humidity sensor. «ChannelID» is an important field. It is this field, which is used by the service to identify a data channel. The rest fields are optional:

Adding a temperature sensor channel 1 to xively.com

Adding the channel of the temperature sensor 2 in the service xively.com

Adding a humidity sensor channel to xively.com

The last version of API service by xively.com eliminates a necessity to add sensors manually. All sensors will be created automatically on the first successful script request to the service. A description and other specifications of sensors in a web interface of the service xively.com can be added as an option.

Afterwards there is a need to add data for working with a registered account xively.com into the PowerShell script according to the screenshot below:

Changes to the PowerShell script to work with your registered xively.com account

Change the data in the next rows:

  • $apiKey= "your_API_key_here"
  • $feed_url= "https://api.xively.com/v2/feeds/your_feed_here"

Afterwards configure automatic start of the PowerShell script. A recommended interval to run it is 5 minutes. To configure it properly, you can use the next article at the Microsoft website or a similar one from other sources.

Triggers

One of the interesting auxiliary options of the service is triggers, particularly a possibility to send any HTTP POST request on a specified event at a sensor. Let us configure an example of a trigger, which will fire when a temperature exceeds «temp_sensor1» above 30 degrees of Celcius. For example, on such event we will switch on a relay Relay1 of a NetPing device, and switch it off when a temperature decreases. Any cooling device can be connected to a relay.

The section «Triggers». Add a new trigger – «Add Trigger»:

Button for adding a new trigger in the service xively.com

Select temp_sensor1:

Adding a thermal sensor to the xively.com service trigger settings

And specify conditions:

Configuring trigger conditions in xively.com

For a condition > 30 a URL-encoded command to switch on a relay will be executed:

  • http://user:password@xively.netping.ru:8080/relay.cgi?rn=1

where:

user – is a login to access a NetPing device;
password – is a password to access a NetPing device;
n – is a serial number of a relay, written after the character «?r» (there is a need to indicate 1)

Set up a trigger on xively.com

The second trigger is a added similarly, but with another condition and action:

Configuring the trigger in xively.com

For a condition <= 30 a URL-encoded command for switching off a relay will be executed:

  • http://user:password@xively.netping.ru:8080/relay.cgi?rn=0

where:

user – is a login to access a NetPing device;
password – is a password to access a NetPing device;
n – is a serial number of a relay, written after the character «?r» (there is a need to indicate 1)

It is possible to find more detailed information on other URL-encoded commands in the documentation.

Result

Therefore, if everything has been configured correctly, the script would take readings from temperature sensors, humidity sensor and send them to the server of xively.com each 5 minutes. You can monitor change in data in the form of graphs in the control panel at xively.com, and use the incredibly rich API of xively.com to implement any own projects. In addition, if specified events took place, triggers would run corresponding HTTP POST requests.


comments powered by Disqus