Skip to content

Commit 6d70824

Browse files
authored
Merge pull request #2 from ErnestoELC/master
Add example for LoRa communication via MKR WAN 13x0 using the OpenMV RPC API
2 parents 1b16fac + c1dbf05 commit 6d70824

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// LoRa example for the Edge control.
2+
//
3+
// This script configures your Arduino Edge control to receive Lora messages from another Arduino.
4+
// Requirements:
5+
// 1 Edge Control board
6+
// 2 MKR1300/1310 boards (sender and receiver)
7+
//
8+
// Connect one of the MKR1300 to the MKR2 socket in the EdgeControl board
9+
// This script is designed to pair with "LORA_receiver-EC.ino". Also
10+
// you will need a LoRa transmitter. We have used the LoRa Sender example from the other
11+
// MKR1300.
12+
//
13+
// The sketch make use of the OpenMV RPC library for communicating between the EdgeControl
14+
// and the MKR1300 via UART
15+
//
16+
// Created 20 April. 2021
17+
// by e.lopez
18+
19+
20+
#include <Arduino_EdgeControl.h>
21+
#include <openmvrpc.h>
22+
23+
openmv::rpc_scratch_buffer<256> scratch_buffer; // All RPC objects share this buffer.
24+
openmv::rpc_hardware_serial1_uart_master rpc(115200);
25+
26+
//LoRa message received interrupt pin
27+
const byte interruptPin = PIN_WIRE_SCL1;
28+
29+
bool message_received = false;
30+
uint16_t msg_count {0};
31+
32+
33+
//////////////////////////////////////////////////////////////
34+
// Call Back Handlers
35+
//////////////////////////////////////////////////////////////
36+
37+
void rpc_retrieve_LoRa_data()
38+
{
39+
rpc.begin();
40+
void *message;
41+
size_t result_data_len;
42+
43+
if (rpc.call_no_copy_no_args(F("retrieve_msg"), &message, &result_data_len) ) {
44+
45+
char buff[result_data_len + 1]; memset(buff, 0, result_data_len + 1);
46+
// Copy what we received into our data type container.
47+
memcpy(buff, message, result_data_len);
48+
// Use it now.
49+
Serial.print(F(": "));
50+
Serial.println(buff);
51+
52+
//print on LCD
53+
//LCD.setCursor(0, 0);
54+
//LCD.print("LoRa MSG:");
55+
//LCD.setCursor(0, 1);
56+
//LCD.print(buff);
57+
58+
} else {
59+
Serial.print(F("Error:rpc_retrieve_LoRa_data() failed! "));
60+
}
61+
rpc.end();
62+
}
63+
64+
//*******************
65+
//SETUP
66+
//*******************
67+
void setup()
68+
{
69+
//LoRa data available interrupt
70+
pinMode(interruptPin, INPUT_PULLUP);
71+
attachInterrupt(digitalPinToInterrupt(interruptPin), LoRa_ISR, FALLING);
72+
73+
EdgeControl.begin();
74+
75+
Power.on(PWR_3V3);
76+
Power.on(PWR_VBAT);
77+
78+
Power.on(PWR_MKR2);
79+
delay(5000); // Wait for MKR2 to power-on'
80+
81+
Serial.begin(115200);
82+
while (!Serial);
83+
84+
// //LCD init
85+
// LCD.begin(16, 2); // set up the LCD's number of columns and rows:
86+
// LCD.home(); // go home
87+
// LCD.backlight(); // turn on Backlight
88+
// LCD.print("EDGE:"); // Print a message to the LCD.
89+
90+
String serialNumber = EdgeControl.serialNumber();
91+
Serial.print("Serial Number: ");
92+
Serial.println(serialNumber);
93+
}
94+
95+
//*******************
96+
//LOOP
97+
//*******************
98+
void loop()
99+
{
100+
if (message_received) {
101+
Serial.print("Message ");
102+
Serial.print(++msg_count);
103+
rpc_retrieve_LoRa_data();
104+
message_received = false;
105+
}
106+
}
107+
108+
void LoRa_ISR() {
109+
message_received = true;
110+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// LoRa example for the Edge control.
2+
//
3+
// This script configures your MKR1300 to receive Lora messages and send it to the
4+
// Arduino Edge control.
5+
//
6+
// Connect one of the MKR1300 to the MKR2 socket in the EdgeControl board
7+
// This script is designed to pair with "LORA_EdgeControl.ino". Also
8+
// you will need a LoRa transmitter. We have used the LoRa Sender example from the other
9+
// MKR1300.
10+
//
11+
// The sketch make use of the OpenMV RPC library for communicating between the EdgeControl
12+
// and the MKR1300 via UART
13+
//
14+
// Created 20 April. 2021
15+
// by e.lopez
16+
17+
#include <SPI.h>
18+
#include <LoRa.h>
19+
#include <openmvrpc.h>
20+
21+
openmv::rpc_scratch_buffer<256> scratch_buffer; // All RPC objects share this buffer.
22+
openmv::rpc_callback_buffer<8> callback_buffer; // All RPC objects share this buffer.
23+
openmv::rpc_hardware_serial_uart_slave rpc(115200);
24+
25+
String LoRa_data = "";
26+
//output interrupt pin
27+
constexpr uint8_t interruptPin {12};
28+
29+
//***********************
30+
// Call Backs
31+
//***********************
32+
//**** retrieve data ****
33+
void rpc_retrieve_LoRa_msg(void **in_data, size_t* in_data_len) {
34+
35+
size_t LoRa_data_len = sizeof(LoRa_data);
36+
memcpy(in_data, &LoRa_data, LoRa_data_len);
37+
}
38+
39+
//***********************
40+
// LoRa
41+
//***********************
42+
void ParsePacket_LoRa() {
43+
// try to parse packet
44+
int packetSize = LoRa.parsePacket();
45+
if (packetSize) {
46+
// received a packet
47+
Serial.print("Received packet '");
48+
LoRa_data = "";
49+
while (LoRa.available()) {
50+
LoRa_data += (char)LoRa.read();
51+
}
52+
Serial.print(LoRa_data);
53+
// print RSSI of packet
54+
Serial.print("' with RSSI ");
55+
Serial.println(LoRa.packetRssi());
56+
//trigger the interrupt
57+
digitalWrite(interruptPin, LOW);
58+
delay(1);
59+
digitalWrite(interruptPin, HIGH);
60+
}
61+
}
62+
63+
//***********************
64+
// SETUP
65+
//***********************
66+
void setup() {
67+
pinMode(interruptPin, OUTPUT);
68+
digitalWrite(interruptPin, HIGH);
69+
//register callback
70+
rpc.register_callback(F("retrieve_msg"), rpc_retrieve_LoRa_msg);
71+
//rpc init
72+
rpc.begin();
73+
//Serial init
74+
Serial.begin(115200);
75+
while (!Serial);
76+
//LoRa init
77+
Serial.println("LoRa Receiver");
78+
if (!LoRa.begin(868E6)) {
79+
Serial.println("Starting LoRa failed!");
80+
while (1);
81+
}
82+
}
83+
84+
//***********************
85+
// LOOP
86+
//***********************
87+
void loop() {
88+
ParsePacket_LoRa();
89+
rpc.loop();
90+
}

0 commit comments

Comments
 (0)