Zwer2k / WeatherStationDataRx

Arduino library for read weather data from Venus W174/W132 (tested), Auriol H13726, Hama EWS 1500, Meteoscan W155/W160
MIT License
22 stars 8 forks source link

example gives me lots of WeatherStationDataRx Test #19

Closed 885narrow closed 1 year ago

885narrow commented 2 years ago

Hi, I am using your example and tried to get it working with mqtt and wifi. I am not that good in programming and your code gives me even with delay very fast a lot of: WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test WeatherStationDataRx Test **does it mean it is restarting my esp8266? because the delay (5000) is not used.

i also do not know why PairedDeviceAdded2 is calling itself? looped? for my understanding I renamed the function to see the difference.**

`void PairedDeviceAdded2(byte newID) {

if defined(ESP8266) || defined(ESP32)

Serial.printf("esp New device paired %d\r\n", newID);

else

Serial.print("New device paired "); Serial.println(newID, DEC);

endif

wsdr.pair(PairedDeviceAdded2);

// If you already have a sensor ID for your device, you can set it as follows. //byte myDeviceIDs[] = {236, 65};

//wsdr.pair(myDeviceIDs, sizeof(myDeviceIDs)); //byte myDeviceIDs[] = { 98, 67 }; //wsdr.pair(myDeviceIDs, sizeof(myDeviceIDs)); } ` Due to my low knowledge I did not get the known IDS to pair as well, where do I need to set these?

somehow I get my data in IOBROKER via MQTT, but not reliable. I use a RXB6 with Antenna

I will add my code here, maybe somebody can use it too, or give me a hint why not reliable.

`

define wifi etc. here.

define ESP8266

include

include

include

define MQTT_ALIVE_TOPIC "Wetterstation/%s/alive"

define MQTT_DATA_TOPIC "Wetterstation/%s/%s"

define MQTT_KEEPALIVE 10

String value;

WiFiClient wifiClient; byte mac[6]; char sensorId[7]; char topicBuffer[255]; char messageBuffer[255];

PubSubClient mqttClient(wifiClient);

void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(WIFI_SSID);

WiFi.mode(WIFI_STA); // disable hotspot WiFi.begin(WIFI_SSID, WIFI_PASSWORD); wdt_reset(); // Watchdock resetten

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

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }

extern const char MQTT_FALSE = "0"; extern const char MQTT_TRUE = "1";

char MQTT_CLIENT_ID[15]; void setup_mqtt() {

mqttClient.setServer(MQTT_HOST, MQTT_PORT);

byte mac[6];

WiFi.macAddress(mac);

sprintf(MQTT_CLIENTID, "ESP8266%02X%02X%02X", mac[3], mac[4], mac[5]); }

void connect_mqtt(char* MQTT_TOPIC) {

// Loop until we're reconnected

while (!mqttClient.connected()) {

Serial.print("Attempting MQTT connection...");

// Attempt to connect

if defined(MQTT_ALIVE_TOPIC)

byte mac[6];

WiFi.macAddress(mac);

char aliveTopicId[7];

sprintf(aliveTopicId, "%02X%02X%02X", mac[3], mac[4], mac[5]);

char aliveTopic[255];

sprintf(aliveTopic, MQTT_ALIVE_TOPIC, aliveTopicId);

if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD, aliveTopic, 1, 1, MQTT_FALSE)) {

  Serial.println("connected with last will and testament enabled");

  mqttClient.publish(aliveTopic, MQTT_TRUE, 1);

  Serial.print("Sent alive message to ");

  Serial.println(aliveTopic);

else

if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {

  Serial.println("connected");

endif

  if (MQTT_TOPIC && mqttClient.subscribe(MQTT_TOPIC, 1)) {

    Serial.print("Subscribed to ");

    Serial.println(MQTT_TOPIC);
  }

} else {

  Serial.print("failed, rc=");

  Serial.print(mqttClient.state());

  Serial.println(" try again in 5 seconds");

  // Wait 5 seconds before retrying

  delay(5000);

  wdt_reset();
}

} }

ifdef ESP8266

define DATA_PIN D5

else

define DATA_PIN 2

endif

WeatherStationDataRx wsdr(DATA_PIN, false, ARMUseAsConfirmation, false);

void PairedDeviceAdded2(byte newID) {

if defined(ESP8266) || defined(ESP32)

Serial.printf("esp New device paired %d\r\n", newID);

else

Serial.print("New device paired "); Serial.println(newID, DEC);

endif

wsdr.pair(PairedDeviceAdded2);

// If you already have a sensor ID for your device, you can set it as follows. //byte myDeviceIDs[] = {236, 65};

//wsdr.pair(myDeviceIDs, sizeof(myDeviceIDs)); //byte myDeviceIDs[] = { 98, 67 }; //wsdr.pair(myDeviceIDs, sizeof(myDeviceIDs)); }

void setup() { Serial.begin(57600); delay(5000); Serial.println("WeatherStationDataRx Test"); wsdr.begin(); wsdr.pair(PairedDeviceAdded2);

wdt_enable(WDTO_8S);

WiFi.macAddress(mac); sprintf(sensorId, "%02X%02X%02X", mac[3], mac[4], mac[5]); wdt_reset(); setup_wifi(); wdt_reset(); // Watchdog resetten setup_mqtt(); wdt_reset(); // Watchdog resetten

}

void loop() { wdt_reset(); // Watchdog resetten connect_mqtt(0); wdt_reset(); // Watchdog resetten mqttClient.loop();

byte newDataState = wsdr.readData(true); if (mqttClient.connected() && (newDataState > 0)) { Serial.print("SensorID "); Serial.println(wsdr.sensorID());

if (wsdr.dataHas(newDataState, NDTemperature)) {
  Serial.print("Battery: ");
  Serial.println(bitRead(wsdr.batteryStatus(), 0) == 0 ? "OK" : "Low");

  if (bitRead(wsdr.batteryStatus(), 0) == 0) {
    value = "OK";
  } else {
    value = "Low";
  }
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Battery");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
  Serial.println(topicBuffer);
}

if (wsdr.dataHas(newDataState, NDTemperature)) {
  Serial.print("Temperature: ");
  Serial.print(wsdr.readTemperature());
  Serial.println("*C");
  value = String(wsdr.readTemperature());
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Temperature");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
}

if (wsdr.dataHas(newDataState, NDHumidity)) {
  Serial.print("Humidity: ");
  Serial.print(wsdr.readHumidity());
  Serial.println("%");
  value = String(wsdr.readHumidity());
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Humidity");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
}

if (wsdr.dataHas(newDataState, NDWindSpeed)) {
  Serial.print("Wind speed: ");
  Serial.print(wsdr.readWindSpeed());
  Serial.println("m/s");
  Serial.print(wsdr.readWindSpeed() * 3.6);
  Serial.println("km/h");
  value = String((wsdr.readWindSpeed() * 3.6));
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Windspeed");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
}

if (wsdr.dataHas(newDataState, NDWindDirection)) {
  Serial.print("Wind direction: ");
  Serial.print(wsdr.readWindDirection());
  Serial.println("°");
  value = String(wsdr.readWindDirection());
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Winddirection_Degrees");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));

  if (wsdr.readWindDirection() == 0) {
    value = "N";
  } else if (wsdr.readWindDirection() == 45) {
    value = "NE";
  }

  else if (wsdr.readWindDirection() == 90) {
    value = "E";
  }

  else if (wsdr.readWindDirection() == 135) {
    value = "SE";
  }

  else if (wsdr.readWindDirection() == 180) {
    value = "S";
  } else if (wsdr.readWindDirection() == 225) {
    value = "SW";
  } else if (wsdr.readWindDirection() == 270) {
    value = "W";
  } else if (wsdr.readWindDirection() == 315) {
    value = "NW";
  }
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Winddirection");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
}

if (wsdr.dataHas(newDataState, NDWindGust)) {
  Serial.print("Wind gust: ");
  Serial.print(wsdr.readWindGust());
  Serial.println("m/s");
  Serial.print(wsdr.readWindGust() * 3.6);
  Serial.println("km/h");
  value = String((wsdr.readWindGust() * 3.6));
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "WindGust");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));
}

if (wsdr.dataHas(newDataState, NDRainVolume)) {
  Serial.print("Rain volume: ");
  Serial.print(wsdr.readRainVolume());
  Serial.println("mm");
  value = String(wsdr.readRainVolume());
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "Rainvolume");
  value.toCharArray(messageBuffer, 255);
  mqttClient.publish(topicBuffer, (byte*)messageBuffer, strlen(messageBuffer));

}

if (wsdr.dataHas(newDataState, NDError)) {
  Serial.println("Error: pairing required");
  value = ("Error: pairing required");
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "INFO");
  value.toCharArray(messageBuffer,255);
  mqttClient.publish(topicBuffer,(byte*)messageBuffer,strlen(messageBuffer));
}

else {
  value = "no Error";
  sprintf(topicBuffer, MQTT_DATA_TOPIC, sensorId, "INFO");
  value.toCharArray(messageBuffer,255);
  mqttClient.publish(topicBuffer,(byte*)messageBuffer,strlen(messageBuffer));
}

Serial.println("------------");
wdt_reset();  // Watchdog resetten
mqttClient.loop();

} } `

herzlichen Dank! auch fuer die viele Arbeit die du in dieses Projekt gesteckt hast. LG

Zwer2k commented 1 year ago

Hallo,

sorry, dass ich mich erst jetzt melde. Hast du es inzwischen zum Laufen gebracht? Ich gebe zu, dass ich letzte Versionen nicht mit esp8266 getestet habe. Reicht dir der Arbeitspeicher von dem esp8266, der ist schon recht knapp. Hast du mal den Beispielcode ohne WiFi/MQTT mal getestet? Funktioniert es dann? Ich gehe davon aus, dass dein WiFi/MQTT Code ohne der WeatherStationDataRx richtig funktioniert.

885narrow commented 1 year ago

Hallo , Danke fuer deine Antwort , Es zeigt sich das das eigentlich kein Fehler ist ...er findet dann die Stationen und auch die Daten werden ordentlich geliefert. Vielen Dank!