Communication between Openhabian (Raspberry Pi 4B) and Arduino Mega 2560 R3 via USB serial

Hello friends,

i try with a string transmitted serially via USB for example “Light_GaesteZ_:OFF;Light_SleepZ_General_:OFF;Light_SleepZ_Cabinet_:OFF”.
to report the status of the light switches.

For example, I have the switch item:
switch light_guestroom_general “light guestroom”

How can I read the feedback from the Arduino and change the switch status e.g. at “Licht_GaesteZ_:ON” light symbol is on and at “Licht_GaesteZ_:OFF” light symbol is off.

Thanks in advance for your support!

Greeting
Tobi

Translated with www.DeepL.com/Translator (free version)

Have you looked at the v1 serial binding?

Hello Bruce,

hello bruce, thank you for your quick feedback! :slight_smile:

yes, i downloaded that and released the serial ports in the files of openhab.
String Arduino “Arduino [%s]” (arduino) {serial="/dev/ttyACM0"}

The response of e.g. “Licht_GaesteZ_” will be displayed despite the error:
“2019-12-28 21:32:02.381 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘’ to a state type which item ‘Light_Guestroom_General’ accepts: [OnOffType, UnDefType].”.
is shown as “ON” or “OFF”, but not, if I query the next switch e.g. “Light_Bedroom_General_”, then only the first one is recognized, but both are shown in the .log with the same error:

"2019-12-28 21:32:02.381 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘’ to a state type which item ‘Light_Bedroom_General’ accepts: [OnOffType, UnDefType].

2019-12-28 21:32:02.394 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘’ to a state type which item ‘Light_Bedroom_General’ accepts: [OnOffType, UnDefType].

rule “Read Arduino”
when

Item Arduino received update

then

var String ArduinoUpdate = Arduino.state.toString.trim

var int LightGuestroomGeneralStartAt = ArduinoUpdate.indexOf(“Light_GaesteZ_:”) + “Light_GaesteZ_:”.length
var String LightGuestroomGeneral = ArduinoUpdate.mid(LightGuestroomGeneralStart On, ArduinoUpdate.indexOf(’;’)-LightGuestroomGeneralStart On)

Licht_Gaestezimmer_Allgemein.postUpdate(LichtGaestezimmerAllgemein)

var int LightSleeping_roomGeneralStartAt = ArduinoUpdate.indexOf(“Light_Sleeping_room_General_:”) + “Light_Sleeping_room_General_:”.length
var String LightBedroomGeneral = ArduinoUpdate.mid(LightBedroomGeneralStart On, ArduinoUpdate.indexOf(’;’)-LightBedroomGeneralStart On)

Light_Bedroom_General.postUpdate(LightBedroom_General)

end

If you have control of this, why not send your string as JSON, as there are many useful parsing tools already in existence.

You cannot just declare an int type and assign a string value to it. You need to parse strings into numbers.

Hey rossko57,

thank you very much for your reply!

I just got your note.
“You cannot just declare an int type and assign a string value to it. You need to parse strings into numbers.”

and already looking for the right solution.
Thanks for shortening the search and maybe the link to the solution!

I read your link and try it! :slight_smile:
I will give feedback again!

Greetings
Tobi

Hello again :slight_smile:

i’ve been busy with the REGEX variant for hours now and have been able to filter out the individual words, but i can manage to convert the individual words into the corresponding commands.

For example I filtered out the “;” with the following:
[\w]+[\w]

Light_GaesteZ_:OFF;Light_SleepZ_General_:OFF;Light_SleepZ_Cabinet_:OFF;Light_WorkZ_:OFF;Light_LivingZ_General_:OFF;Light_LivingZ_LED_:ON;Light_kitchen_:OFF;Light_balcony_:OFF;Light_corridor_:OFF;Light_side_corridor_:OFF;Light_storage_room_:OFF;Light_bathroom_:OFF;Light_guestsWC_:OFF;

Now I don’t know how to proceed with this or how I could assign this to the individual items.

I would like to ask again if I really have to convert the words in the string into an integer to process them further or if I can filter out the words with “REGEX” as shown above and process them further.

Thanks again for your support!

Best regards
Tobi

Hello, friends,

…I still can’t get any further, and I’ve spent many hours…

One looked very simple and feasible, but again I can’t get it to work with more than one query…

rule “Arduino on update”

when 

    Item Arduino received update

then

 //   var String ArduinoUpdate = Arduino.state.toString.trim 

    ArduinoUpdate = ArduinoUpdate + Arduino.state.toString.trim

    if (ArduinoUpdate.contains(";")) {

        if (ArduinoUpdate.contains("Licht_GaesteZ")) {

            postUpdate(Licht_Gaestezimmer_Allgemein, if(ArduinoUpdate.contains("ON")) ON else OFF)
        }
        if (ArduinoUpdate.contains("Licht_SchlafZ_Allgemein")) {

            postUpdate(Licht_Schlafzimmer_Allgemein, if(ArduinoUpdate.contains("ON")) ON else OFF)
        }
        ArduinoUpdate = ""
    }

end

With my “old” rule I have now read out the “0” or “1” and must now link them to the status of the light switches.

Do I have to put the “0” and “1” from the signal contact of the light relay on a “contact” item and then link it to the “switch” item?

rule “Arduino Lesen”

when

Item Arduino received update

then

var String ArduinoUpdate = Arduino.state.toString.trim

var int LichtGaestezimmerAllgemeinStartetBei = ArduinoUpdate.indexOf(“Licht_GaesteZ,”) + “Licht_GaesteZ,”.length

var String LichtGaestezimmerAllgemein = ArduinoUpdate.mid(LichtGaestezimmerAllgemeinStartetBei, ArduinoUpdate.indexOf(’;’)-LichtGaestezimmerAllgemeinStartetBei)

//var Double LichtGaestezimmerAllgemeinAlsDouble = new Double(LichtGaestezimmerAllgemein)

Licht_Gaestezimmer_Allgemein.postUpdate(LichtGaestezimmerAllgemein)

end