(SOLVED) ESP8266 with Json by wifi

Hi Nilton

I have started replacing nearly all my own ESP8266 code (both LUA and Arduino) because I have found ESPEASY does almost everything that I need. It is well documented.

For this post, you can switch an output pin high or low via http

However, as I already run an MQTT Broker on the same Raspberry PI as OH2, I simply create the OH2 switches and use the default MQTT pub/sub commands in OH2 to control any output on any ESP8266 device I have on the network by name. No need to know the IP address of the ESP.

For example, my OH2 .items file might contain something like the following to control my 8 channel relay board
Switch RELAY_BOARD_CH1 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/05:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/05:command:OFF:0]"}
Switch RELAY_BOARD_CH2 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/04:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/04:command:OFF:0]"}
Switch RELAY_BOARD_CH3 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/00:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/00:command:OFF:0]"}
Switch RELAY_BOARD_CH4 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/02:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/02:command:OFF:0]"}
Switch RELAY_BOARD_CH5 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/16:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/16:command:OFF:0]"}
Switch RELAY_BOARD_CH6 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/14:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/14:command:OFF:0]"}
Switch RELAY_BOARD_CH7 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/12:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/12:command:OFF:0]"}
Switch RELAY_BOARD_CH8 {mqtt=">[mqttserver:ESP8266-RELAYS/gpio/13:command:ON:1],>[mqttserver:ESP8266-RELAYS/gpio/13:command:OFF:0]"}

Where ESP8266-RELAYS is the system name of my ESP controlling the relay board
There is almost nothing to configure to have ESPEASY work with OH2 - after configuring the WiFi network details, all you really need to do is add an OpenHAB MQTT controller.

It works wonderfully. I have also used IFTTT and my Google Home to make a call directly to an ESP that I have exposed (for testing purposes only) to the internet, so that IFTTT can access it.

Have fun.

Cheers MG

1 Like

Wonderful Mark,

Iā€™m going to study the code.

Thank you very much.