Skip to content

Commit

Permalink
Merge pull request #10 from soracom/support-downlink-callback
Browse files Browse the repository at this point in the history
Support downlink callback
  • Loading branch information
j3tm0t0 authored Aug 23, 2017
2 parents f73e790 + 38be33f commit ab57319
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
40 changes: 40 additions & 0 deletions examples/Downlink/Downlink.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <lorawan_client.h>
#define LED_PIN 13

LoRaWANClient client;

bool ledOn = false;

void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial.print("Connecting ... ");
if(! client.connect())
{
Serial.println(" failed to connect. Halt...");
for(;;){};
}
}

void loop() {
char *data="Hello! LoRa";

Serial.print("Sending ... ");
Serial.println(data);
client.sendData(data, 1, cb);

if (ledOn) {
digitalWrite(LED_PIN, HIGH);
}
delay(10000);

digitalWrite(LED_PIN, LOW);
}

void cb(String s) {
Serial.print("Received: '");
Serial.print(s);
Serial.println("'");
ledOn = true;
}

55 changes: 41 additions & 14 deletions lorawan_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool LoRaWANClient::connect(bool force_reconnect){
if(!force_reconnect)
{
Serial.println("Checking if already joined or not ... ");
if (!sendCmd("lorawan get_join_status", "unjoined", true, waitTime)) {
if (!sendCmd("lorawan get_join_status", "unjoined", NULL, true, waitTime)) {
Serial.println("already joined.");
return true;
}
Expand All @@ -32,19 +32,19 @@ bool LoRaWANClient::connect(bool force_reconnect){
//
// LoRa module status clear
//
if (!sendCmd("mod factory_reset", "Ok", true, waitTime)) {
if (!sendCmd("mod factory_reset", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod set_echo off", "Ok", true, waitTime)) {
if (!sendCmd("mod set_echo off", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod save", "Ok", true, waitTime)) {
if (!sendCmd("mod save", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod reset", "", true, waitTime)) {
if (!sendCmd("mod reset", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
Expand All @@ -53,23 +53,23 @@ bool LoRaWANClient::connect(bool force_reconnect){
// LoRa module various value get
//

if (!sendCmd("mod get_hw_model", "", true, waitTime)) {
if (!sendCmd("mod get_hw_model", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod get_ver", "", true, waitTime)) {
if (!sendCmd("mod get_ver", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("lorawan get_deveui", "", true, waitTime)) {
if (!sendCmd("lorawan get_deveui", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}

// LoRa module join to Network Server by OTAA
//
int retry=0;
while (!sendCmd("lorawan join otaa", "accepted", true, JOIN_RETRY_INTERVAL)) {
while (!sendCmd("lorawan join otaa", "accepted", NULL, true, JOIN_RETRY_INTERVAL)) {
retry++;
Serial.print("'lorawan join otaa' Failed (");
Serial.print(retry);
Expand All @@ -85,7 +85,7 @@ bool LoRaWANClient::connect(bool force_reconnect){
return true;
}

bool LoRaWANClient::sendCmd(String cmd, String waitStr, bool echo, int waitTime){
bool LoRaWANClient::sendCmd(String cmd, String waitStr, CALLBACK p, bool echo, int waitTime){
unsigned long tim;
String str;

Expand All @@ -101,7 +101,10 @@ bool LoRaWANClient::sendCmd(String cmd, String waitStr, bool echo, int waitTime)
char ch = ss.read();
ECHO(ch);
str += String(ch);
if (str.indexOf("\n> ") >= 0) break;
checkRx(&str, p);
if (commandCompleted(cmd, str)) {
break;
}
}
}
if (waitStr == NULL) return true;
Expand All @@ -110,6 +113,30 @@ bool LoRaWANClient::sendCmd(String cmd, String waitStr, bool echo, int waitTime)
return false;
}

void LoRaWANClient::checkRx(String* rsp, CALLBACK p) {
if (!rsp || !p) {
return;
}

int rxIdx = rsp->indexOf("> rx");
if (rxIdx >= 0) {
String str = rsp->substring(rxIdx + 5 /* length of "> rx " */);
int crIdx = str.indexOf("\n");
if (crIdx >= 0) {
p(str.substring(str.indexOf(" ") + 1, crIdx));
rsp->remove(0, rxIdx);
rsp->remove(0, crIdx);
}
}
}

bool LoRaWANClient::commandCompleted(String cmd, String rsp) {
if (cmd.indexOf("lorawan tx") < 0) {
return rsp.indexOf("\n> ") >= 0;
}
return (rsp.indexOf("> tx_ok") >= 0 || rsp.indexOf("> err") >= 0);
}

bool LoRaWANClient::sendData(char *data, short port, CALLBACK p, bool echo){
int i,j;
char cmdLine[32];
Expand All @@ -135,7 +162,7 @@ bool LoRaWANClient::sendData(char *data, short port, CALLBACK p, bool echo){
sprintf(cmdLine, "lorawan tx ucnf %d %s", port, payload);
ECHOLN(cmdLine);

if(sendCmd(cmdLine, "tx_ok", true, NETWORK_WAIT_TIME))
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
Expand All @@ -160,7 +187,7 @@ bool LoRaWANClient::sendData(unsigned long data, short port, CALLBACK p, bool ec
sprintf(cmdLine, "lorawan tx ucnf %d %08lx", port, data);
// ECHOLN(cmdLine);

if(sendCmd(cmdLine, "tx_ok", true, NETWORK_WAIT_TIME))
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
Expand Down Expand Up @@ -192,7 +219,7 @@ bool LoRaWANClient::sendBinary(byte *data_pointer, int data_size, short port, CA
strcat(cmdLine, tmp);
}
ECHOLN(cmdLine);
if(sendCmd(cmdLine, "tx_ok", true, NETWORK_WAIT_TIME))
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
Expand Down
8 changes: 6 additions & 2 deletions lorawan_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define JOIN_RETRY_MAX 0 // 0 = unlimited
#define MAX_PAYLOAD_SIZE 11

typedef void (* CALLBACK)(char *, int);
typedef void (* CALLBACK)(String);

class LoRaWANClient {
private:
Expand All @@ -21,10 +21,14 @@ class LoRaWANClient {
public:
LoRaWANClient();
bool connect(bool force_reconnect=true);
bool sendCmd(String cmd, String waitStr, bool echo=true, int waitTime=SERIAL_WAIT_TIME);
bool sendCmd(String cmd, String waitStr, CALLBACK p=NULL, bool echo=true, int waitTime=SERIAL_WAIT_TIME);
bool sendBinary(byte *data_pointer, int data_size, short port=1, CALLBACK p=NULL, bool echo=true);
bool sendData(char *msg, short port=1, CALLBACK p=NULL, bool echo=true);
bool sendData(unsigned long, short port=1, CALLBACK p=NULL, bool echo=true);

private:
void checkRx(String* rsp, CALLBACK p);
bool commandCompleted(String cmd, String rsp);
};

#endif

0 comments on commit ab57319

Please sign in to comment.