PubSub 3sensors onChange asSeperateTopics
This example code publishes information about 3 analog sensor values only when they pass above or bellow a threshold value. The rise or fall of each sensor value is published to it's own subtopic. A second ESP subscribes to all sensor subtopics and turns it's LEDs on/off depending on the rise or fall of the other ESPs sensor values. Both ESPs do the same so that sensors on the one, control LEDs on the other.
Upload this code to two ESPs. The light sensors on one ESP with control the LEDs on the other.
The Circuit:
Each ESP has 3 LEDs wired to: int outputPins[] = {25, 26, 27};
Each ESP has 3 light sensors wired to: int inputPins[] = {36, 39, 34};
ARDUINO CODE for ESP32s
Change the following lines to be different for each ESP:
ESP1:
const char* myName = "esp_dome1";
String myPubTopic = "dome1/sensors/";
String mySubTopic = "dome2/sensors/";
ESP2:
const char* myName = "esp_dome2";
String myPubTopic = "dome2/sensors/";
String mySubTopic = "dome1/sensors/";
// publishes 3 sensor values when these change,
// subscribes to 3 sensor values which turns on/off LED lights.
// using Shiftr Desktop Broker: https://www.shiftr.io/desktop
#include <WiFi.h>
#include <MQTT.h>
WiFiClient net;
MQTTClient client;
const char* myName = "esp_dome2";
String myPubTopic = "dome2/sensors/";
String mySubTopic = "dome1/sensors/";
int outputPins[] = {25, 26, 27};
int inputPins[] = {36, 39, 34};
int sensorValues[3];
int previousSensorValues[3];
int threshold = 2500;
const char ssid[] = "yourWifi";
const char pass[] = "yourWifiPassword";
void setup() {
//declare INPUTS:
//declare OUTPUTS:
for (int i = 0; i < 3; i++) {
pinMode(outputPins[i], OUTPUT);
}
Serial.begin(9600); //begin serial communication
// start wifi and mqtt:
WiFi.begin(ssid, pass);
client.begin("192.168.0.9", net);
client.onMessage(messageReceived);
connect();
}
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
//client.connect("client ID", "name of your instance", "token secret")
//when connecting to local broker leave name and token empty:
while (!client.connect(myName, "", "")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe(mySubTopic + "*");
}
void messageReceived(String &topic, String &payload) {
// print the message received to serial port:
Serial.println("Subscribed: " + topic + " / " + payload);
for (int i = 0; i < 3; i++) {
if (topic == mySubTopic + String(i)) {
if (payload == "0") digitalWrite(outputPins[i], HIGH);
if (payload == "1") digitalWrite(outputPins[i], LOW);
}
}
}
void loop() {
// perform important MQTT things:
client.loop();
// check if connected
if (!client.connected()) {
connect();
}
// publish to my topic whenever sensor values fall bellow threshold:
for (int i = 0; i < 3; i++) {
sensorValues[i] = analogRead(inputPins[i]);
Serial.print(sensorValues[i]);
Serial.print(" ");
String myCurrentPubTopic = myPubTopic + i;
if (sensorValues[i] < threshold && previousSensorValues[i] > threshold) client.publish(myCurrentPubTopic, "0");
if (sensorValues[i] > threshold && previousSensorValues[i] < threshold) client.publish(myCurrentPubTopic, "1");
previousSensorValues[i] = sensorValues[i];
//Serial.println("myPubTopic: " + myPubTopic + " / " + myMessage);
}
Serial.println();
}