gmag11 / EnigmaIOT

Secure sensor and gateway platform based on ESP8266 and ESP32

Home Page:https://gmag11.github.io/EnigmaIOT

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MQTT command line to switch LED off/on

GeoffreyMills opened this issue · comments

Hi,
FYI This is a documentation change/advisory maybe??
I wanted to control an LED on a node, so I used your code (under examples LED Controller) and added this to the node software I have been using (with a BME280 sensor). I am running Mosquitto (and Thingsboard) on a Centos VM, so I used the linux command line below to switch the LED status:
mosquitto_pub -h localhost -t Mesh/6130/set/data -m "{"cmd":"led","led":1}"

The node did not like this, the data was not recognised as MSG_PACK or CAYENNELPP. But other MQTT commands work e.g:
mosquitto_pub -h localhost -t Mesh/6130/get/version -m " "

I was reading the ArduinoJSON website about deserializing JSON Objects when I saw the formatting on their JSON examples:
char json[] = "{"hello":"world"}";

So I formatted the mosquitto_pub command line to match:
mosquitto_pub -h localhost -t Mesh/6130/set/data -m "{"cmd":"led","led":1}"

and viola! working now :)
processRxCommand() | Data: {"cmd":"led","led":1}
[BME280Controller.cpp:176] processRxCommand() | Set LED status to ON

Hmm, seems so obvious now... might save someone some time one day

Regards
Geoffrey Mills

Hi @GeoffreyMills ,

You are right. I'll fix it in dev branch, so that it is included on next release. Thank you!!!

Hi,

I read my original notes and I was confused by what I had written.!!Then I realised that the html formatting ruined another key point (and it was badly written).

Key point: I am using Centos 7 VM running Mosquitto Broker and using the mosquitto_pub command in a Linux bash shell on that VM) to send commands to nodes via MQTT. Using this environment then inside the curly braces { } every speechmark must be prefixed with a backslash, see the formatting displayed nicely here: https://arduinojson.org/v6/api/json/deserializejson/ - scroll down to Examples

So in the Linux bash shell I must type in:
mosquitto_pub -h localhost -t Mesh/B994/set/data -m "{\"cmd\":\"led\",\"led\":1}"

I run up another bash shell and used this command: mosquitto_sub -h localhost -v -t '#'
to monitor the MQTT traffic.

So running the mosquitto_pub command above returns (in the mosquitto_sub traffic):
Mesh/B994/set/data {"cmd":"led","led":1} <- we sent this command
Mesh/B994/data {"cmd":"led","led":1} <- MQTT sends back confirmation
The LED turns on

If you omit the 6 backslashes ( {\"cmd\":\"led\",\"led\":1} ) then you do not get the confirmation back via MQTT (Mesh/B994/data {"cmd":"led","led":1} )
and you get the payroll encoding error on the ESP device:
[BME280Controller.cpp:50] processRxCommand(): 255441 Heap: 231172 Wrong payload encoding
And of course the LED does not turn on

Update 4th Jan 2022 - Suddenly clicked that error message is originating from the node (BME280 Controller - wrong payroll encoding). Which means the gateway has "successfully" (?) read the input from Mosquitto Broker, encoded the contents and sent this downstream to the node. Maybe ArduinoJSON is mangling the payroll, because the content is not 100% json compatible?

Apologies for my bad writing earlier, and this is a documentation change rather than an issue. The documentation uses the basic format Mesh/B994/set/data {"cmd":"led","led":1} and it took me quite a while to work out why the LED would not work. I am finding with the Enigma software that the code is usually right... and the issue is elsewhere

Regards
Geoffrey