A quick-n-dirty implementation of the IR NEC protocol using the standard Toit RMT library. Impress your friends by turning their TV off with an ESP32!
Encode, transmit, receive, and decode IR pulses using the standard RMT library and the #NEC IR protocol.
The NEC IR protocol lets you send/receive just two bytes: address
and command
.
Note: The code in this package isn't well tested (other than sending various numbers back and forth), so expect bugs and most likely less than ideal performance. It'd be great to have a robust IR package for Toit (that supports other protocols too) - if you find any bugs or have suggestsions, please create an issue or a pull-request!
To transmit: Connect an IR LED (with a suitable resistor) to a GPIO pin on your ESP32. To receive: Connect an IR receiver with the signal pin connected to a GPIO pin on your ESP32. Most receivers have 3 pins: 3.3V, signal, and ground.
Tip: For testing purposes you can just hook up the receiver and the transmitter on the same board using different GPIO pins.
The library provides two classes Transmitter
and Receiver
.
Both constructors expect a gpio.Pin
as the first argument. The pin is configured in the class constructor.
Use Transmitter.send --address=10 --command=100
to send data.
Use Receiver.read
to listen for IR signals. This method returns a ByteArray
with two bytes when a signal is successfully received and decoded.
You can enable verbose debug level logging by passing in a log.Log
instance to the --logger
argument on Transmitter
and Reciever
. You can also change the default logger level - see example below.
Connect your IR LED (positive lead) to GPIO 17 and the signal pin of your IR receiver to pin 18.
import gpio
import ..src.transmitter
import ..src.receiver
import log
main:
log.set-default (log.default.with-level log.INFO-LEVEL)
tx-pin := gpio.Pin.out 17
tx := Transmitter tx-pin
rx-pin := gpio.Pin.out 18
rx := Receiver rx-pin
while 1:
rx.start-reading
tx.send 10 100
rx.read
sleep --ms=2000