diff --git a/API.md b/API.md index 81112d8..dfa30ca 100644 --- a/API.md +++ b/API.md @@ -151,6 +151,8 @@ unsigned long id = LoRaNow.id(); LoRaNow.setId(id); ``` +* `id` - identification number (4 bytes) + ### Count The count number, always increment by one when a message is send. @@ -158,8 +160,6 @@ The count number, always increment by one when a message is send. ```c byte count = LoRaNow.count(); ``` - - * `id` - identification number (4 bytes) ### Gateway diff --git a/README.md b/README.md index f687bed..50a1dae 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ This is the payload format. ## Installation * Install the library by [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3) -* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/LoRaNow/archive/1.0.0.zip) or one of the [releases](https://github.com/ricaun/LoRaNow/releases) ZIP files. +* **OR** by [Importing the .zip library](https://www.arduino.cc/en/Guide/Libraries#toc4) using either the [master](https://github.com/ricaun/LoRaNow/archive/1.0.1.zip) or one of the [releases](https://github.com/ricaun/LoRaNow/releases) ZIP files. ## Examples @@ -66,7 +66,7 @@ This libary is [licensed](LICENSE) under the [MIT Licence](https://en.wikipedia. ## To do - [x] Example for ESP8266 board -- [ ] Example for ESP32 board +- [x] Example for ESP32 board - [ ] Improve documentation - [ ] State machine diff --git a/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino b/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino new file mode 100644 index 0000000..8dbef4b --- /dev/null +++ b/examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino @@ -0,0 +1,128 @@ +/* + LoRaNow Simple Gateway with ESP32 + + This code creates a webServer to show the LoRa messages. + + created 05 04 2019 + by Luiz H. Cassettari +*/ + +#include +#include +#include +#include + +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); + + 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(); + + 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_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino b/examples/LoRaNow_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino index 75ba167..5e44c5c 100644 --- a/examples/LoRaNow_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino +++ b/examples/LoRaNow_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino @@ -12,8 +12,8 @@ #include #include -const char *ssid = "oooooooo"; -const char *password = "oooooooo"; +const char *ssid = ""; +const char *password = ""; ESP8266WebServer server(80); @@ -52,25 +52,9 @@ void setup(void) { Serial.begin(115200); - // 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); - - if (!LoRaNow.begin()) { - Serial.println("LoRa init failed. Check your connections."); - while (true); - } - - LoRaNow.onMessage(onMessage); - LoRaNow.gateway(); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); + if (ssid != "") WiFi.begin(ssid, password); + WiFi.begin(); Serial.println(""); // Wait for connection @@ -89,9 +73,27 @@ void setup(void) { 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); + + 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(); } diff --git a/library.properties b/library.properties index 2f11003..6e48970 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=LoRaNow -version=1.0.0 +version=1.0.1 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 1457fdd..90242ad 100644 --- a/src/LoRaNow.cpp +++ b/src/LoRaNow.cpp @@ -29,10 +29,6 @@ #ifndef SIGRD #define SIGRD 5 #endif -#elif defined(ARDUINO_ARCH_ESP8266) -#include -#elif defined(ARDUINO_ARCH_ESP32) -#include "WiFi.h" #endif LoRaNowClass::LoRaNowClass() @@ -258,15 +254,10 @@ uint32_t LoRaNowClass::makeId() (uint32_t)boot_signature_byte_get(0x16) << 8 | (uint32_t)boot_signature_byte_get(0x17); return _id; -#elif (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)) - uint8_t mac[6]; - WiFi.macAddress(mac); - uint32_t _id = - (uint32_t)mac[0] << 24 | - (uint32_t)mac[1] << 16 | - (uint32_t)mac[4] << 8 | - (uint32_t)mac[5]; - return _id; +#elif defined(ARDUINO_ARCH_ESP8266) + return ESP.getChipId(); +#elif defined(ARDUINO_ARCH_ESP32) + return ESP.getEfuseMac(); #endif return 0; } diff --git a/src/LoRaNow.h b/src/LoRaNow.h index 55a6ed4..08211f5 100644 --- a/src/LoRaNow.h +++ b/src/LoRaNow.h @@ -47,8 +47,12 @@ // HELTEC #define LORANOW_DEFAULT_SS_PIN 18 #define LORANOW_DEFAULT_DIO0_PIN 26 -//#define LORANOW_DEFAULT_SS_PIN 26 -//#define LORANOW_DEFAULT_DIO0_PIN 5 + +#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