PubSub 3sensors onChange asSeperateTopics

Aus hyperdramatik
Version vom 16. November 2021, 06:40 Uhr von HannahPernerWilson (Diskussion | Beiträge) (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…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen

ARDUINO CODE for ESP32

// 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[] = "topofthepops";
const char pass[] = "abarakadabarabistaklassahabara";

//const char ssid[] = "puppe1119";
//const char pass[] = "puppe2010";


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();
}