Meteorological visibility from a sensor?

A cheap man’s version is to replicate fog in the shower.
Not 100% accurate but it will give you a start to see if it’s feasible.

But I’m quite sure fog means 100% humidity.
Perhaps you can look at historical data?

How have you connected the MLX90614?
How did you configure it in ESPHome (I assume)?

Yes, I use the MLX90614 with ESPHome via I2C. It’s a custom component until I find the time for a proper contribution.

mlx90614_sensor.h

#include "esphome.h"
#include <Wire.h>
#include "Adafruit_MLX90614.h"

#define TAG "mlx90614"

class MLX90614Sensor : public PollingComponent {

  public:
    Adafruit_MLX90614 mlx;

    Sensor *ambient_temperature_sensor = new Sensor();
    Sensor *object_temperature_sensor = new Sensor();

    MLX90614Sensor() : PollingComponent(15000) { }

    void setup() override {
      ESP_LOGCONFIG(TAG, "Setting up MLX90614Sensor...");
      mlx.begin();
      LOG_SENSOR("  ", "Ambient temperature", this->ambient_temperature_sensor);
      LOG_SENSOR("  ", "Object temperature", this->object_temperature_sensor);
    }

    void update() override {
      double ambient_temperature = mlx.readAmbientTempC();
      double object_temperature = mlx.readObjectTempC();

      ESP_LOGD(TAG, "Ambient temperature measured: %f", ambient_temperature);
      ESP_LOGD(TAG, "Object temperature measured: %f", object_temperature);

      ambient_temperature_sensor->publish_state(ambient_temperature);
      object_temperature_sensor->publish_state(object_temperature);
    }
};

And in your device configuration:

esphome:
  ...
  includes:
    - "mlx90614_sensor.h"
  libraries:
    - "Adafruit MLX90614 Library"

i2c:

sensor:
  - platform: custom
    lambda: |-
      auto mlx90614_sensor = new MLX90614Sensor();
      App.register_component(mlx90614_sensor);
      return {mlx90614_sensor->ambient_temperature_sensor, mlx90614_sensor->object_temperature_sensor};
    sensors:
      - name: "Ambient Temperature"
        unit_of_measurement: °C
        accuracy_decimals: 1
      - name: "Object Temperature"
        unit_of_measurement: °C
        accuracy_decimals: 1
2 Likes

This looks very interesting!
What kind of sensor did you buy.
I see they come in different shapes, one is just the sensor and another is the sensor on a board with what looks like smt capacitor or resistor(s).
How did you calibrate it, or was it good enough when you got it?

I’m using this type of breakout board:
MLX90614

It came “factory calibrated” and at least the ambient temperature seems to be good enough.

I couldn’t find that type here locally.
I found the same type (by the looks) on wish.com.

I found this type in a store in my country.


But sadly they both have a very long delivery time. So I bought them both, lets see which one wins…

Thank you for your help!

1 Like

thanks for your great code, I waited for a long time who made this custom components thx again !!

1 Like

I have finally got the first sensor.

Where do I place the mlx90614_sensor.h and the Adafruit MLX90614 Library?
I read through the custom sensor components page of ESPHome but I could not see where to place the files so that the compiler finds them.

It says:

First, create a new file called my_custom_sensor.h in your configuration directory and copy the source code from above into that file.

I can’t find configuration as a directory. I see config, where all the yaml files are (?).
But I can’t find anything about libraries, only that it should be added in the yaml, nothing about where to put it.

Yes, you have to put the .h file into the same directory as the .yaml file.

You don’t have to put the library anywhere. This should be downloaded as part of the build process automatically.

1 Like

Ahh… Thats clever…

Thanks.

Got it working. Kind of…
I mean, it could be correct. It is rather hot in here

image

Even if I disconnect it I get the same result. I will have to keep trying tomorrow.

Please have a look at my latest code update where I built in a filter that discards any values outside of the specified range: esphome-customisations/mlx90614 at master · exxamalte/esphome-customisations · GitHub

1 Like

Will do, but I doubt that is the problem.
I could not get a single value below this value.

I have the same result (sensor reading - 1037.5°C) … Have you found a solution? P.S. - I use updated files from github.

No, I have given up on that sensor.
I waited for the other sensor I bought but I believe I’m not going to get it.

As just mentioned on the GitHub issue you reported, this temperature value was derived from value 0xFFFF from the sensor. Could indicate a wiring issue. Please also note that there appear to be 3V and 5V versions of this sensor.

I have 2 of these sensors and they currently appear to be working fine and report very reasonable temperatures. Originally I had issues with out-of-range values, hence implemented that small improvement in the custom code to discard those values, but in those cases the sensor was reporting those values just right after starting up for a few minutes, not constantly.

As I am not yet understanding all scripts especially the low level parts I had to stick to trial and error and tried other libraries and replacements of all kinds and it did not work. Also changing the frequency did not solve it and changing the address (as proposed in other sensors libraries) didnt help.

But then found something that fixed it immediately and now is stable for thousands of readings (one per second since last eventing):

  double ambient_temperature = mlx.readAmbientTempC();
  double object_temperature = mlx.readObjectTempC();

I do two readings shortly after each other by just duplicating the first line and creating a dummy variable to feed into. That solved it for the ambient temp.

double dummy = mlx.readAmbientTempC();

After that the object temp started to fail. So I did the same for that line (double it and first reading to dummy2).

double dummy2 = mlx.readObjectTempC();

This solved it for me by dropping the first reading then only using the second one. Not sure of the root cause (and if I knew about these things a nice solution could be found) but it works here.

1 Like

Dear exxamalte,

Can you share the connection diagram betwwen ESP and the sensor?, and where is defined the pins for SCL and SDA?.

Thanks in advance,

Jose

Sure, this is how I wired up a Lolin D1 Mini Pro with a MLX90614 module:

  • Sensor VIN → ESP 5V (3.3V works as well)
  • Sensor GND → ESP GND
  • Sensor SCL → ESP GPIO5 (D1)
  • Sensor SDA → ESP GPIO4 (D2)

Unfortunately I don’t have that ESP module nor the MLX90614 as Fritzing parts, but I found a diagram that is very close to how my wiring looks like:

If you use the ESP’s standard I2C pins then you don’t need to actually define the pins in the ESPHome configuration.

Thanks exxamalte, it’s working ok now.

1 Like