Skip to content

Instantly share code, notes, and snippets.

@plopp
Last active February 20, 2017 18:53
Show Gist options
  • Save plopp/f35194c343076f55ab65a27f3b199aa5 to your computer and use it in GitHub Desktop.
Save plopp/f35194c343076f55ab65a27f3b199aa5 to your computer and use it in GitHub Desktop.
#include <Sodaq_RN2483.h>
#include <Sodaq_wdt.h>
#include <StringLiterals.h>
#include <Switchable_Device.h>
#include <Utils.h>
/* Docs
Commands reference RN2483 - http://ww1.microchip.com/downloads/en/DeviceDoc/40001784B.pdf
RN2483 in general - http://www.microchip.com/wwwproducts/en/RN2483
*/
/* Tillgänliga defs
LED_RED
LED_GREEN
LED_BLUE
LED_BUILTIN (D13)
BLUETOOTH_WAKE
BT_RESET
BUTTON
LORA_RESET
TEMP_SENSOR
*/
#define debugSerial SerialUSB
#define loraSerial Serial2
#define btSerial Serial1
#define uartSerial Serial
//Använd egen devEUI. Avläses genom att skicka "sys get hweui\r\t"
const uint8_t devEUI[8] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Använd appKey från TalkPool-portalen
const uint8_t appSKey[16] =
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
// Använd appEUI från TalkPool-portalen
const uint8_t appEUI[8] =
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
uint8_t testPayload[] =
{
0x30, 0x31, 0xFF, 0xDE, 0xAD
};
void setup() {
// put your setup code here, to run once:
debugSerial.begin(57600); //Host
uartSerial.begin(57600); //D1,D0
btSerial.begin(57600); //Bluetooth module
loraSerial.begin(57600); //RN2483
while ((!debugSerial) && (millis() < 10000)) {
// Wait for SerialUSB or start after 30 seconds
}
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
debugSerial.println("Startad.");
//pin 4 låg här - återställer lora-modulen
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(200);
digitalWrite(4, LOW);
delay(200);
digitalWrite(4, HIGH);
delay(1000);
LoRaBee.setDiag(debugSerial); // optional - verkar inte fungera
while(1) {
if (LoRaBee.initOTA(loraSerial, devEUI, appEUI, appSKey, true))
{
debugSerial.println("Connection to the network was successful.");
break;
}
else
{
debugSerial.println("Connection to the network failed!");
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
delay(5000);
}
}
void loop() {
debugSerial.println("Sleeping for 5 seconds before starting sending out test packets.");
for (uint8_t i = 5; i > 0; i--)
{
debugSerial.println(i);
delay(1000);
}
// send 10 packets, with at least a 5 seconds delay after each transmission (more seconds if the device is busy)
uint8_t i = 10;
while (i > 0)
{
testPayload[0] = i; // update first byte
switch (LoRaBee.sendReqAck(1, testPayload, 5, 3))
{
case NoError:
debugSerial.println("Successful transmission.");
i--;
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
break;
case NoResponse:
debugSerial.println("There was no response from the device.");
break;
case Timeout:
debugSerial.println("Connection timed-out. Check your serial connection to the device! Sleeping for 20sec.");
delay(20000);
break;
case PayloadSizeError:
debugSerial.println("The size of the payload is greater than allowed. Transmission failed!");
break;
case InternalError:
debugSerial.println("Oh No! This shouldn't happen. Something is really wrong! Try restarting the device!\r\nThe program will now halt.");
while (1) {};
break;
case Busy:
debugSerial.println("The device is busy. Sleeping for 10 extra seconds.");
delay(10000);
break;
case NetworkFatalError:
debugSerial.println("There is a non-recoverable error with the network connection. You should re-connect.\r\nThe program will now halt.");
while (1) {};
break;
case NotConnected:
debugSerial.println("The device is not connected to the network. Please connect to the network before attempting to send data.\r\nThe program will now halt.");
while (1) {};
break;
case NoAcknowledgment:
debugSerial.println("There was no acknowledgment sent back!");
break;
default:
break;
}
delay(5000);
}
while (1) { } // block forever
//Passthrough - Avkommentera och kör enbart detta för att kommunicera direkt med LORA-modulen via debug-serial.
/*while (debugSerial.available())
{
loraSerial.write((char)debugSerial.read());
}
while (loraSerial.available())
{
debugSerial.write((char)loraSerial.read());
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment