Awesome! LoRa Soil sensor

I found this awesome soil sensor and I finally got it to work great with home assistant so I thought i’d share in case some of you need long range soil sensors.

**** Please read through, there has been a lot of changes since the original post. Many of great additions. I hope this is what you’ve been looking for! ****

*** UPDATE****

Since I have posted this, the maker has moved onto a newer version that I have NOT tested. Here is the newer revision: Lora Temperature/ Humidity/ Soil Moisture Sensor V3 | Makerfabs

Another update
Wow this seems to have gained traction! YAY glad i could give back to the community!
Please jump to the latest as there are some smart people who have been moving this project along nicely and I have been away.


I’m using this great project called OpenMqttGateway with LoRa and BLE enabled as the receiver. This is a great project I highly recommend it if you’re not familiar. I use the BLE for the Flower care probes that work well, they just don’t have the range I need.

Anyhoot these LoRa probes are amazing so here it is:

Arduino code I tweaked based on the example provided by the maker (I don’t really know how to code, this is me figuring out due to necessity) so if anyone has a better way to write this, by all means post in the thread!
Also keep in mind that there are frequency laws to respect based on your county’s laws, here I am using 915E6, but make sure to us the appropriate value for you. Ensure this is consistent and matches the board you are using for OpenMqttGateway, the antenna, the and the OpenMqttGateway LoRa configuration.

#include <avr/wdt.h>
#include <avr/sleep.h>
//#include <RadioLib.h>
#include "I2C_AHT10.h"
#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <ArduinoJson.h>

#define NODENAME "Soil_1"

#define DIO0 2
#define DIO1 6
#define DIO2 7
#define DIO5 8

#define LORA_RST 4
#define LORA_CS 10

#define SS      10   // GPIO18 -- SX1278's CS
#define RST     4   // GPIO14 -- SX1278's RESET
#define DI0     2   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  915E6

//#define SPI_MOSI 11
//#define SPI_MISO 12
//#define SPI_SCK 13

#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13
//#define SPI_SS 10

int ledPin = 13;
int shu = 0;
int sensorPin = A2; // select the input pin for the potentiometer
int sensorPowerCtrlPin = 5;

int sensorValue = 0;     // variable to store the value coming from the sensor
int16_t packetnum = 0;   // packet counter, we increment per xmission
float temperature = 0.0; //
float humidity = 0.0;
//float dewpoint = 0.0;
String errcode = "";


// SX1278 radio = new Module(LORA_CS, DIO0, LORA_RST, DIO1, SPI, SPISettings());
AHT10 humiditySensor;

ISR(WDT_vect)
{
    Serial.print("[Watch dog]");
    Serial.println(shu);
    delay(100);
    shu++;
    wdt_reset();
}

void setup()
{
    wdt_disable();
    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
    Serial.println("[Start]");
    delay(100);
    SPI.begin();

    //setup start
    Serial.println("[Setup]");
    delay(100);

    // initialize SX1278 with default settings
    Serial.println(String("Sensor name is :") + String(NODENAME));
    Serial.print(F("Initializing ... "));
    SPI.begin();
    LoRa.setPins(SS,RST,DI0);
    if (!LoRa.begin(915E6)) {
      Serial.println("Starting LoRa failed!");
      while (1);
    }
//    else
//    {
//        Serial.print(Success!));
//        Serial.println("LoRa ok");
//        while (0)
//            ;
//    }
    LoRa.sleep();

    //AHT10
    pinMode(sensorPowerCtrlPin, OUTPUT);
    digitalWrite(sensorPowerCtrlPin, HIGH); //Sensor power on

    Wire.begin();
    if (humiditySensor.begin() == false)
    {
        Serial.println("AHT10 not detected. Please check wiring. Freezing.");
    }
    else
        Serial.println("AHT10 acknowledged.");

    read_sensor();
    //setup over

    low_power_set();
}

void loop()
{
    wdt_disable();

    if (shu > 7) //(7+1) x 8S
    {
        //code start
        Serial.println("Code start*************************************");

        read_sensor();
        //code end
        Serial.println("Code end*************************************");

        //count init
        shu = 0;
    }

    watchdog_init();
    delay(10);
    sleep_cpu();
}

//Set low power mode and into sleep
void low_power_set()
{
    Serial.println("[Set]Sleep Mode Set");
    delay(100);
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    watchdog_init();
    delay(10);
    sleep_cpu();
}

//Enable watch dog
void watchdog_init()
{
    MCUSR &= ~(1 << WDRF);
    WDTCSR |= (1 << WDCE) | (1 << WDE);

    //WDTCSR = 1 << WDP1 | 1 << WDP2; //1S
    WDTCSR = 1 << WDP0 | 1 << WDP3; //8S

    WDTCSR |= _BV(WDIE); //not rst, inter interrutp
    wdt_reset();
}

void read_sensor()
{
    digitalWrite(sensorPowerCtrlPin, HIGH); //Sensor power on
    for (int i = 0; i < 3; i++)
    {
        sensorValue = analogRead(sensorPin);
        delay(200);
        if (humiditySensor.available() == true)
        {
            temperature = humiditySensor.getTemperature();
            humidity = humiditySensor.getHumidity();
        }
        if (isnan(humidity) || isnan(temperature))
        {
            Serial.println(F("Failed to read from AHT sensor!"));
        }
    }
    digitalWrite(sensorPowerCtrlPin, LOW); //Sensor power on

    String lora_msg = "#" + (String)packetnum +" NAME:" + (String)NODENAME + " H:" + (String)humidity + "% T:" + (String)temperature + " C" + " ADC:" + (String)sensorValue;

StaticJsonBuffer<200> jsonBuffer;

JsonObject& root = jsonBuffer.createObject();
root["id"] = "LoRaADC";
root["name"] = NODENAME;
root["model"] = "LSMS092D";
root["tempc"] = temperature;
root["hum"] = humidity;
//root[" Dew Point"] = dewpoint;
root["adc"] = sensorValue;
//root["topic"] = home/OMG_01/LORAtoMQTT;

//JsonArray& data = root.createNestedArray("data");
//data.add(temperature,6);  // 6 is the number of decimals to print
//data.add(humidity);   // if not specified, 2 digits are printed

//root.printTo(Serial);
//root.printTo(LoRa);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
    
    Serial.println(lora_msg);
    packetnum++;
    LoRa.beginPacket();
//    LoRa.print("test");
    root.printTo(LoRa);
//    LoRa.print(temperature);
//    LoRa.print(humidity);
//    LoRa.print((String)sensorValue);
    LoRa.endPacket();
//    radio.transmit(lora_msg);
    delay(1000);
    LoRa.sleep();
}

Don’t forget to grab the library files from the maker’s github (I2C_AHT10.h)

Arduino board should be “Arduino Pro or Pro Mini”
Processor should be “ATmega328p (3.3, 8 MHz)”

Pinout is defined in the silkscreen on the board.

When flashing the code to the device i had to briefly press the onboard reset button just before the upload took place.

Finally, in home assistant i’m using this as my configuration:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).adc }}'
  unit_of_measurement: 'µS/cm'

- platform: mqtt
  name: "Soil_1_humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).hum }}'
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_1_temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).tempc }}'
  unit_of_measurement: '°C'

And a HUGE thank you to all the people who worked on this!!!

ENJOY!

10 Likes

Update:

Here is the updated code to support multiple sensors:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_1_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'

- platform: mqtt
  name: "Soil_1_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_1_Humidity") }}
    {% endif %}
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_1_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ states("sensor.soil_1_Temperature") }}
    {% endif %}
  unit_of_measurement: '°C'
1 Like

Thank you for publishing this wonderful project. It is really works fine.
With great regards.

1 Like

Hi. I have the same exact LoRa soil moisture sensor. I am new to all this, but I have the OpenMQTT Gateway all configured and ready. I have two questions. The first being where does the home assistant configuration code go in HA?

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).adc }}'
  unit_of_measurement: 'µS/cm'

- platform: mqtt
  name: "Soil_1_humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).hum }}'
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_1_temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).tempc }}'
  unit_of_measurement: '°C'

and the second question is how do I get mosquito to listen to all lora messages in home assistant? Thank you

Hi there!
So in your config directory on the SMB share of your homeassistant volume, you should have a sensors.yaml

More on that here:

The sensor configurations you listed, should be in the sensors.yaml

And for your second question regarding how mosquito listens, well as quoted to me by @1technophile “Regarding MQTT via LORA, the gateway LORAtoMQTT will deal with this”.

You’ll need to setup your home assistant credentials in the openmqttgateway configuration.

In the user_config.h of your openmqttgateway instance, you’ll see the MQTT credentials section that you can fill out with your information. Then all lora messages are passed to home assistant.

I hope this helps!

So i’ve been running these for a few days now and everything worked really well for the one test sensor.
Now, with a second sensor, and the updated yaml to support multiple sensors (written with help from a fellow HA user), occasionally the temperature from one sensor affects the reading of the another sensor and i’m not sure why.
The reading looks fine and comes in as expected, but when the reading from the next sensor arrives, it is added to the first sensor - so the value jumps up until the next “correct” reading arrives.

i’m using this:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_1_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'

- platform: mqtt
  name: "Soil_1_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_1_Humidity") }}
    {% endif %}
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_1_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ states("sensor.soil_1_Temperature") }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_2_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_2_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  
- platform: mqtt
  name: "Soil_2_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_2_Humidity") }}
    {% endif %}
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_2_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ states("sensor.soil_2_Temperature") }}
    {% endif %}
  unit_of_measurement: '°C'

Is it possible i need to add some sort of “time out” so that it doesn’t take the second reading into account? I would think that the “if” argument in the yaml would do this but i’m not fluent in code so i any help figuring this out would be greatly appreciated!

Thanks in advance!

I’m wondering if having a different state state_topic for each sensor wouldn’t solve your issue.
for example
sensor1 state topic /home/OMG_01/LORAtoMQTT
sesnor2 state topic /home/OMG_02/LORAtoMQTT
I realize you are filtering in your template via name, but you could probably do away with that by using a different topic. setting a different topic for each device in the arduino code as well. so you have a unique pub and a unique sub.

I thought the same thing but if i’m not mistaken, that is governed by openmqttgateway. If someone were to have a second gateway, then it would be /home/OMG_02/LORAtoMQTT from what I understand…

I was thinking maybe it should be /home/OMG_01/LORAtoMQTT/Soil_1
but I don’t know if that’s possible (too many subtopics?), and I wouldn’t know how to implement that.
Perhaps it would be a feature request for OpenMqttGateway?

Thanks!

I took a quick look at their docs and didn’t see any specific limitation.
looking at your arduino code the topic is commented out.
//root[“topic”] = home/OMG_01/LORAtoMQTT;
you should be able to uncomment that and make it home/whatever/youwant (note: I do think it has to start with home/)
do that for each device recompile and re-upload
set 1 to 01 and the other to 02 you could do something like
home/sensors/soil01 and home/sensors/soil02
that should work. then adjust your HA config as needed

Thanks for your help!

So i’ve implemented this in the arduino code:

root["topic"] = "omg/LSMS092D/"NODENAME;

with the nodename being Soil01 now as suggested.

the message arrives as expected (using mqtteplorer):

{"rssi":-73,"snr":9.75,"pferror":18530,"packetSize":123,"message":"{\"id\":\"LoRaADC\",\"name\":\"Soil01\",\"model\":\"LSMS092D\",\"tempc\":22.26582,\"hum\":43.31064,\"adc\":877,\"topic\":\"omg/LSMS092D/Soil01\"}"}

but i’m not sure how to parse that information and use it as the state topic in my sensor.yaml.

- platform: mqtt
  name: "Soil01_ADC"
  state_topic: "omg/LSMS092D/Soil01"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil01" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil01_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'

This didn’t seem to work.

Any ideas?

Thanks again!

It’s weird too because this only seems to affect temperature…
Additionally, the messages shown in mqttexplorer all appear as expected… so it leads me to believe this a Home assistant issue or an issue with the sensor.yaml

{"rssi":-70,"snr":9.25,"pferror":7507,"packetSize":61,"message":"{\"name\":\"Soil_02\",\"temp_c\":20.08705,\"hum\":52.47841,\"adc\":877}"}

If anyone has any ideas to try, I welcome them.
Thanks!

edit: i’ve even tried giving the two different sensors completely different names (one had tempc and the other had temp_c, as well as different nodenames) and they still somehow affect the temperature vales of one another. Meanwhile the mqttexplorer show the values to be as expected…

Edit 2:
Trying things out, i moved the temperature thinking maybe because it was at the bottom of the configuration, moving might reveal something. It didn’t still the same behavior.
I’ve also slimmed down my lora message as i’ve read that it is best to keep them as short as possible…

- platform: mqtt
  name: "Soil_01_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_01_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  icon: mdi:flower

- platform: mqtt
  name: "Soil_01_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).temp_c }}
    {% else %}
      {{ states("sensor.soil_01_Temperature") | round(2)  }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_01_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_01_Humidity") | round(2)  }}
    {% endif %}
  unit_of_measurement: '%'
  icon: mdi:water-percent

- platform: mqtt
  name: "Soil_02_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_02" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_02_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  icon: mdi:flower

- platform: mqtt
  name: "Soil_02_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_02" %}
      {{ (value_json.message|from_json).temp_c }}
    {% else %}
      {{ states("sensor.soil_02_Temperature") | round(2) }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_02_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_02" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_02_Humidity") | round(2) }}
    {% endif %}
  unit_of_measurement: '%'
  icon: mdi:water-percent

Also, added the rounding to see if that would reveal anything.
What’s interesting, is that the rounded value sometimes gets lost and i get the non rounded value which appear as the proper value. When i recieve the rounded value, it is now rounded but the incorrect “added” value (the sum of soil_1 and soil_2) appears.
The icons load properly so that’s working, but it now does this:

• Sometimes drop the rounding (appears as non rounded values) but those are the proper values.
• Rounded values appear to be the sum of soil_1 temperature and soil_2 temperature.

any ideas?
Thanks

Your state topic is the same as before. it has to be what you changed it to in the arduino code.
try this in your HA config

state_topic: “omg/LSMS092D/Soil01”

for the value template, since it’s a unique name now, you don’t need the if then
probably just this line will work
{{ (value_json.message|from_json).hum }}

I actually did try this, but then edited my code because it didn’t work. (3 posts ago).

I can try again with your new {{ (value_json.message|from_json).hum }}
but i have a feeling because the topic is embedded in the message that this might not work.

I’ll give it a go.
Thanks!

Edit: tried it again and it didn’t work…
I’m guessing because the topic is embedded in the message, we would effectively be doing the same as what I currently have - a filter to extract the topic… right?

OK! i figured it out!

well kinda…

I changed:

states("sensor.soil_01_adc")

to

is_state_attr("sensor.soil_01_adc")

So the sensors no longer affect each others’ behavior which is awesome!
Bu the rounding isn’t working, so there is still something buggy… but getting there!! i can finally test out several nodes at once!

here is the working code to support multiple sensors:

- platform: mqtt
  name: "Soil_01_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  icon: mdi:flower

- platform: mqtt
  name: "Soil_01_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_Temperature") | round(2) }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_01_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_Humidity") | round(2) }}
    {% endif %}
  unit_of_measurement: '%'
  icon: mdi:water-percent

I suppose i should also share the arduino code i’m usin since i messed around with it a bit:

#include <avr/wdt.h>
#include <avr/sleep.h>
//#include <RadioLib.h>
#include "I2C_AHT10.h"
#include <Wire.h>
#include <SPI.h>
#include <LoRa.h>
#include <ArduinoJson.h>

#define NODENAME "Soil_01"

#define DIO0 2
#define DIO1 6
#define DIO2 7
#define DIO5 8

#define LORA_RST 4
#define LORA_CS 10

#define SS      10   // GPIO18 -- SX1278's CS
#define RST     4   // GPIO14 -- SX1278's RESET
#define DI0     2   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  915E6

#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13
//#define SPI_SS 10

int ledPin = 13;
int shu = 0;
int sensorPin = A2; // select the input pin for the potentiometer
int sensorPowerCtrlPin = 5;

int sensorValue = 0;     // variable to store the value coming from the sensor
int16_t packetnum = 0;   // packet counter, we increment per xmission
float temperature = 0.0; //
float humidity = 0.0;
//float dewpoint = 0.0;
String errcode = "";


// SX1278 radio = new Module(LORA_CS, DIO0, LORA_RST, DIO1, SPI, SPISettings());
AHT10 humiditySensor;

ISR(WDT_vect)
{
    Serial.print("[Watch dog]");
    Serial.println(shu);
    delay(100);
    shu++;
    wdt_reset();
}

void setup()
{
    wdt_disable();
    pinMode(ledPin, OUTPUT);
    Serial.begin(115200);
    Serial.println("[Start]");
    delay(100);
    SPI.begin();

    //setup start
    Serial.println("[Setup]");
    delay(100);

    // initialize SX1278 with default settings
    Serial.println(String("Sensor name is :") + String(NODENAME));
    Serial.print(F("Initializing ... "));
    SPI.begin();
    LoRa.setPins(SS,RST,DI0);
    if (!LoRa.begin(915E6)) {
      Serial.println("Starting LoRa failed!");
      while (1);
    }
//    else
//    {
//        Serial.print(Success!));
//        Serial.println("LoRa ok");
//        while (0)
//            ;
//    }
    LoRa.sleep();

    //AHT10
    pinMode(sensorPowerCtrlPin, OUTPUT);
    digitalWrite(sensorPowerCtrlPin, HIGH); //Sensor power on

    Wire.begin();
    if (humiditySensor.begin() == false)
    {
        Serial.println("AHT10 not detected. Please check wiring. Freezing.");
    }
    else
        Serial.println("AHT10 acknowledged.");

    read_sensor();
    //setup over

    low_power_set();
}

void loop()
{
    wdt_disable();

    if (shu > 7) //(7+1) x 8S
    {
        //code start
        Serial.println("Code start*************************************");

        read_sensor();
        //code end
        Serial.println("Code end*************************************");

        //count init
        shu = 0;
    }

    watchdog_init();
    delay(10);
    sleep_cpu();
}

//Set low power mode and into sleep
void low_power_set()
{
    Serial.println("[Set]Sleep Mode Set");
    delay(100);
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    watchdog_init();
    delay(10);
    sleep_cpu();
}

//Enable watch dog
void watchdog_init()
{
    MCUSR &= ~(1 << WDRF);
    WDTCSR |= (1 << WDCE) | (1 << WDE);

    //WDTCSR = 1 << WDP1 | 1 << WDP2; //1S
    WDTCSR = 1 << WDP0 | 1 << WDP3; //8S

    WDTCSR |= _BV(WDIE); //not rst, inter interrutp
    wdt_reset();
}

void read_sensor()
{
    digitalWrite(sensorPowerCtrlPin, HIGH); //Sensor power on
    for (int i = 0; i < 3; i++)
    {
        sensorValue = analogRead(sensorPin);
        delay(200);
        if (humiditySensor.available() == true)
        {
            temperature = humiditySensor.getTemperature();
            humidity = humiditySensor.getHumidity();
        }
        if (isnan(humidity) || isnan(temperature))
        {
            Serial.println(F("Failed to read from AHT sensor!"));
        }
    }
    digitalWrite(sensorPowerCtrlPin, LOW); //Sensor power on

    String lora_msg = "#" + (String)packetnum +" NAME:" + (String)NODENAME + " H:" + (String)humidity + "% T:" + (String)temperature + " C" + " ADC:" + (String)sensorValue;

StaticJsonBuffer<200> jsonBuffer;

JsonObject& root = jsonBuffer.createObject();
//root["id"] = "LoRaADC";
root["name"] = NODENAME;
//root["model"] = "LSMS092D";
root["tempc"] = temperature;
root["hum"] = humidity;
//root[" Dew Point"] = dewpoint;
root["adc"] = sensorValue;
//root["topic"] = "omg/LSMS092D/"NODENAME;

//JsonArray& data = root.createNestedArray("data");
//data.add(temperature,6);  // 6 is the number of decimals to print
//data.add(humidity);   // if not specified, 2 digits are printed

//root.printTo(Serial);
//root.printTo(LoRa);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
    
    Serial.println(lora_msg);
    packetnum++;
    LoRa.beginPacket();
//    LoRa.print("test");
    root.printTo(LoRa);
//    LoRa.print(temperature);
//    LoRa.print(humidity);
//    LoRa.print((String)sensorValue);
    LoRa.endPacket();
//    radio.transmit(lora_msg);
    delay(1000);
    LoRa.sleep();
}

Enjoy!

I am about to implement this, but before I do are there any further updates or tips?

And is the code in post 2 the one to install?

Hey sorry I missed this.
Did you get it all working?
Cheers

Hey, I have allmost the same setup as you, but I have problems to get the communication working - maybe you chave an idea?

Lora bridge setup with a heltec ESP32 Lora an OpenMQTTGateway, I think the gateway is configured correct, cause I can see communication between the gateway and MQTT in the Mosquitto log, also it was auto configured in HASS.

I bought the Makerfab Lora Soil Sensor V3, powered it up ( frequency matching with gateway), but I can´t see any communication from the sensor in the log… What I’m doing wrong?

I had the same issue and it only worked when I added WIP Receive properly LORA messages · kamaradclimber/OpenMQTTGateway@7968bae · GitHub patch.
Maybe it could help you

2 Likes

Thanks, but it looks like that the patch is allready integrated in the current code. :confused:

Are you sure it has already been integrated? I have not submitted a PR and the upstream version of the file does not set all those parameters except in MQTT2Lora method.

1 Like