举例一个lorawan在arduino中数据发送有看门狗的代码
时间: 2024-05-30 08:12:24 浏览: 160
NanoWatchdog:基于Arduino Nano的PC看门狗
以下是一个LoraWAN在Arduino中数据发送有看门狗的示例代码:
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>
#define BUILTIN_LED 13
#define SDA_PIN 4
#define SCL_PIN 5
#define SERIAL_BAUDRATE 9600
#define NODE_ADDRESS "0011223344556677"
#define APP_EUI "0123456789ABCDEF"
#define APP_KEY "0123456789ABCDEF0123456789ABCDEF"
#define TX_INTERVAL 60000
#define TX_TIMEOUT 5000
#define WATCHDOG_TIMEOUT 30000
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 10,
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 6, 7},
};
// LoRaWAN keys
static u1_t DEVEUI[8] = { 0 };
static u1_t APPEUI[8] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};
static u1_t APPKEY[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};
// LoRaWAN data
static uint8_t message[] = { 0x01, 0x02, 0x03 };
// Watchdog timer
static uint32_t last_tx_time = 0;
static uint32_t last_ping_time = 0;
void setup() {
// Initialize serial port
Serial.begin(SERIAL_BAUDRATE);
// Initialize I2C bus
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize LoRaWAN stack
os_init();
LMIC_reset();
LMIC_setSession(DEVEUI, APPEUI, APPKEY);
LMIC_setLinkCheckMode(0);
LMIC_setDrTxpow(DR_SF7, 14);
// Start watchdog timer
last_tx_time = millis();
last_ping_time = millis();
}
void loop() {
// Update watchdog timer
uint32_t now = millis();
if (now - last_tx_time > TX_INTERVAL) {
// Send data
os_setTimedCallback(&sendjob, now + random(TX_TIMEOUT), do_send);
last_tx_time = now;
}
if (now - last_ping_time > WATCHDOG_TIMEOUT) {
// Stop and reset the device
Serial.println("Watchdog timeout, resetting!");
while (1) { }
}
// Handle LoRaWAN events
os_runloop_once();
}
void do_send(osjob_t* j) {
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println("OP_TXRXPEND, not sending");
} else {
// Prepare payload
LMIC.frame[0] = 0x01;
LMIC.frame[1] = 0x02;
LMIC.frame[2] = 0x03;
LMIC_setTxData2(1, LMIC.frame, 3, 0);
Serial.println("Packet queued");
}
}
阅读全文