So my sketch is pretty simple, connects to wifi, then to adafruit.io to send and receive data.
I've created my 2 subscription feeds as required:
- Code: Select all | TOGGLE FULL SIZE
const char ONOFF_RELAY1_FEED[] PROGMEM = AIO_USERNAME "/feeds/relay-01";
Adafruit_MQTT_Subscribe relay1 = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_RELAY1_FEED);
const char ONOFF_WANHAOi3[] PROGMEM = AIO_USERNAME "/feeds/wanhao-i3";
Adafruit_MQTT_Subscribe WANHAOi3 = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_WANHAOi3);
I then have my 2 subscribe commands
- Code: Select all | TOGGLE FULL SIZE
mqtt.subscribe(&relay1);
mqtt.subscribe(&WANHAOi3);
And now the subscription code:
- Code: Select all | TOGGLE FULL SIZE
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &relay1) {
char *MQTTVal = (char *)relay1.lastread;
Serial.print(F("Relay 1 - Got: "));
Serial.println(MQTTVal);
if (0 == strcmp((char *)relay1.lastread, "OFF")) {
Serial.println(F("Turning Relay 1 OFF"));
digitalWrite(Relay_1, RELAY_OFF);
}
if (0 == strcmp((char *)relay1.lastread, "ON")) {
Serial.println(F("Turning Relay 1 ON"));
digitalWrite(Relay_1, RELAY_ON);
}
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", OFF) = ")); Serial.println(strcmp((char *)relay1.lastread, "OFF"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", ON) = ")); Serial.println(strcmp((char *)relay1.lastread, "ON"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", OFF1) = ")); Serial.println(strcmp((char *)relay1.lastread, "OFF1"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", ON1) = ")); Serial.println(strcmp((char *)relay1.lastread, "ON1"));
}
else if (subscription == &WANHAOi3) {
char *MQTTVal = (char *)WANHAOi3.lastread;
Serial.print(F("Wanhao i3 - Got: "));
Serial.println(MQTTVal);
if (0 == strcmp((char *)WANHAOi3.lastread, "OFF1")) {
Serial.println(F("Turning Wanhao i3 OFF"));
digitalWrite(WANHAO_RELAY, TTL_RELAY_OFF);
}
if (0 == strcmp((char *)WANHAOi3.lastread, "ON1")) {
Serial.println(F("Turning Wanhao i3 OFF"));
digitalWrite(WANHAO_RELAY, TTL_RELAY_ON);
}
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", OFF) = ")); Serial.println(strcmp((char *)WANHAOi3.lastread, "OFF"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", ON) = ")); Serial.println(strcmp((char *)WANHAOi3.lastread, "ON"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", OFF1) = ")); Serial.println(strcmp((char *)WANHAOi3.lastread, "OFF1"));
Serial.print(F("STRCMP(")); Serial.print(MQTTVal); Serial.print(F(", ON1) = ")); Serial.println(strcmp((char *)WANHAOi3.lastread, "ON1"));
}
}
Now, I'm really really confused and stuck. For "Relay 1" everything works perfectly.
I added some addition serial prints to help me debug.
So for relay 1 the output is:
- Code: Select all | TOGGLE FULL SIZE
Relay 1 - Got: OFF
Turning Relay 1 OFF
STRCMP(OFF, OFF) = 0
STRCMP(OFF, ON) = -8
STRCMP(OFF, OFF1) = -49
STRCMP(OFF, ON1) = -8
When, however, I toggle the switch for the second relay, it fails. The strcmp is way off but I don't understand why. At first I had just copy and pasted the code, then when testing, adafruit.io appeared to sending ON1 or OFF1 instead of just ON or OFF.
Here's the results of the same strcmp output for the Wanhao i3 relay
- Code: Select all | TOGGLE FULL SIZE
Wanhao i3 - Got: ON1
STRCMP(ON1, OFF) = 8
STRCMP(ON1, ON) = 49
STRCMP(ON1, OFF1) = 8
STRCMP(ON1, ON1) = 27
Unless I'm missing something stupid, surely STRCMP(ON1, ON1) *should* = 0 ??
What am I doing wrong?