This example demonstrates how to use an Arduino as a gateway to a SLIP enabled device.
#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
#define LED_TXRX
#define SLIP_DEBUG
#define DEBUG_LED_PIN A3
#define UIP_BUFFER_SIZE MAX_PAYLOAD_SIZE
uint8_t slip_buf[UIP_BUFFER_SIZE];
void networkToSLIP();
void setup() {
Serial.begin(115200);
mesh.setNodeID(0);
mesh.begin();
slipdev_init(Serial);
pinMode(DEBUG_LED_PIN, OUTPUT);
#if defined(SLIP_DEBUG)
digitalWrite(DEBUG_LED_PIN, HIGH);
delay(200);
digitalWrite(DEBUG_LED_PIN, LOW);
#endif
}
uint32_t active_timer = 0;
void loop() {
if (millis() > 10000) {
mesh.DHCP();
}
while (network.available()) {
RF24NetworkHeader header;
network.read(header, 0, 0);
}
if (mesh.update() == EXTERNAL_DATA_TYPE) {
networkToSLIP();
}
uint16_t len;
if ((len = slipdev_poll()) > 0) {
if (len > MAX_PAYLOAD_SIZE) {
return;
}
RF24NetworkHeader header(01, EXTERNAL_DATA_TYPE);
uint8_t meshAddr;
uint8_t lastOctet = slip_buf[19];
if ((meshAddr = mesh.getAddress(lastOctet)) > 0) {
header.to_node = meshAddr;
#if defined(LED_TXRX)
digitalWrite(DEBUG_LED_PIN, HIGH);
#endif
network.write(header, &slip_buf, len);
#if defined(LED_TXRX)
digitalWrite(DEBUG_LED_PIN, LOW);
#endif
} else {
flashLED();
}
}
}
void networkToSLIP() {
RF24NetworkFrame* frame = network.frag_ptr;
size_t size = frame->message_size;
uint8_t* pointer = frame->message_buffer;
slipdev_send(pointer, size);
}
void flashLED() {
#if defined(SLIP_DEBUG)
digitalWrite(DEBUG_LED_PIN, HIGH);
delay(200);
digitalWrite(DEBUG_LED_PIN, LOW);
delay(200);
digitalWrite(DEBUG_LED_PIN, HIGH);
delay(200);
digitalWrite(DEBUG_LED_PIN, LOW);
delay(200);
digitalWrite(DEBUG_LED_PIN, HIGH);
delay(200);
digitalWrite(DEBUG_LED_PIN, LOW);
delay(200);
#endif
}