Code, sorry for this type of pasting, i donât realy know how to do it correctly, i just deleted LoRa keys and deveui, another thing is like in my code, and i found the way to this device works, i use luke ups module for 18650, and when there is no battery there the device wonât start at all, but when there is a battery it works correctly
#include âLoRaWan_APP.hâ
#include âArduino.hâ
#include <CayenneLPP.h>//the library is needed ��https://github.com/ElectronicCats/CayenneLPP��
#include <Wire.h>
#include âAdafruit_SHT31.hâ
#include <NDIRZ16.h>
#include <SoftwareSerial.h>
/*
set LoraWan_RGB to Active,the RGB active in loraWan
RGB red means sending;
RGB purple means joined done;
RGB blue means RxWindow1;
RGB yellow means RxWindow2;
RGB green means received done;
*/
/* OTAA para*/
/* ABP para*/
/LoraWan channelsmask, default channels 0-7/
/LoraWan region, select in arduino IDE tools/
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
/LoraWan Class, Class A and Class C are supported/
DeviceClass_t loraWanClass = LORAWAN_CLASS;
/the application data transmission duty cycle. value in [ms]./
uint32_t appTxDutyCycle = (10 * 60 * 1000 - 41 * 1000);
/OTAA or ABP/
bool overTheAirActivation = LORAWAN_NETMODE;
/ADR enable/
bool loraWanAdr = LORAWAN_ADR;
/* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
bool keepNet = LORAWAN_NET_RESERVE;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = LORAWAN_UPLINKMODE;
/* Application port /
uint8_t appPort = 2;
/!
Number of trials to transmit the frame, if the LoRaMAC layer did not
receive an acknowledgment. The MAC performs a datarate adaptation,
according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
to the following table:
Transmission nb |
Data Rate |
1 (first) |
DR |
2 |
DR |
3 |
max(DR-1,0) |
4 |
max(DR-1,0) |
5 |
max(DR-2,0) |
6 |
max(DR-2,0) |
7 |
max(DR-3,0) |
8 |
max(DR-3,0) |
Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
the datarate, in case the LoRaMAC layer did not receive an acknowledgment
*/
uint8_t confirmedNbTrials = 4;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
SoftwareSerial mySerial(GPIO2, GPIO1);
NDIRZ16 mySensor = NDIRZ16(&mySerial);
unsigned long i;
/* Prepares the payload of the frame */
static void prepareTxFrame( uint8_t port )
{
/*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in âcommissioning.hâ.
appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
if enabled AT, donât modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
for example, if use REGION_CN470,
the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in âRegionCN470.hâ.
*/
// Vext ON
digitalWrite(Vext, LOW);
mySerial.begin(9600);
delay(40000);
while (!sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println(âCouldnât find SHT31â);
delay(200);
}
float t = sht31.readTemperature();
float h = sht31.readHumidity();
Wire.end();
// Vext OFF
// digitalWrite(Vext, HIGH);
if (! isnan(t)) { // check if âis not a numberâ
Serial.print("Temp *C = "); Serial.println(t);
} else {
Serial.println(âFailed to read temperatureâ);
}
if (! isnan(h)) { // check if âis not a numberâ
Serial.print("Hum. % = "); Serial.println(h);
} else {
Serial.println(âFailed to read humidityâ);
}
Serial.println();
i=millis();
while (!mySensor.measure()) {
Serial.print("CO2 Concentration is ");
Serial.print(mySensor.ppm);
Serial.println();
// if(millis()> i +(1601000)){
// break;
// }
// ppm=mySensor.ppm;
}
float ppm = mySensor.ppm;
Serial.println(ppm);
i = 0;
digitalWrite(Vext, LOW);
float batteryVoltage = (getBatteryVoltage() * 0.001);
CayenneLPP lpp(LORAWAN_APP_DATA_MAX_SIZE);
lpp.addTemperature(1, t);
lpp.addRelativeHumidity(2, h);
lpp.addAnalogInput(3, ppm * 0.01);
lpp.addAnalogInput(4, batteryVoltage);
lpp.getBuffer(),
appDataSize = lpp.getSize();
memcpy(appData, lpp.getBuffer(), appDataSize);
}
void setup() {
boardInitMcu();
Serial.begin(115200);
#if(AT_SUPPORT)
enableAt();
#endif
deviceState = DEVICE_STATE_INIT;
LoRaWAN.ifskipjoin();
pinMode(Vext, OUTPUT);
}
void loop()
{
switch ( deviceState )
{
case DEVICE_STATE_INIT:
{
#if(AT_SUPPORT)
getDevParam();
#endif
printDevParam();
LoRaWAN.init(loraWanClass, loraWanRegion);
deviceState = DEVICE_STATE_JOIN;
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
{
prepareTxFrame( appPort );
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Schedule next packet transmission
txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep();
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}