Last active
July 11, 2019 08:32
-
-
Save rbricheno/1e0bb1ecec1a9d44502ffd2da4166fb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CayenneLPP.h> | |
#include <Adafruit_BME280.h> | |
#include "RAK811.h" | |
#include "SoftwareSerial.h" | |
#define WORK_MODE LoRaWAN // LoRaWAN or LoRaP2P | |
#define JOIN_MODE OTAA // OTAA or ABP | |
#if JOIN_MODE == OTAA | |
String DevEui = "xxx"; // Fill this out | |
String AppEui = "xxx"; // Fill this out | |
String AppKey = "xxx"; // Fill This out | |
#else JOIN_MODE == ABP | |
String NwkSKey = ""; | |
String AppSKey = ""; | |
String DevAddr = ""; | |
#endif | |
#define TXpin 11 // Set the virtual serial port pins | |
#define RXpin 10 | |
#define DebugSerial Serial | |
Adafruit_BME280 bme; // I2C | |
CayenneLPP lpp(51); | |
SoftwareSerial RAKSerial(RXpin,TXpin); // Declare a virtual serial port | |
int RESET_PIN = 12; | |
bool InitLoRaWAN(void); | |
RAK811 RAKLoRa(RAKSerial,DebugSerial); | |
void setup() { | |
//Define Reset Pin | |
pinMode(RESET_PIN, OUTPUT); | |
//Setup Debug Serial on USB Port | |
DebugSerial.begin(9600); | |
while(DebugSerial.read()>= 0) {} | |
while(!DebugSerial); | |
//Print debug info | |
DebugSerial.println("StartUP"); | |
DebugSerial.println("Reset"); | |
//Reset the RAK Module | |
digitalWrite(RESET_PIN, LOW); // turn the pin low to Reset | |
digitalWrite(RESET_PIN, HIGH); // then high to enable | |
DebugSerial.println("Success"); | |
RAKSerial.begin(9600); // Arduino Shield | |
delay(100); | |
DebugSerial.println(RAKLoRa.rk_getVersion()); | |
delay(200); | |
DebugSerial.println(RAKLoRa.rk_getBand()); | |
delay(200); | |
while (!InitLoRaWAN()); | |
bme.begin(); | |
} | |
bool InitLoRaWAN(void) | |
{ | |
RAKLoRa.rk_setWorkingMode(WORK_MODE); | |
RAKLoRa.rk_recvData(); | |
RAKLoRa.rk_recvData(); | |
if ( RAKLoRa.rk_recvData() == "OK") | |
{ | |
if (RAKLoRa.rk_initOTAA(DevEui, AppEui, AppKey)) | |
{ | |
DebugSerial.println("You init OTAA parameter is OK!"); | |
while (RAKLoRa.rk_joinLoRaNetwork(JOIN_MODE)) | |
{ | |
bool flag = false; | |
for (unsigned long start = millis(); millis() - start < 90000L;) | |
{ | |
String ret = RAKLoRa.rk_recvData(); | |
if (ret.startsWith(STATUS_JOINED_SUCCESS)) | |
{ | |
DebugSerial.println("You join Network success!"); | |
return true; | |
} | |
else if (ret.startsWith(STATUS_RX2_TIMEOUT) || ret.startsWith(STATUS_JOINED_FAILED)) | |
{ | |
DebugSerial.println("You join Network Fail!"); | |
flag = true; | |
DebugSerial.println("The device will try to join again after 5s"); | |
delay(5000); | |
} | |
} | |
if (flag == false) | |
{ | |
DebugSerial.println("Pleases Reset the module!"); | |
delay(1000); | |
return false; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
void loop() { | |
Serial.print("Temperature = "); | |
Serial.print(bme.readTemperature()); | |
Serial.println(" *C"); | |
lpp.reset(); | |
lpp.addTemperature(1, bme.readTemperature()); | |
char buildBuffer[4] = {0}; | |
char compositionBuffer[lpp.getSize()*2+1] = {0}; // this will hold a string we build | |
for (int i = 0; i < lpp.getSize(); i++) { | |
sprintf( buildBuffer, "%02X", (uint8_t)lpp.getBuffer()[i]); | |
strcat( compositionBuffer, buildBuffer); | |
} | |
DebugSerial.print(compositionBuffer); | |
DebugSerial.println(" (HEX)"); | |
int packetsflag = 1; // 0: unconfirmed packets, 1: confirmed packets | |
if (RAKLoRa.rk_sendData(packetsflag, 1, compositionBuffer )) | |
{ | |
for (unsigned long start = millis(); millis() - start < 90000L;) | |
{ | |
String ret = RAKLoRa.rk_recvData(); | |
if (ret.startsWith(STATUS_TX_COMFIRMED) || ret.startsWith(STATUS_TX_UNCOMFIRMED)) | |
{ | |
DebugSerial.println("Send data ok!"); | |
delay(60000); | |
return; | |
} | |
} | |
DebugSerial.println("Send data error!"); | |
while (1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment