Hey guys, I'm working on a code that enables me to send commands to my Arduino Ethernet using MQTT, I then have a TRIAC connected. It works as long as I keep sending over values, once I stop for around a minute or so, I can't get the Arduino to respond. Any idea why this is?
Also the script seems quite long, I don't know too much about which formulas I should use but if anyone has any advice on how to simplify the code that would be great
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };
IPAddress ip(XXX, XXX, X, XX);
IPAddress server(XXX, XXX, X, XX);
const int lightPin = 5;
char msg[64];
char topic[32];
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
memcpy(msg, payload, length);
msg[length] = '\0';
if(strcmp(msg, "100") == 0) {
attachInterrupt(0, lightHigh, FALLING);
client.publish("outTopic", "Light 100"); }
if(strcmp(msg, "90") == 0) {
attachInterrupt(0, light90, FALLING);
client.publish("outTopic", "Light 90"); }
if(strcmp(msg, "80") == 0) {
attachInterrupt(0, light80, FALLING);
client.publish("outTopic", "Light 80"); }
if(strcmp(msg, "70") == 0) {
attachInterrupt(0, light70, FALLING);
client.publish("outTopic", "Light 70"); }
if(strcmp(msg, "60") == 0) {
attachInterrupt(0, light60, FALLING);
client.publish("outTopic", "Light 60"); }
if(strcmp(msg, "50") == 0) {
attachInterrupt(0, light50, FALLING);
client.publish("outTopic", "Light 50"); }
if(strcmp(msg, "40") == 0) {
attachInterrupt(0, light40, FALLING);
client.publish("outTopic", "Light 40"); }
if(strcmp(msg, "30") == 0) {
attachInterrupt(0, light30, FALLING);
client.publish("outTopic", "Light 30"); }
if(strcmp(msg, "20") == 0) {
attachInterrupt(0, light20, FALLING);
client.publish("outTopic", "Light 20"); }
if(strcmp(msg, "10") == 0) {
attachInterrupt(0, light10, FALLING);
client.publish("outTopic", "Light 10"); }
if(strcmp(msg, "OFF") == 0) {
attachInterrupt(0, lightOff, FALLING);
client.publish("outTopic", "Light Off"); }
}
void setup()
{
pinMode(lightPin, OUTPUT);
digitalWrite(lightPin, LOW);
Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
client.publish("outTopic","hello world");
client.subscribe("inTopic");
}
}
void lightHigh() {
digitalWrite(lightPin, HIGH); }
void lightOff() {
digitalWrite(lightPin, LOW); }
void light90() {
delayMicroseconds(34*(255-170));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light80() {
delayMicroseconds(34*(255-160));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light70() {
delayMicroseconds(34*(255-150));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light60() {
delayMicroseconds(34*(255-135));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light50() {
delayMicroseconds(34*(255-120));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light40() {
delayMicroseconds(34*(255-100));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light30() {
delayMicroseconds(34*(255-90));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light20() {
delayMicroseconds(34*(255-85));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void light10() {
delayMicroseconds(34*(255-80));
digitalWrite(lightPin, HIGH);
digitalWrite(lightPin, LOW); }
void loop()
{
client.loop();
}