I have a Purple Air sensor that I would like to have send its data to openHAB. The Purple Air will send data to a user specified http port using GET or POST requests with a format for ThingSpeak. A sample GET request looks like this:
Curl is for requesting web pages, not for accepting web pages. You have to set up a web server that looks like the ThingSpeak web server (i.e. it supports accepting a GET or a POST to update/json that accepts two URL parameters. I think a lot of people set up Node.js for something like this but there are literally dozens of options you can use.
In case it helps anyone else, I’ve put my Node.js code below. Node running this code sets up a port where the GET requests can come in (can be modified for POST if desired), picks out one of the key/value pairs, and does a POST request to openHAB.
Please note that I am a total beginner using this. There may be significant issues with the code below and there may be better ways to do this. Any suggestions for improvement or better methods are highly appreciated. So far it is working for me.
const express = require('express')
const http = require('http')
const app = express()
var options = {
// set the openHAB IP/hostname and port here
host: '192.168.86.212',
port: 8080,
// set the openHAB item here
path: '/rest/items/PurpleAir',
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Accept': 'application/json'
}
};
app.get('/', (req, res) => {
// loop through the keys and find the one of interest:
for (const key in req.query) {
// checking for the desired key in the GET request:
if (key == 'field1') {
console.log('Found Purple Air data key')
postReq = http.request(options, function(res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
postReq.on('error', (error) => {
console.error(error) });
// push out the value collected from the GET here to openHAB:
postReq.write(req.query[key]);
postReq.end();
}
}
res.send('Okay')
})
// choose an unused port for this use:
app.listen(8088)