Chirpstack v3 decoder for MKR1310 + DHT22

Hello,
I am having problems configuring my codec decoder for an AM2302 (DHT22) connected to an MKR1310. Much appreciated so far for the help I have received getting the MKR1310 to join the chirpstack gateway (RAK7244).

My IDE monitor shows that the device is correctly sending out the temp and hum.

I have setup the following codec for my device profile but have a problem as I’m not familiar with the format or syntax needed to decode the payload:

Blockquote

function Decode(fPort, bytes) {
var decoded = {};
decoded.temperature = (bytes[0] << 8) | bytes[1] / 100;
decoded.humidity = (bytes[2] << 8) | bytes[3] / 100;
return decoded;
}

Blockquote

I get the following confirmed data on the gateway:

applicationID:“49”
applicationName:“mkr1310”
deviceName:“mkr1310”
devEUI:“3930313052317908”
rxInfo:
frequency:904300000
modulation:“LORA”

bandwidth:125
spreadingFactor:10
codeRate:"4/5"
polarizationInversion:false

adr:true
dr:0
fCnt:665
fPort:0
data:null
humidity:0
temperature:0
tags:

    confirmedUplink:true
    devAddr:"00b6dc9d"
    publishedAt:"2024-10-27T15:55:24.571191170Z"
    deviceProfileID:"4f2437ec-ed99-43e5-8931-614b53d6eb56"
    deviceProfileName:"mkr1310-device"

Blockquote

It gives both temp and hum as “0”
Thanks for any help here. I am not a programmer.
Cheers,
Bob

Are you sure your sensor sends data?

What if you manually put some numbers on bytes[] on the sensor?

Thanks, clavisound. Not sure where the “bytes” would go which you refer to.
However, the program is running on the IDE serial monitor every couple of minutes and give the following output:

15:33:02.413 -> Temperature:
15:33:02.413 -> 22.90
15:33:02.413 -> Humidity:
15:33:02.413 -> 43.70

My program is this:

//DHT22_MKR_oct26.ino
//=============================
#include <MKRWAN_v2.h>
#include <DHT.h>

LoRaModem modem(Serial1);

String appEui = "0000000000000000"; // Add your own AppEUI here
String appKey = "01020304050607080910111213141516"; // Add your own AppKey here

DHT dht(2, DHT22);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  // change this to your regional band (eg. US915, AS923, ...)
  if (!modem.begin(US915)) {
    Serial.println("Failed to start module");
    while (1) {}
  };
  
  dht.begin();

  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
}

void loop() {
  int t = dht.readTemperature() * 100;
  int h = dht.readHumidity() * 100;

  byte payload[4];
  payload[0] = highByte(t);
  payload[1] = lowByte(t);
  payload[2] = highByte(h);
  payload[3] = lowByte(h);

  Serial.println("Temperature: ");
  Serial.println(dht.readTemperature());
  Serial.println("Humidity: ");
  Serial.println(dht.readHumidity());

  modem.beginPacket();
  modem.write(payload, sizeof(payload));
  modem.endPacket(true);
  delay(100000);
}

Does this show that it’s sending data? Maybe it’s just going to the serial monitor but not out to the LoRa payload?
Bob

Why fport=0?

The fport for uplink MUST > 0.
You may look at the wrong packet.
Or you send the wrong fport=0.

2 Likes

Thanks, but how do I change fport from 0 to a greater value?

Before modem.beginPacket add a line

modem.setPort(1);
1 Like

That did it! Thank you all and especially clavisound!

1 Like

No, @IoTThinks made the nice catch :wink:

Have a nice day.

And @clavisound has nice solution.
Glad it works for you.