I know the code is probably horrible as I am not a real programmer, but, if the Arduino Uno is connected to the programming computer and there is a serial monitor open, it works perfectly.
As soon as the serial monitor is closed it no longer works.
I have tried moving the while serial command into the void loop() section to no avail.
There is an ethernet hat on the Arduino Uno and a DMX hat on top of that.
The serial monitor piece is the problem and any guidance appreciated.
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
2023-11-12
works, need to check it starts elegantly
maybe need to stop all serial print statements
figure the data seen on the serial monitor is the rs-485 dmx data
the ipaddress line is the ipaddress for the arduino and needs to be correct
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <ArduinoRS485.h> // the ArduinoDMX library depends on ArduinoRS485
#include <ArduinoDMX.h>
const int universeSize = 16;
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(xxx, xxx, xxx, xxx);
IPAddress server(xxx, xxx, xxx, xxx);
int dmx_channel1;
int dmx_channel2;
int dmx_channel3;
int dmx_channel4;
int dmx_channel5;
int dmx_channel6;
int dmx_channel7;
int dmx_channel8;
int dmx_channel9;
int dmx_channel10;
int dmx_channel11;
int dmx_data1;
int dmx_data2;
int dmx_data3;
int dmx_data4;
int dmx_data5;
int dmx_data6;
int dmx_data7;
int dmx_data8;
int dmx_data9;
int dmx_data10;
int dmx_data11;
int mqtt_data;
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
//Serial.print("Message arrived [");
//Serial.print(topic);
//Serial.print(": ");
//Serial.print(mqtt_data);
//Serial.print("] ");
payload[length] = '\0';
char * charPointer = (char *)payload;
double f=0;
int i=0;
String s="";
if(isDigit(charPointer[0])) {
f = atof(charPointer);
i = atoi(charPointer);
mqtt_data = i;
}
//Serial.begin(19200);
//Serial.print("topic just in: ");
//Serial.println(mqtt_data);
//Serial.println();
//rr stuff from the internet
if (strcmp(topic,"/all/on/")==0){
// possibly not required
}
if (strcmp(topic,"/all/off/")==0) {
// possibly not required
}
if (strcmp(topic,"/all/hello/")==0) {
// possibly not required
}
if (strcmp(topic,"/lightone/ch1/")==0) {
dmx_channel1 = 1;
dmx_data1 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch2/")==0){
dmx_channel2 = 2;
dmx_data2 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch3/")==0) {
dmx_channel3 = 3;
dmx_data3 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch4/")==0) {
dmx_channel4 = 4;
dmx_data4 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch5/")==0) {
dmx_channel5 = 5;
dmx_data5 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch6/")==0){
dmx_channel6 = 6;
dmx_data6 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch7/")==0) {
dmx_channel7 = 7;
dmx_data7 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch8/")==0) {
dmx_channel8 = 8;
dmx_data8 = mqtt_data;
DMX.beginTransmission();
DMX.write(8,dmx_data8);
DMX.endTransmission();
}
if (strcmp(topic,"/lightone/ch9/")==0) {
dmx_channel9 = 9;
dmx_data9 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch10/")==0){
dmx_channel10 = 10;
dmx_data10 = atoi(payload);
}
if (strcmp(topic,"/lightone/ch11/")==0) {
dmx_channel11 = 11;
dmx_data11 = atoi(payload);
}
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void setup()
{
//Serial.begin(19200);
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this use the
// following to increase the buffer size:
// client.setBufferSize(255);
if (client.connect("arduinoClient", "xyzxyz", "abcdef")) {
client.publish("outTopic","hello from richard's new world empire");
client.subscribe("inTopic");
client.subscribe("/all/on/");
client.subscribe("/all/off/");
client.subscribe("/all/hello/");
client.subscribe("/lightone/ch1/"); //ch_1 pan data 0 to 255
client.subscribe("/lightone/ch2/"); //ch_2 pan fine data 0 to 255
client.subscribe("/lightone/ch3/"); //ch_3 tilt data 0 to 255
client.subscribe("/lightone/ch4/"); //ch_4 tilt fine data 0 to 255
client.subscribe("/lightone/ch5/"); //ch_5 colour #1 0-7, #2 8-14, #3 15-21, #4 22-28, #5 29-35, #6 36-42, #7 43-49, #8 50-56, half colour 57-127, fast-slow-stop 128-189, slow-fast 190-193, unused 194-255
client.subscribe("/lightone/ch6/"); //ch_6 gobo effects refer to datasheet
client.subscribe("/lightone/ch7/"); //ch_7 dimming on 0-7, off 8-15, slow-fast 16-131, off 132-139, fast-slow-off 140-181, off 182-189, close quickly and open slowly 190-231, off 232-239, dimming 240-247, off 248-255
client.subscribe("/lightone/ch8/"); //ch_8 lighting data 0 to 255
client.subscribe("/lightone/ch9/"); //ch_9 fast-slow pan and tilt data 0 to 255
//client.subscribe("/lightone/ch10/"); //ch_10 model refer to datasheet
//client.subscribe("/lightone/ch11/"); //ch_11 dimming mode standard 0-20, stage 21-40, tv 41-60, building 61-80, theatre 81-100, unused 101-255
}
Serial.begin(19200); //have tried 250k baud and commenting out all together
while (!Serial);
// initialize the DMX library with the universe size
if (!DMX.begin(universeSize)) {
//Serial.println("Failed to initialize DMX!");
while (1); // wait for ever
}
}
void loop()
{
client.loop();
DMX.beginTransmission();
DMX.write(1, dmx_data1);
DMX.write(2, dmx_data2);
DMX.write(3, dmx_data3);
DMX.write(4, dmx_data4);
DMX.write(5, dmx_data5);
//DMX.write(6, dmx_data6); //gobo unused for now
DMX.write(7, dmx_data7);
DMX.write(8, dmx_data8);
DMX.write(9, dmx_data9);
//DMX.write(10, dmx_data10); //unlikely to be used
//DMX.write(11, dmx_data11);
DMX.endTransmission();
//delay(100);
}