Jump to content

Banner.jpg.b83b14cd4142fe10848741bb2a14c66b.jpg

Setting up an MQTT system for Weather Sensing and Astro Control


Gina

Recommended Posts

18 minutes ago, stash_old said:

The MQTT client name/ID isn't dupilcated is it - has to be unique

I'm wondering if there's something I've missed in my understanding of MQTT.  I have different Topic names eg. esp/temperature, esp/humidity and outside/temperature, outside/humidity.  What exactly do you mean by MQTT client name/ID?

Link to comment
Share on other sites

"

ClientId

The client identifier (ClientId) identifies each MQTT client that connects to an MQTT broker. The broker uses the ClientID to identify the client and the current state of the client.Therefore, this ID should be unique per client and broker. In MQTT 3.1.1 (the current standard), you can send an empty ClientId, if you don’t need a state to be held by the broker. The empty ClientID results in a connection without any state. In this case, the clean session flag must be set to true or the broker will reject the connection."

client.connect("ESP8266Client")
Edited by stash_old
Link to comment
Share on other sites

I think I may be beginning to see the light!

This is an extract of the Living Room client sketch.

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

And this from the MQTT_Obsy_ESP32_and_BME280_and_DHT22 sketch that runs the outside and obsy topics.

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

So I guess this is calling both the same ID of "espClient".  I hadn't taken in this part of the concept.

I guess I need to change one to something else.

Edited by Gina
Link to comment
Share on other sites

I thought "espClient" referred to the type of client rather than the ID on the MQTT network.  Looks like to problem is solved.  I'll set up the outside unit on the table and connect power and see if I repeat what I had before.  Then, change the code and re-upload and try again.

Link to comment
Share on other sites

Interesting point.... You're creating a WiFiClient earlier in the sketch, and using the return value as the mqtt client ID. I would imagine (though can't confirm) that WiFiClient returns a unique code of some sort, that would satisfy the unique mqtt client id requirement.

Personally, I use the MAC address of the client device as the mqtt client id, and apparently you can get that with WiFi.macAddress()

Link to comment
Share on other sites

Not that after all!!  Changed the obsy client to "obsClient".  So one is "espClient" and the other "obsClient".  The obsClient is working the espClient isn't.

WiFiClient obsClient;
PubSubClient client(obsClient);
long lastMsg = 0;
char msg[50];
int value = 0;

1450839066_Screenshotfrom2020-08-1917-49-47.png.85e83a9d610b743a52da900aecd23ffe.png

Link to comment
Share on other sites

5 hours ago, Gina said:

I've no idea what Forced Mode is - I've just followed the tutorials so I'll have to do some research.

Forced Mode is the recommendation in the spec sheets if you are doing one off readings intermittently. The default mode is for continuous readings I think. Something like this if using the Adafruit BME280 library (taken from their advanced example on github)......

 

    bme.setSampling(Adafruit_BME280::MODE_FORCED,
                    Adafruit_BME280::SAMPLING_X1, // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1, // humidity
                    Adafruit_BME280::FILTER_OFF   );

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

45 minutes ago, Gina said:

Not that after all!!  Changed the obsy client to "obsClient".  So one is "espClient" and the other "obsClient".  The obsClient is working the espClient isn't.



 

Grasping the proverbial straws here: I don't know if you also have to restart the espclient. Or rename the espclient to indoorclient.

On a different note: I rewrote the code for my mini meteo station (ir-temperature sensor for clouds, and bme280) to post to my mqtt broker (mosquitto on an old raspberry pi). So if I have time, I can start playing around with Node Red. (The esp meteo station is on my patio. We are still enjoying quite nice weather up here.)

An INDI driver that can act as a MQTT client to get weather info, would be nice.

1597929719_Skrmklipp2020-08-1919_34_41.thumb.png.509164c4a562a05605148a10becfa6a3.png

  • Like 1
Link to comment
Share on other sites

My initial thought was the client ID as well.

You could eliminate the esp's completely and just run a couple of scripts on the Mint box, that publishes to the MQTT broker. That would a) test that the Pi0 can handle the multiple clients (which I doubt is the issue), and b) check to see if multiple clients are working in general. Its pretty easy to set that up in via e.g. a Python script.

Once you know that part is OK, stop one of the scripts, and introduce one of the esp's. If you still have both clients (the script and the esp) working, then introduce the 2nd esp. If the script is still publishing OK and the esp stops, then at least you know for sure its related to the esp's. Then perhaps modify the esp client to just publish a number every second to remove the sensor hardware. If that is still working, then it hopefully helps you narrow down the issue.

  • Thanks 1
Link to comment
Share on other sites

Changed from "espClient" to  "indoorClient".   Again worked until I switched on the obsClient.  Had the Serial Monitor working during this process.  When the obsClient was turned on the indoorClient started having connection problems.

637402244_Screenshotfrom2020-08-1919-06-32.thumb.png.36dd5ab9bcce8811e6a37ce4530189cc.png

Link to comment
Share on other sites

2 hours ago, Gina said:

I think I may be beginning to see the light!

This is an extract of the Living Room client sketch.


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

And this from the MQTT_Obsy_ESP32_and_BME280_and_DHT22 sketch that runs the outside and obsy topics.


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

So I guess this is calling both the same ID of "espClient".  I hadn't taken in this part of the concept.

I guess I need to change one to something else.

Forgive me but I think you are changing the wrong bit it is the MQTT connect statement that has the ID - although I am having a bad day so probably wrong. 🙂

https://pubsubclient.knolleary.net/api.html

API Documentation

  • Thanks 1
Link to comment
Share on other sites

Yes - the line to change is 

// Attempt to connect
    if (client.connect("ESP8266Client"))

This is a hard-coded mqtt client ID. The client referred to in the PubSubClient is a network client to use on connection.

  • Thanks 1
Link to comment
Share on other sites

New reconnect code. Changed to if (client.connect("indoorClient"))  {

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("indoorClient")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("esp32/output");
      client.subscribe("esp32/roof");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

And it works!!

Finally, problem SOLVED

  • Like 2
  • Haha 1
Link to comment
Share on other sites

Next is to change the code in the obsClient sketch to use the second BME280 instead of the DHT22.  Then I can make a link and set the present BME280 to address 77 and then take the unit out to the obsy and plug the other BME280 in (the one in the Stevenson screen).  Maybe not tonight as it's raining again!!

Link to comment
Share on other sites

Have the obsy sensor unit on the table running from bench PSU.  The second BME280 is working but doesn't look right.  I can't see it being right that the humidity one side of the room is 67% and the other side just 20%.  Temperature seems sensible though - the table is near the big picture window and expected to be a bit cooler.

The reason there's no readings for Outside is that the outside BME280 is not plugged in.

1528640117_Screenshotfrom2020-08-1921-40-56.png.b9f49f6d82dd1b18db9e3f81fb8d1aba.png

Edited by Gina
Link to comment
Share on other sites

For my weather station the pressure reading is firmly stuck at 1006 hPa. Temperature and humidity vary as expected, so I hope it's just a coding issue.

Edited by wimvb
  • Like 1
Link to comment
Share on other sites

Found my problem - yep - coding wrong.  Had the humidity reading temperature 🤣  When I looked at MQTT Explorer and saw that the humidity and temperature were exactly the same, I twigged!

  • Like 1
Link to comment
Share on other sites

Now to the spikes problem.  Adding this code to the Setup section.

    bme2.setSampling(Adafruit_BME280::MODE_FORCED,
                    Adafruit_BME280::SAMPLING_X1, // temperature
                    Adafruit_BME280::SAMPLING_X16, // pressure
                    Adafruit_BME280::SAMPLING_X1, // humidity
                    Adafruit_BME280::FILTER_OFF   );

Result is that the readings are not changing at all so not the entire solution!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.