Hallo,
gibt es eine möglichkeit beim MQTT, zu warten bzw. abzufragen ob/wann das erste mal Daten gesendet wurden?
Meine Problem ist jense, dass ich gerne per Python und user input (GUI) dynamische Topic anlgen möchte.
Bedeutet wenn ich einen neuen Sensor in mein System einbringen will, dann soll ich dem Sensor mittels GUI Informationen zuordnen und über eine dieser Informationen soll dann der ESP8266 wo meine Sensoren angeschlossen sind, das Topic auf welchem er dann die Daten sendet wählen.
Somit ein dynamisches Auswählen der Topic mittels User Input über eine GUI.
Jemand dazu eine Idee?
Hier mal mein versuch aber das läuft natürlich so nicht..
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define S0 16 //D0 /* Assign Multiplexer pin S0 connect to pin D0 of NodeMCU */
#define S1 5 //D1 /* Assign Multiplexer pin S1 connect to pin D1 of NodeMCU */
#define S2 4 //D2 /* Assign Multiplexer pin S2 connect to pin D2 of NodeMCU */
#define S3 0 //D3 /* Assign Multiplexer pin S3 connect to pin D3 of NodeMCU */
#define SIG A0 /* Assign SIG pin as Analog output for all 16 channels of Multiplexer to pin A0 of NodeMCU */
int decimal = 2; // Decimal places of the sensor value outputs
int sensor0; /* Assign the name "sensor0" as analog output value from Channel C0 */
int sensor1; /* Assign the name "sensor1" as analog output value from Channel C1 */
int sensor2; /* Assign the name "sensor2" as analog output value from Channel C2 */
int sensor3; /* Assign the name "sensor3" as analog output value from Channel C3 */
int sensor4; /* Assign the name "sensor4" as analog output value from Channel C4 */
int sensor5; /* Assign the name "sensor5" as analog output value from Channel C5 */
int sensor6; /* Assign the name "sensor6" as analog output value from Channel C6 */
int sensor7; /* Assign the name "sensor7" as analog output value from Channel C7 */
int sensor8; /* Assign the name "sensor8" as analog output value from Channel C8 */
int sensor9; /* Assign the name "sensor9" as analog output value from Channel C9 */
int sensor10; /* Assign the name "sensor10" as analog output value from Channel C10 */
int sensor11; /* Assign the name "sensor11" as analog output value from Channel C11 */
int sensor12; /* Assign the name "sensor12" as analog output value from Channel C12 */
int sensor13; /* Assign the name "sensor13" as analog output value from Channel C13 */
int sensor14; /* Assign the name "sensor14" as analog output value from Channel C14 */
int sensor15; /* Assign the name "sensor15" as analog output value from Channel C15 */
const char* ssid = "xxxx";
const char* password = "xxxx";
const char* mqtt_server = "xxxx";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
#define DHTPIN 12 //D6 // Digital pin connected to the DHT sensor
#define MoisturePin sensor0 // used for Arduino and ESP8266
#define rainAnalog sensor1
#define rainDigital 13 //D7
#define ONE_WIRE_BUS 2 //D4
float wet = 905;
float dry = 420;
float MoistureDeconnect = 18;
int percentageHumididy = 0;
#define DHTTYPE DHT11 // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
uint32_t delayMS;
String topicout;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
while (length == 0){
}
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
topicout = topicout + (char) payload[i]; // convert *byte to string
Serial.print("Topic out:");
Serial.println(topicout);
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
client.subscribe("basetopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(S0,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin SO */
pinMode(S1,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S1 */
pinMode(S2,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S2 */
pinMode(S3,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S3 */
pinMode(SIG, INPUT); /* Define analog signal pin as input or receiver from the Multiplexer pin SIG */
Serial.begin(9600);
// Initialize device.
dht.begin();
Serial.println(F("DHTxx Unified Sensor Example"));
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
Serial.println("Dallas Temperature IC Control Library Demo");
sensors.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(rainDigital,INPUT);
}
void loop() {
while (topicout.length() == 0){
}
String t_DHT11_o = topicout+ "/" + "t_DHT11_o";
String m_DHT11_o = topicout+ "/" + "m_DHT11_o";
String m_val_o = topicout+ "/" + "m_val_o";
String m_perc_o = topicout+ "/" + "m_perc_o";
String raing_d_o = topicout+ "/" + "rain_d_o";
String rain_a_o = topicout+ "/" + "rain_a_o";
String t_plant_o = topicout+ "/" + "t_plant_o";
if (!client.connected()) {
reconnect();
}
client.loop();
// Delay between measurements.
delay(delayMS);
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
client.publish(t_DHT11_o, "Error reading temperature!");
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
float Temp_DHT11_f = event.temperature;
char Temp_DHT11[10];
dtostrf(Temp_DHT11_f, 9, 2, Temp_DHT11);
client.publish(t_DHT11_o, Temp_DHT11);
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
client.publish(m_DHT11_o, "Error reading humidity!");
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
float Mois_DHT11_f = event.relative_humidity;
char Mois_DHT11[10];
dtostrf(Mois_DHT11_f, 9, 2, Mois_DHT11);
client.publish(m_DHT11_o, Mois_DHT11);
}
digitalWrite(S0, LOW);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
float MoistureVal = analogRead(MoisturePin);
Serial.print("Moisture Sensor Raw Value = ");
Serial.println(MoistureVal);
percentageHumididy = (MoistureVal - wet) * (100 - 0) / (dry - wet) + 0;
Serial.print("Humidity Moisture Sensor = ");
Serial.print(percentageHumididy);
Serial.println("%");
char Moisture[10];
dtostrf(MoistureVal, 9, 2, Moisture);
client.publish(m_val_o, Moisture);
char Moisture_percent[10];
dtostrf(percentageHumididy, 9, 2, Moisture_percent);
client.publish(m_perc_o, Moisture_percent);
int rainDigitalVal = digitalRead(rainDigital);
Serial.print(F("Rain digital: "));
Serial.println(rainDigitalVal);
char RAIN_DIG[10];
dtostrf(rainDigitalVal, 9, 2, RAIN_DIG);
client.publish(raing_d_o, RAIN_DIG);
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
int rainAnalogVal = analogRead(rainAnalog);
Serial.print(F("Rain analog: "));
Serial.println(rainAnalogVal);
char RAIN_ANALOG[10];
dtostrf(rainAnalogVal, 9, 2, RAIN_ANALOG);
client.publish(rain_a_o, RAIN_ANALOG);
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
// Check if reading was successful
if(tempC != DEVICE_DISCONNECTED_C)
{
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(tempC);
char Temp_Plant[10];
dtostrf(tempC, 9, 2, Temp_Plant);
client.publish(t_plant_o, Temp_Plant);
}
else
{
Serial.println("Error: Could not read temperature data");
client.publish(t_plant_o, "Error: Could not read temperature data");
}
delay(delayMS);
}
Topicout sollte dann eben per GUI von Python gesendet werden über das Basetopic damit dann die Sensoren an Topicout ihre werte senden..
bei Fragen einfach melden..
Have a nice day! THX