Hi again. I'm hoping this is just a matter of my code being wrong but I have gone over it and over it and i can not see where the problem might be so i'm wondering if there might be something astray in the library.
I have a shield that has 6 buttons and 6 leds. Each press of a button toggles the corresponding led and publishes this to 'led/[led number]/[0|1]' This is working fine as much that i can press any button and the corresponding led comes on and the value is published UNTIL i have more than one in the ON state and then the library seems to think it is no longer connected to the broker and goes into a connect loop yet the broker does not seem to show the connection was dropped, nor does it see the attempts to reconnect.
The value of the second led does get published.
This is the cut down version of the sketch i am using to replicate the problem - Can you see any reason for this behavior?
# include
# include
# include
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip;
IPAddress MQTT_Server;
byte server[] = { 192, 168, 0, 1 };
// constants won't change. They're used here to
// set pin numbers:
int buttonPin[] = {4, 5, 6, 7, 8, 9}; // the number of the pushbutton pin
int ledPin[] = {14, 15, 16, 17, 18, 19}; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
byte ledState[] = {0,0,0,0,0,0}; // variable for storing the led state
EthernetClient ethClient;
PubSubClient MQTTClient(server, 1883, callback, ethClient);
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500; // interval at which to blink (milliseconds)
void setup() {
for( byte i = 0; i < 6; i++) {
// initialize the LED pin as an output:
pinMode(ledPin[i], OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin[i], INPUT);
// Turn it off
digitalWrite(ledPin[i], ledState[i]);
}
// open the serial port
Serial.begin(9600);
digitalWrite(13, HIGH);
// start the Ethernet connection:
delay(500);
digitalWrite(13, LOW);
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
char startTime[40] = "";
char s1[20];
itoa(millis(), s1, 10);
strcat(startTime, "started ");
strcat(startTime, s1);
MQTTClient.publish("foo", startTime);
digitalWrite(13, HIGH);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
```
for( int i = 0; i < 6; i++) {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin[i]);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
if (ledState[i] == HIGH) {
ledState[i] = LOW;
}
else {
ledState[i] = HIGH;
}
digitalWrite(ledPin[i], ledState[i]);
publishLedState(i, ledState[i]);
}
}
```
}
mqttConnect();
MQTTClient.loop();
}
void publishLedState(int ledNo, char ledToState) {
char s[10];
char t[1];
itoa(ledToState, s, 10);
itoa(ledNo, t, 10);
char publishString[6] = "led/";
publishString[4] = t[0];
Serial.print(publishString);
Serial.print("/");
Serial.println(s);
MQTTClient.publish(publishString, s);
}
void mqttConnect() {
if (!MQTTClient.connected()) {
Serial.println("Connecting to broker");
if (MQTTClient.connect("arduino")){
}else{
Serial.println("Failed to connect!");
}
}
}
void callback(char\* topic, byte\* payload,unsigned int length) {
Serial.print("Received topic update: ");
Serial.println(topic);
int i = topic[4];
int newState = atoi((char \* ) payload);
byte rv;
Serial.println(newState);
if (newState > 0) {
rv = HIGH;
} else {
rv = LOW;
}
digitalWrite(ledPin[i], rv);
}