OpenHAB and Arduino COM port

Hello there!

I downloaded and installed OpenHAB 2.0 on windows 10. I also have Arduino connected on COM20, with this sketch:

String cmd = “”;

setup() {

// Open serial communications and wait for port to open:
Serial.begin(115200);

//Serial.println(“Initializing SERIAL…”);

while(!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

//Serial.println(“SERIAL initialized”);

delay(1000);

}

loop() {

while(Serial.available() > 0) {

// read the incoming byte:
incomingByte = Serial.read();

cmd += incomingByte;

delay(10);

}

if(cmd != “”) {

Serial.println(cmd);

cmd = "";

}

}

and all works fine from Arduino IDE monitor. I get all the sent commands printed on monitor’s screen.

I’m now learning OpenHAB, and I got some code from Internet (I also followed the tutorial on the site):

default.items:

Group All

String Arduino “Arduino” (All) {serial=“COM20”}

default.sitemap:

sitemap default label=“My first sitemap” {

Text item=Garage_Davide icon="garagedoor" label=""
Switch item=Garage_Davide_Button label="Garage" mappings=[ON="Go!"] icon="energy"

}

default.rules:

rule “Garage Davide”

when

Item Garage_Davide_Button received command

then

sendCommand (Arduino , “1”)

end

OK, it seems to work when I switch the button, but nothing is sent to COM20? This is a part of openhab.log:

[…]
2017-03-22 18:15:34.672 [INFO ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at ‘items/Garage_Davide_Button’ for the unknown item ‘Garage_Davide_Button’.

Thanks in advance!

Bye,

Davide

There’s the clue. Should you have defined a switch Item for that in your .items file? (the sitemap is just about displaying items)

Hi, thanks for your help. I’ll try to fix that, from the log it seems some POST request is received, so something is received when I switch the button. The sendCommand, then, should be fired…or not? I also was thinking that Arduino code is setting 115200 baud, but what about OpenHAB code? Should I set 115200 even there? But, how?

Yes, your UI (browser) tries to send/get updated info to/from OpenHab for the missing Item. OH cannot do that and reports an error.

If there is no Item to trigger the rule, there will be no sendCommand.

Seems like a good idea. Unless the bauds match, it will not work. OH cannot guess what you want to do.

you should use something like this:

String Arduino "Arduino" (All) {serial="COM20@115200"}

Many thanks to all for the help, now I got this working:

default.items:

Group All

String Arduino “Test” { serial=“COM20@115200” }
String Garage_Davide “Garage"
Switch Garage_Davide_Button { tcp=”>[ON:192.168.1.101:8080" }

default.sitemap:

sitemap default label=“My first sitemap” {

Text item=Garage_Davide icon="garagedoor" label=""
Switch item=Garage_Davide_Button label="Garage" mappings=[ON="Go!"] icon="energy"

}

default.rules:

rule “Garage Davide”

when

Item Garage_Davide_Button received command

then

sendCommand (Arduino , “1”)

logInfo(“Garage_Davide_Button”,“sendCommand”)

end

My Arduino got the command. Also I’ve noticed Arduino’s reply in events.log

2 Likes

Thanks!