Vitaliy Rudnytskiy [SAP]Craig Cmehil
Published © Apache-2.0

Weather Station Data From Tessel 2 via HTTP

Sending environmental data from Tessel 2 to SAP Cloud Platform IoT Service for Neo using HTTP protocol.

EasyProtip30 minutes309
Weather Station Data From Tessel 2 via HTTP

Things used in this project

Hardware components

Tessel 2 (main module)
Tessel 2 (main module)
×1
Climate Module for Tessel
Climate Module for Tessel
×1

Software apps and online services

SAP Cloud Platform IoT for the Neo environment
SAP Cloud Platform IoT for the Neo environment

Story

Read more

Code

scpiot_neo_tessel2_http.js

JavaScript
t2 run scpiot_neo_tessel2_http.js
var accountOwner = 'p1940048985'; // can be as well like 'p1940048985', 's000338523'
var messageTypeID = 'c6a49b942355d081949e';
var deviceId = 'bbf4a02f-45ad-4cc2-af90-18b68f7ce4ec';
var authToken = 'b3f90838e0c515e6ac71e9844af6e';
var portIoT = 443;
var hostIoT = 'iotmms'+accountOwner+'trial.hanatrial.ondemand.com';
var pathIoT = '/com.sap.iotservices.mms/v1/api/http/data/';
var authStrIoT = 'Bearer ' + authToken;

var tessel = require('tessel');
var climatelib = require('climate-si7020');
var climate = climatelib.use(tessel.port['A']);

climate.on('ready', function () {
	console.log('Connected to si7020');

	// Loop forever - every 5 secs
	setImmediate(function loop() {
		climate.readTemperature('c', function (err, temp) {
			climate.readHumidity(function (err, humid) {
				console.log('Degrees:', temp.toFixed(4) + 'C', 'Humidity:', humid.toFixed(4) + '%RH');
				updateIoT(temp.toFixed(4), humid.toFixed(4));
				setTimeout(loop, 5000);
			});
		});
	});
});

climate.on('error', function (err) {
	console.log('error connecting module', err);
});

function updateIoT(lv_temperature, lv_humidity) {
	var http = require('https');
	var options = {
		host : hostIoT,
		port : portIoT,
		path : pathIoT + deviceId,
		agent : false,
		headers : {
			'Authorization' : authStrIoT,
			'Content-Type' : 'application/json;charset=utf-8'
		},
		method : 'POST',
	};
	options.agent = new http.Agent(options);
	callback = function (response) {
		var body = '';
		response.on('data', function (data) {
			body += data;
		});
		response.on('end', function () {
			console.log("END:", response.statusCode, JSON.parse(body).msg);
		});
		response.on('error', function (e) {
			console.error(e);
		});
	}
	var req = http.request(options, callback);
	req.on('error', function (e) {
		console.error(e);
	});
	date = new Date();

	req.shouldKeepAlive = false;
	var jsonData = {
		"mode" : "sync",
		"messageType" : messageTypeID,
		"messages" : [{
				"temperature" : lv_temperature,
				"humidity" : lv_humidity,
				"timestamp" : date
			}
		]
	};

	var strData = JSON.stringify(jsonData);
	console.log("POST jsonData:" + strData);
	req.write(strData);
	req.end();
}

Credits

Vitaliy Rudnytskiy [SAP]

Vitaliy Rudnytskiy [SAP]

1 project • 4 followers
SAP Developer Relations
Craig Cmehil

Craig Cmehil

1 project • 5 followers
A developer, writer, photographer, talker...

Comments

Add projectSign up / Login