Skip to content

Commit

Permalink
Add setPinsSPI
Browse files Browse the repository at this point in the history
Add setPinsSPI function to help config SPI on ESP32 patform.
  • Loading branch information
ricaun committed Apr 27, 2019
1 parent dc33581 commit 65e1e37
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 18 deletions.
15 changes: 15 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/LoRaNow_Gateway/LoRaNow_Gateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <LoRaNow.h>
#include <WiFi.h>
#include <WebServer.h>
#include <StreamString.h>

#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 = "<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);

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();
}
2 changes: 2 additions & 0 deletions examples/LoRaNow_Node/LoRaNow_Node.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sleep KEYWORD2
isSleep KEYWORD2
isReady KEYWORD2
delay KEYWORD2
setPinsSPI KEYWORD2

showStatus KEYWORD2

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.3
version=1.0.4
author=Luiz Henrique Cassettari
maintainer=Luiz Henrique Cassettari <[email protected]>
sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol.
Expand Down
30 changes: 23 additions & 7 deletions src/LoRaNow.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -65,6 +67,10 @@ byte LoRaNowClass::begin()
sleep();
return 1;
}
else
{
LORANOW_DEBUG_PRINTLN("[ln] Begin Fail");
}
return 0;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 2 additions & 10 deletions src/LoRaNow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +116,7 @@ class LoRaNowClass : public Stream

// ----------------------------------------------- //
// State machine

unsigned int wait = 0;
unsigned long time = 0;
// ----------------------------------------------- //
Expand All @@ -140,7 +133,6 @@ class LoRaNowClass : public Stream
uint8_t payload_position = 0;
// ----------------------------------------------- //
public:

uint8_t state = 0;

LoRaNowClass();
Expand All @@ -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);
Expand Down Expand Up @@ -190,7 +183,6 @@ class LoRaNowClass : public Stream
void onSleep(void (*cb)());

private:

bool isSleep();
bool isReady();

Expand Down

0 comments on commit 65e1e37

Please sign in to comment.