How to connect Interface to Server on nodejs?

I have added a tag, I have added slider. On my iPhone I can see the slider projected over the tag, and I can move it.

How do I pick up the slider value on the server side? Where does obj.write("led", sliderValue / 255); send it’s data to? I see a folder called HardwareInterface. But I can’t figure out what script to modify.

I run openhybrid on a raspberry pi with nodejs.

That’s a good question. The answer depends on what you want to do with the value. If you want to write a value to the GPIO pins of the PI you can use the raspberryPI hardware interface. You will only have to enable the interface by setting exports.enabled to true and edit the config.json file: id is the name of your HybridObject as it appears on the web configuration page, ioName is the name of the I/O point, direction, and edge correspond with the onOff libraries parameters. pin corresponds with the gpio parameter of onOff, it specifies the pin to use. onOff is the underlying library used to access the GPIO pins.

So let’s say to write a value to the I/O point digital of the led1 HybridObject in the example config.json you would have to call obj.write("digital", 0); in the script section of the led1 object’s index.html.

If you want to do something else entirely, you can create your own hardware interface. Let us know if you want to do that. It’s not yet documented how to do that but maybe you will figure it out by just looking at the code of the already implemented interfaces or you can just ask if you have questions.

1 Like

I actually do want to make my own hardware interface. I want a RPi + BrickPi to run connected Lego Motors. I know how to run motors from nodejs, that’s easy. I don’t know how to expose the motors to reality editor or connect the index.html interface of the hybrid object to the motor.

Does it have to do with this function?

exports.receive= function (objectExp, objectLookup, clear, developer, directoryName, callback){

    // here I want to add code that makes lego motors run. I just need to grab a value from the interface...

};

Running a Lego motor at 50% power is then as simple as:

var ev3 = require('ev3dev-lang');
var motor = new ev3.Motor('ttyAMA0:outA');
motor.dutyCycleSp = 50;
motor.command = 'run-forever';

First of all if you want to implement a new hardwareInterface you should checkout the beta_hardwareInterfaces branch or even better the Bugfix_reinit branch.

I will explain the procedure for the Bugfix_reinit branch because it will soon be merged into the beta_hardwareInterfaces branch and the procedure slightly changed. We are currently starting to work on a proper documentation, sorry that it’s not yet available… It’s all a work in progress…

Take a look at the emptyExample. It describes the functions you need to implement. Another important file is the HardwareInterfaces library. It defines several functions you will have to use:

  • addIO(objName, ioName, plugin, type): Adds an I/O point to the specified object. For plugin just use “default”, type is whatever you decide to call your HW interface it must be the name of the folder you create under hardwareInterfaces. Call this from within the exports.init() function
  • clearIO(type): call this after all your addIO() calls have finished. Call this from within the exports.init() function
  • writeIOToServer(objName, ioName, value, mode): Call this whenever you receive new values from your hardware. It writes the value of the specified I/O point to the server. This should be called from within the exports.receive() function of your hardware interface. For mode please read this post

Now the functions you need to implement:

  • exports.init(): this is called several times by the server, whenever it it necessary to reinitialize the hardwareInterface, for example when a target is added. Place your calls to addIO() and clearIO() in here. It is called for the first time before exports.receive() is called
  • exports.receive(): this is called ONCE by the server. Start your event loop here. This is where you get values from your hardware and pass them on to the server by writeIOToServer()
  • exports.send(objName, ioName, value, mode, type): Send data to your hardware from within this function. This is called whenever your hardware interface receives new data passed on from a link or the Reality Editor

You should look at the mpdClient hardwareInterface. I think it’s structure is the easiest to understand. I hope this helps to get you started…

1 Like

Thanks. The first test is running!

3 Likes