Skip to content

Commit

Permalink
Add ESP32
Browse files Browse the repository at this point in the history
Add Example ESP32
Change ESP8266 id
Change ESP32 id
  • Loading branch information
ricaun committed Apr 5, 2019
1 parent f2863e4 commit 442971f
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 40 deletions.
4 changes: 2 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ 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.

```c
byte count = LoRaNow.count();
```

* `id` - identification number (4 bytes)

### Gateway

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
128 changes: 128 additions & 0 deletions examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino
Original file line number Diff line number Diff line change
@@ -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 <LoRaNow.h>
#include <WiFi.h>
#include <WebServer.h>
#include <StreamString.h>

const char *ssid = "";
const char *password = "";

WebServer server(80);

const char *script = "<script>function loop() {var resp = GET_NOW('loranow'); var area = document.getElementById('area').value; document.getElementById('area').value = area + resp; setTimeout('loop()', 1000);} function GET_NOW(get) { var xmlhttp; if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); xmlhttp.open('GET', get, false); xmlhttp.send(); return xmlhttp.responseText; }</script>";

void handleRoot()
{
String str = "";
str += "<html>";
str += "<head>";
str += "<title>ESP32 - LoRaNow</title>";
str += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
str += script;
str += "</head>";
str += "<body onload='loop()'>";
str += "<center>";
str += "<textarea id='area' style='width:800px; height:400px;'></textarea>";
str += "</center>";
str += "</body>";
str += "</html>";
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();
}
42 changes: 22 additions & 20 deletions examples/LoRaNow_Gateway_ESP8266/LoRaNow_Gateway_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <ESP8266WebServer.h>
#include <StreamString.h>

const char *ssid = "oooooooo";
const char *password = "oooooooo";
const char *ssid = "";
const char *password = "";

ESP8266WebServer server(80);

Expand Down Expand Up @@ -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
Expand All @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LoRaNow
version=1.0.0
version=1.0.1
author=Luiz Henrique Cassettari
maintainer=Luiz Henrique Cassettari <[email protected]>
sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol.
Expand Down
17 changes: 4 additions & 13 deletions src/LoRaNow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
#ifndef SIGRD
#define SIGRD 5
#endif
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include "WiFi.h"
#endif

LoRaNowClass::LoRaNowClass()
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions src/LoRaNow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 442971f

Please sign in to comment.