This code I customized and I am sharing works perfectly for me.
I’m using version 2.1.16
I use the default libraries of seeeduino.
#include <TheThingsNetwork.h>
#include <LoRaWan.h>
#include “DHT.h”
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned char data[4];
char buffer[256];
void setup(void)
{
dht.begin();
SerialUSB.begin(115200);
while(!SerialUSB);
lora.init();
memset(buffer, 0, 256);
lora.getVersion(buffer, 256, 1);
SerialUSB.print(buffer);
memset(buffer, 0, 256);
lora.getId(buffer, 256, 1);
SerialUSB.print(buffer);
lora.setId("XXXXXXXX", "XXXXXXXXXXX","XXXXXXXXXXX" );
lora.setKey(NULL, NULL, "XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
lora.setDeciveMode(LWOTAA);
lora.setDataRate(DR5, EU868);
lora.setAdaptiveDataRate(true);
lora.setChannel(0, 868.1);
lora.setChannel(1, 868.3);
lora.setChannel(2, 868.5);
lora.setChannel(3, 867.1);
lora.setChannel(4, 867.3);
lora.setChannel(5, 867.5);
lora.setChannel(6, 867.7);
lora.setReceiceWindowFirst(1, 868.3);
lora.setReceiceWindowSecond(869.525, DR0);
lora.setPower(14);
while(!lora.setOTAAJoin(JOIN));
}
void loop(void)
{
bool result = false;
delay(2000);
SerialUSB.println("-- LOOP");
uint32_t humidity = dht.readHumidity(false) * 100;
// float t = dht.readTemperature(false);
uint32_t temperature = dht.readTemperature(false) * 100;
byte payload[4];
payload[0] = highByte(temperature);
payload[1] = lowByte(temperature);
payload[2] = highByte(humidity);
payload[3] = lowByte(humidity);
SerialUSB.print("Temperatura: ");
SerialUSB.println(temperature);
SerialUSB.print("Umidade: ");
SerialUSB.println(humidity);
// SerialUSB.print("Temperatura: " + String(temperature));
// SerialUSB.print("Umidade: " + String(humidity));
//byte payload[4];
//data[0] = highByte(t);
//data[0] = lowByte(t);
//data[2] = highByte(h);
// data[1] = lowByte(h);
// data[1] = h;
// data[2] = t;
result = lora.transferPacket(payload, 4, 10);
//result = lora.transferPacket("Marcelo Balisteri!", 10);
if(result)
{
short length;
short rssi;
memset(buffer, 0, 256);
length = lora.receivePacket(buffer, 256, &rssi);
if(length)
{
SerialUSB.print("Length is: ");
SerialUSB.println(length);
SerialUSB.print("RSSI is: ");
SerialUSB.println(rssi);
SerialUSB.print("Data is: ");
for(unsigned char i = 0; i < length; i ++)
{
SerialUSB.print("0x");
SerialUSB.print(buffer[i], HEX);
SerialUSB.print(" ");
}
SerialUSB.println();
}
}
delay(5000);
}
This is the TTN decoder Temp and Hum
// Decodificação do payload no tttn
function Decoder(bytes, port) {
var humidity = (bytes[0]<< 8) | bytes[1];
var temperature = (bytes[2] <<8) | bytes[3];
return {
humidity: Umidade,
temperature: Temperatura
}
}