* This example shows LoRaWan protocol joining the network in OTAA mode, class A, region EU868.
* Device will send uplink every 20 seconds.
***/
#define analogPin1 PA10
#define analogPin2 PB2
#define analogPin3 PB4
#define analogPin4 PB3
#define analogPin5 PA15
#define digitalPin1 PA8
#define digitalPin2 PA9
#define digitalPin3 PA0
#define digitalPin4 PB5
#define OTAA_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 OTAA_BAND (RAK_REGION_US915)
#define OTAA_DEVEUI {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}
#define OTAA_APPEUI {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
#define OTAA_APPKEY {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x0AA, 0xAA, 0xAA, 0xAA}
/** Packet buffer for sending */
int fPort = 0;
uint8_t collected_data[200] = { 0 };
int aVal1 = 0;
int aVal2 = 0;
int aVal3 = 0;
int aVal4 = 0;
int aVal5 = 0;
int cVal1 = 0;
int cVal2 = 0;
int cVal3 = 0;
int cVal4 = 0;
int cVal5 = 0;
int payload_counter = 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);
}
/*************************************
* enum type for LoRa Event
RAK_LORAMAC_STATUS_OK = 0,
RAK_LORAMAC_STATUS_ERROR,
RAK_LORAMAC_STATUS_TX_TIMEOUT,
RAK_LORAMAC_STATUS_RX1_TIMEOUT,
RAK_LORAMAC_STATUS_RX2_TIMEOUT,
RAK_LORAMAC_STATUS_RX1_ERROR,
RAK_LORAMAC_STATUS_RX2_ERROR,
RAK_LORAMAC_STATUS_JOIN_FAIL,
RAK_LORAMAC_STATUS_DOWNLINK_REPEATED,
RAK_LORAMAC_STATUS_TX_DR_PAYLOAD_SIZE_ERROR,
RAK_LORAMAC_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOSS,
RAK_LORAMAC_STATUS_ADDRESS_FAIL,
RAK_LORAMAC_STATUS_MIC_FAIL,
RAK_LORAMAC_STATUS_MULTICAST_FAIL,
RAK_LORAMAC_STATUS_BEACON_LOCKED,
RAK_LORAMAC_STATUS_BEACON_LOST,
RAK_LORAMAC_STATUS_BEACON_NOT_FOUND,
*************************************/
void sendCallback(int32_t status)
{
if (status == RAK_LORAMAC_STATUS_OK) {
Serial.println("Successfully sent");
} else {
Serial.println("Sending failed");
}
}
void setup()
{
Serial.begin(115200, RAK_AT_MODE);
delay(2000);
randomSeed(micros());
fPort = random(10,200);
Serial.print("Fport is: ");Serial.println(fPort);
// Setup output control pins
analogReadResolution(12);
pinMode(digitalPin1, OUTPUT);
pinMode(digitalPin2, OUTPUT);
pinMode(digitalPin3, OUTPUT);
pinMode(digitalPin4, OUTPUT);
digitalWrite(digitalPin1, LOW);
digitalWrite(digitalPin2, LOW);
digitalWrite(digitalPin3, LOW);
digitalWrite(digitalPin4, LOW);
Serial.println("RAKwireless LoRaWan OTAA Example");
Serial.println("------------------------------------------------------");
if(api.lorawan.nwm.get() != 1)
{
Serial.printf("Set Node device work mode %s\r\n",
api.lorawan.nwm.set(1) ? "Success" : "Fail");
api.system.reboot();
}
// OTAA Device EUI MSB first
uint8_t node_device_eui[8] = OTAA_DEVEUI;
// OTAA Application EUI MSB first
uint8_t node_app_eui[8] = OTAA_APPEUI;
// OTAA Application Key MSB first
uint8_t node_app_key[16] = OTAA_APPKEY;
if (!api.lorawan.appeui.set(node_app_eui, 8)) {
Serial.printf("LoRaWan OTAA - set application EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.appkey.set(node_app_key, 16)) {
Serial.printf("LoRaWan OTAA - set application key is incorrect! \r\n");
return;
}
if (!api.lorawan.deui.set(node_device_eui, 8)) {
Serial.printf("LoRaWan OTAA - set device EUI is incorrect! \r\n");
return;
}
if (!api.lorawan.band.set(OTAA_BAND)) {
Serial.printf("LoRaWan OTAA - set band is incorrect! \r\n");
return;
}
if (!api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)) {
Serial.printf("LoRaWan OTAA - 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 OTAA - set network join mode is incorrect! \r\n");
return;
}
if (!api.lorawan.join()) // Join to Gateway
{
Serial.printf("LoRaWan OTAA - join fail! \r\n");
return;
}
/** 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 OTAA - set adaptive data rate is incorrect! \r\n");
return;
}
if (!api.lorawan.rety.set(1)) {
Serial.printf("LoRaWan OTAA - set retry times is incorrect! \r\n");
return;
}
if (!api.lorawan.cfm.set(1)) {
Serial.printf("LoRaWan OTAA - 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", OTAA_PERIOD);
Serial.println("");
api.lorawan.registerRecvCallback(recvCallback);
api.lorawan.registerJoinCallback(joinCallback);
api.lorawan.registerSendCallback(sendCallback);
}
void uplink_routine()
{
uint8_t assigned_dev_addr[4] = { 0 };
api.lorawan.daddr.get(assigned_dev_addr, 4);
digitalWrite(digitalPin1, HIGH);
delay(200);
aVal1 = analogRead(analogPin1);
Serial.println(aVal1);
digitalWrite(digitalPin1, LOW);
digitalWrite(digitalPin2, HIGH);
delay(200);
aVal2 = analogRead(analogPin2);
Serial.println(aVal2);
digitalWrite(digitalPin2, LOW);
digitalWrite(digitalPin3, HIGH);
delay(200);
aVal3 = analogRead(analogPin3);
Serial.println(aVal3);
digitalWrite(digitalPin3, LOW);
digitalWrite(digitalPin4, HIGH);
delay(200);
aVal4 = analogRead(analogPin4);
Serial.println(aVal4);
digitalWrite(digitalPin4, LOW);
aVal5 = analogRead(analogPin5);
Serial.println(aVal5);
float res = 8059;
cVal1 = (int)(aVal1 * res / 10000);
Serial.println(cVal1);
cVal2 = (int)(aVal2 * res / 10000);
Serial.println(cVal2);
cVal3 = (int)(aVal3 * res / 10000);
Serial.println(cVal3);
cVal4 = (int)(aVal4 * res / 10000);
Serial.println(cVal4);
cVal5 = (int)(aVal5 * res / 10000);
Serial.println(cVal5);
/** Payload of Uplink */
uint8_t data_len = 0;
collected_data[data_len++] = (uint8_t) 0x7E;
collected_data[data_len++] = (uint8_t) 0;
collected_data[data_len++] = (uint8_t) 0;
collected_data[data_len++] = (uint8_t) assigned_dev_addr[0];
collected_data[data_len++] = (uint8_t) assigned_dev_addr[1];
collected_data[data_len++] = (uint8_t) assigned_dev_addr[2];
collected_data[data_len++] = (uint8_t) assigned_dev_addr[3];
collected_data[data_len++] = (uint8_t) highByte(cVal5);
collected_data[data_len++] = (uint8_t) lowByte(cVal5);
collected_data[data_len++] = (uint8_t) payload_counter;
collected_data[data_len++] = (uint8_t) 1;
collected_data[data_len++] = (uint8_t) highByte(cVal1);
collected_data[data_len++] = (uint8_t) lowByte(cVal1);
collected_data[data_len++] = (uint8_t) 2;
collected_data[data_len++] = (uint8_t) highByte(cVal2);
collected_data[data_len++] = (uint8_t) lowByte(cVal2);
collected_data[data_len++] = (uint8_t) 3;
collected_data[data_len++] = (uint8_t) highByte(cVal3);
collected_data[data_len++] = (uint8_t) lowByte(cVal3);
collected_data[data_len++] = (uint8_t) 4;
collected_data[data_len++] = (uint8_t) highByte(cVal4);
collected_data[data_len++] = (uint8_t) lowByte(cVal4);
collected_data[data_len++] = (uint8_t) 0xFF;
Serial.println("Data Packet:");
for (int i = 0; i < data_len; i++) {
Serial.printf("0x%02X ", collected_data[i]);
}
payload_counter++;
/** Send the data package */
if (api.lorawan.send(data_len, (uint8_t *) & collected_data, fPort, true, 1)) {
Serial.println("Sending is requested");
} else {
Serial.println("Sending failed");
}
}
void loop()
{
static uint64_t last = 0;
static uint64_t elapsed;
if ((elapsed = millis() - last) > OTAA_PERIOD) {
uplink_routine();
last = millis();
}
Serial.printf("Try sleep %ums..", OTAA_PERIOD);
api.system.sleep.all(OTAA_PERIOD);
Serial.println("Wakeup..");
}