Hello, adding upon the sample MQTT sketch I have the below sketch to publish 2 sensors and subscribe 2 relays. Only the one sensor publishes and creates a variable.
Can anyone tell me what I am missing?
#define VARIABLE_LABEL_PUBLISH "sensor"
#define VARIABLE_LABEL_SUBSCRIBE "relay"
#define VARIABLE_LABEL_PUBLISH "sensor2"
#define VARIABLE_LABEL_SUBSCRIBE "relay2"
#define DEVICE_LABEL "esp32"
#define SENSOR 14
#define RELAY 16
#define SENSOR2 13
#define RELAY2 15
char mqttBroker[] = "things.ubidots.com";
char payload[100];
char payload2[100];
char topic[150];
char topic2[150];
// Space to store values to send
char str_sensor[10];
char str_sensor2[10];
/****************************************
* Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message == "0") {
digitalWrite(RELAY, LOW);
} else {
digitalWrite(RELAY, HIGH);
}
Serial.write(payload, length);
Serial.println();
}
void callback2(char* topic2, byte* payload2, unsigned int length) {
char q[length + 1];
memcpy(q, payload2, length);
q[length] = NULL;
String message(q);
if (message == "0") {
digitalWrite(RELAY2, LOW);
} else {
digitalWrite(RELAY2, HIGH);
}
Serial.write(payload2, length);
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
// Assign the pins as INPUT/OUTPUT
pinMode(SENSOR, INPUT);
pinMode(RELAY, OUTPUT);
pinMode(SENSOR2, INPUT);
pinMode(RELAY2, OUTPUT);
Serial.println();
Serial.print("Wait for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
client.setServer(mqttBroker, 1883);
client.setCallback(callback2);
client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");
}
void loop() {
if (!client.connected()) {
reconnect();
client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");
}
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload2, "%s", ""); // Cleans the payload2
sprintf(payload, "{\"%s\":", VARIABLE_LABEL_PUBLISH); // Adds the variable label
sprintf(payload2, "{\"%s\":", VARIABLE_LABEL_PUBLISH); // Adds the variable label
float sensor = analogRead(SENSOR);
float sensor2 = analogRead(SENSOR2);
/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(sensor, 4, 2, str_sensor);
dtostrf(sensor2, 4, 2, str_sensor2);
sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value
sprintf(payload2, "%s {\"value\": %s}}", payload2, str_sensor2); // Adds the value
Serial.println("Publishing data to Ubidots Cloud");
client.publish(topic, payload);
client.publish(topic2, payload2);
client.loop();
delay(1000);
}