JSON over MQTT?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
spachal
Normal user
Posts: 55
Joined: 22 Feb 2018, 22:11

JSON over MQTT?

#1 Post by spachal » 28 Feb 2018, 22:55

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! :]
Please excuse my English, thanks!

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: JSON over MQTT?

#2 Post by grovkillen » 01 Mar 2018, 07:34

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.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

spachal
Normal user
Posts: 55
Joined: 22 Feb 2018, 22:11

Re: JSON over MQTT?

#3 Post by spachal » 03 Mar 2018, 20:53

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 5529 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 ..
Please excuse my English, thanks!

ebtkr
New user
Posts: 2
Joined: 04 Jul 2021, 23:07

Re: JSON over MQTT?

#4 Post by ebtkr » 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]"}"

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: JSON over MQTT?

#5 Post by TD-er » 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

ebtkr
New user
Posts: 2
Joined: 04 Jul 2021, 23:07

Re: JSON over MQTT?

#6 Post by ebtkr » 19 Aug 2021, 22:54

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

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: JSON over MQTT?

#7 Post by TD-er » 19 Aug 2021, 23:57

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"

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests