My code is as below.
I wrote the code for sending data every five minutes, but the COMPLETE code didn’t come up at all
Is there a problem with my code?
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Arduino.h> // required before wiring_private.h
#include “wiring_private.h” // pinPeripheral() function
#include <PMS.h>
#define DEVICE_1_CHANNEL_FREQUENCY 922100000 // Frequency for Device 1 (in Hz)
#define DEVICE_1_DR_RANGE_MAP DR_RANGE_MAP(DR_SF12, DR_SF7) // Data rate range for Device 1
#define ContinueMode 0
#define PollingMode 1
String value_convert(String value);
static const u1_t PROGMEM APPEUI[8] = { 0x25, 0xDE, 0xB2, 0x9A, 0xFF, 0xD9, 0xE0, 0x9E };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
// This should also be in little endian format
static const u1_t PROGMEM DEVEUI[8] = { 0x52, 0x00, 0x06, 0xD0, 0x7E, 0xD5, 0xB3, 0x70 };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }
// This key should be in big endian format
static const u1_t PROGMEM APPKEY[16] = { 0x92, 0x92, 0x4E, 0x98, 0xB0, 0x7B, 0xC6, 0x45, 0x7F, 0x71, 0x97, 0xBE, 0x76, 0xC6, 0x0B, 0x6F };
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
static uint8_t mydata[50] = “\0”;
static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 300;
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 8,
.rxtx = LMIC_UNUSED_PIN,
.rst = LMIC_UNUSED_PIN,
.dio = {3, 5, LMIC_UNUSED_PIN},
};
Uart Serial2(&sercom5, A5, 6, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM5_Handler()
{
Serial2.IrqHandler();
}
//no2
Uart Serial3(&sercom1, 12, 10, SERCOM_RX_PAD_3, UART_TX_PAD_2);
void SERCOM1_Handler()
{
Serial3.IrqHandler();
}
//so2
Uart Serial4(&sercom0, A4, A3, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM0_Handler()
{
Serial4.IrqHandler();
}
PMS pms(Serial1);
PMS::DATA data;
char buffer[30];
char CO_data[50];
char NO2_data[50];
char SO2_data[50];
int dust = 0;
void readData(){
pms.requestRead();
if(pms.readUntil(data)){
dust = data.PM_AE_UG_2_5;
}
#if PollingMode
Serial2.write(‘\r’);
Serial3.write(‘\r’);
Serial4.write(‘\r’);
delay(1000);
#else
delay(100);
#endif
int i = 0;
while (Serial2.available()){
CO_data[i] = Serial2.read();
i++;
}
i = 0;
while (Serial3.available()){
NO2_data[i] = Serial3.read();
i++;
}
i = 0;
while (Serial4.available()){
SO2_data[i] = Serial4.read();
i++;
}
delay(100);
String str_co_data = “”;
String str_no2_data = “”;
String str_so2_data = “”;
for(int j = 12; j < 18; j++){
str_co_data += CO_data[j];
str_no2_data += NO2_data[j];
str_so2_data += SO2_data[j];
}
String dustData, coPPB, no2PPB, so2PPB;
dustData = String(dust);
coPPB = value_convert(str_co_data);
no2PPB = value_convert(str_no2_data);
so2PPB = value_convert(str_so2_data);
snprintf(mydata, sizeof(mydata), “%s,%s,%s,%s,%s”, dustData.c_str(), coPPB.c_str(), no2PPB.c_str(), so2PPB.c_str());
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F(“EV_SCAN_TIMEOUT”));
break;
case EV_BEACON_FOUND:
Serial.println(F(“EV_BEACON_FOUND”));
break;
case EV_BEACON_MISSED:
Serial.println(F(“EV_BEACON_MISSED”));
break;
case EV_BEACON_TRACKED:
Serial.println(F(“EV_BEACON_TRACKED”));
break;
case EV_JOINING:
Serial.println(F(“EV_JOINING”));
break;
case EV_JOINED:
Serial.println(F(“EV_JOINED”));
// Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time).
LMIC_setLinkCheckMode(0);
break;
case EV_RFU1:
Serial.println(F(“EV_RFU1”));
break;
case EV_JOIN_FAILED:
Serial.println(F(“EV_JOIN_FAILED”));
break;
case EV_REJOIN_FAILED:
Serial.println(F(“EV_REJOIN_FAILED”));
break;
break;
case EV_TXCOMPLETE:
Serial.println(F(“EV_TXCOMPLETE (includes waiting for RX windows)”));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F(“Received ack”));
if (LMIC.dataLen) {
Serial.println(F(“Received “));
Serial.println(LMIC.dataLen);
Serial.println(F(” bytes of payload”));
}
// Schedule next transmission
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F(“EV_LOST_TSYNC”));
break;
case EV_RESET:
Serial.println(F(“EV_RESET”));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F(“EV_RXCOMPLETE”));
break;
case EV_LINK_DEAD:
Serial.println(F(“EV_LINK_DEAD”));
break;
case EV_LINK_ALIVE:
Serial.println(F(“EV_LINK_ALIVE”));
break;
default:
Serial.println(F(“Unknown event”));
break;
}
}
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F(“OP_TXRXPEND, not sending”));
} else {
// Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
Serial.println(F(“Packet queued”));
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
Serial4.begin(9600);
//co
pinPeripheral(6, PIO_SERCOM);
pinPeripheral(A5, PIO_SERCOM_ALT);
//no2
pinPeripheral(10, PIO_SERCOM);
pinPeripheral(12, PIO_SERCOM);
//so2
pinPeripheral(17ul, PIO_SERCOM_ALT);
pinPeripheral(18ul, PIO_SERCOM_ALT);
//pms
pinPeripheral(1, PIO_SERCOM_ALT);
pinPeripheral(0, PIO_SERCOM_ALT);
pms.passiveMode();
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
LMIC_setDrTxpow(DR_SF7, 14);
LMIC_setupChannel(0, DEVICE_1_CHANNEL_FREQUENCY, DEVICE_1_DR_RANGE_MAP, BAND_CENTI);
readData();
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
void loop() {
os_runloop_once();
}
String value_convert(String value){
int start = value.indexOf(‘,’) + 1; // 시작 인덱스
int end = value.indexOf(‘,’, start); // 끝 인덱스
// 쉼표와 쉼표 사이의 값
String con_value = value.substring(start, end);
//int convalue = con_value.toInt();
return con_value;
}