Convert post request from sensor to openHAB compatible

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:<write_api_key>&field1=123

But that means I need something to listen on a port, extract the data, and format it to send it on to the REST api of openHAB.

Do you have any recommendations for this? I am thinking about using PHP with its curl library.

I’ve searched and found some information on how to go to ThingSpeak from openHAB, but not too much on accepting this style of input.

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.

For some basic info on Node.js see this.

Then, to run it as a service, see this.

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: '',
  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.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:

// choose an unused port for this use:

1 Like