Hello everyone, I am new to this forum, can you help me see that I am doing wrong in making the connection with Lorawan, I am using the example of Water Level.
I understand that with this code the lorawan connection data is taken from the data edited with wistoolbox
I have the same data created in Chirpstack V4
use another code where the connection data is written and in this way if it connects to the Chirpstack, it can tell me that I may be doing it wrong
I attached code that works for me
#include "rak1901.h"
void uplink_routine();
#define SMART_FARM_PERIOD (20000)
/*************************************
LoRaWAN band setting:
RAK_REGION_EU433
RAK_REGION_CN470
RAK_REGION_RU864
RAK_REGION_IN865
RAK_REGION_EU868
RAK_REGION_US915
RAK_REGION_AU915
RAK_REGION_KR920
RAK_REGION_AS923
*************************************/
#define SMART_FARM_BAND (RAK_REGION_US915)
#define SMART_FARM_DEVEUI {0x4b, 0x77, 0x09, 0xbb, 0x66, 0x03, 0x5c, 0x29}
#define SMART_FARM_APPEUI {0x0b, 0xe2, 0x43, 0x68, 0x0b, 0x6f, 0x54, 0x83}
#define SMART_FARM_APPKEY {0x7f, 0x5a, 0x94, 0x9c, 0xb5, 0x33, 0x5a, 0x40, 0x94, 0xa5, 0x32, 0x94, 0xb9, 0x5a, 0x63, 0xd7}
/** Temperature & Humidity sensor **/
rak1901 th_sensor;
/** Packet buffer for sending */
uint8_t collected_data[64] = { 0 };
void recvCallback(SERVICE_LORA_RECEIVE_T * data)
{
if (data->BufferSize > 0) {
Serial.println("Something received!");
for (int i = 0; i < data->BufferSize; i++) {
Serial.printf("%x", data->Buffer[i]);
}
Serial.print("\r\n");
}
}
void joinCallback(int32_t status)
{
Serial.printf("Join status: %d\r\n", status);
}
void sendCallback(int32_t status)
{
if (status == 0) {
Serial.println("Successfully sent");
} else {
Serial.println("Sending failed");
}
}
void setup()
{
Serial.begin(115200, RAK_AT_MODE);
delay(2000);
Serial.println("RAKwireless Smart Farm Example");
Serial.println("------------------------------------------------------");
// OTAA Device EUI MSB first
uint8_t node_device_eui[8] = SMART_FARM_DEVEUI;
// OTAA Application EUI MSB first
uint8_t node_app_eui[8] = SMART_FARM_APPEUI;
// OTAA Application Key MSB first
uint8_t node_app_key[16] = SMART_FARM_APPKEY;
if (!api.system.lpm.set(1)) {
Serial.printf("LoRaWan Smart Farm - set low power mode is incorrect! \r\n");
return;
}
if (!api.lorawan.nwm.set(1)) {
Serial.printf("LoRaWan Smart Farm - set network working mode is incorrect! \r\n");
return;
}
if (!api.lorawan.appeui.set(node_app_eui, 8)) {
Serial.printf("LoRaWan Smart Farm - set application EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.appkey.set(node_app_key, 16)) {
Serial.printf("LoRaWan Smart Farm - set application key is incorrect! \r\n");
return;
}
if (!api.lorawan.deui.set(node_device_eui, 8)) {
Serial.printf("LoRaWan Smart Farm - set device EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.band.set(SMART_FARM_BAND)) {
Serial.printf("LoRaWan Smart Farm - set band is incorrect! \r\n");
return;
}
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)) {
Serial.printf("LoRaWan Smart Farm - set device class is incorrect! \r\n");
return;
}
if (!api.lorawan.njm.set(RAK_LORA_OTAA)) // Set the network join mode to OTAA
{
Serial.printf("LoRaWan Smart Farm - set network join mode is incorrect! \r\n");
return;
}
if (!api.lorawan.join()) // Join to Gateway
{
Serial.printf("LoRaWan Smart Farm - join fail! \r\n");
return;
}
Serial.println("++++++++++++++++++++++++++");
Serial.println("RUI3 Environment Sensing");
Serial.println("++++++++++++++++++++++++++");
Wire.begin(); // Start I2C Bus
Serial.printf("RAK1901 init %s\r\n", th_sensor.init()? "success" : "fail"); // Check if RAK1901 init success
/** Wait for Join success */
while (api.lorawan.njs.get() == 0) {
Serial.print("Wait for LoRaWAN join...");
api.lorawan.join();
delay(10000);
}
if (!api.lorawan.adr.set(true)) {
Serial.printf("LoRaWan Smart Farm - set adaptive data rate is incorrect! \r\n");
return;
}
if (!api.lorawan.rety.set(1)) {
Serial.printf("LoRaWan Smart Farm - set retry times is incorrect! \r\n");
return;
}
if (!api.lorawan.cfm.set(1)) {
Serial.printf("LoRaWan Smart Farm - set confirm mode is incorrect! \r\n");
return;
}
/** Check LoRaWan Status*/
Serial.printf("Duty cycle is %s\r\n", api.lorawan.dcs.get()? "ON" : "OFF"); // Check Duty Cycle status
Serial.printf("Packet is %s\r\n", api.lorawan.cfm.get()? "CONFIRMED" : "UNCONFIRMED"); // Check Confirm status
uint8_t assigned_dev_addr[4] = { 0 };
api.lorawan.daddr.get(assigned_dev_addr, 4);
Serial.printf("Device Address is %02X%02X%02X%02X\r\n", assigned_dev_addr[0], assigned_dev_addr[1], assigned_dev_addr[2], assigned_dev_addr[3]); // Check Device Address
Serial.printf("Uplink period is %ums\r\n", SMART_FARM_PERIOD);
Serial.println("");
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerSendCallback(sendCallback);
if (api.system.timer.create(RAK_TIMER_0, (RAK_TIMER_HANDLER)uplink_routine, RAK_TIMER_PERIODIC) != true) {
Serial.printf("LoRaWan Smart Farm - Creating timer failed.\r\n");
return;
}
if (api.system.timer.start(RAK_TIMER_0, SMART_FARM_PERIOD, NULL) != true) {
Serial.printf("LoRaWan Smart Farm - Starting timer failed.\r\n");
return;
}
}
void uplink_routine()
{
/** Get sensor RAK1901 values */
th_sensor.update();
float temp_f = th_sensor.temperature();
float humid_f = th_sensor.humidity();
Serial.printf("T %.2f H %.2f\r\n", temp_f, humid_f);
uint16_t t = (uint16_t) (temp_f * 10.0);
uint16_t h = (uint16_t) (humid_f * 2);
/** Cayenne Low Power Payload */
uint8_t data_len = 0;
collected_data[data_len++] = 0x01; //Data Channel: 1
collected_data[data_len++] = 0x67; //Type: Temperature Sensor
collected_data[data_len++] = (uint8_t) (t >> 8);
collected_data[data_len++] = (uint8_t) t;
collected_data[data_len++] = 0x02; //Data Channel: 2
collected_data[data_len++] = 0x68; //Type: Humidity Sensor
collected_data[data_len++] = (uint8_t) h;
Serial.println("Data Packet:");
for (int i = 0; i < data_len; i++) {
Serial.printf("0x%02X ", collected_data[i]);
}
Serial.println("");
/** Send the data package */
if (api.lorawan.send(data_len, (uint8_t *) & collected_data, 2, true, 1)) {
Serial.println("Sending is requested");
} else {
Serial.println("Sending failed");
}
}
void loop()
{
/* Destroy this busy loop and use timer to do what you want instead,
* so that the system thread can auto enter low power mode by api.system.lpm.set(1); */
api.system.scheduler.task.destroy();
}
Best Regards.