Skip to content

Commit

Permalink
Change callback
Browse files Browse the repository at this point in the history
Change onMessage callback to loop
Add onSleep callback to loop
Remove isSleep
Fix examples
Add 'delay' onMessage callback to fix no receive data
  • Loading branch information
ricaun committed Apr 24, 2019
1 parent a09b32b commit dc33581
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 57 deletions.
29 changes: 18 additions & 11 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ LoRaNow.send();

#### Register callback

### onMessege

Register a callback function for when a valid payload is received.

```c
Expand All @@ -87,26 +89,31 @@ void onMessage(uint8_t *buffer, size_t size) {
* `onMessage` - function to call when a valid payload is received.
## State machine
### Loop
### onSleep
This function need to be on the loop to work properly
Register a callback function for when a the protocol is on sleep mode.
```c
LoRaNow.loop();
LoRaNow.onSleep(onSleep);
void onSleep() {
// ...
}
```

### Is sleep
* `onSleep` - function to call when a protocol is on sleep mode.

## State machine

Function used on the loop to detect when the protocol is on sleep mode.
### Loop

This function need to be on the loop to work properly
* This function uses `millis()`
* All callback is called by `LoRaNow.loop()`

```c
if (LoRaNow.isSleep()) {
...
}
LoRaNow.loop();
```
Returns true when the protocol is on the sleep mode.

## Radio parameters

Expand Down
26 changes: 20 additions & 6 deletions examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ void handleLoRaNow()
}
}

void setup(void) {
void setup(void)
{

Serial.begin(115200);

WiFi.mode(WIFI_STA);
if (ssid != "") WiFi.begin(ssid, password);
if (ssid != "")
WiFi.begin(ssid, password);
WiFi.begin();
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Expand All @@ -83,16 +86,19 @@ void setup(void) {
// LoRaNow.setSpreadingFactor(sf);
// LoRaNow.setPins(ss, dio0);

if (!LoRaNow.begin()) {
if (!LoRaNow.begin())
{
Serial.println("LoRa init failed. Check your connections.");
while (true);
while (true)
;
}

LoRaNow.onMessage(onMessage);
LoRaNow.gateway();
}

void loop(void) {
void loop(void)
{
LoRaNow.loop();
server.handleClient();
}
Expand All @@ -111,6 +117,14 @@ void onMessage(uint8_t *buffer, size_t size)
Serial.println();
Serial.println();

if (string.available() > 512)
{
while (string.available())
{
string.read();
}
}

string.print("Node Id: ");
string.println(id, HEX);
string.print("Count: ");
Expand Down
26 changes: 20 additions & 6 deletions examples/LoRaNow_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ void handleLoRaNow()
}
}

void setup(void) {
void setup(void)
{

Serial.begin(115200);

WiFi.mode(WIFI_STA);
if (ssid != "") WiFi.begin(ssid, password);
if (ssid != "")
WiFi.begin(ssid, password);
WiFi.begin();
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Expand All @@ -83,16 +86,19 @@ void setup(void) {
// LoRaNow.setSpreadingFactor(sf);
// LoRaNow.setPins(ss, dio0);

if (!LoRaNow.begin()) {
if (!LoRaNow.begin())
{
Serial.println("LoRa init failed. Check your connections.");
while (true);
while (true)
;
}

LoRaNow.onMessage(onMessage);
LoRaNow.gateway();
}

void loop(void) {
void loop(void)
{
LoRaNow.loop();
server.handleClient();
}
Expand All @@ -111,6 +117,14 @@ void onMessage(uint8_t *buffer, size_t size)
Serial.println();
Serial.println();

if (string.available() > 512)
{
while (string.available())
{
string.read();
}
}

string.print("Node Id: ");
string.println(id, HEX);
string.print("Count: ");
Expand Down
21 changes: 12 additions & 9 deletions examples/LoRaNow_Node/LoRaNow_Node.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,12 @@ void setup() {
}

LoRaNow.onMessage(onMessage);
LoRaNow.onSleep(onSleep);
LoRaNow.showStatus(Serial);
}

void loop() {
LoRaNow.loop();
if (LoRaNow.isSleep())
{
Serial.println("Send Message");

LoRaNow.print("LoRaNow Node Message ");
LoRaNow.print(millis());
LoRaNow.send();
}
}

void onMessage(uint8_t *buffer, size_t size)
Expand All @@ -49,4 +42,14 @@ void onMessage(uint8_t *buffer, size_t size)
Serial.write(buffer, size);
Serial.println();
Serial.println();
}
}

void onSleep()
{
Serial.println("Sleep");
delay(5000); // "kind of a sleep"
Serial.println("Send Message");
LoRaNow.print("LoRaNow Node Message ");
LoRaNow.print(millis());
LoRaNow.send();
}
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ LoRaNowLayer KEYWORD1
setId KEYWORD2
id KEYWORD2
gateway KEYWORD2
count KEYWORD2

begin KEYWORD2
end KEYWORD2
Expand All @@ -26,6 +27,9 @@ delay KEYWORD2

showStatus KEYWORD2

onMessage KEYWORD2
onSleep KEYWORD2

beginPacket KEYWORD2
endPacket KEYWORD2

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=LoRaNow
version=1.0.2
version=1.0.3
author=Luiz Henrique Cassettari
maintainer=Luiz Henrique Cassettari <[email protected]>
sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol.
paragraph=LoRaNow is a open source communication protocol to make easier to understand the concept of Node / Gateway communication using LoRa technology. Support: RFM95 + Arduino / ESP.
category=Communication
url=https://github.com/ricaun/LoRaNow
architectures=*
architectures=avr, esp8266, esp32
includes=LoRaNow.h
61 changes: 46 additions & 15 deletions src/LoRaNow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// ---------------------------------------------------- //
// LoRaNow.cpp
// ---------------------------------------------------- //
// 23/04/2019 - Add onSleep callback
// 22/04/2019 - Add LORA_STATE_RECEIVE on loop - fix interrupt reset on esp32/esp8266
// 20/04/2019 - Fix esp32 id
// 03/04/2019 - First version release
// 02/04/2019 - Add boards pinout
Expand Down Expand Up @@ -60,8 +62,7 @@ byte LoRaNowClass::begin()
LORANOW_DEBUG_PRINTLN("[ln] Begin");
LoRa.onReceive(LoRaNowClass::onReceive);
LoRa.onTxDone(LoRaNowClass::onTxDone);
LoRa.sleep();
state = LORA_STATE_SLEEP;
sleep();
return 1;
}
return 0;
Expand All @@ -77,19 +78,28 @@ byte LoRaNowClass::loop()
state_change(state);
}
}

if (isSleep())
{
if (LoRaNow.sleepCallback)
{
LoRaNow.sleepCallback();
}
else
{
LoRaNow.receive();
}
}

return 1;
}

void LoRaNowClass::state_change(byte _state, unsigned long _wait)
{
static unsigned long tempo = 0;

wait = _wait;
state = _state;
if (wait == 0)
{
if (state == LORA_STATE_TX_DONE)
tempo = time;
state_do(state);
}
}
Expand Down Expand Up @@ -131,6 +141,15 @@ void LoRaNowClass::state_do(byte _state)
case LORA_STATE_SLEEP:
sleep();
break;
case LORA_STATE_RECEIVE:
if (LoRaNow.messageCallback)
{
LoRaNow.messageCallback((uint8_t *)LoRaNow.buffer(), LoRaNow.available());
}
LoRaNow.clear();
if (state != LORA_STATE_TX_WAIT)
state_change(LORA_STATE_SLEEP);
break;
}
}

Expand Down Expand Up @@ -234,7 +253,7 @@ void LoRaNowClass::gateway(bool gateway)
{
_gateway = gateway;
rxwindow = 0;
state_change(LORA_STATE_RX1);
//state_change(LORA_STATE_RX1);
}

void LoRaNowClass::setId(uint32_t _id)
Expand All @@ -258,8 +277,8 @@ uint32_t LoRaNowClass::makeId()
#elif defined(ARDUINO_ARCH_ESP8266)
return ESP.getChipId();
#elif defined(ARDUINO_ARCH_ESP32)
uint32_t _id = (uint32_t) ((uint64_t)ESP.getEfuseMac()>>16);
return ((((_id) & 0xff000000) >> 24) | (((_id) & 0x00ff0000) >> 8) | (((_id) & 0x0000ff00) << 8) | (((_id) & 0x000000ff) << 24)); // swap bits
uint32_t _id = (uint32_t)((uint64_t)ESP.getEfuseMac() >> 16);
return ((((_id)&0xff000000) >> 24) | (((_id)&0x00ff0000) >> 8) | (((_id)&0x0000ff00) << 8) | (((_id)&0x000000ff) << 24)); // swap bits
#endif
return 0;
}
Expand Down Expand Up @@ -519,35 +538,47 @@ void LoRaNowClass::rxMode()
LoRa.receive();
}

void LoRaNowClass::receive()
{
state_change(LORA_STATE_RX1);
}

// ---------------------------------------------------- //
// LoRa
// callback
// ---------------------------------------------------- //

void LoRaNowClass::onMessage(void (*cb)(uint8_t *payload, size_t size))
{
messageCallback = cb;
}

void LoRaNowClass::onSleep(void (*cb)())
{
sleepCallback = cb;
}

// ---------------------------------------------------- //
// LoRa
// ---------------------------------------------------- //

void LoRaNowClass::onReceive(int packetSize)
{
LORANOW_DEBUG_PRINTLN("[ln] Receive");
LoRaNow.time = millis();
LoRaNow.beginDecode();
while (LoRa.available())
{
LoRaNow.write(LoRa.read());
}
if (LoRaNow.endDecode())
{
if (LoRaNow.messageCallback)
{
LoRaNow.messageCallback((uint8_t *)LoRaNow.buffer(), LoRaNow.available());
}
LoRaNow.state_change(LORA_STATE_RECEIVE, LORANOW_WAIT_RECEIVE);
}
else
{
LoRa.receive();
LoRaNow.clear();
}
LoRaNow.clear();
}

void LoRaNowClass::onTxDone()
Expand Down
Loading

0 comments on commit dc33581

Please sign in to comment.