Hi
Not being very familiar with C/C++ and Arduino world, trying to get my RAK3172 and two sensors to work properly on Lora (for now I’m just getting the VOC sensor to work as the temp/humidity one has a hardware issue for now). I got lot of help from Rak staffs and used also blog posts to find out at best how to do it ! It runs but has following issues:
→ sends Lora payload only one time after programming (I’d like it sends payload with all datas every 20 minutes for example)
→ unable anymore to use AT commands on the RAK3172 and if I try to use the ATC+SENDINT command to setup sleep interval, it states it’s an unknown command
Here is the code (be cool as it’s my first code in Arduino and also with RUI3
#include "rak1906.h"
#include <SensirionI2CSgp40.h> // Click here to get the library: http://librarymanager/All#SensirionI2CSgp40
#include <Wire.h>
#include <Arduino.h>
#include <CayenneLPP.h>
#define LPP_CHANNEL_BATT 1 // Base Board
#define LPP_CHANNEL_SWITCH 48 // RAK13011
/* rak1906 rak1906;
float Temperature;
float Humidity; */
/** Flag if TX is active */
volatile bool tx_active = false;
volatile uint8_t events_queue[50] = {0};
volatile uint8_t event_ptr = 0;
/** LoRaWAN packet */
CayenneLPP g_solution_data(255);
/** fPort to send packages */
uint8_t set_fPort = 2;
/** Packet is confirmed/unconfirmed (Set with AT commands) */
bool g_confirmed_mode = false;
/** If confirmed packet, number or retries (Set with AT commands) */
uint8_t g_confirmed_retry = 0;
/** Data rate (Set with AT commands) */
uint8_t g_data_rate = 3;
/** Frequent data sending time */
uint32_t g_send_repeat_time = 30000;
SensirionI2CSgp40 sgp40;
void setup()
{
uint16_t error;
char errorMessage[256];
uint16_t serialNumber[3];
uint8_t serialNumberSize = 3;
uint8_t ret;
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("RAK12047 SGP40 example");
Wire.begin();
sgp40.begin(Wire);
error = sgp40.getSerialNumber(serialNumber, serialNumberSize);
if (error)
{
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
Serial.print("Serial Number:");
Serial.print("0x");
for (size_t i = 0; i < serialNumberSize; i++)
{
uint16_t value = serialNumber[i];
Serial.print(value < 4096 ? "0" : "");
Serial.print(value < 256 ? "0" : "");
Serial.print(value < 16 ? "0" : "");
Serial.print(value, HEX);
}
Serial.println();
}
uint16_t testResult;
error = sgp40.executeSelfTest(testResult);
if (error)
{
Serial.print("Error trying to execute executeSelfTest(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else if (testResult != 0xD400)
{
Serial.print("executeSelfTest failed with error: ");
Serial.println(testResult);
}
/* Serial.begin(115200);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
Wire.begin(); // Begin function for I2C
rak1906.init(); //Start rak1901 */
// OTAA Device EUI MSB
uint8_t node_device_eui[8] = {0xAC, 0x1F, 0x09, 0xFF, 0xFE, 0x09, 0x61, 0x7B};
// OTAA Application EUI MSB
uint8_t node_app_eui[8] = {0xAC, 0x1F, 0x09, 0xFF, 0xF8, 0x68, 0x31, 0x72};
// OTAA Application Key MSB
uint8_t node_app_key[16] = {0xAC, 0x1F, 0x09, 0xFF, 0xFE, 0x09, 0x61, 0x7B, 0xAC, 0x1F, 0x09, 0xFF, 0xF8, 0x68, 0x31, 0x72};
// Set App EUI
if (!(ret = api.lorawan.appeui.set(node_app_eui, 8)))
{
Serial.printf("RUI3 - set device EUI failed! \r\n");
return;
}
// Set App Key
if (!(ret = api.lorawan.appkey.set(node_app_key, 16)))
{
Serial.printf("RUI3 - set application EUI failed! \r\n");
return;
}
// Set Device EUI
if (!(ret = api.lorawan.deui.set(node_device_eui, 8)))
{
Serial.printf("RUI3 - set application key failed! \r\n");
return;
}
// Set LoRaWAN Region
if (!(ret = api.lorawan.band.set(RAK_REGION_EU868)))
{
Serial.printf("RUI3 - set band failed! \r\n");
return;
}
// Set device class
if (!(ret = api.lorawan.deviceClass.set(RAK_LORA_CLASS_A)))
{
Serial.printf("RUI3 - set device class failed! \r\n");
return;
}
// Set network join mode
if (!(ret = api.lorawan.njm.set(RAK_LORA_OTAA)))
{
Serial.printf("RUI3 - set network join mode failed! \r\n");
return;
}
// Start to join the network
if(!(ret = api.lorawan.join()))
{
Serial.printf("RUI3 - join fail! \r\n");
return;
}
g_confirmed_mode = api.lorawan.cfm.get();
g_confirmed_retry = api.lorawan.rety.get();
g_data_rate = api.lorawan.dr.get();
}
bool has_joined = false;
void loop()
{
uint16_t error;
char errorMessage[256];
uint16_t srawVoc = 0;
float vocIndex = 0;
/*
* @brief Set the relative humidity and temperature in the current environment.
* Temperature and humidity calibration has been performed inside the sensor.
* RH/ticks=RH/%×65535/100
* T/ticks=(T/°C + 45)×65535/175
*/
uint16_t defaultRh = 0x8000; // 50 %RH
uint16_t defaultT = 0x6666; // 25 ℃
// delay(1000);
error = sgp40.measureRawSignal(defaultRh, defaultT, srawVoc);
if (error)
{
Serial.print("Error trying to execute measureRawSignal(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
Serial.print("SRAW_VOC:");
Serial.print(srawVoc);
vocIndex = (float)srawVoc/131.07 ;
/* VOC index.
* The voc index can directly indicate the quality of the air. The higher the value, the worse the air quality.
* 0-100,no need to ventilate,purify.
* 100-200,no need to ventilate,purify.
* 200-400,ventilate,purify.
* 400-500,ventilate,purify intensely.
*/
Serial.print(" VOC Index:");
Serial.println(vocIndex);
}
/* // put your main code here, to run repeatedly:
digitalWrite(LED_BLUE, HIGH);
delay(1000);
digitalWrite(LED_BLUE, LOW);
delay(1000);
/* if (rak1906.update()) {
Temperature = rak1906.temperature();
Humidity = rak1906.humidity();
} else {
Serial.println("Please plug in the sensor RAK1906 and Reboot");
}
Serial.printf("Temperature = %.2f%°C\r\n", Temperature);
Serial.printf("Humidity = %.2f%%\r\n", Humidity);
delay(5000); */
if (!tx_active && api.lorawan.njs.get())
{
event_ptr -= 1;
// Clear payload
g_solution_data.reset();
// g_solution_data.addPresence(LPP_CHANNEL_SWITCH, events_queue[event_ptr] == 0 ? 0 : 1);
g_solution_data.addAnalogInput(1, vocIndex);
// Add battery voltage
g_solution_data.addVoltage(LPP_CHANNEL_BATT, api.system.bat.get());
// Send the packet
Serial.printf("Send packet with size %d on port %d\n", g_solution_data.getSize(), set_fPort);
// Send the packet
if (api.lorawan.send(g_solution_data.getSize(), g_solution_data.getBuffer(), set_fPort)) // , g_confirmed_mode, g_confirmed_retry))
{
tx_active = true;
Serial.println("Packet enqueued");
}
else
{
Serial.println("Send failed");
}
}
else
{
Serial.println("Busy or not connected");
}
if (event_ptr != 0)
{
// Event queue is not empty. Trigger next packet in 5 seconds
api.system.timer.start(RAK_TIMER_3, 5000, NULL);
}
// api.system.scheduler.task.destroy();
}
Thanks a lot for help,
Vincèn