Using Opto-Coupler with STM32
From the data sheet, Forward diode voltage is 1.2V, and it seems to want 20mA.  

Thus, the resistor has 4.2-1.2=3.0V across it.  For 20mA, Ohm's law says you want 150 Ohms.

There will be some current when you have the input at 3.3V, and it remains to be seen if it's too much for an "off".

You could avoid the issue of the off current by grounding pine 2 and driving the LED from the MCU I/O pin idrectly, if it can provide 20mA (in fact, you'd already be sinking 20mA the way you're doing it now).  In that case, the voltage drop across the resistor would be 3.3-1.2=2.1V, and you would use a resistor of 100 Ohms.  If the MCU can't do 20mA, you could probably configure the pin as open collector.