PubSub 3sensors continuous asStringList

Aus hyperdramatik
Zur Navigation springen Zur Suche springen

This example code publishes 3 analog sensor values continuously as a String and subscribes to a String of 3 analog sensor values from another ESP, unpacks these into integers and turn on/off LEDs depending on the integer values in comparison to a threshold you can set.

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 analog sensor values continuously as a String 
// subscribes to 3 analog sensor values and unpacks the string into integers
// turns on/off LEDs depending on incoming sensor values
// using Shiftr Desktop Broker: https://www.shiftr.io/desktop

#include <WiFi.h>
#include <MQTT.h>

WiFiClient net;
MQTTClient client;

const char* myName = "esp_dome1";
String myPubTopic = "dome2/sensors";
String mySubTopic = "dome1/sensors";
int threshold = 2500;
int publishFrequency = 33; //publish every x miliseconds
unsigned long lastMillis = 0;

int inputPin1 = 36;
int inputPin2 = 39;
int inputPin3 = 34;
int outputPin1 = 25;
int outputPin2 = 26;
int outputPin3 = 27;
int incomingSensorValues[3];

const char ssid[] = "topofthepops";
const char pass[] = "abarakadabarabistaklassahabara";

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


void setup() {
  //declare INPUTS:
  pinMode(inputPin1, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
  //declare OUTPUTS:
  pinMode(outputPin1, OUTPUT);
  pinMode(outputPin2, OUTPUT);
  pinMode(outputPin3, 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) {
  Serial.println("mySubTopic: " + topic + " / " + payload);

  //call function that converts the incoming payload String to an array of Integers:
  convertStringToInttegers(payload);

  if (incomingSensorValues[0] < threshold) digitalWrite(outputPin1, HIGH);
  else digitalWrite(outputPin1, LOW);
  
  if (incomingSensorValues[1] < threshold) digitalWrite(outputPin2, HIGH);
  else digitalWrite(outputPin2, LOW);
  
  if (incomingSensorValues[2] < threshold) digitalWrite(outputPin3, HIGH);
  else digitalWrite(outputPin3, LOW);
}


void loop() {
  client.loop();  // perform important MQTT things!
  // check if connected
  if (!client.connected()) {
    connect();
  }

  // publish a message every x miliseconds.
  if (millis() - lastMillis > publishFrequency) {
    lastMillis = millis();
    String myMessage = String(analogRead(inputPin1)) + " " + String(analogRead(inputPin2)) + " " + String(analogRead(inputPin3));
    client.publish(myPubTopic, myMessage);
    Serial.println("myPubTopic: " + myPubTopic + " / " + myMessage);
  }
}

void convertStringToInttegers (String stringToConvert) {
  char myCharArray[15];
  int i = 0;

  stringToConvert.toCharArray(myCharArray, 15);
  char *ptr;
  ptr = strtok(myCharArray, " ");

  while (ptr != NULL) {
    incomingSensorValues[i] = String(ptr).toInt();
    ptr = strtok(NULL, " ");
    i++;
  }
}