433mhz, infrared IR to and from MQTT on ESP8266

You can test with your board that you have. It should Work.

I tried it with a previous version of board manager and a Node MCU v0.9 and it was not working. It was giving a stack trace on the serial monitor.

You can retest it and you will see.

Iā€™ll give it a go with the IR components tomorrow night and keep you both posted

Hi

So I hooked up the esp8266 nodemcu V0.9 and got it to compile successfully (I needed to exclude the example fan folder as that was causing compile errors).

The IR receiver seems to be working to capture a IR value from a remote button press (values range between 4-5 different values for the same remote button press). However when I pass that code into a published MQTT message from my controller box (Raspberrypi3 hosting the MQTT server and Home Assistant) my ESP8266 Serial monitor acknowledges that the MQTT message is received but I dont get any activity on my physical device being controlled by IR (e.g. the device doesnt turn on/off)?

Below is a snippet of my serial monitor:
Receiving data by MQTT
home/commands/MQTTtoIR
Acknowedgement of reception published
Callback value
3268051071
Converted value to unsigned long
3268051071
Hey I got a callback
Not processing this callback contains $subjectAck

Iā€™m thinking it might be something to do with my wiring perhaps? Or the capacitor? I used the same design as your pin out minus the RF receiver and transmitter.

Any tips to help me troubleshoot?

Hello,

Yes if you have a multimeter check that your circuit is well powered (5V at least), if you donā€™t have you can replace your IR led with a normal one to see if it light on when sending MQTT signal.
If the led is well powered (light on or have 5V when sending mqtt) try to put your led closer to your physical device. Try also with the different codes. What is you physical device brand?

Itā€™s hard to tell from the pictures provided, but Iā€™ve had very good luck(i.e. long IR distance) using the below circuit (sorry for the ASCII art) with the ESP8266 and Wemos D1 mini. For your circuit, you may want to make sure the resistor connected to the transistor base isnā€™t too large a value, otherwise there may not be enough current to turn your transistor on.

                       Collector
                     +------------------------- GND  
       15K ohm    | /
   -----/\/\/-----|K 
             Base | \      18 ohm    IR LED
                     V------/\/\/------|<------ 5v
                       Emitter  <-- current flow direction

Thank you 1technophile for your hard work figuring this all out!. Once I put everything together and tried it, I wasnā€™t having any luck. After quite a bit of trial and error I figured everything out. Hereā€™s what I did to get things workingā€¦

HOW TO GUIDE:

Guide: This is the original guide I used as a starting point (credit to 1technophile):

Hardware: I used an ESP8266 via a Wemos D1 mini, RF-5V 433Mhz Receiver and FS1000A 433Mhz transmitter, and Etekcity Outlets. For the transmit antenna, I used this guide with good results.

Compiling: I ran into issues compiling OpenMQTTGateway.ini since the #ifdef ESP8266 commands were not being recognized and I was seeing errors related to an Arduino and not the ESP8266. I realized that I was using an old version of the Arduino software for the Mac, which didnā€™t handle precompiler directives properly. I would recommend using Arduino 1.6.6, or newer, to compile, or else you may get bit by this problem as well.

Once I had everything compiled I used the Arduino Serial monitor output to look at the RF codes and copied them down. I tried sending RF commands via MQTT at the command line of a raspberry pi, but I nothing worked. It took me a while to understand that I probably needed to know the RF pulselength and protocol to use for my Etekcity outlets.

I donā€™t believe the original instructions mentioned how to find your RF pulselength and protocol. In order to find that out, I needed to compile and run the ReceiveDemo_Advanced.ino example in the rc-switch library. In my case I used mySwitch.enableReceive(5); since my receiver is on pin D1(GPIO 5) on the WeMos D1. After I compiled and ran this I was able to determine the pulselength and protocol used by my etekcity outlets (in my case it was 180 microseconds using RF protocol 1). Once I knew that information I tried sending user defined pulselength and protocol information to OpenMQTTGateway.ini, but sadly it didnā€™t work. I realized that the following two lines of code trounce the user defined pulselength and protocol information.

if (valuePLSL != 0) valuePRT = 1;
if (valuePRT != 0) valuePLSL = 350;

Once I deleted these two lines of code and recompiled everything went as expected. (FYI, I opened an issue on github to hopefully get that resolved.) Now that it was working I was able to issue the following commands via my Home Assistant command line with an RF Pulse Length of 180 and RF Protocol number 1 (By the way, my MQTT server expects a username and password, so I used the -u and -p options. You may not need to do this).

mosquitto_pub -d -u username -P password -t home/commands/MQTTto433/PLSL_180/433_1 -m 5321996

After that everything worked as expected. From there I integrated this into my Home Assistant configuration.yaml file as shown below:

switch:
  - platform: mqtt
    name: "outlet5"
    state_topic: "home/commands/433toMQTT"
    command_topic: "home/commands/MQTTto433/PLSL_180/433_1"
    payload_on: "5321987"
    payload_off: "5321996"
    optimistic: true
    retain: true

and also added the following to my configuration.yaml to show a switch:

group:
  default_view:
    entities:
      - switch.outlet5

I hope this helps save someone a couple hours of grief! :slight_smile:

Optional: I like to keep my networked devices on a fixed IP address for future trouble shooting. For that reason, I added static IP address capability, to OpenMQTTGateway.ini, as well as Serial Monitor IP address reporting. I did this by replacing the #ifdef statement in the setup() section of the code with the following:

  #ifdef ESP8266
    //Begining wifi connection in case of ESP8266
    IPAddress ip(192, 168, 1, 200);
    IPAddress gateway(192,168,1,1);
    IPAddress subnet(255,255,255,0); 
    WiFi.begin(wifi_ssid, wifi_password);
    WiFi.config(ip, gateway, subnet); 
    Serial.print("My IP address is: ");   
    Serial.println(WiFi.localIP());
  #else
    //Begining ethernet connection in case of Arduino + W5100
    Ethernet.begin(mac, ip);
  #endif

Hello,

Thank for the feedback!
Iā€™m planning to add a function to see the equivalent of receive demo advanced into an mqtt topic, this will avoid to load this specific sketch for sniffing the rf codes.
Regarding compilations errors could you point me which board and parameter you are choosing in the ide so as to do some tests?
About the default values for rf sending i will publish a correction soon.

Hi 1technophile,

Thank you for your quick follow up. Agreed, adding RF code sniffing would be an excellent addition.

FYI, I used the following, when compiling:

OS: OS X 10.11.3
Software: Arduino 1.6.5
Board: WeMos D1 R2 and mini
Flash Size: 4M (3M SPIFFS)
CPU Frequency: 80 MHz
Upload Speed: 115200

I did some research and found that there was a bug in the Arduino Mac version prior to 1.6.6, which didnā€™t handle preprocessor directives properly. After updating to the latest Arduino Mac version (1.8.1) and recompiling everything worked perfectly! Sorry for the false alarm on that, luckily updating the Arduino software is a pretty simple fix. Iā€™ve revised my feedback above to take that into account.

Thanks for the tips, soooo after lots of trial and error I found that the cable supplying 5v vcc to the IR led was faulty, I also found that what I thought was a transistor 2N2222 was in fact a lm335 temperature sensor transistorā€¦ So two things wrong in my initial wiring up. I found that once i replaced the faulty wire that a colour LED was constantly on with the existing pin out (using the LM335 transistor) which led me to inspect the transistor more carefully.

I attempted to bypass the transistor altogether and run the power feed off D5 via the 330 ohms resistor to the LED and then finally to ground. This wiring worked great for the coloured LED, in that it turned on successfully with every message received via MQTT. However I strongly suspect that the power sourced from D5 isnt sufficient to power the IR LED to the required level. And hence my IR LED project is still ongoing.

Iā€™m attempting to turn on / off my samsung blueray player with IR, should i use the IR_SAMSUNG or just the MQTTtoIR message feed? Iā€™ve also noticed that when I record the IR signal using the IR receiver that out of 10 tries i get maybe 4 consistent hits on the same long integer, but the remaining 6 values seem to be random numbers? Is this normal behaviour?

I will try and find some 2N2222 transistors tomorrow to wire up and retry. Iā€™ll also get a new battery for my multimeter to test out voltages from my power supply and across the circuit.

Thanks again for the assistance

Great to hear, I have added wemos mini to the continuous integration builds, even if it is not compiled to a mac environment it can be usefull to check that it compiles well.

Regarding RF special protocol and/or pulselength Iā€™ve pusblished a correction should be ok withou modifying the code now.

It could work but you have to be very close to your physical device, around 10cm.

Try IR_SAMSUNG and IR_NEC, itā€™s possible but not confirmed that you need to send a raw signal which is not yet handle by the gateway

I have the same behaviour and tested the different signals to isolate the good one

Yes, the code compiles very well using the latest Arduino environment. Thank you so much for the fix!

A new functionnality is available:
publication of advanced RF data has been added, data is published to ā€œhome/433toMQTTAdvancedā€

More info on the wiki

@1technophile

I want to buy second-hand, rf-brand ā€œchaconā€ sockets, how do I know the protocol is supported?

Finally got it working :slight_smile: Once I changed out the IR diode with a new one and used a 2N2222 transistor. Brilliant

I can now control my Samsung Tv and Samsung HiFi system, next challenge is my HTPC. Iā€™ve tried all the different device types with no luck, so Iā€™m assuming Iā€™ll need to try Raw IR feeds.

Im going to try and hook up Home Assistant now to streamline the command interface with the MQTT server/

Cheers for the help

I donā€™t think they will be compatible with rcswitch library

Great! Could you publish some tested mqtt command samples to give example to other samsung owners?

Would you have any example configuration.yaml to hook up Home Assistant to publish MQTT messages to send IR codes directly from the configuration file? Iā€™m unsure whether it can be done directly from the configuration.yaml file or whether a python script needs to be created as an intermediary to fire the MQTT message?

For Samsung users, below are some MQTT messages which turn on/off the following devices:

Samsung TV (UA46D7000LMXXY)
mosquitto_pub -t home/commands/IR_SAMSUNG -m 3772793023

Samsung Blueray / Surround Sound System ( HT-6550WM):
mosquitto_pub -t home/commands/IR_SAMSUNG -m 3268051071