Page 1 of 1

JSON over MQTT?

Posted: 28 Feb 2018, 22:55
by spachal
Hallo, at first, excuse my English. ESPEasy is a great piece of work and I love it, thanks all for this great effort!!

And my question, I'd like to send from my ESP8266 a JSON mqtt message to my mqtt broker. Is there any way how to do it?
If I use url http://<myesp>/json, I receive correct JSON message and I want to the same data using mqtt. Is there any way how to use a rule to do so?

Of course, I can send, by rules, payload of every sensor separately, but I'd like to receive all informations in one JSON message and parse them directly in openHAB.

My configuration:
- ESP8266 D1 mini Pro
- latest MEGA build
- working MQTT communication to broker (openHAB MQTT @ rasppi3)
- set of sensors (DHT11. DHT22, HTU21D)

Thanks for any help! :]

Re: JSON over MQTT?

Posted: 01 Mar 2018, 07:34
by grovkillen
The only thing with that is that you cannot know the age of the values since they will all be sent at the same time. Individual sent values will be sent on time of change or interval.

Re: JSON over MQTT?

Posted: 03 Mar 2018, 20:53
by spachal
Hi, thx for reply, there is no need to receive sensor's data asap on every change, in my project :] ..

I found some solution, it works for me. I dodn't know that I can publish text&variables as <value>, but it's possible, obviously.
So here my dev board, 1x DGT11, 1xDHT22 and one HTU21D (I2C):

esp01.jpg
esp01.jpg (56.81 KiB) Viewed 5660 times

And simple rule with mqtt publish

Code: Select all

on System#Boot do
  timerSet,1,1
endon

On Rules#Timer=1 do
    Publish /%sysname%/JSON,{"Sysname":"%sysname%","Uptime":"%uptime%","DHT11indoorTemperature":"[DHT11indoor#Temperature]","DHT11indoorHumidity":"[DHT11indoor#Humidity]","DHT22outdoorTemperature":"[DHT22outdoor#Temperature]","DHT22outdoorHumidity":"[DHT22outdoor#Humidity]","HTU21DTemperature":"[HTU21D#Temperature]","HTU21DHumidity":"[HTU21D#Humidity]"}
  timerSet,1,10
endon

The result is valid JSON message:

Code: Select all

{"Sysname":"ESPsensor","Uptime":"1","DHT11indoorTemperature":"23","DHT11indoorHumidity":"30","DHT22outdoorTemperature":"-1.20","DHT22outdoorHumidity":"42.90","HTU21DTemperature":"23.41","HTU21DHumidity":"100.00"}

I have to write it manually, but it works! Maybe it can help someone else ..

Re: JSON over MQTT?

Posted: 19 Aug 2021, 18:32
by ebtkr
I'm using the version ESP_Easy_mega_20210802_normal_ESP8266_4M1M and the form that work fine is put payload between quotes "" like this:

publish,home/temp,"{"Time":"%systime%","Temp":"[temp#temp1]"}"

Re: JSON over MQTT?

Posted: 19 Aug 2021, 21:45
by TD-er
ebtkr wrote: 19 Aug 2021, 18:32 I'm using the version ESP_Easy_mega_20210802_normal_ESP8266_4M1M and the form that work fine is put payload between quotes "" like this:

publish,home/temp,"{"Time":"%systime%","Temp":"[temp#temp1]"}"
I guess you need to use different quotes for the last parameter of publish, like this:

Code: Select all

publish,home/temp,'{"Time":"%systime%","Temp":"[temp#temp1]"}'
For explanation, see: https://github.com/letscontrolit/ESPEasy/issues/2724

Re: JSON over MQTT?

Posted: 19 Aug 2021, 22:54
by ebtkr
TD-er wrote: 19 Aug 2021, 21:45
ebtkr wrote: 19 Aug 2021, 18:32 I'm using the version ESP_Easy_mega_20210802_normal_ESP8266_4M1M and the form that work fine is put payload between quotes "" like this:

publish,home/temp,"{"Time":"%systime%","Temp":"[temp#temp1]"}"
I guess you need to use different quotes for the last parameter of publish, like this:

Code: Select all

publish,home/temp,'{"Time":"%systime%","Temp":"[temp#temp1]"}'
For explanation, see: https://github.com/letscontrolit/ESPEasy/issues/2724
You're right. Work fine with both kind of quotes

Re: JSON over MQTT?

Posted: 19 Aug 2021, 23:57
by TD-er
It only works fine with the same quotes if the argument parsing is set to be more tolerant.
Either by checking this option in the tools->Advanced page, or you're running one of the commands that explicitly have the parameter parsing a bit more tolerant.
Not sure which commands have it, but I guess it might be "logentry"