PubSub 3sensors onChange asSeperateTopics: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 1: | Zeile 1: | ||
Upload this code to two ESPs. | 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};<br> | Each ESP has 3 LEDs wired to: int outputPins[] = {25, 26, 27};<br> | ||
Each ESP has 3 light sensors wired to: int inputPins[] = {36, 39, 34}; | 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: | Change the following lines to be different for each ESP: | ||
==ESP1:== | ===ESP1:=== | ||
const char* myName = "esp_dome1"; | const char* myName = "esp_dome1"; | ||
Zeile 13: | Zeile 17: | ||
String mySubTopic = "dome2/sensors/"; | String mySubTopic = "dome2/sensors/"; | ||
==ESP2:== | ===ESP2:=== | ||
const char* myName = "esp_dome2"; | const char* myName = "esp_dome2"; | ||
Zeile 21: | Zeile 25: | ||
String mySubTopic = "dome1/sensors/"; | String mySubTopic = "dome1/sensors/"; | ||
<source lang="js" line start="2" highlight="4-6"> | <source lang="js" line start="2" highlight="4-6"> | ||
Version vom 16. November 2021, 04:48 Uhr
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();
}