Whel, I have made some steps. With a tutorial from here I can now send 2 button presses and switch 2 led’s from openHAB basicUI and from physical buttons.
items:
String ArduinoData "ArduinoData [%s]" (arduino) {serial="COM5@250000"}
Number ArduinoSW1 "SW1 [%.1f]" (arduino)
Number ArduinoSW2 "SW2 [%.1f]" (arduino)
Number ArduinoSW3 "SW3 [%.1f]" (arduino)
Switch led1 "led1" (arduino)
Switch led2 "led2" (arduino)
readrules:
rule "ArduinoRead"
when
Item ArduinoData received update
then
var String ArduinoDataUpdate = ArduinoData.state.toString.trim
var int S1StartsOn = ArduinoDataUpdate.indexOf("S1:") + "S1:".length
var String S1 = ArduinoDataUpdate.mid(S1StartsOn, ArduinoDataUpdate.indexOf('_')-S1StartsOn)
var Double S1Double = new Double(S1)
ArduinoSW1.postUpdate(S1Double)
var int S2StartsOn = ArduinoDataUpdate.indexOf("S2:") + "S2:".length
var String S2 = ArduinoDataUpdate.mid(S2StartsOn, ArduinoDataUpdate.indexOf(';')-S2StartsOn)
var Double S2Double = new Double(S2)
ArduinoSW2.postUpdate(S2Double)
end
arduino wride:
rule "arduino2"
when
Item led1 changed
then
var state = led1.state as OnOffType
sendCommand (ArduinoData , "led1=" + state)
end
rule "arduino1"
when
Item led2 changed
then
var state = led2.state as OnOffType
sendCommand (ArduinoData , "led2=" + state)
end
rule "arduino3"
when
Item ArduinoSW1 changed from 0 to 1
then
sendCommand (ArduinoData , "led1=ON")
end
rule "arduino4"
when
Item ArduinoSW1 changed from 1 to 0
then
sendCommand (ArduinoData , "led1=OFF")
end
rule "arduino5"
when
Item ArduinoSW2 changed from 0 to 1
then
sendCommand (ArduinoData , "led2=ON")
end
rule "arduino6"
when
Item ArduinoSW2 changed from 1 to 0
then
sendCommand (ArduinoData , "led2=OFF")
end
Arduino code:
#define led_1 2
#define led_2 3
#define sw_1 4
#define sw_2 5
#define sw_3 6
String inputString = "";
bool stringComplete = false;
bool S_1 = 0;
bool S_2 = 0;
bool S_3 = 0;
bool H_1 = 0;
bool H_2 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(250000);
pinMode(sw_1, INPUT_PULLUP);
pinMode(sw_2, INPUT_PULLUP);
pinMode(sw_3, INPUT_PULLUP);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
digitalWrite(led_1, HIGH);
digitalWrite(led_2, HIGH);
delay(1000);
digitalWrite(led_1, LOW);
digitalWrite(led_2, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
knoppen_lezen();
zend();
serialEvent();
outputs();
delay(150);
}
void knoppen_lezen() {
if (digitalRead(sw_1) == LOW) {
S_1 = 1;
}
else
{
S_1 = 0;
}
//------------------------------------
if (digitalRead(sw_2) == LOW) {
S_2 = 1;
}
else
{
S_2 = 0;
}
//-----------------------------------
if (digitalRead(sw_3) == LOW) {
S_3 = 1;
}
else
{
S_3 = 0;
}
}
void zend() {
Serial.print("S1:");
Serial.print(S_1);
Serial.print("_S2:");
Serial.print(S_2);
Serial.println(";");
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
void outputs() {
if (inputString == "led1=ON") {
digitalWrite(led_1, HIGH);
//Serial.println("led 1 aan");
}
else if (inputString == "led1=OFF") {
digitalWrite(led_1, LOW);
//Serial.println("led 1 uit");
}
else if (inputString == "led2=ON") {
digitalWrite(led_2, HIGH);
//Serial.println("led 2 aan");
}
else if (inputString == "led2=OFF") {
digitalWrite(led_2, LOW);
//Serial.println("led 2 uit");
}
Serial.println(inputString);
inputString = "";
stringComplete = false;
//delay(500);
}
But now I was attempting to add more than one variable from the Arduino to openHAB just by adding more switches like this.
Serial.print("S1:");
Serial.print(S_1);
Serial.print("_S2:");
Serial.print(S_2);
Serial.print("_S3:");
Serial.print(S_3);
Serial.println(";");
and
rule "ArduinoRead"
when
Item ArduinoData received update
then
var String ArduinoDataUpdate = ArduinoData.state.toString.trim
var int S1StartsOn = ArduinoDataUpdate.indexOf("S1:") + "S1:".length
var String S1 = ArduinoDataUpdate.mid(S1StartsOn, ArduinoDataUpdate.indexOf('_')-S1StartsOn)
var Double S1Double = new Double(S1)
ArduinoSW1.postUpdate(S1Double)
var int S2StartsOn = ArduinoDataUpdate.indexOf("S2:") + "S2:".length
var String S2 = ArduinoDataUpdate.mid(S2StartsOn, ArduinoDataUpdate.indexOf('_')-S2StartsOn)
var Double S2Double = new Double(S2)
ArduinoSW2.postUpdate(S2Double)
var int S3StartsOn = ArduinoDataUpdate.indexOf("S3:") + "S3:".length
var String S3 = ArduinoDataUpdate.mid(S3StartsOn, ArduinoDataUpdate.indexOf(';')-S3StartsOn)
var Double S3Double = new Double(S3)
ArduinoSW3.postUpdate(S3Double)
end
But I just get a lot of errors
What am I doing wrong? Can somebody point me in the right direction?
Thank you guys!