esp dmx send via mqtt help with errors

hi i was trying to use the espDMX library i wake it work for som minutes with this code but after i get this errors which result to the restart of the esp.

/*


*/
#include <espDMX.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "@@@@@@";
const char* password = "@@@@!";
const char* mqtt_server = "@@@@@@";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxA.setChans(payload, 1);

    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxB.setChans(payload, 101);
  }
  Serial.println();

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("group1","admin","@@@@@!")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outlight/dmx1/", "On Dmx Esp");
      // ... and resubscribe
      client.subscribe("inlight/dmx1/");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
    // Start dmxA, status LED on pin 12 with full intensity
  dmxA.begin(12);

  // Start dmxB, status LED on pin 13 with 75/255 intensity
  dmxB.begin(13, 75);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  

     long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "On Dmx Esp #%ld", value);
  //  Serial.print("Publish message: ");
  //  Serial.println(msg);
    client.publish("outlight/dmx1/", msg);
  
  }
}
Exception (0):
epc1=0x40202814 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys 
sp: 3ffffc50 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffdf0:  00000017 0000001d 00000019 00000080  
3ffffe00:  4020c416 3fff0134 3ffe961e 4000050c  
3ffffe10:  4000437b 00000030 00000016 ffffffff  
3ffffe20:  60000200 00000008 ffffffff 80000000  
3ffffe30:  20000000 3fff14f8 80000000 203fc1c0  
3ffffe40:  80000000 3fffc6fc 3ffedea8 3fff14fc  
3ffffe50:  00000134 003fc1c0 60000600 00000030  
3ffffe60:  40106b64 3fff087c 00000000 00000001  
3ffffe70:  4020ce55 3ffee328 3fff087c 4020258b  
3ffffe80:  40105244 40106ab8 3fffc250 40202820  
3ffffe90:  00000000 00000000 0000001f 40105101  
3ffffea0:  4000050c 008ebc8e 40211437 4000050c  
3ffffeb0:  40000f68 00000030 0000001c ffffffff  
3ffffec0:  40000f58 00000000 00000020 00000000  
3ffffed0:  00000013 40208fcc 3ffedea8 00000001  
3ffffee0:  ffffffff 3ffe8b04 3ffedea8 3fffdab0  
3ffffef0:  00000000 3fffdcb0 3ffedee0 00000030  
3fffff00:  00000000 400042db 00000064 60000600  
3fffff10:  40004b31 3fff133c 000002f4 003fc000  
3fffff20:  40105666 3ffeded0 3ffecf70 40106ee4  
3fffff30:  40208a9d 3ffecf70 3ffeded0 0212bf8a  
3fffff40:  3fff133c 00001000 40208f32 00000008  
3fffff50:  40105e24 00000000 40208fdf 3ffed024  
3fffff60:  3ffeded0 008f30d0 3ffeded0 60000600  
3fffff70:  4021a6c1 3ffed024 3ffeded0 02129dc6  
3fffff80:  4021a706 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffedee8 00000000 40000f65 3fffdab0  
3fffffa0:  40000f49 000182ab 3fffdab0 40000f49  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld

[/code]
do you have any idea why

wdt reset

I'm surprised MQTT can handle the speed & amount of traffic generated by DMX.

If the watchdog is triggering on an ESP8266 (not the ESP32) then it might be your doing to much processing without releasing the processor to handle wi-fi stuff. try putting a delay(1) between the print & publish.

i have a dmx power supply(i dont know the correct word ) which controls the the robotics and the lights throu console but i have some lights that only needs dimming and i want to use the esp to control the lights .
so its only going to be a slider in nodered with the values 0 to 255 that post the values via mqtt ,
I think the callback funktion si destroying it but when i tried to put the dmx commands in the void loop i get the error that the payload is not declared and i am having problems with that cause i dont know to do that with the mqtt the whole char confuses me a lot . Can you guide me with that ?
thanks in advance
chris

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  

     long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "On Dmx Esp #%ld", value);
  //  Serial.print("Publish message: ");
  //  Serial.println(msg);
    client.publish("outlight/dmx1/", msg);
    delay(3000);
    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxA.setChans(payload, 1);

    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxB.setChans(payload, 101);
  
  }
}

Is node-red sending the payload as a single byte value or as an ASCII string representation of the payload value?

Can you explain in more detail what your setup is.

Do you have a single DMX light on a single DMX channel to control light intensity, several lights on different channels that need setting to different light levels or RGB lights that need 3 or more channels setting to alter light level output?
Why are you using 2 DMX universes. Are the lights on several DMX outputs or is this just copy and paste of the example code?

Hi I am am have 12 led dimmable lights that are on one dmx universe I just have made a protobroard with two dmx outputs that’s why I had the two universes in the code one is not needed .
So I will use only the the dmxa but as I found out the command dmxa.setchans has to be in the void loop and not in the void callback cause in the callback it make the esp restart after some time.
But I don’t know how to write it when I type in the void loop dmxA.setChans(payload ,1 ) it says I haven’t declare it because I don’t know how to save the payload for later use in the code .
The slider is just a basic slider from dashboard nodered with values 10-255 I have changed anything else .
Thanks in advance
Chris

papas_17:
Hi I am am have 12 led dimmable lights that are on one dmx universe I just have made a protobroard with two dmx outputs that’s why I had the two universes in the code one is not needed .
Do all 12 led lights use the same single channel or are they on 12 separate channels?

The slider is just a basic slider from dashboard nodered with values 10-255 I have changed anything else .
Does it continually publish the slider value to MQTT or does it only publish when the value changes?

hi yes all 12 leds to 1 dmx channel and the slider is only sending when the value changes
thanks in advance for your help
chris

I'm not near a PC and hardware to test this but the basic code should look something like...

/*
  
  
*/
#include <espDMX.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "@@@@@@";
const char* password = "@@@@!";
const char* mqtt_server = "@@@@@@";

WiFiClient espClient;
PubSubClient client(espClient);
uint32_t lastMsg = 0;
char msg[50];
uint16_t value = 0;

void setup_wifi() {
  
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  randomSeed(micros());
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  // Serial.print("Message arrived [");
  // Serial.print(topic);
  // Serial.print("] ");
  // for (int i = 0; i < length; i++) {
  // Serial.print((char)payload[i]);
  // }
  // Serial.println();
  
  // Should really check the topic matches the exact one your subscribed to before using value
  dmxA.setChans(payload[0], 1);
  
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("group1","admin","@@@@@!")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outlight/dmx1/", "On Dmx Esp");
      // ... and resubscribe
      client.subscribe("inlight/dmx1/");
      } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // Start dmxA, status LED on pin 12 with full intensity
  dmxA.begin(12);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  delay(2000);
}

void loop() {
  
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  if (millis() - lastMsg > 2000) {
    lastMsg = millis();
    snprintf (msg, 49, "On Dmx Esp #%ld", value++);
    //  Serial.print("Publish message: ");
    //  Serial.println(msg);
    client.publish("outlight/dmx1/", msg);
    
  }
  delay(10);
}

hi i get this error now

C:\Users\Chris\Documents\Arduino\EspDmx_Group_fixed\EspDmx_Group_fixed.ino: In function 'void callback(char*, uint8_t*, unsigned int)':

EspDmx_Group_fixed:69: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

   dmxA.setChans(payload[0], 1);

                          ^

In file included from C:\Users\Chris\Documents\Arduino\EspDmx_Group_fixed\EspDmx_Group_fixed.ino:20:0:

C:\Users\Chris\Documents\Arduino\libraries\espDMX/espDMX.h:63:14: error:   initializing argument 1 of 'void espDMX::setChans(uint8_t*, uint16_t)' [-fpermissive]

         void setChans(byte *data, uint16_t numChans) {

              ^

exit status 1
invalid conversion from 'uint8_t {aka unsigned char}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

, also you think that keeping the dmx command in the voidcallbackloop and not in the void loop is better ?you dont think that this was reseting the esp ?
thanks in advance for your help
chris

Just change the offending line back to

  dmxA.setChans(payload, 1);

and it should compile.

A couple of possibilities of why it crashes are...
Maybe the DMX library does not play well with Serial so try commenting out any Serial prints that happen in your code.
Another possible cause might be that I think DMX protocol has a minimum transmit interval and a single channel will not be long enough. You could set the payload length to 10 or more and still only use the first channel.

ok thanks for your fast response i try it but still the same isue and also something i forgot to mention is that at full brithness 255 in the slider the lights are dimmed always half its like it doents understant the values send to it .
also i was thinking of something like this but i still get this error so i havent compile it yet

 /*

*/
#include <espDMX.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "@@@@@";
const char* password = "@@@@@!";
const char* mqtt_server = "192.168.1.177";
volatile char newTopic[100]={0};
volatile byte newPayLoad[100]={0};
volatile int newLength=0;
volatile bool newPayLoadRecieved=false;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

// copy new data to volatile variables
void callback(char* topic, byte* payload, unsigned int length) {
  strcpy((char*)newTopic, topic);
  newLength=length;
  for (int i = 0; i < length; i++) {
    newPayLoad[i]=payload[i];
    }
  newPayLoadRecieved=true;
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("group1","admin","@@@@@!")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outlight/dmx1/", "On Dmx Esp");
      // ... and resubscribe
      client.subscribe("inlight/dmx1/");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
    // Start dmxA, status LED on pin 12 with full intensity
  dmxA.begin(12);

  // Start dmxB, status LED on pin 13 with 75/255 intensity
  dmxB.begin(13, 75);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  if(newPayLoadRecieved)
{
  Serial.print("Message arrived [");
  Serial.print((char *)newTopic);
  Serial.print("] ");
  for (int i = 0; i < newLength; i++) {
    Serial.print(newPayLoad[i]);
    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxA.setChans(newPayLoad[i], 10, 1);
    // Map values 1-5 to dmx channels 101 - 105 on dmxB
    dmxB.setChans(newPayLoad[i], 5, 101);
   }
  }
  Serial.println();
    newPayLoadRecieved=false;
  

     long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "On Dmx Esp #%ld", value);
  //  Serial.print("Publish message: ");
  //  Serial.println(msg);
    client.publish("outlight/dmx1/", msg);
    // Map values 1-5 to dmx channels 101 - 105 on dmxB
   
}
}

errors

: In function 'void loop()':

example:104: error: invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

     dmxA.setChans(newPayLoad, 10, 1);

                                    ^

In file included from C:\Users\Fitness Gym\Documents\Arduino\example\example.ino:4:0:

C:\Users\Fitness Gym\Documents\Arduino\libraries\espDMX/espDMX.h:66:14: error:   initializing argument 1 of 'void espDMX::setChans(uint8_t*, uint16_t, uint16_t)' [-fpermissive]

         void setChans(byte*, uint16_t, uint16_t);

              ^

example:106: error: invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

     dmxB.setChans(newPayLoad, 5, 101);

                                     ^


C:\Users\Fitness Gym\Documents\Arduino\libraries\espDMX/espDMX.h:66:14: error:   initializing argument 1 of 'void espDMX::setChans(uint8_t*, uint16_t, uint16_t)' [-fpermissive]

         void setChans(byte*, uint16_t, uint16_t);

              ^

exit status 1
invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

The code you pasted above is not the code I posted in reply #7. With #7 code it compiled okay after changing from payload[0] to payload.

I was going to suggest you replace the callback code with

Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("Length [");
Serial.print(length);
Serial.print("] ");
for (int i = 0; i < length; i++) 
{
  Serial.print((char)payload[i]);
}
Serial.println();

to allow us to determine the format of the message received from node-red. I suspect node-red is probably sending the level 0-255 as a string instead of a binary value so will need converting back into an byte value before passing to the dmx function. Also without the dmx function in the callback we can prove that is the cause of reboots.

If I get time on Friday I will try and make time to compile and test the code but without any DMX lights attached.

hi i have checked what you wanted and yes the mqtt value is string and not binary how do i change that ?
One more question do you thing that why it is reseting the esp ? and what about the code i send you do you think we should use it ?
thanks in advance
chris

I have now had chance to test/debug your code and it is attached below.

I switched the output to use dmxB instead of dmxA so I could also use Serial.print commands to do debugging and with a few tweaks the code does not crash in the callback.
If you wish to use dmxA instead (or as well) then you must remove all references to Serial from the sketch as the main problem you was having was contention between DMX and Serial as DMX uses both hardware USART ports of an ESP8266.
I have not tested the output DMX signal though.

Also included at the end of the sketch is the node-red flow I used for testing. You would need to setup the correct username and password in the mqtt node and also put your correct wifi/mqtt details in the sketch.

espDMX.ino (6.1 KB)

hi thanks for your help so far .
i tried the code the esp doesnt reset and mqtt works ,but when i send the command to esp say 255 full light it always stay the lights like they are 50% not 100% and what i realized is that you cant change the number-payload after so it doesnt dimm the lights ,whatever value you send its alwys like the lights are at 50% but one good thing is that they can stay open for hours (without the esp reseting),but as i said no control at the dimming .
do you have any idea whats going on ?
thanks in advance
chris

oops, my mistake. I forgot to change the payload for the dmx so it was like it used to be and setting the ASCII value instead of the byte value.

To save RAM you could reduce the 512 to something lower like 2 in this line

uint8_t dmxPayload[512];

And the typo is you need to change the setChans line to this

  dmxB.setChans(dmxPayload, 1);

Hi how are you ? Thank you so much for your help I tried the software and it worked fine for 2 days and after it started resetting again every 2 minutes . Do you have any idea why ?
Do you think that the mqtt sending makes the the memory full ?

Do you know how to include in the programm a line that resets the memory every time it gets a mqtt message or make until it reach a specific on time ?
What do you think is best ?

Thanks so much for your help in advance

Chris

papas_17:
Hi how are you ? Thank you so much for your help I tried the software and it worked fine for 2 days and after it started resetting again every 2 minutes . Do you have any idea why ?
Do you think that the mqtt sending makes the the memory full ?

To check how much memory is being used you could either publish another mqtt value or replace the current outgoing msg with the free memory reading.
Replace

    snprintf (msg, 49, "On Dmx Esp #%ld", value++);

with

    uint32_t free = ESP.getFreeHeap();
    itoa(free, msg, 10);

then in node-red put the value on a dashboard graph so you can see a history of memory usage to see if you have a memory leak.

It could also just be something with the ESP8266 board as I have several esp modules send sensor readings to MQTT but one of them will lock up after a few days and pressing reset does not fix the problem so I have to unplug/replug the module to get it working again. I suspected it was because it's plugged into breadboard and finishing the project properly with a circuit board would fix the problem.

Hi again sorry for the long absence I was really busy with work and didn’t have time to test good the esp .
So I am still getting the disconnection( more like resetting ) after a clean install it take about two hours after a couple of days in about half hour or 15 minutes ( the esp is being turned off power everyday and when the lights are not needed , it’s not always on ) .
What I am thing is that ram is running full so I was thing if the mqtt message publish is the problem cause even though I have made the esp send a mqtt meassage “on esp “ every 1 minute every couple or 4 minutes it skips one
.
The first thing I did is delete the count in the mqtt publish loop ( it had a great affect in the time )
So is there any other way than Millis to make a meassage loop that doesn’t get to much memory ? And secondly will the char [50] and the 49 in the spnprintf change it with something lower will it also make any difference ?
Also I am using 4m 3m spiffs will this make the esp les functional for what we need more memory ?

Or what do you think is best what should I do ?
Ps when the power in the esp is turned off does also free memory ? If not is there a way to put it in the code to free memory in everyb boot ?
What do you think ?
Thanks in advance
Chris