diff --git a/API.md b/API.md index 10fec5b..49cb3f8 100644 --- a/API.md +++ b/API.md @@ -27,6 +27,21 @@ LoRaNow.setPins(ss, dio0); This call is optional and only needs to be used if you need to change the default pins used. +### Set pins SPI + +Override the SPI pins and the default `SS`, and `DIO0` pins used by the library. **Must** be called before `LoRaNow.begin()`. + +```c +LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); +``` + * `sck` - new sck pin to use for SPI communication + * `miso` - new miso pin to use for SPI communication + * `mosi` - new mosi pin to use for SPI communication + * `ss` - new slave select pin to use, defaults to `10` or `gpio16` + * `dio0` - new DIO0 pin to use, defaults to `2` or `gpio15`. **Must** be interrupt capable via [attachInterrupt(...)](https://www.arduino.cc/en/Reference/AttachInterrupt). + +This call is optional and only works on ESP32 platform. + ### End Stop the library diff --git a/examples/LoRaNow_Gateway/LoRaNow_Gateway.ino b/examples/LoRaNow_Gateway/LoRaNow_Gateway.ino index 9fd13c3..981fcef 100644 --- a/examples/LoRaNow_Gateway/LoRaNow_Gateway.ino +++ b/examples/LoRaNow_Gateway/LoRaNow_Gateway.ino @@ -22,6 +22,8 @@ void setup() { // LoRaNow.setSpreadingFactor(sf); // LoRaNow.setPins(ss, dio0); + // LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32 + if (!LoRaNow.begin()) { Serial.println("LoRa init failed. Check your connections."); while (true); diff --git a/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino b/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino index 375380c..8b81277 100644 --- a/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino +++ b/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino @@ -86,6 +86,8 @@ void setup(void) // LoRaNow.setSpreadingFactor(sf); // LoRaNow.setPins(ss, dio0); + // LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32 + if (!LoRaNow.begin()) { Serial.println("LoRa init failed. Check your connections."); diff --git a/examples/LoRaNow_Gateway_ESP32_setPins/LoRaNow_Gateway_ESP32_setPins.ino b/examples/LoRaNow_Gateway_ESP32_setPins/LoRaNow_Gateway_ESP32_setPins.ino new file mode 100644 index 0000000..bff7c5e --- /dev/null +++ b/examples/LoRaNow_Gateway_ESP32_setPins/LoRaNow_Gateway_ESP32_setPins.ino @@ -0,0 +1,150 @@ +/* + LoRaNow Simple Gateway with ESP32 setPins + + This code creates a webServer to show the LoRa messages. + + created 27 04 2019 + by Luiz H. Cassettari +*/ + +#include +#include +#include +#include + +#define SCK 5 +#define MISO 19 +#define MOSI 27 +#define SS 18 +#define DIO0 26 + +const char *ssid = ""; +const char *password = ""; + +WebServer server(80); + +const char *script = ""; + +void handleRoot() +{ + String str = ""; + str += ""; + str += ""; + str += "ESP32 - LoRaNow"; + str += ""; + str += script; + str += ""; + str += ""; + str += "
"; + str += ""; + str += "
"; + str += ""; + str += ""; + server.send(200, "text/html", str); +} + +static StreamString string; + +void handleLoRaNow() +{ + server.send(200, "text/plain", string); + while (string.available()) // clear + { + string.read(); + } +} + +void setup(void) +{ + + Serial.begin(115200); + + WiFi.mode(WIFI_STA); + if (ssid != "") + WiFi.begin(ssid, password); + WiFi.begin(); + Serial.println(""); + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + server.on("/", handleRoot); + server.on("/loranow", handleLoRaNow); + server.begin(); + Serial.println("HTTP server started"); + + // LoRaNow.setFrequencyCN(); // Select the frequency 486.5 MHz - Used in China + // LoRaNow.setFrequencyEU(); // Select the frequency 868.3 MHz - Used in Europe + // LoRaNow.setFrequencyUS(); // Select the frequency 904.1 MHz - Used in USA, Canada and South America + // LoRaNow.setFrequencyAU(); // Select the frequency 917.0 MHz - Used in Australia, Brazil and Chile + + // LoRaNow.setFrequency(frequency); + // LoRaNow.setSpreadingFactor(sf); + // LoRaNow.setPins(ss, dio0); + + LoRaNow.setPinsSPI(SCK, MISO, MOSI, SS, DIO0); // Only works with ESP32 + + if (!LoRaNow.begin()) + { + Serial.println("LoRa init failed. Check your connections."); + while (true) + ; + } + + LoRaNow.onMessage(onMessage); + LoRaNow.gateway(); +} + +void loop(void) +{ + LoRaNow.loop(); + server.handleClient(); +} + +void onMessage(uint8_t *buffer, size_t size) +{ + unsigned long id = LoRaNow.id(); + byte count = LoRaNow.count(); + + Serial.print("Node Id: "); + Serial.println(id, HEX); + Serial.print("Count: "); + Serial.println(count); + Serial.print("Message: "); + Serial.write(buffer, 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: "); + string.println(count); + string.print("Message: "); + string.write(buffer, size); + string.println(); + string.println(); + + // Send data to the node + LoRaNow.clear(); + LoRaNow.print("LoRaNow Gateway Message "); + LoRaNow.print(millis()); + LoRaNow.send(); +} \ No newline at end of file diff --git a/examples/LoRaNow_Node/LoRaNow_Node.ino b/examples/LoRaNow_Node/LoRaNow_Node.ino index 85d8b32..6e75c76 100644 --- a/examples/LoRaNow_Node/LoRaNow_Node.ino +++ b/examples/LoRaNow_Node/LoRaNow_Node.ino @@ -22,6 +22,8 @@ void setup() { // LoRaNow.setSpreadingFactor(sf); // LoRaNow.setPins(ss, dio0); + // LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32 + if (!LoRaNow.begin()) { Serial.println("LoRa init failed. Check your connections."); while (true); diff --git a/keywords.txt b/keywords.txt index 155ede9..6562b2e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -24,6 +24,7 @@ sleep KEYWORD2 isSleep KEYWORD2 isReady KEYWORD2 delay KEYWORD2 +setPinsSPI KEYWORD2 showStatus KEYWORD2 diff --git a/library.properties b/library.properties index 2f535c0..1309f89 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=LoRaNow -version=1.0.3 +version=1.0.4 author=Luiz Henrique Cassettari maintainer=Luiz Henrique Cassettari sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol. diff --git a/src/LoRaNow.cpp b/src/LoRaNow.cpp index 139134d..801bd0b 100644 --- a/src/LoRaNow.cpp +++ b/src/LoRaNow.cpp @@ -1,6 +1,8 @@ // ---------------------------------------------------- // // LoRaNow.cpp // ---------------------------------------------------- // +// 27/04/2019 - Add setPinsSPI to help esp32 boards +// 24/04/2019 - Fix LoRaNow board mosfet // 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 @@ -46,7 +48,7 @@ byte LoRaNowClass::begin() #if defined(LORANOW_MOSFET_P) pinMode(LORANOW_MOSFET_P, OUTPUT); digitalWrite(LORANOW_MOSFET_P, LOW); - delay(1); + delay(5); #endif if (LoRa.begin(frequency)) @@ -65,6 +67,10 @@ byte LoRaNowClass::begin() sleep(); return 1; } + else + { + LORANOW_DEBUG_PRINTLN("[ln] Begin Fail"); + } return 0; } @@ -109,7 +115,8 @@ void LoRaNowClass::state_do(byte _state) switch (_state) { case LORA_STATE_INIT: - begin(); + if (!begin()) + state = LORA_STATE_END; break; case LORA_STATE_END: end(); @@ -212,6 +219,15 @@ void LoRaNowClass::showStatus(Stream &out) } } +void LoRaNowClass::setPinsSPI(int sck, int miso, int mosi, int ss, int dio0) +{ +#if defined(ARDUINO_ARCH_AVR) +#elif defined(ARDUINO_ARCH_ESP8266) +#elif defined(ARDUINO_ARCH_ESP32) + SPI.begin(sck, miso, mosi, ss); +#endif + setPins(ss, dio0); +} void LoRaNowClass::setPins(int ss, int dio0) { _ss = ss; @@ -326,16 +342,16 @@ int LoRaNowClass::peek() void LoRaNowClass::flush() { - - if (state == LORA_STATE_NONE) + LORANOW_DEBUG_PRINT("[ln] flush "); + LORANOW_DEBUG_PRINTLN(state); + if (state == LORA_STATE_NONE || state == LORA_STATE_END) { state_change(LORA_STATE_INIT); } - if (state == LORA_STATE_END) + if (state == LORA_STATE_SLEEP || state == LORA_STATE_RECEIVE) { - state_change(LORA_STATE_INIT); + state_change(LORA_STATE_TX_INIT); } - state_change(LORA_STATE_TX_INIT); } void LoRaNowClass::send() diff --git a/src/LoRaNow.h b/src/LoRaNow.h index 02d14cc..5a08b4c 100644 --- a/src/LoRaNow.h +++ b/src/LoRaNow.h @@ -45,15 +45,8 @@ #define LORANOW_WAIT_RECEIVE 100 #if defined(ARDUINO_ARCH_ESP32) -// HELTEC #define LORANOW_DEFAULT_SS_PIN 18 #define LORANOW_DEFAULT_DIO0_PIN 26 - -#if defined(ARDUINO_MH_ET_LIVE_ESP32MINIKIT) -#define LORANOW_DEFAULT_SS_PIN 26 -#define LORANOW_DEFAULT_DIO0_PIN 5 -#endif - #elif defined(ARDUINO_ARCH_ESP8266) #define LORANOW_DEFAULT_SS_PIN 16 #define LORANOW_DEFAULT_DIO0_PIN 15 @@ -123,7 +116,7 @@ class LoRaNowClass : public Stream // ----------------------------------------------- // // State machine - + unsigned int wait = 0; unsigned long time = 0; // ----------------------------------------------- // @@ -140,7 +133,6 @@ class LoRaNowClass : public Stream uint8_t payload_position = 0; // ----------------------------------------------- // public: - uint8_t state = 0; LoRaNowClass(); @@ -154,6 +146,7 @@ class LoRaNowClass : public Stream void showStatus(Stream &out); + void setPinsSPI(int sck, int miso, int mosi, int ss = LORA_DEFAULT_SS_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN); void setPins(int ss = LORA_DEFAULT_SS_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN); void setFrequency(long _frequency); @@ -190,7 +183,6 @@ class LoRaNowClass : public Stream void onSleep(void (*cb)()); private: - bool isSleep(); bool isReady();