Skip to content

Commit 65e1e37

Browse files
committed
Add setPinsSPI
Add setPinsSPI function to help config SPI on ESP32 patform.
1 parent dc33581 commit 65e1e37

File tree

9 files changed

+198
-18
lines changed

9 files changed

+198
-18
lines changed

API.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ LoRaNow.setPins(ss, dio0);
2727

2828
This call is optional and only needs to be used if you need to change the default pins used.
2929

30+
### Set pins SPI
31+
32+
Override the SPI pins and the default `SS`, and `DIO0` pins used by the library. **Must** be called before `LoRaNow.begin()`.
33+
34+
```c
35+
LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0);
36+
```
37+
* `sck` - new sck pin to use for SPI communication
38+
* `miso` - new miso pin to use for SPI communication
39+
* `mosi` - new mosi pin to use for SPI communication
40+
* `ss` - new slave select pin to use, defaults to `10` or `gpio16`
41+
* `dio0` - new DIO0 pin to use, defaults to `2` or `gpio15`. **Must** be interrupt capable via [attachInterrupt(...)](https://www.arduino.cc/en/Reference/AttachInterrupt).
42+
43+
This call is optional and only works on ESP32 platform.
44+
3045
### End
3146

3247
Stop the library

examples/LoRaNow_Gateway/LoRaNow_Gateway.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void setup() {
2222
// LoRaNow.setSpreadingFactor(sf);
2323
// LoRaNow.setPins(ss, dio0);
2424

25+
// LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32
26+
2527
if (!LoRaNow.begin()) {
2628
Serial.println("LoRa init failed. Check your connections.");
2729
while (true);

examples/LoRaNow_Gateway_ESP32/LoRaNow_Gateway_ESP32.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ void setup(void)
8686
// LoRaNow.setSpreadingFactor(sf);
8787
// LoRaNow.setPins(ss, dio0);
8888

89+
// LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32
90+
8991
if (!LoRaNow.begin())
9092
{
9193
Serial.println("LoRa init failed. Check your connections.");
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
LoRaNow Simple Gateway with ESP32 setPins
3+
4+
This code creates a webServer to show the LoRa messages.
5+
6+
created 27 04 2019
7+
by Luiz H. Cassettari
8+
*/
9+
10+
#include <LoRaNow.h>
11+
#include <WiFi.h>
12+
#include <WebServer.h>
13+
#include <StreamString.h>
14+
15+
#define SCK 5
16+
#define MISO 19
17+
#define MOSI 27
18+
#define SS 18
19+
#define DIO0 26
20+
21+
const char *ssid = "";
22+
const char *password = "";
23+
24+
WebServer server(80);
25+
26+
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>";
27+
28+
void handleRoot()
29+
{
30+
String str = "";
31+
str += "<html>";
32+
str += "<head>";
33+
str += "<title>ESP32 - LoRaNow</title>";
34+
str += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
35+
str += script;
36+
str += "</head>";
37+
str += "<body onload='loop()'>";
38+
str += "<center>";
39+
str += "<textarea id='area' style='width:800px; height:400px;'></textarea>";
40+
str += "</center>";
41+
str += "</body>";
42+
str += "</html>";
43+
server.send(200, "text/html", str);
44+
}
45+
46+
static StreamString string;
47+
48+
void handleLoRaNow()
49+
{
50+
server.send(200, "text/plain", string);
51+
while (string.available()) // clear
52+
{
53+
string.read();
54+
}
55+
}
56+
57+
void setup(void)
58+
{
59+
60+
Serial.begin(115200);
61+
62+
WiFi.mode(WIFI_STA);
63+
if (ssid != "")
64+
WiFi.begin(ssid, password);
65+
WiFi.begin();
66+
Serial.println("");
67+
68+
// Wait for connection
69+
while (WiFi.status() != WL_CONNECTED)
70+
{
71+
delay(500);
72+
Serial.print(".");
73+
}
74+
75+
Serial.println("");
76+
Serial.print("Connected to ");
77+
Serial.println(ssid);
78+
Serial.print("IP address: ");
79+
Serial.println(WiFi.localIP());
80+
81+
server.on("/", handleRoot);
82+
server.on("/loranow", handleLoRaNow);
83+
server.begin();
84+
Serial.println("HTTP server started");
85+
86+
// LoRaNow.setFrequencyCN(); // Select the frequency 486.5 MHz - Used in China
87+
// LoRaNow.setFrequencyEU(); // Select the frequency 868.3 MHz - Used in Europe
88+
// LoRaNow.setFrequencyUS(); // Select the frequency 904.1 MHz - Used in USA, Canada and South America
89+
// LoRaNow.setFrequencyAU(); // Select the frequency 917.0 MHz - Used in Australia, Brazil and Chile
90+
91+
// LoRaNow.setFrequency(frequency);
92+
// LoRaNow.setSpreadingFactor(sf);
93+
// LoRaNow.setPins(ss, dio0);
94+
95+
LoRaNow.setPinsSPI(SCK, MISO, MOSI, SS, DIO0); // Only works with ESP32
96+
97+
if (!LoRaNow.begin())
98+
{
99+
Serial.println("LoRa init failed. Check your connections.");
100+
while (true)
101+
;
102+
}
103+
104+
LoRaNow.onMessage(onMessage);
105+
LoRaNow.gateway();
106+
}
107+
108+
void loop(void)
109+
{
110+
LoRaNow.loop();
111+
server.handleClient();
112+
}
113+
114+
void onMessage(uint8_t *buffer, size_t size)
115+
{
116+
unsigned long id = LoRaNow.id();
117+
byte count = LoRaNow.count();
118+
119+
Serial.print("Node Id: ");
120+
Serial.println(id, HEX);
121+
Serial.print("Count: ");
122+
Serial.println(count);
123+
Serial.print("Message: ");
124+
Serial.write(buffer, size);
125+
Serial.println();
126+
Serial.println();
127+
128+
if (string.available() > 512)
129+
{
130+
while (string.available())
131+
{
132+
string.read();
133+
}
134+
}
135+
136+
string.print("Node Id: ");
137+
string.println(id, HEX);
138+
string.print("Count: ");
139+
string.println(count);
140+
string.print("Message: ");
141+
string.write(buffer, size);
142+
string.println();
143+
string.println();
144+
145+
// Send data to the node
146+
LoRaNow.clear();
147+
LoRaNow.print("LoRaNow Gateway Message ");
148+
LoRaNow.print(millis());
149+
LoRaNow.send();
150+
}

examples/LoRaNow_Node/LoRaNow_Node.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ void setup() {
2222
// LoRaNow.setSpreadingFactor(sf);
2323
// LoRaNow.setPins(ss, dio0);
2424

25+
// LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0); // Only works with ESP32
26+
2527
if (!LoRaNow.begin()) {
2628
Serial.println("LoRa init failed. Check your connections.");
2729
while (true);

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ sleep KEYWORD2
2424
isSleep KEYWORD2
2525
isReady KEYWORD2
2626
delay KEYWORD2
27+
setPinsSPI KEYWORD2
2728

2829
showStatus KEYWORD2
2930

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=LoRaNow
2-
version=1.0.3
2+
version=1.0.4
33
author=Luiz Henrique Cassettari
44
maintainer=Luiz Henrique Cassettari <[email protected]>
55
sentence=LoRaNow Library is a simple LoRa Node <> Gateway communication protocol.

src/LoRaNow.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// ---------------------------------------------------- //
22
// LoRaNow.cpp
33
// ---------------------------------------------------- //
4+
// 27/04/2019 - Add setPinsSPI to help esp32 boards
5+
// 24/04/2019 - Fix LoRaNow board mosfet
46
// 23/04/2019 - Add onSleep callback
57
// 22/04/2019 - Add LORA_STATE_RECEIVE on loop - fix interrupt reset on esp32/esp8266
68
// 20/04/2019 - Fix esp32 id
@@ -46,7 +48,7 @@ byte LoRaNowClass::begin()
4648
#if defined(LORANOW_MOSFET_P)
4749
pinMode(LORANOW_MOSFET_P, OUTPUT);
4850
digitalWrite(LORANOW_MOSFET_P, LOW);
49-
delay(1);
51+
delay(5);
5052
#endif
5153

5254
if (LoRa.begin(frequency))
@@ -65,6 +67,10 @@ byte LoRaNowClass::begin()
6567
sleep();
6668
return 1;
6769
}
70+
else
71+
{
72+
LORANOW_DEBUG_PRINTLN("[ln] Begin Fail");
73+
}
6874
return 0;
6975
}
7076

@@ -109,7 +115,8 @@ void LoRaNowClass::state_do(byte _state)
109115
switch (_state)
110116
{
111117
case LORA_STATE_INIT:
112-
begin();
118+
if (!begin())
119+
state = LORA_STATE_END;
113120
break;
114121
case LORA_STATE_END:
115122
end();
@@ -212,6 +219,15 @@ void LoRaNowClass::showStatus(Stream &out)
212219
}
213220
}
214221

222+
void LoRaNowClass::setPinsSPI(int sck, int miso, int mosi, int ss, int dio0)
223+
{
224+
#if defined(ARDUINO_ARCH_AVR)
225+
#elif defined(ARDUINO_ARCH_ESP8266)
226+
#elif defined(ARDUINO_ARCH_ESP32)
227+
SPI.begin(sck, miso, mosi, ss);
228+
#endif
229+
setPins(ss, dio0);
230+
}
215231
void LoRaNowClass::setPins(int ss, int dio0)
216232
{
217233
_ss = ss;
@@ -326,16 +342,16 @@ int LoRaNowClass::peek()
326342

327343
void LoRaNowClass::flush()
328344
{
329-
330-
if (state == LORA_STATE_NONE)
345+
LORANOW_DEBUG_PRINT("[ln] flush ");
346+
LORANOW_DEBUG_PRINTLN(state);
347+
if (state == LORA_STATE_NONE || state == LORA_STATE_END)
331348
{
332349
state_change(LORA_STATE_INIT);
333350
}
334-
if (state == LORA_STATE_END)
351+
if (state == LORA_STATE_SLEEP || state == LORA_STATE_RECEIVE)
335352
{
336-
state_change(LORA_STATE_INIT);
353+
state_change(LORA_STATE_TX_INIT);
337354
}
338-
state_change(LORA_STATE_TX_INIT);
339355
}
340356

341357
void LoRaNowClass::send()

src/LoRaNow.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,8 @@
4545
#define LORANOW_WAIT_RECEIVE 100
4646

4747
#if defined(ARDUINO_ARCH_ESP32)
48-
// HELTEC
4948
#define LORANOW_DEFAULT_SS_PIN 18
5049
#define LORANOW_DEFAULT_DIO0_PIN 26
51-
52-
#if defined(ARDUINO_MH_ET_LIVE_ESP32MINIKIT)
53-
#define LORANOW_DEFAULT_SS_PIN 26
54-
#define LORANOW_DEFAULT_DIO0_PIN 5
55-
#endif
56-
5750
#elif defined(ARDUINO_ARCH_ESP8266)
5851
#define LORANOW_DEFAULT_SS_PIN 16
5952
#define LORANOW_DEFAULT_DIO0_PIN 15
@@ -123,7 +116,7 @@ class LoRaNowClass : public Stream
123116

124117
// ----------------------------------------------- //
125118
// State machine
126-
119+
127120
unsigned int wait = 0;
128121
unsigned long time = 0;
129122
// ----------------------------------------------- //
@@ -140,7 +133,6 @@ class LoRaNowClass : public Stream
140133
uint8_t payload_position = 0;
141134
// ----------------------------------------------- //
142135
public:
143-
144136
uint8_t state = 0;
145137

146138
LoRaNowClass();
@@ -154,6 +146,7 @@ class LoRaNowClass : public Stream
154146

155147
void showStatus(Stream &out);
156148

149+
void setPinsSPI(int sck, int miso, int mosi, int ss = LORA_DEFAULT_SS_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
157150
void setPins(int ss = LORA_DEFAULT_SS_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
158151

159152
void setFrequency(long _frequency);
@@ -190,7 +183,6 @@ class LoRaNowClass : public Stream
190183
void onSleep(void (*cb)());
191184

192185
private:
193-
194186
bool isSleep();
195187
bool isReady();
196188

0 commit comments

Comments
 (0)