GIRNI

Grupo de Investigación de Redes de iNformación Inalámbricas

LoRaWan - Pluviometro: Chirpstack y HA

Las instrucciones para interpretar la trama en el gestor de gateways Chirpstack se realizan en JavaScript, siguiendo el mismo orden de bytes transmitido desde el dispositivo.

Para el caso de valores de temperatura se usan números enteros de 2 bytes con signo con dos decimales, según lo permite el sensor usado. El valor antes de transmitir se multiplica por 100. En la recepción se restaura el formato del número al dividirlo para 100.

Al final se añade el acumulado del pluviómetro de los últimos 5 minutos como pluviosum para fines generales.

Con esta parte se habilita la lectura de los parámetros para gestionar los datos en HomeAssistant mediante un mensaje Mqtt.

ChirpStack

// Decode decodes an array of bytes into an object.
//  - fPort contains the LoRaWAN fPort number
//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
//  - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes, variables) {
  var Down_rssi = -1*parseInt(bytes[0]);
  var Down_snr = bytes[1];
  var Down_datarate = bytes[2];

  // usando entero sin signo
  var bateria = (bytes[4] << 8) |(bytes[3]);
  bateria = (bateria/1000);
  bateria = +bateria.toFixed(2);
  
  // usando entero con signo
  var unalectura = (bytes[6] << 8) |(bytes[5]); var unsigno = (unalectura>>>15 === 0) ? 1.0 : -1.0;
  var unvalor = unalectura & 0x7fff;
  if (unsigno<0){unvalor = unvalor - 32768;}
  var ht_temp = unvalor/100;
  ht_temp = +ht_temp.toFixed(2);

  var ht_humi = (bytes[8] << 8) |(bytes[7]);
  ht_humi = ht_humi/100;
  ht_humi = +ht_humi.toFixed(2);
  
  var unalectura = (bytes[10] << 8) |(bytes[9]); var unsigno = (unalectura>>>15 === 0) ? 1.0 : -1.0;
  var unvalor = unalectura & 0x7fff;
  if (unsigno<0){unvalor = unvalor - 32768;}
  var bar_temp = unvalor/100;
  bar_temp = +bar_temp.toFixed(2);
  
  var unalectura = (bytes[14] << 24) | (bytes[13]<< 16) | (bytes[12] << 8) |(bytes[11]); var unsigno = (unalectura>>>31 === 0) ? 1.0 : -1.0;
  var exponente = unalectura>>>23 & 0xff;
  var mantisa = (exponente === 0) ? (unalectura & 0x7fffff)<<1 : (unalectura & 0x7fffff) | 0x800000;
  var NumReal = unsigno * mantisa * Math.pow(2, exponente - 150);
  var bar_press = NumReal;
  
  var pluvi0 = bytes[15];
  var pluvi1 = bytes[16];
  var pluvi2 = bytes[17];
  var pluvi3 = bytes[18];
  var pluvi4 = bytes[19];
  var pluviosum = pluvi0 + pluvi1 + pluvi2 + pluvi3 + pluvi4;
  var appData = {'Down_rssi':Down_rssi,'Down_snr':Down_snr,
                 'Down_datarate':Down_datarate,
                 'bateria_V':bateria,
                 'ht_temp':ht_temp,'ht_humi':ht_humi,
                 'bar_temp':bar_temp,'bar_press':bar_press,
                 'pluvi0':pluvi0,'pluvi1':pluvi1,'pluvi2':pluvi2,'pluvi3':pluvi3,'pluvi4':pluvi4,
                 'pluviosum':pluviosum,
                }
  return appData;
}

Home Assistant

mqtt:
  sensor:
    - name: "pluviometro01"
      state_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"
      unit_of_measurement: "mm/m2"
      value_template: "{% set valores = value_json.objectJSON |from_json %} {{valores.pluviosum}}"
      #availability:
      #  - topic: "home/sensor1/status"
      payload_available: "online"
      payload_not_available: "offline"
      json_attributes_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"

    - name: "Temperatura_pluvi01"
      state_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"
      unit_of_measurement: "°C"
      value_template: "{% set valores = value_json.objectJSON |from_json %} {{valores.ht_temp}}"

    - name: "Humedad_pluvi01"
      state_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"
      unit_of_measurement: "%"
      value_template: "{% set valores = value_json.objectJSON |from_json %} {{valores.ht_humi}}"

    - name: "Temperatura_pluvi01"
      state_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"
      unit_of_measurement: "°C"
      value_template: "{% set valores = value_json.objectJSON |from_json %} {{valores.bar_temp}}"

    - name: "Presion_pluvi01"
      state_topic: "application/4/device/2e4fa4ddf02f06eb/event/up"
      unit_of_measurement: "Pa"
      value_template: "{% set valores = value_json.objectJSON |from_json %} {{valores.bar_press}}"

..