PubSub 3sensors onChange asSeperateTopics: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(Die Seite wurde neu angelegt: „ARDUINO CODE for ESP32 <source lang="js" line start="2" highlight="4-6"> // publishes 3 sensor values when these change, // subscribes to 3 sensor values whi…“) |
Keine Bearbeitungszusammenfassung |
||
Zeile 1: | Zeile 1: | ||
Upload this code to two ESPs. 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/"; | |||
==ARDUINO CODE for ESP32s== | |||
<source lang="js" line start="2" highlight="4-6"> | <source lang="js" line start="2" highlight="4-6"> | ||
Zeile 23: | Zeile 34: | ||
int threshold = 2500; | int threshold = 2500; | ||
const char ssid[] = " | const char ssid[] = "yourWifi"; | ||
const char pass[] = " | const char pass[] = "yourWifiPassword"; | ||
Version vom 16. November 2021, 04:45 Uhr
Upload this code to two ESPs. 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/";
ARDUINO CODE for ESP32s
// 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();
}