Englisch version   Deutsche Version


Version Modifications
1.0 (2020-05-15) Initial version

Motivation

The remote control for my TV is always hidden in a sneaky way. To find my smartphone is much easier. If necessary, I can call it, than it answers acoustically. Unfortunately there is no app for my TV, a Metz device, that sends suitable IR codes :-(. With this extension for the MIT App Inventor I was able to easily develop a suitable app myself.


Content

Download

Usage

Reference

Blocks

Error Codes

Example

Remote Control for Metz TV

Recording IR Codes

Tools

Download

The ZIP-Archiv UrsAI2IrXmitter for Download. The archive contains the source code, the compiled binary to upload to App Inventor and the sample application.

Usage

The extension was designed so that as little effort as possible is required in the AI2 blocks. In visual programming comprehensive logic in the blocks quickly leads to confusing applications. I wanted to avoid that.

The extension is a wrapper for the Android class ConsumerIrManager. More details can be found there.

With the extension, code packets can be sent single packets (transmit) or with automatic repetition. In the case of repetitions the same code packet can be sent multiple times (RepeatedTransmit) or, after a start packet, the subsequent packets can have a different code sequence (RepeatedAlternateTransmit). The latter function is mostly used for the On/Off sequence. It would otherwise be quite fatal if the device to be operated switched on and off in time with the repetitions. Repetitions can be limited or sent endlessly. In both cases, StopTransmit interrupts an ongoing transmission process.

The pauses between the starting package and the subsequent packets can be set with the property StartDelay. The interval between subsequent packets is determined by the property RepetitionDelay.

After a single packet has been transmitted, the PatternXmitted event is triggered. When the entire repetition sequence has been processed or canceled, the TransmitDone event is raised.

Both events are triggered also when single patterns are sent, but then they appear directly one after the other. That is not always desirable. A flashing symbol in the example app signals the transmission of a packet. With the PatternXmitted event , the color of the flashing area is switched, with TransmitDone it is switched to colorless again. In the case of a single packet dispatch, the signal area would practically never flash. If the property EventTransmitDoneDelay is set to a value greater than zero, the specified delay is inserted between the two events.

You can configure the extension to automatically react on pressing or releasing Buttons. The functions RegisterButtonForSinglePattern, RegisterButtonForRepeatedPattern and RegisterButtonForRepeatAltPattern can be used to register Button components to the specified functionality. At the TouchDown event of the button the specified function is triggered, TouchUp stopps repeated transmissions. This reduces the programming effort of a button to a single instruction. E.g.:

Die Extension lässt sich über eine Reihe von Eigenschaften konfigurieren. HasIrEmitter gibt an, ob  das Gerät überhaupt über einen IR-Sender verfügt. Ist das nicht der Fall, führen alle anderen Funktionen zu Fehlern. Über die Eigenschaft CarrierFrequency wird die Trägerfrequenz eingestellt. Am Häufigsten wird 38 kHz verwandt. Es lässt sich jedoch nicht jede beliebige Frequenz einstellen. Welche möglich sind, kann über CarrierFrequencies bzw. CarrierFrequencyList abgerufen werden.

The extension can be configured via a number of properties. HasIrEmitter indicates whether the device has an IR transmitter at all. If this is not the case, all other functions lead to errors. The carrier frequency of the IR signal is set via the CarrierFrequency property . The most common one is 38 kHz. The properties CarrierFrequencies or CarrierFrequencyList list the supported frequencies.

The extension has no error messages that a user could react to. For this reason, the AI2 standard error event ErrorOccurred is used for the error message.

Finally (see examples) there is a small Arduino program which you can use to determine the pattern of the IR codes.

 Packet-Pattern

The IR code to be sent is represented by a list of numbers, each of which alternates between the duration of Mark (an IR signal is sent at the set frequency) and Space (pause between the signals) in μs. The code always starts with a Mark.

mark-space

Reference

Blocks

Block Function Comment
Configuration
HasIrEmitter  Indicates whether the device has an infrared transmitter. The return value is true or false.
CarrierFrequencies Returns the supported carrier frequencies as a comma-separated String. Only these frequencies can be assigned to the CarrierFrequency property.
CarrierFrequencyList Returns the supported carrier frequencies as a comma-separated List.  
CarrierFrequency Sets or gets the carrier frequency to be used. The specification is in Hertz (Hz). The default value is 38,000 (38 kHz). Only values ​​that are shown by CarrierFrequencies or CarrierFrequencyList can be set.

This property is available in the designer too.

If unsupported values ​​are specified, event Screen.ErrorOccurred is triggered with error code 17302.
StartDelay With automatic repetition, StartDelay defines the pause between the first and the subsequent packets in milliseconds (ms). Valid values ​​are 0..1000 . The default value is 500 ms.

This property is available in the designer too.

If unsupported values ​​are specified, event Screen.ErrorOccurred is triggered with error code 17308.
RepetitionDelay RepetitionDelay specifies the pause between the subsequent packets in milliseconds (ms) for automatic repetition. Valid values ​​are 0..1000 . The default value is 500 ms.

This property is available in the designer too.

If unsupported values ​​are specified, event Screen.ErrorOccurred is triggered with error code 17309.
EventTransmitDoneDelay (Additional) delay for the TransmitDone event after sending a single packet. Valid values ​​are 0..1000. After a packet has been sent, the PatternXmitted event is triggered and, at the end of a repeat sequence, the TransmitDone event is raised. If a single package was sent, both events would be triggered directly after each other. For some applications, for example if the sending is linked to an optical signal, it is more appropriate if a certain time elapses between the two events.

The default value is 0 ms.

This property is available in the designer too.

If unsupported values ​​are specified, event Screen.ErrorOccurred is triggered with error code 17307.
Sending Packets
Transmit Transmits a single packet.

Pattern: The alternating on/off pattern in microseconds to transmit.
This method is synchronous; when it returns the pattern has been transmitted.

After transmitting is complete, the events PatternXmitted and TransmitDone are triggered (see also EventTransmitDoneDelay).

Pattern is either a String with a comma-separated list of integers (eg "300, 800, 300, ...") or a List with integers. The alternating on/off pattern in microseconds to transmit.

The Android method ConsumerIrManager.transmit() is called internally.

Nested transmits are impossible. If another transmit is already active (triggered by one of the Repeated ... methods ), Screen.ErrorOccurred event is triggered with error code 17305.

If Pattern cannot be converted to an integer array, the Screen.ErrorOccurred event is triggered with error code 17303.
RepeatedTransmit Sends the packet several times.

Repetitions: number of repetitions. -1 sends endlessly until StopTransmission is called.

Pattern: The alternating on/off pattern in microseconds to transmit.
The transmit is asynchronous. The method returns immediately.

Pattern is either a String with a comma-separated list of integers (eg "300, 800, 300, ...") or a List with integers. The alternating on/off pattern in microseconds to transmit.

The Android method ConsumerIrManager.transmit() is called internally.

After the transmit of each packet the PatternXmitted event is triggered. After the transmit is completed or has been canceled, TransmitDone is raised.

Nested transmits are impossible. If another transmit is already active (triggered by one of the Repeated ... methods ), Screen.ErrorOccurred event is triggered with error code 17305.

If Pattern cannot be converted to an integer array, the Screen.ErrorOccurred event is triggered with error code 17303.
RepeatedAlternateTransmit Versendet ein Start-Paket und mehrfach ein Folgepaket.

Repetitions: Anzahl der Wiederholungen für das Folgepaket. -1 versendet endlos, solange bis StopTransmission ausgerufen wird.

Pattern: The alternating on/off pattern in microseconds to transmit.

AltPattern: Folgepaket, ein abwechselndes Ein- / Aus-Muster in Mikrosekunden zum Senden.
The transmit is asynchronous. The method returns immediately.

Pattern is either a String with a comma-separated list of integers (eg "300, 800, 300, ...") or a List with integers. The alternating on/off pattern in microseconds to transmit.

The Android method ConsumerIrManager.transmit() is called internally.

After the transmit of each packet the PatternXmitted event is triggered. After the transmit is completed or has been canceled, TransmitDone is raised.

Nested transmits are impossible. If another transmit is already active (triggered by one of the Repeated ... methods ), Screen.ErrorOccurred event is triggered with error code 17305.

If Pattern cannot be converted to an integer array, the Screen.ErrorOccurred event is triggered with error code 17303 or 17304 for AltPattern.
StopTransmission  Stops a repeating transmission. Der Abbruch der Sequenz erfolgt nach dem ein Paket komplett übertragen wurde oder die jerweilige Pausenzeit abgelaufen ist. Einzelne Pakete oder Pausen werden nicht unterbrochen.
The sequence is aborted after a packet has been completely transmitted or the pause time has expired. Packets or breaks are not interrupted.
Events
PatternXmitted The PatternXmitted event is raised after each package is dispatched.  
TransmitDone The event is raised when a transmission sequence is stopped or interrupted. When sending individual packages, there is the option of triggering the event with a delay (see above).
ErrorOccurred Message in case of errors in usage of the extension. There are no errors that a user could respond to. Errors in are therefore raised via the central error event.
Automatic
RegisterButtonForSinglePattern Registers the specified Button component for automatic single-pack transmission. Button.TouchDown runs Transmit (see above) with the specified Pattern.
RegisterButtonForRepeatedPattern  Registers the specified Button component for automatic packet transmission with repetitions. Button.TouchDown runs RepeatedTransmit (see above) with the specified Pattern.

At Button.TouchUp, StopTransmission ( see above ) is executed.
RegisterButtonForRepeatAltPattern Registers the specified Button component for automatic packet transmission with repetitions. Button.TouchDown runs RepeatedAlternateTransmit (see above) with specified Pattern and AltPattern.

At Button.TouchUp, StopTransmission ( see above ) is executed.

Error Codes

Code Message Meaning
117301 Carrier frequency is not supported  
17302 Device does not have an IR transmitter  
17303 Cannot convert pattern to integers The Pattern parameter has non-convertible content.
17304 Cannot convert pattern to integers The AltPattern  parameter has non-convertible content.
17305 Nested transmits are not allowed  
17306 Component is not a Button The component passed is not of the Button type .
17307 Invalid value for EventTransmitDoneDelay Invalid value for the EventTransmitDoneDelay property. The range of values ​​is 0..1000.
117308 Invalid value for StartDelay Invalid value for the StartDelay property. The range of values ​​is 0..1000.
17309 Invalid value for RepetitionDelay Invalid value for the RepetitionDelay property. The range of values ​​is 0..1000.

Example

Remote control for Metz TV

A small sample app shows the use of the extension (see Download). It emulates the Metz RM18 remote control.

Screenshot Screenshot

The necessary code for the core of the app is long, because there are many buttons to ontrol, but quite simply structured.

Blocks

Recording of IR-Codes

Wiring

To record the IR codes, I connected an IR receiver to an Arduino. It should be noted that the receiver is designed for the correct carrier frequency.

The program for recording IR codes (see Download)is based on the example IRrecord of the library IRremote by Ken Shirriff. For the purpose of supplying code sequences for the extension, I have adapted the program. First of all, I kept only the part of the code that is necessary to receive the signals.

After the first attempts I found that the remote control works with the following signal lengths:

The code takes this into account and tries to map the times to these values ​​(can be switched off). You get an output like this:

800,2400,400,1000,400,1000,400,1700,400,1700,400,1000,400,1700,400,1000,400,↵
1000,400,1700,400,1000,400,1000,400,1000,400,1700,400,1000,400,1700,400,↵
1000,400,1700,400,1700,400,1700,400,1000,400,1700,400,1000,400,1700,400↵

Den Output kann man direkt in die Eigenschaft Pattern der Transmit-Funktionen der Extension kopieren.

Für diejenigen, die sich näher mit der Funktionsweise beschäftigen wollen: Die zu Grunde liegende Bibliothek initialisiert einen Timer auf eine Periodenlänge von 50 μs. Im der Timer-Interrupt-Service-Routine wird dann das Eingangssignal abgetastet und an einen Zustandsautomaten weiter gereicht.

The output can be copied directly into the Pattern property of the Transmit functions of the extension.

For those who want to take a closer look at how IRremote works: The underlying library initializes a timer to a period length of 50 μs. The input signal is then sampled in the timer interrupt service routine and passed on to a state machine:

ISR-State

Tools

For developing own extensions I gathered some tips: AI2 FAQ: Develop Extensions.