MQTT in Wettermonster Code

Hallo,

habe versucht als Anfänger in den Wettermonster code meiner Wetterstation zusätzlich den Versand der Messwerte an meinen netzwerkinternen MQTT Server zu senden. Komm aber hier einfach nicht weiter.

Ich hoffe jemand kann mir einen Tipp geben, wie ich das am besten bewerkstellige.
Anbei mein bisheriger Code. Die zusätzlichen Code Zeilen, die ich eingefügt habe, sind mit “//add for MQTT” gekennzeichnet.

Ziel ist es wie gesagt, alle Messwerte:

float temperature;
float humidity;
float pressure;
float Percipitation;
float numClicksRain;
float windSpeed;
float numRevsAnemometer;
char* windDirection;
float luminosity;

zusätzlich an meinen MQTT zu senden.

Hier noch der Gesamt Code:



#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Adafruit_TSL2591.h"
#include <PubSubClient.h> //*add for MQTT

Adafruit_BME280 bme;
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);


const char* ssid = "XXXX";
const char* password = "XXXX";
const char* id = "XXXX";
const char* key = "XXXX";
const int interval = 5;

const char* MQTT_BROKER = "192.168.178.2"; //add for MQTT

float temperature;
float humidity;
float pressure;
float Percipitation;
float numClicksRain;
float windSpeed;
float numRevsAnemometer;
char* windDirection;
float luminosity;
volatile unsigned long previousTimeRain=0, previousTimeSpeed=0, delayTime=20;
unsigned long lastMillis;


WiFiClient client;
PubSubClient client(client); //add for MQTT


void ICACHE_RAM_ATTR countAnemometer();
void ICACHE_RAM_ATTR countRain();

void sendToWettermonster() {

	int i = 0;
	while(WiFi.status() != WL_CONNECTED && i <= 5)
	{
   i++;
   Serial.println("WiFi nicht verbunden. Versuche neu zu verbinden...");
   WiFi.disconnect();
   WiFi.mode(WIFI_OFF);
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);
   delay(1000);
	}

	if(i > 5)
	{
   Serial.println("Verbindnung zu " + String(ssid) + " fehlgeschlagen. Neustart.");
   ESP.restart();
	}

	if (WiFi.status() == WL_CONNECTED && client.connect("upload.wettermonster.de", 80)) // Verbindung zum Server aufbauen
	{

		Serial.println ("Verbunden mit upload.wettermonster.de");
		client.print("GET /speichern.php");
		client.print("?id=");
		client.print(id);
		client.print("&schluessel=");
		client.print(key);
		client.print("&temperatur=");
		client.print(temperature);
		client.print("&luftfeuchtigkeit=");
		client.print(humidity);
		client.print("&luftdruck=");
		client.print(pressure);
		client.print("&niederschlag=");
		client.print(Percipitation);
		client.print("&windgeschwindigkeit=");
		client.print(windSpeed);
		client.print("&windrichtung=");
		client.print(windDirection);
		client.print("&helligkeit=");
		client.print(luminosity);
		client.println(" HTTP/1.1");
		client.println("Host: upload.wettermonster.de");
		client.println("User-Agent: Wettermonster");
		client.println("Accept: text/html");
		client.println();

		unsigned long timeout = millis();
		    while (client.available() == 0) {
		      yield();
		      if (millis() - timeout > 5000) {
		        Serial.println("Timeout !");
		        client.stop();
		        return;
		      }
		    }

		Serial.println("Daten an Wettermonster gesendet");
	}

	else
	{
		Serial.println("Verbindung fehlgeschlagen");
	}

	client.stop();
}

void readBME() {

	temperature = bme.readTemperature();
	humidity = bme.readHumidity();
	pressure = bme.readPressure() / 100.0F;

}
void readTSL() {

	uint32_t lum;
	uint16_t ir, full, visible, gain, timing;
	boolean change = false;
	String url_temp;

	tslagain:

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);

 	for (int i=0; i == 0 || luminosity > 150000.0 || luminosity < 0; i++){
	while (((ir>16000) || (full > 40000) || (luminosity <= 0)) && (gain > 0))  {
		change = true;

		switch(gain)
		{
		case TSL2591_GAIN_MED:
			tsl.setGain(TSL2591_GAIN_LOW);
			break;
		case TSL2591_GAIN_HIGH:
			tsl.setGain(TSL2591_GAIN_MED);
			break;
		case TSL2591_GAIN_MAX:
			if (timing > 0) {
				timing--;
				tsl.setTiming(tsl2591IntegrationTime_t(timing));
			}
			else {
				tsl.setGain(TSL2591_GAIN_MED);
			}
			break;
			default:
			break;
		}

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);
	delay (500);
	}

	while ((ir<500) && (full < 1000) && (timing < 5))  {
		change = true;

		switch(gain)
		{
			case TSL2591_GAIN_LOW:
				tsl.setGain(TSL2591_GAIN_MED);
	 			break;
			case TSL2591_GAIN_MED:
	 			tsl.setGain(TSL2591_GAIN_HIGH);
				break;
			case TSL2591_GAIN_HIGH:
			 	tsl.setGain(TSL2591_GAIN_MAX);
				break;
			case TSL2591_GAIN_MAX:
				if (timing < 5) {
					timing++;
					tsl.setTiming(tsl2591IntegrationTime_t(timing));
				}
				break;
				default:
				break;
		}

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);
	delay (500);
	}

	if (change == true) {
		lum = tsl.getFullLuminosity();
		gain = tsl.getGain();
		timing = tsl.getTiming();
		ir = lum >> 16;
		full = lum & 0xFFFF;
		luminosity = tsl.calculateLux(full, ir);
	}
	delay (500);
	}

}

void countAnemometer() {
	if((millis() - previousTimeSpeed) > delayTime) {
		numRevsAnemometer++;
 		previousTimeSpeed = millis();
	}
}

void countRain() {
	if((millis() - previousTimeRain) > delayTime) {
		numClicksRain++;
		previousTimeRain = millis();
	}
}

void readWeatherMeters() {

	windSpeed = (numRevsAnemometer / (interval * 60)) * 2.4;
	numRevsAnemometer = 0;

	Percipitation = 0.2794 * (numClicksRain * 60 / interval);
	numClicksRain = 0;

	int windDirectionVoltage = analogRead(A0);

	if (windDirectionVoltage >= 212 && windDirectionVoltage < 273)    {windDirection = "N";}
	else if (windDirectionVoltage >= 577 && windDirectionVoltage < 665) {windDirection = "NNE";}
	else if (windDirectionVoltage >= 483 && windDirectionVoltage < 577) {windDirection = "NE";}
	else if (windDirectionVoltage >= 929 && windDirectionVoltage < 943) {windDirection = "ENE";}
	else if (windDirectionVoltage >= 906 && windDirectionVoltage < 929) {windDirection = "E";}
	else if (windDirectionVoltage >= 943 && windDirectionVoltage < 1023){windDirection = "ESE";}
	else if (windDirectionVoltage >= 795 && windDirectionVoltage < 858) {windDirection = "SE";}
	else if (windDirectionVoltage >= 858 && windDirectionVoltage < 906) {windDirection = "SSE";}
	else if (windDirectionVoltage >= 665 && windDirectionVoltage < 748) {windDirection = "S";}
	else if (windDirectionVoltage >= 748 && windDirectionVoltage < 795) {windDirection = "SSW";}
	else if (windDirectionVoltage >= 348 && windDirectionVoltage < 399) {windDirection = "SW";}
	else if (windDirectionVoltage >= 399 && windDirectionVoltage < 483) {windDirection = "WSW";}
	else if (windDirectionVoltage >= 0 && windDirectionVoltage < 106)   {windDirection = "W";}
	else if (windDirectionVoltage >= 163 && windDirectionVoltage < 212) {windDirection = "WNW";}
	else if (windDirectionVoltage >= 106 && windDirectionVoltage < 163) {windDirection = "NW";}
	else if (windDirectionVoltage >= 273 && windDirectionVoltage < 348) {windDirection = "NNW";}
}

void setup() {
	Serial.begin(115200);

 WiFi.mode(WIFI_STA);
	WiFi.begin(ssid, password);
	Serial.println("");
 Serial.print("Verbinde mit " + String(ssid));

 client.setServer(MQTT_BROKER, 1883); //add for MQTT

	while (WiFi.status() != WL_CONNECTED) {
		Serial.print(".");
		delay(500);
	}

	Serial.println("Wifi Aktiviert");
	Serial.println("");
	Serial.print("Verbunden mit: ");
	Serial.println(ssid);
	Serial.print("IP Adresse: ");
	Serial.println(WiFi.localIP());

	pinMode(2, INPUT_PULLUP);
	attachInterrupt(2, countRain, FALLING);
	pinMode(14, INPUT_PULLUP);
	attachInterrupt(14, countAnemometer, FALLING);

	if (!bme.begin()){
		Serial.println("BME280 konnte nicht gefunden werde, checke bitte die Verbindungen!");
		return;
	}

	if (tsl.begin()){
		tsl.setGain(TSL2591_GAIN_LOW);
		tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);
	}
	else{
		Serial.println("TSL2591 konnte nicht gefunden werde, checke bitte die Verbindungen!");
		return;
	}

	lastMillis = millis();
}

void loop() {

	if (millis() - lastMillis > (interval * 60000)) {
		readBME();
		readTSL();
		readWeatherMeters();
		sendToWettermonster();
		lastMillis = millis();
	}

	if(ESP.getFreeHeap() <= 20000){
		Serial.println("Der freie Heap beträgt nur noch: " + String(ESP.getFreeHeap()) + " Der ESP wird deshalb neu gestartet.");
		ESP.restart();
	}
 
  if (!client.connected()) {              //add for MQTT
     while (!client.connected()) {        //add for MQTT
        client.connect("ESP8266Client");  //add for MQTT

	delay(100);
 
  }                                             //add for MQTT
    client.loop();                              //add for MQTT
    client.publish("/home/data", temperature);  //add for MQTT
  }                                             //add for MQTT

}

Danke

Was die Lösung Deines Problems ist, weiß ich nicht.

Du solltest IMO aber Deinen Code unbedingt ordentlich formatieren - einheitliche Klammerungen, Einzüge … das ist zumindest schon einmal sehr hilfreich beim Lesen Deines Codes.

Gruß

Gregor

Ich glaube es macht keinen Sinn dem Server die Daten zyklisch um die Ohren zu hauen zumal sie sich maximal alle 5 Minuten ändern können…
Außerdem kannst du keine float Werte verschicken sondern musst sie vorher in char oder byte umwandeln.

Gruß

Scherheinz

Hallo Scherheinz,

danke für deine Unterstützung. Du hast recht, da werde ich auf jeden Fall optimieren.
Ich denke aber, dass die Ursache bei einem Denk / Programmierfehler im Anfangsbereich des Code liegt.

speziell geht es um:

WiFiClient client;
PubSubClient client(client); 

Da die Output bei Code verifizieren wie folgt lautet:


/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:46:27: error: conflicting declaration 'PubSubClient client'
 PubSubClient client(client); //add for MQTT
                           ^
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:45:12: error: 'client' has a previous declaration as 'WiFiClient client'
 WiFiClient client;
            ^
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino: In function 'void setup()':
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:275:9: error: 'class WiFiClient' has no member named 'setServer'
  client.setServer(MQTT_BROKER, 1883); //add for MQTT
         ^
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino: In function 'void loop()':
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:328:39: error: no matching function for call to 'WiFiClient::connect(const char [14])'
         client.connect("ESP8266Client");  //add for MQTT
                                       ^
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:328:39: note: candidates are:
In file included from /Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/ESP8266WiFi.h:39:0,
                 from /Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:10:
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:56:15: note: virtual int WiFiClient::connect(IPAddress, uint16_t)
   virtual int connect(IPAddress ip, uint16_t port) override;
               ^
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:56:15: note:   candidate expects 2 arguments, 1 provided
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:57:15: note: virtual int WiFiClient::connect(const char*, uint16_t)
   virtual int connect(const char *host, uint16_t port) override;
               ^
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:57:15: note:   candidate expects 2 arguments, 1 provided
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:58:15: note: virtual int WiFiClient::connect(const String&, uint16_t)
   virtual int connect(const String& host, uint16_t port);
               ^
/Users/thomas/Library/Arduino15/packages/esp8266/hardware/esp8266/2.7.4/libraries/ESP8266WiFi/src/WiFiClient.h:58:15: note:   candidate expects 2 arguments, 1 provided
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:333:12: error: 'class WiFiClient' has no member named 'loop'
     client.loop();                              //add for MQTT
            ^
/Users/thomas/Documents/Arduino/Code-Wettermonster_210429/Code-Wettermonster_210429.ino:334:12: error: 'class WiFiClient' has no member named 'publish'
     client.publish("/home/data", temperature);  //add for MQTT
            ^
Compilation error: Error: 2 UNKNOWN: exit status 1


Ich habe auch schon versucht die clients unterschiedlich zu deklarieren. auch ohne Erfolg.

Wenn ich in anderen Programmcodes nachschau, aus die nur eine MQTT Publishing enthalten und keinen Webserver sieht das z.B. so aus:

WiFiClient espClient;
PubSubClient client(espClient);

eigentlich das gleiche, was ich auch mache, nur das bei mir espClient eben client ist.

Gruß Thomas

Dann zeig mal wie du unterschiedlich deklariert hast.

Gruß

Hallo Scherheinz,

danke für deinen Denkanstoß. Leider hatte ich in letzter Zeit immer nur ein paar Minuten mich damit zu beschäftigen. Daher die späte Rückmeldung.
Ich habe es aber geschafft das Problem zu lösen und den Code so zu modifizieren, das MQTT alle x Sekunden übertragen wird und auch der Versand an Wettermonster funktioniert.

Code stelle ich hier zur verfügung, falls jemand Interesse hat.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Adafruit_TSL2591.h"
#include <PubSubClient.h>  


Adafruit_BME280 bme;
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);


const char* ssid = "XXXX";                  //WLAN SSID eintragen
const char* password = "YYYY";            //WLAN Passwort eintragen
const char* id = "ZZZZZ";                   //ID von wettermonster.de eintragen
const char* key = "UUUUU";                 //KEY von wettermonster.de eintragen
const int interval = 30;                      //MQTT Aktualisierungsintervall anpassen
const char* MQTT_BROKER = "192.168.178.2";    //MQTT Broker IP-Adresse eintragen
const int monsterinterval = 5;                //Aktualisierungsintervall für Wettermonsteraktualisierung 

float temperature;
float humidity;
float pressure;
float Percipitation;
float numClicksRain;
float windSpeed;
float numRevsAnemometer;
char* windDirection;
float luminosity;
volatile unsigned long previousTimeRain=0, previousTimeSpeed=0, delayTime=20;
unsigned long lastMillis;
unsigned long monsterlastMillis; 

WiFiClient client;
PubSubClient MQTTclient(client);  


void ICACHE_RAM_ATTR countAnemometer();
void ICACHE_RAM_ATTR countRain();

void sendToWettermonster() {

	int i = 0;
	while(WiFi.status() != WL_CONNECTED && i <= 5)
	{
   i++;
   Serial.println("WiFi nicht verbunden. Versuche neu zu verbinden...");
   WiFi.disconnect();
   WiFi.mode(WIFI_OFF);
   WiFi.mode(WIFI_STA);
   WiFi.begin(ssid, password);
   delay(1000);
	}

	if(i > 5)
	{
   Serial.println("Verbindnung zu " + String(ssid) + " fehlgeschlagen. Neustart.");
   ESP.restart();
	}

	if (WiFi.status() == WL_CONNECTED && client.connect("upload.wettermonster.de", 80)) // Verbindung zum Server aufbauen
	{

		Serial.println ("Verbunden mit upload.wettermonster.de");
		client.print("GET /speichern.php");
		client.print("?id=");
		client.print(id);
		client.print("&schluessel=");
		client.print(key);
		client.print("&temperatur=");
		client.print(temperature);
		client.print("&luftfeuchtigkeit=");
		client.print(humidity);
		client.print("&luftdruck=");
		client.print(pressure);
		client.print("&niederschlag=");
		client.print(Percipitation);
		client.print("&windgeschwindigkeit=");
		client.print(windSpeed);
		client.print("&windrichtung=");
		client.print(windDirection);
		client.print("&helligkeit=");
		client.print(luminosity);
		client.println(" HTTP/1.1");
		client.println("Host: upload.wettermonster.de");
		client.println("User-Agent: Wettermonster");
		client.println("Accept: text/html");
		client.println();

		unsigned long timeout = millis();
		    while (client.available() == 0) {
		      yield();
		      if (millis() - timeout > 5000) {
		        Serial.println("Timeout !");
		        client.stop();
		        return;
		      }
		    }

		Serial.println("Daten an Wettermonster gesendet");
	}

	else
	{
		Serial.println("Verbindung fehlgeschlagen");
	}

	client.stop();
}

void readBME() {

	temperature = bme.readTemperature();
	humidity = bme.readHumidity();
	pressure = bme.readPressure() / 100.0F;

}
void readTSL() {

	uint32_t lum;
	uint16_t ir, full, visible, gain, timing;
	boolean change = false;
	String url_temp;

	tslagain:

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);

 	for (int i=0; i == 0 || luminosity > 150000.0 || luminosity < 0; i++){
	while (((ir>16000) || (full > 40000) || (luminosity <= 0)) && (gain > 0))  {
		change = true;

		switch(gain)
		{
		case TSL2591_GAIN_MED:
			tsl.setGain(TSL2591_GAIN_LOW);
			break;
		case TSL2591_GAIN_HIGH:
			tsl.setGain(TSL2591_GAIN_MED);
			break;
		case TSL2591_GAIN_MAX:
			if (timing > 0) {
				timing--;
				tsl.setTiming(tsl2591IntegrationTime_t(timing));
			}
			else {
				tsl.setGain(TSL2591_GAIN_MED);
			}
			break;
			default:
			break;
		}

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);
	delay (500);
	}

	while ((ir<500) && (full < 1000) && (timing < 5))  {
		change = true;

		switch(gain)
		{
			case TSL2591_GAIN_LOW:
				tsl.setGain(TSL2591_GAIN_MED);
	 			break;
			case TSL2591_GAIN_MED:
	 			tsl.setGain(TSL2591_GAIN_HIGH);
				break;
			case TSL2591_GAIN_HIGH:
			 	tsl.setGain(TSL2591_GAIN_MAX);
				break;
			case TSL2591_GAIN_MAX:
				if (timing < 5) {
					timing++;
					tsl.setTiming(tsl2591IntegrationTime_t(timing));
				}
				break;
				default:
				break;
		}

	lum = tsl.getFullLuminosity();
	gain = tsl.getGain();
	timing = tsl.getTiming();
	ir = lum >> 16;
	full = lum & 0xFFFF;
	luminosity = tsl.calculateLux(full, ir);
	delay (500);
	}

	if (change == true) {
		lum = tsl.getFullLuminosity();
		gain = tsl.getGain();
		timing = tsl.getTiming();
		ir = lum >> 16;
		full = lum & 0xFFFF;
		luminosity = tsl.calculateLux(full, ir);
	}
	delay (500);
	}

}

void countAnemometer() {
	if((millis() - previousTimeSpeed) > delayTime) {
		numRevsAnemometer++;
 		previousTimeSpeed = millis();
	}
}

void countRain() {
	if((millis() - previousTimeRain) > delayTime) {
		numClicksRain++;
		previousTimeRain = millis();
	}
}

void readWeatherMeters() {

	windSpeed = (numRevsAnemometer / (interval)) * 2.4;
	numRevsAnemometer = 0;

	Percipitation = 0.2794 * (numClicksRain / interval);
	numClicksRain = 0;

	int windDirectionVoltage = analogRead(A0);

	if (windDirectionVoltage >= 794 && windDirectionVoltage < 810)    {windDirection = "N";}
	else if (windDirectionVoltage >= 420 && windDirectionVoltage < 436) {windDirection = "NNO";}
	else if (windDirectionVoltage >= 472 && windDirectionVoltage < 488) {windDirection = "NO";}
	else if (windDirectionVoltage >= 87 && windDirectionVoltage < 100) {windDirection = "ONO";}
	else if (windDirectionVoltage >= 101 && windDirectionVoltage < 114) {windDirection = "O";}
	else if (windDirectionVoltage >= 70 && windDirectionVoltage < 86){windDirection = "OSO";}
	else if (windDirectionVoltage >= 197 && windDirectionVoltage < 213) {windDirection = "SO";}
	else if (windDirectionVoltage >= 136 && windDirectionVoltage < 152) {windDirection = "SSO";}
	else if (windDirectionVoltage >= 300 && windDirectionVoltage < 316) {windDirection = "S";}
	else if (windDirectionVoltage >= 256 && windDirectionVoltage < 272) {windDirection = "SSW";}
	else if (windDirectionVoltage >= 644 && windDirectionVoltage < 660) {windDirection = "SW";}
	else if (windDirectionVoltage >= 612 && windDirectionVoltage < 628) {windDirection = "WSW";}
	else if (windDirectionVoltage >= 953 && windDirectionVoltage < 969)   {windDirection = "W";}
	else if (windDirectionVoltage >= 839 && windDirectionVoltage < 855) {windDirection = "WNW";}
	else if (windDirectionVoltage >= 897 && windDirectionVoltage < 913) {windDirection = "NW";}
	else if (windDirectionVoltage >= 718 && windDirectionVoltage < 734) {windDirection = "NNW";}
}

//MQTT
void mqtt() {
    if (!MQTTclient.connected()) {
        while (!MQTTclient.connected()) {
            MQTTclient.connect("ESP8266_Wetterstation");
            delay(100);
        }
    }

    String mqtttemp = String(temperature);
    String mqtthumi = String(humidity);
    String mqttpres = String(pressure);
    String mqttprec = String(Percipitation);
    String mqttnumC = String(numClicksRain);
    String mqttwinS = String(windSpeed);
    String mqttnumR = String(numRevsAnemometer);
    String mqttwinD = String(windDirection);
    String mqttlumi = String(luminosity);
    String mqttwinV = String(analogRead(A0));

    char msg[100];
    mqttwinS.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Windgeschwindigkeit (km_h)", msg );

    mqttwinD.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Windrichtung", msg );
  
    mqttprec.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Regenmenge (mm)", msg );
  
    mqttlumi.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Lichtsstärke (lux)", msg );
  
    mqtthumi.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Luftfeuchtigkeit (%)", msg );

    mqttpres.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Luftdruck (mbar)", msg );

    mqtttemp.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Lufttemperatur (Celsius)", msg );

    mqttwinV.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Windrichtungsspannung (mV)", msg );

    mqttnumC.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Regenklicks (-)", msg );

    mqttnumR.toCharArray( msg, 100 );
    MQTTclient.publish( "ESP8266-Wetter/Wetterstation/Windradumdrehungen (-)", msg );

    //Serielle Ausgabe
    Serial.println("Bosch BME280 Pressure - Humidity - Temp Sensor");
    Serial.println("Luftdruck\tLuftfeuchtigkeit\tTemperatur(Celsius)");
    Serial.print(mqttpres); Serial.print(" mbar\t"); 
    Serial.print(mqtthumi); Serial.print(" %\t\t\t");
    Serial.print(mqtttemp); Serial.print(" *C\n\n");

    Serial.println("Sparkfun Wind & Regensensor");
    Serial.println("Windgeschwindigkeit\tWindrichtung\t\tRegenmenge");
    Serial.print(mqttwinS); Serial.print(" km/h\t\t");
    Serial.print(mqttwinD); Serial.print(" \t\t\t");
    Serial.print(mqttprec); Serial.print(" mm\n\n");

    Serial.println("Lichtsensor TSL2591");
    Serial.println("Lichtstaerke");
    Serial.print(mqttlumi); Serial.println(" lux\t\n");

    Serial.println("Wetterstation_Kenndaten");
    Serial.println("Regenklicks\tWindumdrehungen\t\tWindrichtungsV");
    Serial.print(mqttnumC); Serial.print(" -\t\t");
    Serial.print(mqttnumR); Serial.print(" \t\t\t");
    Serial.print(mqttwinV); Serial.print(" mV\t\n");

}   



void setup() {
	Serial.begin(115200);

 WiFi.mode(WIFI_STA);
	WiFi.begin(ssid, password);
	Serial.println("");
 Serial.print("Verbinde mit " + String(ssid));

	while (WiFi.status() != WL_CONNECTED) {
		Serial.print(".");
		delay(500);
	}

  MQTTclient.setServer(MQTT_BROKER, 1883); 

	Serial.println("Wifi Aktiviert");
	Serial.println("");
	Serial.print("Verbunden mit: ");
	Serial.println(ssid);
	Serial.print("IP Adresse: ");
	Serial.println(WiFi.localIP());

	pinMode(12, INPUT_PULLUP);
	attachInterrupt(12, countRain, FALLING);
	pinMode(13, INPUT_PULLUP);
	attachInterrupt(13, countAnemometer, FALLING);

	if (!bme.begin(0x76)){
		Serial.println("BME280 konnte nicht gefunden werde, checke bitte die Verbindungen!");
		return;
	}

	if (tsl.begin()){
		tsl.setGain(TSL2591_GAIN_LOW);
		tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);
	}
	else{
		Serial.println("TSL2591 konnte nicht gefunden werde, checke bitte die Verbindungen!");
		return;
	}

	lastMillis = millis();
}

void loop() {

	if (millis() - lastMillis > (interval * 1000)) {
		readBME();
		readTSL();
		readWeatherMeters();
    mqtt();
		lastMillis = millis();
	}

	if (millis() - monsterlastMillis > (monsterinterval * 60000)) {
		sendToWettermonster();
		monsterlastMillis = millis();
	}

	if(ESP.getFreeHeap() <= 20000){
		Serial.println("Der freie Heap beträgt nur noch: " + String(ESP.getFreeHeap()) + " Der ESP wird deshalb neu gestartet.");
		ESP.restart();
	}

	delay(100);

}

Gruß Thomas

Freut mich dass es jetzt funktioniert und danke für die Rückmeldung!

Gruß

Scherheinz

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.