[Solved] Light a LED in ESP32

Hey guys,
I’m trying to light the esp32 builtin led using an action but isn’t works(I’m totally a newbie).
I am using a code for ESP and by live inspector I have:

Blockquote
20:02:40:
[MQTT] Device connected
“Token Ending: f97b9 Client-ID: Sentinela Will-Message: true”
20:02:41:
[MQTT] Device publish
{ “topic”: “tago/data/post”, “payload”: “0600”, “qos”: 1, “messageId”: 1145 }
20:02:41:
Raw payload:
[ { “variable”: “payload”, “value”: “0600”, “metadata”: { “mqtt_topic”: “tago/data/post” } } ]
20:02:41:
Result of [device] payload parser:
[ { “variable”: “payload”, “value”: “0600”, “metadata”: { “mqtt_topic”: “tago/data/post” }, “serie”: “1627945361169” }, { “variable”: “umidsolo”, “value”: 6, “unit”: “%”, “serie”: “1627945361169” }, { “variable”: “led”, “value”: 0, “serie”: “1627945361169” } ]
20:02:41:
Bucket [a5f8]:
“3 Data Added”
20:02:41:
[MQTT] Device disconnected
“socket event: disconnect”

This is my payload parser:

const ignore_vars = ['messageId','topic','qos'];
payload = payload.filter(x => !ignore_vars.includes(x.variable));
const mqtt_payload = payload.find((data) => data.variable === "payload" || (data.metadata && data.metadata.mqtt_topic));
if (mqtt_payload) {
  // Cast the hexadecimal to a buffer.
  const buffer = Buffer.from(mqtt_payload.value, 'hex')

 // Normalize the data to TagoIO format.
 // We use Number function to cast number values, so we can use it on chart widgets, etc.
  const data = [
    { variable: 'umidsolo',  value: buffer.readInt8(0), unit: '%' },
    { variable: 'led',  value: buffer.readInt8(1) },
    //{ variable: 'temperature',  value: buffer.readInt16BE(1) / 100, unit: '°C' },
    //{ variable: 'humidity',  value: buffer.readUInt16BE(3) / 100, unit: '%' },
  ];

  // This will concat the content sent by your device with the content generated in this payload parser.
  // It also adds the field "serie" to be able to group in tables and other widgets.
  const serie = String(new Date().getTime());
  payload = payload.concat(data).map(x => ({ ...x, serie }));
}

The parser result is:

 {
        "variable": "payload",
        "value": "0400",
        "metadata": {
            "mqtt_topic": "tago/data/post"
        },
        "serie": "1627945571473"
    },
    {
        "variable": "umidsolo",
        "value": 4,
        "unit": "%",
        "serie": "1627945571473"
    },
    {
        "variable": "led",
        "value": 0,
        "serie": "1627945571473"
    }

I set an action to save data in the bucket and I think that its working well cause i can see that variables in my dashboard widget.
The problem is the method that I am working with the action to light the led or my code(Arduino MqttClient library example) isn’t working well.
I can see all msg in my terminal…device is sending data to tago but never received nothing from there.
There is an example with a code+payload parser + action to light a led?
Thanks in advance
Kind regards

1 Like

So, everything you mentioned is only relevant for Uplink.

Downlinks, which means sending data to your physical device, doesn’t make use of payload parser or anything else.

On TagoIO side, you can setup an Action to be triggered by Variable or something else, the important is that your action has the type of action set to publish to mqtt Then you will have several additional parameters that you will need to specify:

On your device, you will need to be subscribed to the same topic you’re using here. Then you should be able to receive the payload that is published to the topic.

If you’re using an Action by variable, you can make the publish more dynamic by entering $VALUE$ in the payload parameter. That way, the value of the variable that triggered your action will be sent as payload.

This is the easiest way to setup downlinks from TagoIO side. It can be even more dynamic if you use an Analysis to perform this operation. The analysis section does have an snippet called "Push to MQTT from dashboard which can help you if you want to go the analysis way.

Hello Vitor,
Thanks for your help.
I put some data from the uplink to check if may be anything was working wrong, I am seeing msgs by live inspector like:“socket event: close”, “socket event: error” and “socket event: disconnect”.
About live inspector we can only check the uplinks, right? Or there is a way to see the downlink too?
I made an action based on the docs from this comunity, its like your example above with this parameters:
Topic = mydevice/in(pre-setted in my code into ESP32)
On trigger optios I put “variable is equal to 1(bool)”
I am changing the variable led on my dashboard from an input form linked to my device and variable led, value type = boolean, options = values = 0 and 1.
I think that the action is working because i can see the status like “triggered few seconds ago”.
Should I consider that my code is the problem?
I was working with another code using HTTP method and it worked like a charm but I had problems to send downlinks cause I dont know how to send to device using host address :frowning:
I will learn about analysis.
Thanks a lot

From what you described, your action should be okay. You can paste the screenshot of the action if you want additional feedback.

So the problem should be in your ESP32, you need to check if it’s subscribed to the correct topic (mydevice/in) and if you’re receiving the published data on your end.

If you want to make sure TagoIO is publishing to the topic, the best way would be to use some MQTT software, such as MQTT.fx, connect to TagoIO and subscribe to your topic.

I will follow your last suggestion and than will put the result here.
Thanks

1 Like

I just tested my input form + 2 actions to on/off the led with MQTT.fx(following tago example) and all data are arriving perfectly at the “end device”. So The problem is the code that I’m running on my ESP32 board.
Do you know a library/code to ESP32/arduino that can send/receive data?

Hey Vitor,
Just updating…today a tried another code for esp8266,from library PubSubClient and had the same problem. Uplink arriving ok at tago, data saved at bucket by an action, On-Off LED actions triggering the variable but no data arriving at the end device.
I will try with another codes soon. Any advice?

what port are you using at your code?

I’m with the same problem, and I’ve noted that at mqtt.fx example, they use the port 8883 and at my code I’m using the 1883 port…
If I change the port at my code for 8883 it stop publishing (and I don’t know why).
@vitor does this make any sence for you?

I always using default port on my codes = 1883.
After read your info I changed my port to 8883 and the end device can’t connect to mqtt broker(probably due TLS/SSL questions.

1883 is the port without TSL encryptation. You don’t need to move to 8883 for testing purposes, but we do recommend that when you move to production environment.

Connecting to 8883 TSL port is something you need to prepare in your code for the ESP32.
As TagoIO is publishing in the MQTT topic, the subscribe and receiving the payload published is also on the ESP32.

I can’t help much on this part, but if you provide the code you’re using maybe I can give you some inputs.

I found the problem → me.
I was confusing about the data type between string/binary when that downlink was arrived on my device.

Thanks for your help

1 Like