|
| 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 | +} |
0 commit comments